diff options
| author | joshualitt <[email protected]> | 2025-08-13 12:27:09 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-13 19:27:09 +0000 |
| commit | c0c0e9b7a0a768515181e6ae7defe9f06199ea18 (patch) | |
| tree | bb25174f46c5ab54d2f4d1bcd5d6ca98478ce72a /packages/core/src/tools/read-many-files.ts | |
| parent | 904f4623b6945345d5845649e98f554671b1edfb (diff) | |
feat(core): Migrate read_many_files, shell, and web_fetch. (#6167)
Diffstat (limited to 'packages/core/src/tools/read-many-files.ts')
| -rw-r--r-- | packages/core/src/tools/read-many-files.ts | 279 |
1 files changed, 154 insertions, 125 deletions
diff --git a/packages/core/src/tools/read-many-files.ts b/packages/core/src/tools/read-many-files.ts index 5a0799bb..e4e94799 100644 --- a/packages/core/src/tools/read-many-files.ts +++ b/packages/core/src/tools/read-many-files.ts @@ -4,7 +4,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { BaseTool, Kind, ToolResult } from './tools.js'; +import { + BaseDeclarativeTool, + BaseToolInvocation, + Kind, + ToolInvocation, + ToolResult, +} from './tools.js'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { getErrorMessage } from '../utils/errors.js'; import * as path from 'path'; @@ -138,120 +144,28 @@ const DEFAULT_EXCLUDES: string[] = [ const DEFAULT_OUTPUT_SEPARATOR_FORMAT = '--- {filePath} ---'; -/** - * Tool implementation for finding and reading multiple text files from the local filesystem - * within a specified target directory. The content is concatenated. - * It is intended to run in an environment with access to the local file system (e.g., a Node.js backend). - */ -export class ReadManyFilesTool extends BaseTool< +class ReadManyFilesToolInvocation extends BaseToolInvocation< ReadManyFilesParams, ToolResult > { - static readonly Name: string = 'read_many_files'; - - constructor(private config: Config) { - const parameterSchema = { - type: 'object', - properties: { - paths: { - type: 'array', - items: { - type: 'string', - minLength: 1, - }, - minItems: 1, - description: - "Required. An array of glob patterns or paths relative to the tool's target directory. Examples: ['src/**/*.ts'], ['README.md', 'docs/']", - }, - include: { - type: 'array', - items: { - type: 'string', - minLength: 1, - }, - description: - 'Optional. Additional glob patterns to include. These are merged with `paths`. Example: ["*.test.ts"] to specifically add test files if they were broadly excluded.', - default: [], - }, - exclude: { - type: 'array', - items: { - type: 'string', - minLength: 1, - }, - description: - 'Optional. Glob patterns for files/directories to exclude. Added to default excludes if useDefaultExcludes is true. Example: ["**/*.log", "temp/"]', - default: [], - }, - recursive: { - type: 'boolean', - description: - 'Optional. Whether to search recursively (primarily controlled by `**` in glob patterns). Defaults to true.', - default: true, - }, - useDefaultExcludes: { - type: 'boolean', - description: - 'Optional. Whether to apply a list of default exclusion patterns (e.g., node_modules, .git, binary files). Defaults to true.', - default: true, - }, - file_filtering_options: { - description: - 'Whether to respect ignore patterns from .gitignore or .geminiignore', - type: 'object', - properties: { - respect_git_ignore: { - description: - 'Optional: Whether to respect .gitignore patterns when listing files. Only available in git repositories. Defaults to true.', - type: 'boolean', - }, - respect_gemini_ignore: { - description: - 'Optional: Whether to respect .geminiignore patterns when listing files. Defaults to true.', - type: 'boolean', - }, - }, - }, - }, - required: ['paths'], - }; - - super( - ReadManyFilesTool.Name, - 'ReadManyFiles', - `Reads content from multiple files specified by paths or glob patterns within a configured target directory. For text files, it concatenates their content into a single string. It is primarily designed for text-based files. However, it can also process image (e.g., .png, .jpg) and PDF (.pdf) files if their file names or extensions are explicitly included in the 'paths' argument. For these explicitly requested non-text files, their data is read and included in a format suitable for model consumption (e.g., base64 encoded). - -This tool is useful when you need to understand or analyze a collection of files, such as: -- Getting an overview of a codebase or parts of it (e.g., all TypeScript files in the 'src' directory). -- Finding where specific functionality is implemented if the user asks broad questions about code. -- Reviewing documentation files (e.g., all Markdown files in the 'docs' directory). -- Gathering context from multiple configuration files. -- When the user asks to "read all files in X directory" or "show me the content of all Y files". - -Use this tool when the user's query implies needing the content of several files simultaneously for context, analysis, or summarization. For text files, it uses default UTF-8 encoding and a '--- {filePath} ---' separator between file contents. Ensure paths are relative to the target directory. Glob patterns like 'src/**/*.js' are supported. Avoid using for single files if a more specific single-file reading tool is available, unless the user specifically requests to process a list containing just one file via this tool. Other binary files (not explicitly requested as image/PDF) are generally skipped. Default excludes apply to common non-text files (except for explicitly requested images/PDFs) and large dependency directories unless 'useDefaultExcludes' is false.`, - Kind.Read, - parameterSchema, - ); - } - - validateParams(params: ReadManyFilesParams): string | null { - const errors = SchemaValidator.validate( - this.schema.parametersJsonSchema, - params, - ); - if (errors) { - return errors; - } - return null; + constructor( + private readonly config: Config, + params: ReadManyFilesParams, + ) { + super(params); } - getDescription(params: ReadManyFilesParams): string { - const allPatterns = [...params.paths, ...(params.include || [])]; - const pathDesc = `using patterns: \`${allPatterns.join('`, `')}\` (within target directory: \`${this.config.getTargetDir()}\`)`; + getDescription(): string { + const allPatterns = [...this.params.paths, ...(this.params.include || [])]; + const pathDesc = `using patterns: +${allPatterns.join('`, `')} + (within target directory: +${this.config.getTargetDir()} +) `; // Determine the final list of exclusion patterns exactly as in execute method - const paramExcludes = params.exclude || []; - const paramUseDefaultExcludes = params.useDefaultExcludes !== false; + const paramExcludes = this.params.exclude || []; + const paramUseDefaultExcludes = this.params.useDefaultExcludes !== false; const geminiIgnorePatterns = this.config .getFileService() .getGeminiIgnorePatterns(); @@ -260,7 +174,16 @@ Use this tool when the user's query implies needing the content of several files ? [...DEFAULT_EXCLUDES, ...paramExcludes, ...geminiIgnorePatterns] : [...paramExcludes, ...geminiIgnorePatterns]; - let excludeDesc = `Excluding: ${finalExclusionPatternsForDescription.length > 0 ? `patterns like \`${finalExclusionPatternsForDescription.slice(0, 2).join('`, `')}${finalExclusionPatternsForDescription.length > 2 ? '...`' : '`'}` : 'none specified'}`; + let excludeDesc = `Excluding: ${ + finalExclusionPatternsForDescription.length > 0 + ? `patterns like +${finalExclusionPatternsForDescription + .slice(0, 2) + .join( + '`, `', + )}${finalExclusionPatternsForDescription.length > 2 ? '...`' : '`'}` + : 'none specified' + }`; // Add a note if .geminiignore patterns contributed to the final list of exclusions if (geminiIgnorePatterns.length > 0) { @@ -272,37 +195,29 @@ Use this tool when the user's query implies needing the content of several files } } - return `Will attempt to read and concatenate files ${pathDesc}. ${excludeDesc}. File encoding: ${DEFAULT_ENCODING}. Separator: "${DEFAULT_OUTPUT_SEPARATOR_FORMAT.replace('{filePath}', 'path/to/file.ext')}".`; + return `Will attempt to read and concatenate files ${pathDesc}. ${excludeDesc}. File encoding: ${DEFAULT_ENCODING}. Separator: "${DEFAULT_OUTPUT_SEPARATOR_FORMAT.replace( + '{filePath}', + 'path/to/file.ext', + )}".`; } - async execute( - params: ReadManyFilesParams, - signal: AbortSignal, - ): Promise<ToolResult> { - const validationError = this.validateParams(params); - if (validationError) { - return { - llmContent: `Error: Invalid parameters for ${this.displayName}. Reason: ${validationError}`, - returnDisplay: `## Parameter Error\n\n${validationError}`, - }; - } - + async execute(signal: AbortSignal): Promise<ToolResult> { const { paths: inputPatterns, include = [], exclude = [], useDefaultExcludes = true, - } = params; + } = this.params; const defaultFileIgnores = this.config.getFileFilteringOptions() ?? DEFAULT_FILE_FILTERING_OPTIONS; const fileFilteringOptions = { respectGitIgnore: - params.file_filtering_options?.respect_git_ignore ?? + this.params.file_filtering_options?.respect_git_ignore ?? defaultFileIgnores.respectGitIgnore, // Use the property from the returned object respectGeminiIgnore: - params.file_filtering_options?.respect_gemini_ignore ?? + this.params.file_filtering_options?.respect_gemini_ignore ?? defaultFileIgnores.respectGeminiIgnore, // Use the property from the returned object }; // Get centralized file discovery service @@ -614,3 +529,117 @@ Use this tool when the user's query implies needing the content of several files }; } } + +/** + * Tool implementation for finding and reading multiple text files from the local filesystem + * within a specified target directory. The content is concatenated. + * It is intended to run in an environment with access to the local file system (e.g., a Node.js backend). + */ +export class ReadManyFilesTool extends BaseDeclarativeTool< + ReadManyFilesParams, + ToolResult +> { + static readonly Name: string = 'read_many_files'; + + constructor(private config: Config) { + const parameterSchema = { + type: 'object', + properties: { + paths: { + type: 'array', + items: { + type: 'string', + minLength: 1, + }, + minItems: 1, + description: + "Required. An array of glob patterns or paths relative to the tool's target directory. Examples: ['src/**/*.ts'], ['README.md', 'docs/']", + }, + include: { + type: 'array', + items: { + type: 'string', + minLength: 1, + }, + description: + 'Optional. Additional glob patterns to include. These are merged with `paths`. Example: "*.test.ts" to specifically add test files if they were broadly excluded.', + default: [], + }, + exclude: { + type: 'array', + items: { + type: 'string', + minLength: 1, + }, + description: + 'Optional. Glob patterns for files/directories to exclude. Added to default excludes if useDefaultExcludes is true. Example: "**/*.log", "temp/"', + default: [], + }, + recursive: { + type: 'boolean', + description: + 'Optional. Whether to search recursively (primarily controlled by `**` in glob patterns). Defaults to true.', + default: true, + }, + useDefaultExcludes: { + type: 'boolean', + description: + 'Optional. Whether to apply a list of default exclusion patterns (e.g., node_modules, .git, binary files). Defaults to true.', + default: true, + }, + file_filtering_options: { + description: + 'Whether to respect ignore patterns from .gitignore or .geminiignore', + type: 'object', + properties: { + respect_git_ignore: { + description: + 'Optional: Whether to respect .gitignore patterns when listing files. Only available in git repositories. Defaults to true.', + type: 'boolean', + }, + respect_gemini_ignore: { + description: + 'Optional: Whether to respect .geminiignore patterns when listing files. Defaults to true.', + type: 'boolean', + }, + }, + }, + }, + required: ['paths'], + }; + + super( + ReadManyFilesTool.Name, + 'ReadManyFiles', + `Reads content from multiple files specified by paths or glob patterns within a configured target directory. For text files, it concatenates their content into a single string. It is primarily designed for text-based files. However, it can also process image (e.g., .png, .jpg) and PDF (.pdf) files if their file names or extensions are explicitly included in the 'paths' argument. For these explicitly requested non-text files, their data is read and included in a format suitable for model consumption (e.g., base64 encoded). + +This tool is useful when you need to understand or analyze a collection of files, such as: +- Getting an overview of a codebase or parts of it (e.g., all TypeScript files in the 'src' directory). +- Finding where specific functionality is implemented if the user asks broad questions about code. +- Reviewing documentation files (e.g., all Markdown files in the 'docs' directory). +- Gathering context from multiple configuration files. +- When the user asks to "read all files in X directory" or "show me the content of all Y files". + +Use this tool when the user's query implies needing the content of several files simultaneously for context, analysis, or summarization. For text files, it uses default UTF-8 encoding and a '--- {filePath} ---' separator between file contents. Ensure paths are relative to the target directory. Glob patterns like 'src/**/*.js' are supported. Avoid using for single files if a more specific single-file reading tool is available, unless the user specifically requests to process a list containing just one file via this tool. Other binary files (not explicitly requested as image/PDF) are generally skipped. Default excludes apply to common non-text files (except for explicitly requested images/PDFs) and large dependency directories unless 'useDefaultExcludes' is false.`, + Kind.Read, + parameterSchema, + ); + } + + protected validateToolParams(params: ReadManyFilesParams): string | null { + const errors = SchemaValidator.validate( + this.schema.parametersJsonSchema, + params, + ); + if (errors) { + return errors; + } + return null; + } + + protected createInvocation( + params: ReadManyFilesParams, + ): ToolInvocation<ReadManyFilesParams, ToolResult> { + return new ReadManyFilesToolInvocation(this.config, params); + } +} |
