Elffuss BraidHub

kikocis/continue public

dfc814e4318e · 1.6 KiBRawPermalink
1import { resolveRelativePathInDir } from "core/util/ideUtils";
2import { v4 as uuid } from "uuid";
3import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate";
4import { ClientToolImpl } from "./callClientTool";
5import {
6 performFindAndReplace,
7 validateSingleEdit,
8} from "./findAndReplaceUtils";
9
10export const singleFindAndReplaceImpl: ClientToolImpl = async (
11 args,
12 toolCallId,
13 extras,
14) => {
15 const {
16 filepath,
17 old_string,
18 new_string,
19 replace_all = false,
20 editingFileContents,
21 } = args;
22
23 const streamId = uuid();
24
25 // Validate arguments
26 if (!filepath) {
27 throw new Error("filepath is required");
28 }
29 validateSingleEdit(old_string, new_string);
30
31 // Resolve the file path
32 const resolvedFilepath = await resolveRelativePathInDir(
33 filepath,
34 extras.ideMessenger.ide,
35 );
36 if (!resolvedFilepath) {
37 throw new Error(`File ${filepath} does not exist`);
38 }
39
40 // Read the current file content
41 const originalContent =
42 editingFileContents ??
43 (await extras.ideMessenger.ide.readFile(resolvedFilepath));
44
45 // Perform the find and replace operation
46 const newContent = performFindAndReplace(
47 originalContent,
48 old_string,
49 new_string,
50 replace_all,
51 );
52
53 // Apply the changes to the file
54 void extras.dispatch(
55 applyForEditTool({
56 streamId,
57 toolCallId,
58 text: newContent,
59 filepath: resolvedFilepath,
60 isSearchAndReplace: true,
61 }),
62 );
63
64 // Return success - applyToFile will handle the completion state
65 return {
66 respondImmediately: false, // Let apply state handle completion
67 output: undefined,
68 };
69};