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