Skip to content

Commit ea42ecc

Browse files
Refactor fetch_instructions handling into separate module
1 parent d4c7493 commit ea42ecc

File tree

3 files changed

+89
-57
lines changed

3 files changed

+89
-57
lines changed

src/core/Cline.ts

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
everyLineHasLineNumbers,
3030
} from "../integrations/misc/extract-text"
3131
import { countFileLines } from "../integrations/misc/line-counter"
32-
import { fetchInstructions } from "./prompts/instructions/instructions"
32+
import { fetchInstructionsTool } from "./tools/fetchInstructionsTool"
3333
import { ExitCodeDetails } from "../integrations/terminal/TerminalProcess"
3434
import { Terminal } from "../integrations/terminal/Terminal"
3535
import { TerminalRegistry } from "../integrations/terminal/TerminalRegistry"
@@ -86,7 +86,7 @@ import { readLines } from "../integrations/misc/read-lines"
8686
import { getWorkspacePath } from "../utils/path"
8787
import { isBinaryFile } from "isbinaryfile"
8888

89-
type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
89+
export type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
9090
type UserContent = Array<Anthropic.Messages.ContentBlockParam>
9191

9292
export type ClineEvents = {
@@ -148,9 +148,11 @@ export class Cline extends EventEmitter<ClineEvents> {
148148
private askResponseText?: string
149149
private askResponseImages?: string[]
150150
private lastMessageTs?: number
151-
private consecutiveMistakeCount: number = 0
151+
// Not private since it needs to be accessible by tools
152+
consecutiveMistakeCount: number = 0
152153
private consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
153-
private providerRef: WeakRef<ClineProvider>
154+
// Not private since it needs to be accessible by tools
155+
providerRef: WeakRef<ClineProvider>
154156
private abort: boolean = false
155157
didFinishAbortingStream = false
156158
abandoned = false
@@ -2402,59 +2404,8 @@ export class Cline extends EventEmitter<ClineEvents> {
24022404
}
24032405

24042406
case "fetch_instructions": {
2405-
const task: string | undefined = block.params.task
2406-
const sharedMessageProps: ClineSayTool = {
2407-
tool: "fetchInstructions",
2408-
content: task,
2409-
}
2410-
try {
2411-
if (block.partial) {
2412-
const partialMessage = JSON.stringify({
2413-
...sharedMessageProps,
2414-
content: undefined,
2415-
} satisfies ClineSayTool)
2416-
await this.ask("tool", partialMessage, block.partial).catch(() => {})
2417-
break
2418-
} else {
2419-
if (!task) {
2420-
this.consecutiveMistakeCount++
2421-
pushToolResult(
2422-
await this.sayAndCreateMissingParamError("fetch_instructions", "task"),
2423-
)
2424-
break
2425-
}
2426-
2427-
this.consecutiveMistakeCount = 0
2428-
const completeMessage = JSON.stringify({
2429-
...sharedMessageProps,
2430-
content: task,
2431-
} satisfies ClineSayTool)
2432-
2433-
const didApprove = await askApproval("tool", completeMessage)
2434-
if (!didApprove) {
2435-
break
2436-
}
2437-
2438-
// now fetch the content and provide it to the agent.
2439-
const provider = this.providerRef.deref()
2440-
const mcpHub = provider?.getMcpHub()
2441-
if (!mcpHub) {
2442-
throw new Error("MCP hub not available")
2443-
}
2444-
const diffStrategy = this.diffStrategy
2445-
const context = provider?.context
2446-
const content = await fetchInstructions(task, { mcpHub, diffStrategy, context })
2447-
if (!content) {
2448-
pushToolResult(formatResponse.toolError(`Invalid instructions request: ${task}`))
2449-
break
2450-
}
2451-
pushToolResult(content)
2452-
break
2453-
}
2454-
} catch (error) {
2455-
await handleError("fetch instructions", error)
2456-
break
2457-
}
2407+
fetchInstructionsTool(this, block, askApproval, handleError, pushToolResult)
2408+
break
24582409
}
24592410

24602411
case "list_files": {
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 { fetchInstructions } from "../prompts/instructions/instructions"
3+
import { ClineSayTool } from "../../shared/ExtensionMessage"
4+
import { ToolUse } from "../assistant-message"
5+
import { formatResponse } from "../prompts/responses"
6+
import { AskApproval, HandleError, PushToolResult } from "./types"
7+
8+
export async function fetchInstructionsTool(
9+
cline: Cline,
10+
block: ToolUse,
11+
askApproval: AskApproval,
12+
handleError: HandleError,
13+
pushToolResult: PushToolResult,
14+
) {
15+
switch (true) {
16+
default:
17+
const task: string | undefined = block.params.task
18+
const sharedMessageProps: ClineSayTool = {
19+
tool: "fetchInstructions",
20+
content: task,
21+
}
22+
try {
23+
if (block.partial) {
24+
const partialMessage = JSON.stringify({
25+
...sharedMessageProps,
26+
content: undefined,
27+
} satisfies ClineSayTool)
28+
await cline.ask("tool", partialMessage, block.partial).catch(() => {})
29+
break
30+
} else {
31+
if (!task) {
32+
cline.consecutiveMistakeCount++
33+
pushToolResult(await cline.sayAndCreateMissingParamError("fetch_instructions", "task"))
34+
break
35+
}
36+
37+
cline.consecutiveMistakeCount = 0
38+
const completeMessage = JSON.stringify({
39+
...sharedMessageProps,
40+
content: task,
41+
} satisfies ClineSayTool)
42+
43+
const didApprove = await askApproval("tool", completeMessage)
44+
if (!didApprove) {
45+
break
46+
}
47+
48+
// now fetch the content and provide it to the agent.
49+
const provider = cline.providerRef.deref()
50+
const mcpHub = provider?.getMcpHub()
51+
if (!mcpHub) {
52+
throw new Error("MCP hub not available")
53+
}
54+
const diffStrategy = cline.diffStrategy
55+
const context = provider?.context
56+
const content = await fetchInstructions(task, { mcpHub, diffStrategy, context })
57+
if (!content) {
58+
pushToolResult(formatResponse.toolError(`Invalid instructions request: ${task}`))
59+
break
60+
}
61+
pushToolResult(content)
62+
break
63+
}
64+
} catch (error) {
65+
await handleError("fetch instructions", error)
66+
break
67+
}
68+
}
69+
}

src/core/tools/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ClineAsk, ToolProgressStatus } from "../../schemas"
2+
import { ToolResponse } from "../Cline"
3+
4+
export type AskApproval = (
5+
type: ClineAsk,
6+
partialMessage?: string,
7+
progressStatus?: ToolProgressStatus,
8+
) => Promise<boolean>
9+
10+
export type HandleError = (action: string, error: Error) => void
11+
12+
export type PushToolResult = (content: ToolResponse) => void

0 commit comments

Comments
 (0)