Skip to content

Commit 222b804

Browse files
committed
"Refactor list_files tool to separate module (#2057)"
1 parent 62a7bc7 commit 222b804

File tree

2 files changed

+82
-49
lines changed

2 files changed

+82
-49
lines changed

src/core/Cline.ts

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from "../integrations/misc/extract-text"
3131
import { countFileLines } from "../integrations/misc/line-counter"
3232
import { fetchInstructionsTool } from "./tools/fetchInstructionsTool"
33+
import { listFilesTool } from "./tools/listFilesTool"
3334
import { ExitCodeDetails } from "../integrations/terminal/TerminalProcess"
3435
import { Terminal } from "../integrations/terminal/Terminal"
3536
import { TerminalRegistry } from "../integrations/terminal/TerminalRegistry"
@@ -1517,7 +1518,7 @@ export class Cline extends EventEmitter<ClineEvents> {
15171518
}
15181519

15191520
// If block is partial, remove partial closing tag so its not presented to user
1520-
const removeClosingTag = (tag: ToolParamName, text?: string) => {
1521+
const removeClosingTag = (tag: ToolParamName, text?: string): string => {
15211522
if (!block.partial) {
15221523
return text || ""
15231524
}
@@ -2409,54 +2410,8 @@ export class Cline extends EventEmitter<ClineEvents> {
24092410
}
24102411

24112412
case "list_files": {
2412-
const relDirPath: string | undefined = block.params.path
2413-
const recursiveRaw: string | undefined = block.params.recursive
2414-
const recursive = recursiveRaw?.toLowerCase() === "true"
2415-
const sharedMessageProps: ClineSayTool = {
2416-
tool: !recursive ? "listFilesTopLevel" : "listFilesRecursive",
2417-
path: getReadablePath(this.cwd, removeClosingTag("path", relDirPath)),
2418-
}
2419-
try {
2420-
if (block.partial) {
2421-
const partialMessage = JSON.stringify({
2422-
...sharedMessageProps,
2423-
content: "",
2424-
} satisfies ClineSayTool)
2425-
await this.ask("tool", partialMessage, block.partial).catch(() => {})
2426-
break
2427-
} else {
2428-
if (!relDirPath) {
2429-
this.consecutiveMistakeCount++
2430-
pushToolResult(await this.sayAndCreateMissingParamError("list_files", "path"))
2431-
break
2432-
}
2433-
this.consecutiveMistakeCount = 0
2434-
const absolutePath = path.resolve(this.cwd, relDirPath)
2435-
const [files, didHitLimit] = await listFiles(absolutePath, recursive, 200)
2436-
const { showRooIgnoredFiles = true } =
2437-
(await this.providerRef.deref()?.getState()) ?? {}
2438-
const result = formatResponse.formatFilesList(
2439-
absolutePath,
2440-
files,
2441-
didHitLimit,
2442-
this.rooIgnoreController,
2443-
showRooIgnoredFiles,
2444-
)
2445-
const completeMessage = JSON.stringify({
2446-
...sharedMessageProps,
2447-
content: result,
2448-
} satisfies ClineSayTool)
2449-
const didApprove = await askApproval("tool", completeMessage)
2450-
if (!didApprove) {
2451-
break
2452-
}
2453-
pushToolResult(result)
2454-
break
2455-
}
2456-
} catch (error) {
2457-
await handleError("listing files", error)
2458-
break
2459-
}
2413+
listFilesTool(this, block, askApproval, handleError, pushToolResult, removeClosingTag)
2414+
break
24602415
}
24612416
case "list_code_definition_names": {
24622417
const relPath: string | undefined = block.params.path

src/core/tools/listFilesTool.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as path from "path"
2+
import { Cline } from "../Cline"
3+
import { ClineSayTool } from "../../shared/ExtensionMessage"
4+
import { ToolParamName, ToolUse } from "../assistant-message"
5+
import { formatResponse } from "../prompts/responses"
6+
import { listFiles } from "../../services/glob/list-files"
7+
import { getReadablePath } from "../../utils/path"
8+
import { AskApproval, HandleError, PushToolResult } from "./types"
9+
/**
10+
* Implements the list_files tool.
11+
*
12+
* @param cline - The instance of Cline that is executing this tool.
13+
* @param block - The block of assistant message content that specifies the
14+
* parameters for this tool.
15+
* @param askApproval - A function that asks the user for approval to show a
16+
* message.
17+
* @param handleError - A function that handles an error that occurred while
18+
* executing this tool.
19+
* @param pushToolResult - A function that pushes the result of this tool to the
20+
* conversation.
21+
* @param removeClosingTag - A function that removes a closing tag from a string.
22+
*/
23+
export async function listFilesTool(
24+
cline: Cline,
25+
block: ToolUse,
26+
askApproval: AskApproval,
27+
handleError: HandleError,
28+
pushToolResult: PushToolResult,
29+
removeClosingTag: (tag: ToolParamName, text?: string) => string,
30+
) {
31+
const relDirPath: string | undefined = block.params.path
32+
const recursiveRaw: string | undefined = block.params.recursive
33+
const recursive = recursiveRaw?.toLowerCase() === "true"
34+
const sharedMessageProps: ClineSayTool = {
35+
tool: !recursive ? "listFilesTopLevel" : "listFilesRecursive",
36+
path: getReadablePath(cline.cwd, removeClosingTag("path", relDirPath)),
37+
}
38+
try {
39+
if (block.partial) {
40+
const partialMessage = JSON.stringify({
41+
...sharedMessageProps,
42+
content: "",
43+
} satisfies ClineSayTool)
44+
await cline.ask("tool", partialMessage, block.partial).catch(() => {})
45+
return
46+
} else {
47+
if (!relDirPath) {
48+
cline.consecutiveMistakeCount++
49+
pushToolResult(await cline.sayAndCreateMissingParamError("list_files", "path"))
50+
return
51+
}
52+
cline.consecutiveMistakeCount = 0
53+
const absolutePath = path.resolve(cline.cwd, relDirPath)
54+
const [files, didHitLimit] = await listFiles(absolutePath, recursive, 200)
55+
const { showRooIgnoredFiles = true } = (await cline.providerRef.deref()?.getState()) ?? {}
56+
const result = formatResponse.formatFilesList(
57+
absolutePath,
58+
files,
59+
didHitLimit,
60+
cline.rooIgnoreController,
61+
showRooIgnoredFiles,
62+
)
63+
const completeMessage = JSON.stringify({
64+
...sharedMessageProps,
65+
content: result,
66+
} satisfies ClineSayTool)
67+
const didApprove = await askApproval("tool", completeMessage)
68+
if (!didApprove) {
69+
return
70+
}
71+
pushToolResult(result)
72+
return
73+
}
74+
} catch (error) {
75+
await handleError("listing files", error)
76+
return
77+
}
78+
}

0 commit comments

Comments
 (0)