| 1 | import { validateMultiEdit } from "core/edit/searchAndReplace/multiEditValidation"; |
| 2 | import { executeMultiFindAndReplace } from "core/edit/searchAndReplace/performReplace"; |
| 3 | import { v4 as uuid } from "uuid"; |
| 4 | import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate"; |
| 5 | import { ClientToolImpl } from "./callClientTool"; |
| 6 | import { validateSearchAndReplaceFilepath } from "./singleFindAndReplaceImpl"; |
| 7 | |
| 8 | export const multiEditImpl: ClientToolImpl = async ( |
| 9 | args, |
| 10 | toolCallId, |
| 11 | extras, |
| 12 | ) => { |
| 13 | const { filepath, editingFileContents } = args; |
| 14 | |
| 15 | const resolvedUri = await validateSearchAndReplaceFilepath( |
| 16 | filepath, |
| 17 | extras.ideMessenger.ide, |
| 18 | ); |
| 19 | |
| 20 | const { edits } = validateMultiEdit(args); |
| 21 | |
| 22 | const currentContent = |
| 23 | editingFileContents ?? |
| 24 | (await extras.ideMessenger.ide.readFile(resolvedUri)); |
| 25 | const fileUri = resolvedUri; |
| 26 | |
| 27 | const newContent = executeMultiFindAndReplace(currentContent, edits); |
| 28 | |
| 29 | const streamId = uuid(); |
| 30 | void extras.dispatch( |
| 31 | applyForEditTool({ |
| 32 | streamId, |
| 33 | toolCallId, |
| 34 | text: newContent, |
| 35 | filepath: fileUri, |
| 36 | isSearchAndReplace: true, |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | // Return success - applyToFile will handle the completion state |
| 41 | return { |
| 42 | respondImmediately: false, // Let apply state handle completion |
| 43 | output: undefined, |
| 44 | }; |
| 45 | }; |