diff options
Diffstat (limited to 'packages/core/src/tools/read-file.ts')
| -rw-r--r-- | packages/core/src/tools/read-file.ts | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/packages/core/src/tools/read-file.ts b/packages/core/src/tools/read-file.ts index f92885ea..5c5994d7 100644 --- a/packages/core/src/tools/read-file.ts +++ b/packages/core/src/tools/read-file.ts @@ -5,10 +5,12 @@ */ import path from 'path'; +import micromatch from 'micromatch'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; import { BaseTool, ToolResult } from './tools.js'; import { isWithinRoot, processSingleFileContent } from '../utils/fileUtils.js'; +import { Config } from '../config/config.js'; /** * Parameters for the ReadFile tool @@ -35,8 +37,12 @@ export interface ReadFileToolParams { */ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> { static readonly Name: string = 'read_file'; + private readonly geminiIgnorePatterns: string[]; - constructor(private rootDirectory: string) { + constructor( + private rootDirectory: string, + config: Config, + ) { super( ReadFileTool.Name, 'ReadFile', @@ -64,6 +70,7 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> { }, ); this.rootDirectory = path.resolve(rootDirectory); + this.geminiIgnorePatterns = config.getGeminiIgnorePatterns() || []; } validateToolParams(params: ReadFileToolParams): string | null { @@ -89,6 +96,19 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> { if (params.limit !== undefined && params.limit <= 0) { return 'Limit must be a positive number'; } + + // Check against .geminiignore patterns + if (this.geminiIgnorePatterns.length > 0) { + const relativePath = makeRelative(params.path, this.rootDirectory); + if (micromatch.isMatch(relativePath, this.geminiIgnorePatterns)) { + // Get patterns that matched to show in the error message + const matchingPatterns = this.geminiIgnorePatterns.filter((p) => + micromatch.isMatch(relativePath, p), + ); + return `File path '${shortenPath(relativePath)}' is ignored by the following .geminiignore pattern(s):\n\n${matchingPatterns.join('\n')}`; + } + } + return null; } |
