Truncate files list for large projects

This commit is contained in:
Saoud Rizwan
2024-08-03 15:39:29 -04:00
parent 7656f8d653
commit 7f30a8bea7
2 changed files with 29 additions and 16 deletions

View File

@@ -514,20 +514,7 @@ export class ClaudeDev {
async listFilesTopLevel(dirPath: string): Promise<string> {
try {
const files = await listFiles(dirPath, false)
const result = files
.map((file) => {
const relativePath = path.relative(dirPath, file)
return file.endsWith("/") ? relativePath + "/" : relativePath
})
.sort((a, b) => {
const aIsDir = a.endsWith("/")
const bIsDir = b.endsWith("/")
if (aIsDir !== bIsDir) {
return aIsDir ? -1 : 1
}
return a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" })
})
.join("\n")
const result = this.formatFilesList(dirPath, files)
const { response, text } = await this.ask(
"tool",
JSON.stringify({ tool: "listFilesTopLevel", path: dirPath, content: result } as ClaudeSayTool)
@@ -555,7 +542,7 @@ export class ClaudeDev {
async listFilesRecursive(dirPath: string): Promise<string> {
try {
const files = await listFiles(dirPath, true)
const result = files.map((file) => path.relative(dirPath, file)).join("\n")
const result = this.formatFilesList(dirPath, files)
const { response, text } = await this.ask(
"tool",
JSON.stringify({ tool: "listFilesRecursive", path: dirPath, content: result } as ClaudeSayTool)
@@ -578,6 +565,32 @@ export class ClaudeDev {
}
}
formatFilesList(dirPath: string, files: string[]): string {
const sorted = files
.map((file) => {
// convert absolute path to relative path
const relativePath = path.relative(dirPath, file)
return file.endsWith("/") ? relativePath + "/" : relativePath
})
.sort((a, b) => {
// sort directories before files
const aIsDir = a.endsWith("/")
const bIsDir = b.endsWith("/")
if (aIsDir !== bIsDir) {
return aIsDir ? -1 : 1
}
return a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" })
})
if (sorted.length > 1000) {
const truncatedList = sorted.slice(0, 1000).join("\n")
const remainingCount = sorted.length - 1000
return `${truncatedList}\n\n(${remainingCount} files not listed due to automatic truncation. Try listing files in subdirectories if you need to explore further.)`
} else {
return sorted.join("\n")
}
}
async viewSourceCodeDefinitionsTopLevel(dirPath: string): Promise<string> {
try {
const result = await parseSourceCodeForDefinitionsTopLevel(dirPath)

View File

@@ -77,7 +77,7 @@ export async function listFiles(dirPath: string, recursive: boolean): Promise<st
markDirectories: true, // Append a / on any directories matched
gitignore: recursive, // globby ignores any files that are gitignored
ignore: recursive ? dirsToIgnore : undefined, // just in case there is no gitignore, we ignore sensible defaults
onlyFiles: recursive, // true by default, false means it will list directories on their own too
onlyFiles: false, // true by default, false means it will list directories on their own too
}
// * globs all files in one dir, ** globs files in nested directories
const files = await globby(recursive ? "**" : "*", options)