1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { BaseTool, ToolResult } from './tools.js';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { getErrorMessage } from '../utils/errors.js';
import * as fs from 'fs/promises';
import * as path from 'path';
import fg from 'fast-glob';
/**
* Parameters for the ReadManyFilesTool.
*/
export interface ReadManyFilesParams {
/**
* An array of file paths or directory paths to search within.
* Paths are relative to the tool's configured target directory.
* Glob patterns can be used directly in these paths.
*/
paths: string[];
/**
* Optional. Glob patterns for files to include.
* These are effectively combined with the `paths`.
* Example: ["*.ts", "src/** /*.md"]
*/
include?: string[];
/**
* Optional. Glob patterns for files/directories to exclude.
* Applied as ignore patterns.
* Example: ["*.log", "dist/**"]
*/
exclude?: string[];
/**
* Optional. Search directories recursively.
* This is generally controlled by glob patterns (e.g., `**`).
* The glob implementation is recursive by default for `**`.
* For simplicity, we'll rely on `**` for recursion.
*/
recursive?: boolean;
/**
* Optional. Apply default exclusion patterns. Defaults to true.
*/
useDefaultExcludes?: boolean;
}
/**
* Default exclusion patterns for commonly ignored directories and binary file types.
* These are compatible with glob ignore patterns.
* TODO(adh): Consider making this configurable or extendable through a command line arguement.
* TODO(adh): Look into sharing this list with the glob tool.
*/
const DEFAULT_EXCLUDES: string[] = [
'**/node_modules/**',
'**/.git/**',
'**/.vscode/**',
'**/.idea/**',
'**/dist/**',
'**/build/**',
'**/coverage/**',
'**/__pycache__/**',
'**/*.pyc',
'**/*.pyo',
'**/*.bin',
'**/*.exe',
'**/*.dll',
'**/*.so',
'**/*.dylib',
'**/*.class',
'**/*.jar',
'**/*.war',
'**/*.zip',
'**/*.tar',
'**/*.gz',
'**/*.bz2',
'**/*.rar',
'**/*.7z',
'**/*.png',
'**/*.jpg',
'**/*.jpeg',
'**/*.gif',
'**/*.bmp',
'**/*.tiff',
'**/*.ico',
'**/*.pdf',
'**/*.doc',
'**/*.docx',
'**/*.xls',
'**/*.xlsx',
'**/*.ppt',
'**/*.pptx',
'**/*.odt',
'**/*.ods',
'**/*.odp',
'**/*.DS_Store',
'**/.env',
];
// Default values for encoding and separator format
const DEFAULT_ENCODING: BufferEncoding = 'utf-8';
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<
ReadManyFilesParams,
ToolResult
> {
static readonly Name: string = 'read_many_files';
/**
* Creates an instance of ReadManyFilesTool.
* @param targetDir The absolute root directory within which this tool is allowed to operate.
* All paths provided in `params` will be resolved relative to this directory.
*/
constructor(readonly targetDir: string) {
const parameterSchema: Record<string, unknown> = {
type: 'object',
properties: {
paths: {
type: 'array',
items: { type: 'string' },
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' },
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' },
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,
},
},
required: ['paths'],
};
super(
ReadManyFilesTool.Name,
'ReadManyFiles',
`Reads content from multiple text files specified by paths or glob patterns within a configured target directory and concatenates them into a single string.
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.
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.
This tool should NOT be used for binary files; it attempts to skip them.
Default excludes apply to common non-text files and large dependency directories unless 'useDefaultExcludes' is false.`,
parameterSchema,
);
this.targetDir = path.resolve(targetDir);
}
validateParams(params: ReadManyFilesParams): string | null {
if (
this.schema.parameters &&
!SchemaValidator.validate(
this.schema.parameters as Record<string, unknown>,
params,
)
) {
if (
!params.paths ||
!Array.isArray(params.paths) ||
params.paths.length === 0
) {
return 'The "paths" parameter is required and must be a non-empty array of strings/glob patterns.';
}
return 'Parameters failed schema validation. Ensure "paths" is a non-empty array and other parameters match their expected types.';
}
for (const p of params.paths) {
if (typeof p !== 'string' || p.trim() === '') {
return 'Each item in "paths" must be a non-empty string/glob pattern.';
}
}
if (
params.include &&
(!Array.isArray(params.include) ||
!params.include.every((item) => typeof item === 'string'))
) {
return 'If provided, "include" must be an array of strings/glob patterns.';
}
if (
params.exclude &&
(!Array.isArray(params.exclude) ||
!params.exclude.every((item) => typeof item === 'string'))
) {
return 'If provided, "exclude" must be an array of strings/glob patterns.';
}
return null;
}
getDescription(params: ReadManyFilesParams): string {
const allPatterns = [...params.paths, ...(params.include || [])];
const pathDesc = `using patterns: \`${allPatterns.join('`, `')}\` (within target directory: \`${this.targetDir}\`)`;
let effectiveExcludes =
params.useDefaultExcludes !== false ? [...DEFAULT_EXCLUDES] : [];
if (params.exclude && params.exclude.length > 0) {
effectiveExcludes = [...effectiveExcludes, ...params.exclude];
}
const excludeDesc = `Excluding: ${effectiveExcludes.length > 0 ? `patterns like \`${effectiveExcludes.slice(0, 2).join('`, `')}${effectiveExcludes.length > 2 ? '...`' : '`'}` : 'none explicitly (beyond default non-text file avoidance).'}`;
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}`,
};
}
const {
paths: inputPatterns,
include = [],
exclude = [],
useDefaultExcludes = true,
} = params;
const toolBaseDir = this.targetDir;
const filesToConsider = new Set<string>();
const skippedFiles: Array<{ path: string; reason: string }> = [];
const processedFilesRelativePaths: string[] = [];
let concatenatedContent = '';
const effectiveExcludes = useDefaultExcludes
? [...DEFAULT_EXCLUDES, ...exclude]
: [...exclude];
const searchPatterns = [...inputPatterns, ...include];
if (searchPatterns.length === 0) {
return {
llmContent: 'No search paths or include patterns provided.',
returnDisplay: `## Information\n\nNo search paths or include patterns were specified. Nothing to read or concatenate.`,
};
}
try {
// Using fast-glob (fg) for file searching based on patterns.
// The `cwd` option scopes the search to the toolBaseDir.
// `ignore` handles exclusions.
// `onlyFiles` ensures only files are returned.
// `dot` allows matching dotfiles (which can still be excluded by patterns).
// `absolute` returns absolute paths for consistent handling.
const entries = await fg(searchPatterns, {
cwd: toolBaseDir,
ignore: effectiveExcludes,
onlyFiles: true,
dot: true,
absolute: true,
caseSensitiveMatch: false,
});
for (const absoluteFilePath of entries) {
// Security check: ensure the glob library didn't return something outside targetDir.
// This should be guaranteed by `cwd` and the library's sandboxing, but an extra check is good practice.
if (!absoluteFilePath.startsWith(toolBaseDir)) {
skippedFiles.push({
path: absoluteFilePath,
reason: `Security: Glob library returned path outside target directory. Base: ${toolBaseDir}, Path: ${absoluteFilePath}`,
});
continue;
}
filesToConsider.add(absoluteFilePath);
}
} catch (error) {
return {
llmContent: `Error during file search: ${getErrorMessage(error)}`,
returnDisplay: `## File Search Error\n\nAn error occurred while searching for files:\n\`\`\`\n${getErrorMessage(error)}\n\`\`\``,
};
}
const sortedFiles = Array.from(filesToConsider).sort();
for (const filePath of sortedFiles) {
const relativePathForDisplay = path
.relative(toolBaseDir, filePath)
.replace(/\\/g, '/');
try {
const contentBuffer = await fs.readFile(filePath);
// Basic binary detection: check for null bytes in the first 1KB
const sample = contentBuffer.subarray(
0,
Math.min(contentBuffer.length, 1024),
);
if (sample.includes(0)) {
skippedFiles.push({
path: relativePathForDisplay,
reason: 'Skipped (appears to be binary)',
});
continue;
}
// Using default encoding
const fileContent = contentBuffer.toString(DEFAULT_ENCODING);
// Using default separator format
const separator = DEFAULT_OUTPUT_SEPARATOR_FORMAT.replace(
'{filePath}',
relativePathForDisplay,
);
concatenatedContent += `${separator}\n\n${fileContent}\n\n`;
processedFilesRelativePaths.push(relativePathForDisplay);
} catch (error) {
skippedFiles.push({
path: relativePathForDisplay,
reason: `Read error: ${getErrorMessage(error)}`,
});
}
}
let displayMessage = `### ReadManyFiles Result (Target Dir: \`${this.targetDir}\`)\n\n`;
if (processedFilesRelativePaths.length > 0) {
displayMessage += `Successfully read and concatenated content from **${processedFilesRelativePaths.length} file(s)**.\n`;
displayMessage += `\n**Processed Files (up to 10 shown):**\n`;
processedFilesRelativePaths
.slice(0, 10)
.forEach((p) => (displayMessage += `- \`${p}\`\n`));
if (processedFilesRelativePaths.length > 10) {
displayMessage += `- ...and ${processedFilesRelativePaths.length - 10} more.\n`;
}
} else {
displayMessage += `No files were read and concatenated based on the criteria.\n`;
}
if (skippedFiles.length > 0) {
displayMessage += `\n**Skipped ${skippedFiles.length} item(s) (up to 5 shown):**\n`;
skippedFiles
.slice(0, 5)
.forEach(
(f) => (displayMessage += `- \`${f.path}\` (Reason: ${f.reason})\n`),
);
if (skippedFiles.length > 5) {
displayMessage += `- ...and ${skippedFiles.length - 5} more.\n`;
}
}
if (
concatenatedContent.length === 0 &&
processedFilesRelativePaths.length === 0
) {
concatenatedContent =
'No files matching the criteria were found or all were skipped.';
}
return {
llmContent: concatenatedContent,
returnDisplay: displayMessage,
};
}
}
|