From 7f30a8bea7bb38e0b4bff30f35e5e4d91c990463 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:39:29 -0400 Subject: [PATCH] Truncate files list for large projects --- src/ClaudeDev.ts | 43 ++++++++++++++++++++++------------ src/parse-source-code/index.ts | 2 +- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index 88cd5ee..9e71fa8 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -514,20 +514,7 @@ export class ClaudeDev { async listFilesTopLevel(dirPath: string): Promise { 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 { 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 { try { const result = await parseSourceCodeForDefinitionsTopLevel(dirPath) diff --git a/src/parse-source-code/index.ts b/src/parse-source-code/index.ts index 98a3ea5..5675ac1 100644 --- a/src/parse-source-code/index.ts +++ b/src/parse-source-code/index.ts @@ -77,7 +77,7 @@ export async function listFiles(dirPath: string, recursive: boolean): Promise