Elffuss BraidHub

kikocis/continue public

6e4b7e9fc0a8 · 3.0 KiBRawPermalink
1import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch";
2import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSearchReplaceBlock";
3import { resolveRelativePathInDir } from "core/util/ideUtils";
4import posthog from "posthog-js";
5import { v4 as uuid } from "uuid";
6import { ClientToolImpl } from "./callClientTool";
7
8export const searchReplaceToolImpl: ClientToolImpl = async (
9 args,
10 toolCallId,
11 extras,
12) => {
13 const { filepath, diff } = args;
14
15 const state = extras.getState();
16 const allowAnonymousTelemetry = state.config.config.allowAnonymousTelemetry;
17
18 const streamId = uuid();
19
20 // Resolve the file path
21 const resolvedFilepath = await resolveRelativePathInDir(
22 filepath,
23 extras.ideMessenger.ide,
24 );
25 if (!resolvedFilepath) {
26 throw new Error(`File ${filepath} does not exist`);
27 }
28
29 // Parse all search/replace blocks from the diff content
30 const blocks = parseAllSearchReplaceBlocks(diff);
31
32 if (blocks.length === 0) {
33 throw new Error("No complete search/replace blocks found");
34 }
35
36 try {
37 // Read the current file content
38 const originalContent =
39 await extras.ideMessenger.ide.readFile(resolvedFilepath);
40 let currentContent = originalContent;
41
42 // Apply all replacements sequentially to build the final content
43 for (let i = 0; i < blocks.length; i++) {
44 const block = blocks[i];
45 const { searchContent, replaceContent } = block;
46
47 // Find the search content in the current state of the file
48 const match = findSearchMatch(currentContent, searchContent || "");
49
50 // Because we don't have access to use hooks, we check `allowAnonymousTelemetry`
51 // directly rather than using `CustomPostHogProvider`
52 if (allowAnonymousTelemetry) {
53 // Capture telemetry for tool calls
54 posthog.capture("find_replace_match_result", {
55 matchStrategy: match?.strategyName ?? "noMatch",
56 });
57 }
58
59 if (!match) {
60 throw new Error(
61 `Search content not found in block ${i + 1}:\n${searchContent}`,
62 );
63 }
64
65 // Apply the replacement
66 currentContent =
67 currentContent.substring(0, match.startIndex) +
68 (replaceContent || "") +
69 currentContent.substring(match.endIndex);
70 }
71
72 // Single applyToFile call with all accumulated changes
73 // This works becaues of our logic in `applyCodeBlock` that determines
74 // that the full file rewrite here can be applied instantly, so the diff
75 // lines are just st
76 await extras.ideMessenger.request("applyToFile", {
77 streamId,
78 toolCallId,
79 text: currentContent,
80 filepath: resolvedFilepath,
81 isSearchAndReplace: true,
82 });
83
84 // Return success - applyToFile will handle the completion state
85 return {
86 respondImmediately: false, // Let apply state handle completion
87 output: undefined,
88 };
89 } catch (error) {
90 throw new Error(
91 `Failed to apply search and replace: ${error instanceof Error ? error.message : String(error)}`,
92 );
93 }
94};