From 6ab17f1572e9d8a25e54680a5ffeeccf1856913d Mon Sep 17 00:00:00 2001 From: Denis Lantsman Date: Wed, 18 Dec 2024 15:27:52 -0800 Subject: [PATCH] fixup replace tool --- rplugin/node/magenta/src/tools/diff.ts | 5 ++++- rplugin/node/magenta/src/tools/insert.ts | 2 ++ rplugin/node/magenta/src/tools/replace.ts | 16 +++++++++++----- rplugin/node/magenta/src/tools/toolManager.ts | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/rplugin/node/magenta/src/tools/diff.ts b/rplugin/node/magenta/src/tools/diff.ts index c68f1c7..51ea9e5 100644 --- a/rplugin/node/magenta/src/tools/diff.ts +++ b/rplugin/node/magenta/src/tools/diff.ts @@ -29,7 +29,10 @@ export async function displayDiffs( edits: Edit[], dispatch: Dispatch, ) { - const { nvim } = context; + const { nvim, logger } = context; + logger.trace( + `Attempting to displayDiff for edits ${JSON.stringify(edits, null, 2)}`, + ); // first, check to see if any windows *other than* the magenta plugin windows are open, and close them. const windows = await nvim.windows; diff --git a/rplugin/node/magenta/src/tools/insert.ts b/rplugin/node/magenta/src/tools/insert.ts index 5f04ec3..6911435 100644 --- a/rplugin/node/magenta/src/tools/insert.ts +++ b/rplugin/node/magenta/src/tools/insert.ts @@ -5,6 +5,7 @@ import { Dispatch, Update } from "../tea/tea.ts"; import { d, VDOMNode, withBindings } from "../tea/view.ts"; import { ToolRequestId } from "./toolManager.ts"; import { displayDiffs } from "./diff.ts"; +import { context } from "../context.ts"; export type Model = { type: "insert"; @@ -97,6 +98,7 @@ export function insertThunk(model: Model) { }), ); } catch (error) { + context.logger.error(error as Error); dispatch({ type: "finish", result: { diff --git a/rplugin/node/magenta/src/tools/replace.ts b/rplugin/node/magenta/src/tools/replace.ts index e19a104..e10d705 100644 --- a/rplugin/node/magenta/src/tools/replace.ts +++ b/rplugin/node/magenta/src/tools/replace.ts @@ -5,6 +5,7 @@ import { Dispatch, Update } from "../tea/tea.ts"; import { d, VDOMNode, withBindings } from "../tea/view.ts"; import { ToolRequestId } from "./toolManager.ts"; import { displayDiffs } from "./diff.ts"; +import { context } from "../context.ts"; export type Model = { type: "replace"; @@ -98,6 +99,7 @@ export function insertThunk(model: Model) { }), ); } catch (error) { + context.logger.error(error as Error); dispatch({ type: "finish", result: { @@ -142,7 +144,11 @@ function toolStatusView({ case "editing-diff": return d`Editing diff`; case "done": - return d`Done`; + if (model.state.result.is_error) { + return d`Error: ${JSON.stringify(model.state.result.content, null, 2)}`; + } else { + return d`Done`; + } } } @@ -168,7 +174,7 @@ export function getToolResult(model: Model): ToolResultBlockParam { } export const spec: Anthropic.Anthropic.Tool = { - name: "insert", + name: "replace", description: "Replace text between two strings in a file.", input_schema: { type: "object", @@ -180,16 +186,16 @@ export const spec: Anthropic.Anthropic.Tool = { start: { type: "string", description: - "We will replace text starting with this string. This string is included in the text that is replaced. Please provide a minimal string that uniquely identifies a location in the file.", + "Replace content starting with this text. This text is included in what will be replaced. Please provide just enough text to uniquely identify a location in the file.", }, end: { type: "string", description: - "We will replace text until we encounter this string. This string is included in the text that is replaced. Please provide a minimal string that uniquely identifies a location in the file.", + "Replace content until we encounter this text. This text is included in what will be replaced. Please provide just enough text to uniquely identify a location in the file.", }, content: { type: "string", - description: "Content to insert", + description: "New content that will replace the existing text.", }, }, required: ["filePath", "start", "end", "content"], diff --git a/rplugin/node/magenta/src/tools/toolManager.ts b/rplugin/node/magenta/src/tools/toolManager.ts index df81958..16b4a0c 100644 --- a/rplugin/node/magenta/src/tools/toolManager.ts +++ b/rplugin/node/magenta/src/tools/toolManager.ts @@ -57,7 +57,7 @@ export function renderTool(model: ToolModel, dispatch: Dispatch) { dispatch({ type: "tool-msg", id: model.request.id, - msg: { type: "insert", msg }, + msg: { type: "replace", msg }, }), });