| 1 | import { resolveRelativePathInDir } from "core/util/ideUtils"; |
| 2 | import { v4 as uuid } from "uuid"; |
| 3 | import { updateApplyState } from "../../redux/slices/sessionSlice"; |
| 4 | import { handleEditToolApplyError } from "../../redux/thunks/handleApplyStateUpdate"; |
| 5 | import { ClientToolImpl } from "./callClientTool"; |
| 6 | export const editToolImpl: ClientToolImpl = async ( |
| 7 | args, |
| 8 | toolCallId, |
| 9 | extras, |
| 10 | ) => { |
| 11 | if (!args.filepath || !args.changes) { |
| 12 | throw new Error( |
| 13 | "`filepath` and `changes` arguments are required to edit an existing file.", |
| 14 | ); |
| 15 | } |
| 16 | const firstUriMatch = await resolveRelativePathInDir( |
| 17 | args.filepath, |
| 18 | extras.ideMessenger.ide, |
| 19 | ); |
| 20 | if (!firstUriMatch) { |
| 21 | throw new Error(`${args.filepath} does not exist`); |
| 22 | } |
| 23 | const streamId = uuid(); |
| 24 | extras.dispatch( |
| 25 | updateApplyState({ |
| 26 | streamId, |
| 27 | toolCallId, |
| 28 | status: "not-started", |
| 29 | }), |
| 30 | ); |
| 31 | void extras.ideMessenger |
| 32 | .request("applyToFile", { |
| 33 | streamId, |
| 34 | text: args.changes, |
| 35 | toolCallId, |
| 36 | filepath: firstUriMatch, |
| 37 | }) |
| 38 | .then((res) => { |
| 39 | if (res.status === "error") { |
| 40 | void extras.dispatch( |
| 41 | handleEditToolApplyError({ |
| 42 | toolCallId, |
| 43 | }), |
| 44 | ); |
| 45 | } |
| 46 | }); |
| 47 | return { |
| 48 | respondImmediately: false, |
| 49 | output: undefined, // No immediate output. |
| 50 | }; |
| 51 | }; |