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