| 1 | import { |
| 2 | inferResolvedUriFromRelativePath, |
| 3 | resolveRelativePathInDir, |
| 4 | } from "core/util/ideUtils"; |
| 5 | import { v4 as uuid } from "uuid"; |
| 6 | import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate"; |
| 7 | import { ClientToolImpl } from "./callClientTool"; |
| 8 | import { |
| 9 | performFindAndReplace, |
| 10 | validateCreatingForMultiEdit, |
| 11 | validateSingleEdit, |
| 12 | } from "./findAndReplaceUtils"; |
| 13 | |
| 14 | export const multiEditImpl: ClientToolImpl = async ( |
| 15 | args, |
| 16 | toolCallId, |
| 17 | extras, |
| 18 | ) => { |
| 19 | const { filepath, edits, editingFileContents } = args; |
| 20 | |
| 21 | const streamId = uuid(); |
| 22 | |
| 23 | // Validate arguments |
| 24 | if (!filepath) { |
| 25 | throw new Error("filepath is required"); |
| 26 | } |
| 27 | if (!edits || !Array.isArray(edits) || edits.length === 0) { |
| 28 | throw new Error( |
| 29 | "edits array is required and must contain at least one edit", |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | // Validate each edit operation |
| 34 | for (let i = 0; i < edits.length; i++) { |
| 35 | const edit = edits[i]; |
| 36 | validateSingleEdit(edit.old_string, edit.new_string, i); |
| 37 | } |
| 38 | |
| 39 | // Check if this is creating a new file (first edit has empty old_string) |
| 40 | const isCreatingNewFile = validateCreatingForMultiEdit(edits); |
| 41 | const resolvedUri = await resolveRelativePathInDir( |
| 42 | filepath, |
| 43 | extras.ideMessenger.ide, |
| 44 | ); |
| 45 | |
| 46 | let newContent: string; |
| 47 | let fileUri: string; |
| 48 | if (isCreatingNewFile) { |
| 49 | if (resolvedUri) { |
| 50 | throw new Error( |
| 51 | `file ${filepath} already exists, cannot create new file`, |
| 52 | ); |
| 53 | } |
| 54 | newContent = edits[0].new_string; |
| 55 | const dirs = await extras.ideMessenger.ide.getWorkspaceDirs(); |
| 56 | fileUri = await inferResolvedUriFromRelativePath( |
| 57 | filepath, |
| 58 | extras.ideMessenger.ide, |
| 59 | dirs, |
| 60 | ); |
| 61 | } else { |
| 62 | if (!resolvedUri) { |
| 63 | throw new Error( |
| 64 | `file ${filepath} does not exist. If you are trying to edit it, correct the filepath. If you are trying to create it, you must pass old_string=""`, |
| 65 | ); |
| 66 | } |
| 67 | newContent = |
| 68 | editingFileContents ?? |
| 69 | (await extras.ideMessenger.ide.readFile(resolvedUri)); |
| 70 | fileUri = resolvedUri; |
| 71 | for (let i = 0; i < edits.length; i++) { |
| 72 | const { old_string, new_string, replace_all } = edits[i]; |
| 73 | newContent = performFindAndReplace( |
| 74 | newContent, |
| 75 | old_string, |
| 76 | new_string, |
| 77 | replace_all, |
| 78 | i, |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Apply the changes to the file |
| 84 | void extras.dispatch( |
| 85 | applyForEditTool({ |
| 86 | streamId, |
| 87 | toolCallId, |
| 88 | text: newContent, |
| 89 | filepath: fileUri, |
| 90 | isSearchAndReplace: true, |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | // Return success - applyToFile will handle the completion state |
| 95 | return { |
| 96 | respondImmediately: false, // Let apply state handle completion |
| 97 | output: undefined, |
| 98 | }; |
| 99 | }; |