Skip to content

Commit 71373ac

Browse files
mrubenscte
authored andcommitted
Move search_files to a tool file (#2098)
1 parent 744ca53 commit 71373ac

File tree

2 files changed

+72
-52
lines changed

2 files changed

+72
-52
lines changed

src/core/Cline.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import { applyDiffTool } from "./tools/applyDiffTool"
9090
import { insertContentTool } from "./tools/insertContentTool"
9191
import { searchAndReplaceTool } from "./tools/searchAndReplaceTool"
9292
import { listCodeDefinitionNamesTool } from "./tools/listCodeDefinitionNamesTool"
93+
import { searchFilesTool } from "./tools/searchFilesTool"
9394

9495
export type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
9596
type UserContent = Array<Anthropic.Messages.ContentBlockParam>
@@ -1608,58 +1609,8 @@ export class Cline extends EventEmitter<ClineEvents> {
16081609
)
16091610
break
16101611
case "search_files": {
1611-
const relDirPath: string | undefined = block.params.path
1612-
const regex: string | undefined = block.params.regex
1613-
const filePattern: string | undefined = block.params.file_pattern
1614-
const sharedMessageProps: ClineSayTool = {
1615-
tool: "searchFiles",
1616-
path: getReadablePath(this.cwd, removeClosingTag("path", relDirPath)),
1617-
regex: removeClosingTag("regex", regex),
1618-
filePattern: removeClosingTag("file_pattern", filePattern),
1619-
}
1620-
try {
1621-
if (block.partial) {
1622-
const partialMessage = JSON.stringify({
1623-
...sharedMessageProps,
1624-
content: "",
1625-
} satisfies ClineSayTool)
1626-
await this.ask("tool", partialMessage, block.partial).catch(() => {})
1627-
break
1628-
} else {
1629-
if (!relDirPath) {
1630-
this.consecutiveMistakeCount++
1631-
pushToolResult(await this.sayAndCreateMissingParamError("search_files", "path"))
1632-
break
1633-
}
1634-
if (!regex) {
1635-
this.consecutiveMistakeCount++
1636-
pushToolResult(await this.sayAndCreateMissingParamError("search_files", "regex"))
1637-
break
1638-
}
1639-
this.consecutiveMistakeCount = 0
1640-
const absolutePath = path.resolve(this.cwd, relDirPath)
1641-
const results = await regexSearchFiles(
1642-
this.cwd,
1643-
absolutePath,
1644-
regex,
1645-
filePattern,
1646-
this.rooIgnoreController,
1647-
)
1648-
const completeMessage = JSON.stringify({
1649-
...sharedMessageProps,
1650-
content: results,
1651-
} satisfies ClineSayTool)
1652-
const didApprove = await askApproval("tool", completeMessage)
1653-
if (!didApprove) {
1654-
break
1655-
}
1656-
pushToolResult(results)
1657-
break
1658-
}
1659-
} catch (error) {
1660-
await handleError("searching files", error)
1661-
break
1662-
}
1612+
await searchFilesTool(this, block, askApproval, handleError, pushToolResult, removeClosingTag)
1613+
break
16631614
}
16641615
case "browser_action": {
16651616
const action: BrowserAction | undefined = block.params.action as BrowserAction

src/core/tools/searchFilesTool.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Cline } from "../Cline"
2+
import { ToolUse } from "../assistant-message"
3+
import { AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "./types"
4+
import { ClineSayTool } from "../../shared/ExtensionMessage"
5+
import { getReadablePath } from "../../utils/path"
6+
import path from "path"
7+
import { regexSearchFiles } from "../../services/ripgrep"
8+
9+
export async function searchFilesTool(
10+
cline: Cline,
11+
block: ToolUse,
12+
askApproval: AskApproval,
13+
handleError: HandleError,
14+
pushToolResult: PushToolResult,
15+
removeClosingTag: RemoveClosingTag,
16+
) {
17+
const relDirPath: string | undefined = block.params.path
18+
const regex: string | undefined = block.params.regex
19+
const filePattern: string | undefined = block.params.file_pattern
20+
const sharedMessageProps: ClineSayTool = {
21+
tool: "searchFiles",
22+
path: getReadablePath(cline.cwd, removeClosingTag("path", relDirPath)),
23+
regex: removeClosingTag("regex", regex),
24+
filePattern: removeClosingTag("file_pattern", filePattern),
25+
}
26+
try {
27+
if (block.partial) {
28+
const partialMessage = JSON.stringify({
29+
...sharedMessageProps,
30+
content: "",
31+
} satisfies ClineSayTool)
32+
await cline.ask("tool", partialMessage, block.partial).catch(() => {})
33+
return
34+
} else {
35+
if (!relDirPath) {
36+
cline.consecutiveMistakeCount++
37+
pushToolResult(await cline.sayAndCreateMissingParamError("search_files", "path"))
38+
return
39+
}
40+
if (!regex) {
41+
cline.consecutiveMistakeCount++
42+
pushToolResult(await cline.sayAndCreateMissingParamError("search_files", "regex"))
43+
return
44+
}
45+
cline.consecutiveMistakeCount = 0
46+
const absolutePath = path.resolve(cline.cwd, relDirPath)
47+
const results = await regexSearchFiles(
48+
cline.cwd,
49+
absolutePath,
50+
regex,
51+
filePattern,
52+
cline.rooIgnoreController,
53+
)
54+
const completeMessage = JSON.stringify({
55+
...sharedMessageProps,
56+
content: results,
57+
} satisfies ClineSayTool)
58+
const didApprove = await askApproval("tool", completeMessage)
59+
if (!didApprove) {
60+
return
61+
}
62+
pushToolResult(results)
63+
return
64+
}
65+
} catch (error) {
66+
await handleError("searching files", error)
67+
return
68+
}
69+
}

0 commit comments

Comments
 (0)