mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
Pure refactor: Consolidate isWithinRoot() function calling. (#4163)
This commit is contained in:
committed by
GitHub
parent
e584241141
commit
fefa7ecbea
@@ -124,17 +124,10 @@ export class ReadManyFilesTool extends BaseTool<
|
||||
ToolResult
|
||||
> {
|
||||
static readonly Name: string = 'read_many_files';
|
||||
|
||||
private readonly geminiIgnorePatterns: string[] = [];
|
||||
|
||||
/**
|
||||
* 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,
|
||||
private config: Config,
|
||||
) {
|
||||
constructor(private config: Config) {
|
||||
const parameterSchema: Schema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
@@ -205,7 +198,6 @@ This tool is useful when you need to understand or analyze a collection of 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.`,
|
||||
parameterSchema,
|
||||
);
|
||||
this.targetDir = path.resolve(targetDir);
|
||||
this.geminiIgnorePatterns = config
|
||||
.getFileService()
|
||||
.getGeminiIgnorePatterns();
|
||||
@@ -221,7 +213,7 @@ Use this tool when the user's query implies needing the content of several files
|
||||
|
||||
getDescription(params: ReadManyFilesParams): string {
|
||||
const allPatterns = [...params.paths, ...(params.include || [])];
|
||||
const pathDesc = `using patterns: \`${allPatterns.join('`, `')}\` (within target directory: \`${this.targetDir}\`)`;
|
||||
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 || [];
|
||||
@@ -273,7 +265,6 @@ Use this tool when the user's query implies needing the content of several files
|
||||
// Get centralized file discovery service
|
||||
const fileDiscovery = this.config.getFileService();
|
||||
|
||||
const toolBaseDir = this.targetDir;
|
||||
const filesToConsider = new Set<string>();
|
||||
const skippedFiles: Array<{ path: string; reason: string }> = [];
|
||||
const processedFilesRelativePaths: string[] = [];
|
||||
@@ -293,7 +284,7 @@ Use this tool when the user's query implies needing the content of several files
|
||||
|
||||
try {
|
||||
const entries = await glob(searchPatterns, {
|
||||
cwd: toolBaseDir,
|
||||
cwd: this.config.getTargetDir(),
|
||||
ignore: effectiveExcludes,
|
||||
nodir: true,
|
||||
dot: true,
|
||||
@@ -305,21 +296,21 @@ Use this tool when the user's query implies needing the content of several files
|
||||
const filteredEntries = respectGitIgnore
|
||||
? fileDiscovery
|
||||
.filterFiles(
|
||||
entries.map((p) => path.relative(toolBaseDir, p)),
|
||||
entries.map((p) => path.relative(this.config.getTargetDir(), p)),
|
||||
{
|
||||
respectGitIgnore,
|
||||
},
|
||||
)
|
||||
.map((p) => path.resolve(toolBaseDir, p))
|
||||
.map((p) => path.resolve(this.config.getTargetDir(), p))
|
||||
: entries;
|
||||
|
||||
let gitIgnoredCount = 0;
|
||||
for (const absoluteFilePath of entries) {
|
||||
// Security check: ensure the glob library didn't return something outside targetDir.
|
||||
if (!absoluteFilePath.startsWith(toolBaseDir)) {
|
||||
if (!absoluteFilePath.startsWith(this.config.getTargetDir())) {
|
||||
skippedFiles.push({
|
||||
path: absoluteFilePath,
|
||||
reason: `Security: Glob library returned path outside target directory. Base: ${toolBaseDir}, Path: ${absoluteFilePath}`,
|
||||
reason: `Security: Glob library returned path outside target directory. Base: ${this.config.getTargetDir()}, Path: ${absoluteFilePath}`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -351,7 +342,7 @@ Use this tool when the user's query implies needing the content of several files
|
||||
|
||||
for (const filePath of sortedFiles) {
|
||||
const relativePathForDisplay = path
|
||||
.relative(toolBaseDir, filePath)
|
||||
.relative(this.config.getTargetDir(), filePath)
|
||||
.replace(/\\/g, '/');
|
||||
|
||||
const fileType = detectFileType(filePath);
|
||||
@@ -378,7 +369,7 @@ Use this tool when the user's query implies needing the content of several files
|
||||
// Use processSingleFileContent for all file types now
|
||||
const fileReadResult = await processSingleFileContent(
|
||||
filePath,
|
||||
toolBaseDir,
|
||||
this.config.getTargetDir(),
|
||||
);
|
||||
|
||||
if (fileReadResult.error) {
|
||||
@@ -412,7 +403,7 @@ Use this tool when the user's query implies needing the content of several files
|
||||
}
|
||||
}
|
||||
|
||||
let displayMessage = `### ReadManyFiles Result (Target Dir: \`${this.targetDir}\`)\n\n`;
|
||||
let displayMessage = `### ReadManyFiles Result (Target Dir: \`${this.config.getTargetDir()}\`)\n\n`;
|
||||
if (processedFilesRelativePaths.length > 0) {
|
||||
displayMessage += `Successfully read and concatenated content from **${processedFilesRelativePaths.length} file(s)**.\n`;
|
||||
if (processedFilesRelativePaths.length <= 10) {
|
||||
|
||||
Reference in New Issue
Block a user