Skip to content

Commit 36900b9

Browse files
committed
Move execute_command to a tool file
1 parent 3836508 commit 36900b9

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

src/core/Cline.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import { searchAndReplaceTool } from "./tools/searchAndReplaceTool"
9292
import { listCodeDefinitionNamesTool } from "./tools/listCodeDefinitionNamesTool"
9393
import { searchFilesTool } from "./tools/searchFilesTool"
9494
import { browserActionTool } from "./tools/browserActionTool"
95+
import { executeCommandTool } from "./tools/executeCommandTool"
9596

9697
export type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
9798
type UserContent = Array<Anthropic.Messages.ContentBlockParam>
@@ -181,7 +182,7 @@ export class Cline extends EventEmitter<ClineEvents> {
181182
private presentAssistantMessageHasPendingUpdates = false
182183
private userMessageContent: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] = []
183184
private userMessageContentReady = false
184-
private didRejectTool = false
185+
didRejectTool = false
185186
private didAlreadyUseTool = false
186187
private didCompleteReadingStream = false
187188

@@ -1618,52 +1619,15 @@ export class Cline extends EventEmitter<ClineEvents> {
16181619
break
16191620
}
16201621
case "execute_command": {
1621-
const command: string | undefined = block.params.command
1622-
const customCwd: string | undefined = block.params.cwd
1623-
try {
1624-
if (block.partial) {
1625-
await this.ask("command", removeClosingTag("command", command), block.partial).catch(
1626-
() => {},
1627-
)
1628-
break
1629-
} else {
1630-
if (!command) {
1631-
this.consecutiveMistakeCount++
1632-
pushToolResult(
1633-
await this.sayAndCreateMissingParamError("execute_command", "command"),
1634-
)
1635-
break
1636-
}
1637-
1638-
const ignoredFileAttemptedToAccess = this.rooIgnoreController?.validateCommand(command)
1639-
if (ignoredFileAttemptedToAccess) {
1640-
await this.say("rooignore_error", ignoredFileAttemptedToAccess)
1641-
pushToolResult(
1642-
formatResponse.toolError(
1643-
formatResponse.rooIgnoreError(ignoredFileAttemptedToAccess),
1644-
),
1645-
)
1646-
1647-
break
1648-
}
1649-
1650-
this.consecutiveMistakeCount = 0
1651-
1652-
const didApprove = await askApproval("command", command)
1653-
if (!didApprove) {
1654-
break
1655-
}
1656-
const [userRejected, result] = await this.executeCommandTool(command, customCwd)
1657-
if (userRejected) {
1658-
this.didRejectTool = true
1659-
}
1660-
pushToolResult(result)
1661-
break
1662-
}
1663-
} catch (error) {
1664-
await handleError("executing command", error)
1665-
break
1666-
}
1622+
await executeCommandTool(
1623+
this,
1624+
block,
1625+
askApproval,
1626+
handleError,
1627+
pushToolResult,
1628+
removeClosingTag,
1629+
)
1630+
break
16671631
}
16681632
case "use_mcp_tool": {
16691633
const server_name: string | undefined = block.params.server_name
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Cline } from "../Cline"
2+
import { ToolUse } from "../assistant-message"
3+
import { AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "./types"
4+
import { formatResponse } from "../prompts/responses"
5+
6+
export async function executeCommandTool(
7+
cline: Cline,
8+
block: ToolUse,
9+
askApproval: AskApproval,
10+
handleError: HandleError,
11+
pushToolResult: PushToolResult,
12+
removeClosingTag: RemoveClosingTag,
13+
) {
14+
const command: string | undefined = block.params.command
15+
const customCwd: string | undefined = block.params.cwd
16+
try {
17+
if (block.partial) {
18+
await cline.ask("command", removeClosingTag("command", command), block.partial).catch(() => {})
19+
return
20+
} else {
21+
if (!command) {
22+
cline.consecutiveMistakeCount++
23+
pushToolResult(await cline.sayAndCreateMissingParamError("execute_command", "command"))
24+
return
25+
}
26+
27+
const ignoredFileAttemptedToAccess = cline.rooIgnoreController?.validateCommand(command)
28+
if (ignoredFileAttemptedToAccess) {
29+
await cline.say("rooignore_error", ignoredFileAttemptedToAccess)
30+
pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(ignoredFileAttemptedToAccess)))
31+
32+
return
33+
}
34+
35+
cline.consecutiveMistakeCount = 0
36+
37+
const didApprove = await askApproval("command", command)
38+
if (!didApprove) {
39+
return
40+
}
41+
const [userRejected, result] = await cline.executeCommandTool(command, customCwd)
42+
if (userRejected) {
43+
cline.didRejectTool = true
44+
}
45+
pushToolResult(result)
46+
return
47+
}
48+
} catch (error) {
49+
await handleError("executing command", error)
50+
return
51+
}
52+
}

0 commit comments

Comments
 (0)