From 0e7709d85e2b4243f6275c5e752c95b0c669e9da Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 26 Jun 2025 18:51:53 -0700 Subject: [PATCH] feat: dont use underlying apply logic --- core/index.d.ts | 1 + extensions/vscode/src/apply/ApplyManager.ts | 31 ++++++++++++++----- gui/src/util/clientTools/searchReplaceImpl.ts | 16 +++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index 726beb5c3..1aa65afa4 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1297,6 +1297,7 @@ export interface ApplyToFilePayload { filepath?: string; text: string; toolCallId?: string; + isSearchAndReplace?: boolean; } export interface RangeInFileWithContents { diff --git a/extensions/vscode/src/apply/ApplyManager.ts b/extensions/vscode/src/apply/ApplyManager.ts index 11f15feab..21d08836f 100644 --- a/extensions/vscode/src/apply/ApplyManager.ts +++ b/extensions/vscode/src/apply/ApplyManager.ts @@ -4,6 +4,8 @@ import { getUriPathBasename } from "core/util/uri"; import * as vscode from "vscode"; import { ApplyToFilePayload } from "core"; +import { myersDiff } from "core/diff/myers"; +import { generateLines } from "core/diff/util"; import { ApplyAbortManager } from "core/edit/applyAbortManager"; import { VerticalDiffManager } from "../diff/vertical/manager"; import { VsCodeIde } from "../VsCodeIde"; @@ -25,6 +27,7 @@ export class ApplyManager { filepath, text, toolCallId, + isSearchAndReplace, }: ApplyToFilePayload) { await this.webviewProtocol.request("updateApplyState", { streamId, @@ -43,16 +46,30 @@ export class ApplyManager { return; } - // Regular apply logic const hasExistingDocument = !!activeTextEditor.document.getText().trim(); if (hasExistingDocument) { - await this.handleExistingDocument( - activeTextEditor, - text, - streamId, - toolCallId, - ); + // Currently `isSearchAndReplace` will always provide a full file rewrite + // as the contents of `text`, so we can just instantly apply + if (isSearchAndReplace) { + const diffLinesGenerator = generateLines( + myersDiff(activeTextEditor.document.getText(), text), + ); + + await this.verticalDiffManager.streamDiffLines( + diffLinesGenerator, + true, + streamId, + toolCallId, + ); + } else { + await this.handleExistingDocument( + activeTextEditor, + text, + streamId, + toolCallId, + ); + } } else { await this.handleEmptyDocument( activeTextEditor, diff --git a/gui/src/util/clientTools/searchReplaceImpl.ts b/gui/src/util/clientTools/searchReplaceImpl.ts index bb665b851..cbab201f3 100644 --- a/gui/src/util/clientTools/searchReplaceImpl.ts +++ b/gui/src/util/clientTools/searchReplaceImpl.ts @@ -1,6 +1,7 @@ import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch"; import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSearchReplaceBlock"; import { resolveRelativePathInDir } from "core/util/ideUtils"; +import { v4 as uuid } from "uuid"; import { ClientToolImpl } from "./callClientTool"; export const searchReplaceToolImpl: ClientToolImpl = async ( @@ -10,10 +11,7 @@ export const searchReplaceToolImpl: ClientToolImpl = async ( ) => { const { filepath, diff } = args; - // Check if we have a streamId from a pre-existing apply state - if (!extras.streamId) { - throw new Error("Invalid apply state - no streamId available"); - } + const streamId = uuid(); // Resolve the file path const resolvedFilepath = await resolveRelativePathInDir( @@ -58,11 +56,15 @@ export const searchReplaceToolImpl: ClientToolImpl = async ( } // Single applyToFile call with all accumulated changes + // This works becaues of our logic in `applyCodeBlock` that determines + // that the full file rewrite here can be applied instantly, so the diff + // lines are just st await extras.ideMessenger.request("applyToFile", { - text: currentContent, - streamId: extras.streamId, - filepath: resolvedFilepath, + streamId, toolCallId, + text: currentContent, + filepath: resolvedFilepath, + isSearchAndReplace: true, }); // Return success - applyToFile will handle the completion state