(params: ApplyPatchParams, task: Task, callbacks: ToolCallbacks)
| 53 | } |
| 54 | |
| 55 | async execute(params: ApplyPatchParams, task: Task, callbacks: ToolCallbacks): Promise<void> { |
| 56 | const { patch } = params |
| 57 | const { askApproval, handleError, pushToolResult } = callbacks |
| 58 | |
| 59 | try { |
| 60 | // Validate required parameters |
| 61 | if (!patch) { |
| 62 | task.consecutiveMistakeCount++ |
| 63 | task.recordToolError("apply_patch") |
| 64 | pushToolResult(await task.sayAndCreateMissingParamError("apply_patch", "patch")) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | // Parse the patch |
| 69 | let parsedPatch |
| 70 | try { |
| 71 | parsedPatch = parsePatch(patch) |
| 72 | } catch (error) { |
| 73 | task.consecutiveMistakeCount++ |
| 74 | task.recordToolError("apply_patch") |
| 75 | const errorMessage = |
| 76 | error instanceof ParseError |
| 77 | ? `Invalid patch format: ${error.message}` |
| 78 | : `Failed to parse patch: ${error instanceof Error ? error.message : String(error)}` |
| 79 | pushToolResult(formatResponse.toolError(errorMessage)) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | if (parsedPatch.hunks.length === 0) { |
| 84 | pushToolResult("No file operations found in patch.") |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | // Process each hunk |
| 89 | const readFile = async (filePath: string): Promise<string> => { |
| 90 | const absolutePath = path.resolve(task.cwd, filePath) |
| 91 | return await fs.readFile(absolutePath, "utf8") |
| 92 | } |
| 93 | |
| 94 | let changes: ApplyPatchFileChange[] |
| 95 | try { |
| 96 | changes = await processAllHunks(parsedPatch.hunks, readFile) |
| 97 | } catch (error) { |
| 98 | task.consecutiveMistakeCount++ |
| 99 | task.recordToolError("apply_patch") |
| 100 | const errorMessage = `Failed to process patch: ${error instanceof Error ? error.message : String(error)}` |
| 101 | pushToolResult(formatResponse.toolError(errorMessage)) |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | // Process each file change |
| 106 | for (const change of changes) { |
| 107 | const relPath = change.path |
| 108 | const absolutePath = path.resolve(task.cwd, relPath) |
| 109 | |
| 110 | // Check access permissions |
| 111 | const accessAllowed = task.rooIgnoreController?.validateAccess(relPath) |
| 112 | if (!accessAllowed) { |
nothing calls this directly
no test coverage detected