Skip to content

Commit da2af71

Browse files
authored
Move list_code_definition_names to a tool file (#2097)
1 parent bdb668b commit da2af71

File tree

2 files changed

+80
-60
lines changed

2 files changed

+80
-60
lines changed

src/core/Cline.ts

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import { writeToFileTool } from "./tools/writeToFileTool"
8989
import { applyDiffTool } from "./tools/applyDiffTool"
9090
import { insertContentTool } from "./tools/insertContentTool"
9191
import { searchAndReplaceTool } from "./tools/searchAndReplaceTool"
92+
import { listCodeDefinitionNamesTool } from "./tools/listCodeDefinitionNamesTool"
9293

9394
export type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
9495
type UserContent = Array<Anthropic.Messages.ContentBlockParam>
@@ -1596,66 +1597,16 @@ export class Cline extends EventEmitter<ClineEvents> {
15961597
case "list_files":
15971598
await listFilesTool(this, block, askApproval, handleError, pushToolResult, removeClosingTag)
15981599
break
1599-
case "list_code_definition_names": {
1600-
const relPath: string | undefined = block.params.path
1601-
const sharedMessageProps: ClineSayTool = {
1602-
tool: "listCodeDefinitionNames",
1603-
path: getReadablePath(this.cwd, removeClosingTag("path", relPath)),
1604-
}
1605-
try {
1606-
if (block.partial) {
1607-
const partialMessage = JSON.stringify({
1608-
...sharedMessageProps,
1609-
content: "",
1610-
} satisfies ClineSayTool)
1611-
await this.ask("tool", partialMessage, block.partial).catch(() => {})
1612-
break
1613-
} else {
1614-
if (!relPath) {
1615-
this.consecutiveMistakeCount++
1616-
pushToolResult(
1617-
await this.sayAndCreateMissingParamError("list_code_definition_names", "path"),
1618-
)
1619-
break
1620-
}
1621-
this.consecutiveMistakeCount = 0
1622-
const absolutePath = path.resolve(this.cwd, relPath)
1623-
let result: string
1624-
try {
1625-
const stats = await fs.stat(absolutePath)
1626-
if (stats.isFile()) {
1627-
const fileResult = await parseSourceCodeDefinitionsForFile(
1628-
absolutePath,
1629-
this.rooIgnoreController,
1630-
)
1631-
result = fileResult ?? "No source code definitions found in this file."
1632-
} else if (stats.isDirectory()) {
1633-
result = await parseSourceCodeForDefinitionsTopLevel(
1634-
absolutePath,
1635-
this.rooIgnoreController,
1636-
)
1637-
} else {
1638-
result = "The specified path is neither a file nor a directory."
1639-
}
1640-
} catch {
1641-
result = `${absolutePath}: does not exist or cannot be accessed.`
1642-
}
1643-
const completeMessage = JSON.stringify({
1644-
...sharedMessageProps,
1645-
content: result,
1646-
} satisfies ClineSayTool)
1647-
const didApprove = await askApproval("tool", completeMessage)
1648-
if (!didApprove) {
1649-
break
1650-
}
1651-
pushToolResult(result)
1652-
break
1653-
}
1654-
} catch (error) {
1655-
await handleError("parsing source code definitions", error)
1656-
break
1657-
}
1658-
}
1600+
case "list_code_definition_names":
1601+
await listCodeDefinitionNamesTool(
1602+
this,
1603+
block,
1604+
askApproval,
1605+
handleError,
1606+
pushToolResult,
1607+
removeClosingTag,
1608+
)
1609+
break
16591610
case "search_files": {
16601611
const relDirPath: string | undefined = block.params.path
16611612
const regex: string | undefined = block.params.regex
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { ToolUse } from "../assistant-message"
2+
import { HandleError, PushToolResult, RemoveClosingTag } from "./types"
3+
import { Cline } from "../Cline"
4+
import { AskApproval } from "./types"
5+
import { ClineSayTool } from "../../shared/ExtensionMessage"
6+
import { getReadablePath } from "../../utils/path"
7+
import path from "path"
8+
import fs from "fs/promises"
9+
import { parseSourceCodeForDefinitionsTopLevel, parseSourceCodeDefinitionsForFile } from "../../services/tree-sitter"
10+
11+
export async function listCodeDefinitionNamesTool(
12+
cline: Cline,
13+
block: ToolUse,
14+
askApproval: AskApproval,
15+
handleError: HandleError,
16+
pushToolResult: PushToolResult,
17+
removeClosingTag: RemoveClosingTag,
18+
) {
19+
const relPath: string | undefined = block.params.path
20+
const sharedMessageProps: ClineSayTool = {
21+
tool: "listCodeDefinitionNames",
22+
path: getReadablePath(cline.cwd, removeClosingTag("path", relPath)),
23+
}
24+
try {
25+
if (block.partial) {
26+
const partialMessage = JSON.stringify({
27+
...sharedMessageProps,
28+
content: "",
29+
} satisfies ClineSayTool)
30+
await cline.ask("tool", partialMessage, block.partial).catch(() => {})
31+
return
32+
} else {
33+
if (!relPath) {
34+
cline.consecutiveMistakeCount++
35+
pushToolResult(await cline.sayAndCreateMissingParamError("list_code_definition_names", "path"))
36+
return
37+
}
38+
cline.consecutiveMistakeCount = 0
39+
const absolutePath = path.resolve(cline.cwd, relPath)
40+
let result: string
41+
try {
42+
const stats = await fs.stat(absolutePath)
43+
if (stats.isFile()) {
44+
const fileResult = await parseSourceCodeDefinitionsForFile(absolutePath, cline.rooIgnoreController)
45+
result = fileResult ?? "No source code definitions found in cline file."
46+
} else if (stats.isDirectory()) {
47+
result = await parseSourceCodeForDefinitionsTopLevel(absolutePath, cline.rooIgnoreController)
48+
} else {
49+
result = "The specified path is neither a file nor a directory."
50+
}
51+
} catch {
52+
result = `${absolutePath}: does not exist or cannot be accessed.`
53+
}
54+
const completeMessage = JSON.stringify({
55+
...sharedMessageProps,
56+
content: result,
57+
} satisfies ClineSayTool)
58+
const didApprove = await askApproval("tool", completeMessage)
59+
if (!didApprove) {
60+
return
61+
}
62+
pushToolResult(result)
63+
return
64+
}
65+
} catch (error) {
66+
await handleError("parsing source code definitions", error)
67+
return
68+
}
69+
}

0 commit comments

Comments
 (0)