diff --git a/core/core.ts b/core/core.ts index 911ea0072..b1393f8cd 100644 --- a/core/core.ts +++ b/core/core.ts @@ -36,7 +36,6 @@ import { TTS } from "./util/tts"; import { DiffLine, - ToolExtras, type ContextItemId, type IDE, type IndexingProgressUpdate, @@ -873,7 +872,7 @@ export class Core { }); on("tools/call", async (msg) => { - const { toolCall, selectedModelTitle, applyStreamId } = msg.data; + const { toolCall, selectedModelTitle } = msg.data; const { config } = await this.configHandler.loadConfig(); if (!config) { throw new Error("Config not loaded"); @@ -889,19 +888,6 @@ export class Core { const llm = await this.configHandler.llmFromTitle(selectedModelTitle); - let applyToFile: ToolExtras["applyToFile"] = undefined; - if (applyStreamId) { - applyToFile = async (path: string, contents: string) => { - return await this.messenger.request("applyToFile", { - filepath: path, - text: contents, - streamId: applyStreamId, - curSelectedModelTitle: - config.selectedModelByRole.apply?.title ?? selectedModelTitle, - }); - }; - } - const contextItems = await callTool( tool, JSON.parse(toolCall.function.arguments || "{}"), @@ -911,7 +897,6 @@ export class Core { fetch: (url, init) => fetchwithRequestOptions(url, init, config.requestOptions), tool, - applyToFile, }, ); diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index d90259a9b..f1909afc5 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -17,6 +17,7 @@ import { import { updateIndexingStatus } from "../redux/slices/indexingSlice"; import { addContextItemsAtIndex, + selectLastEditToolApplyState, setInactive, } from "../redux/slices/sessionSlice"; import { setTTSActive } from "../redux/slices/uiSlice"; @@ -245,6 +246,13 @@ function useSetup() { }, [defaultModel], ); + + const lastEditToolApplyState = useAppSelector(selectLastEditToolApplyState); + useEffect(() => { + ideMessenger.post("showToast", ["info", "updated last apply state"]); + // dispatch(setToolCallOutput(output)); + // dispatch(acceptToolCall()); + }, [lastEditToolApplyState]); } export default useSetup; diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 51af56ee7..544a8c401 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -21,7 +21,6 @@ import { ToolCallDelta, ToolCallState, } from "core"; -import { BuiltInToolNames } from "core/tools/builtIn"; import { NEW_SESSION_TITLE } from "core/util/constants"; import { incrementalParseJson } from "core/util/incrementalParseJson"; import { renderChatMessage } from "core/util/messageContent"; @@ -368,12 +367,6 @@ export const sessionSlice = createSlice({ ); } - if ( - toolCallDelta.function?.name === - BuiltInToolNames.EditExistingFile - ) { - state.lastApplyToolStreamId = uuidv4(); - } historyItem.toolCallState = toolCallDeltaToState(toolCallDelta); } state.history.push(historyItem); @@ -559,7 +552,9 @@ export const sessionSlice = createSlice({ state.history[state.history.length - 1].contextItems = contextItems; }, - + setLastToolApplyStreamId: (state, { payload }: PayloadAction) => { + state.lastApplyToolStreamId = payload; + }, updateCurCheckpoint: ( state, { payload }: PayloadAction<{ filepath: string; content: string }>, @@ -781,6 +776,7 @@ export const { deleteSessionMetadata, setNewestCodeblocksForInput, cycleMode, + setLastToolApplyStreamId, } = sessionSlice.actions; export const { diff --git a/gui/src/redux/thunks/callTool.ts b/gui/src/redux/thunks/callTool.ts index 8df0ccfa9..190c25ccf 100644 --- a/gui/src/redux/thunks/callTool.ts +++ b/gui/src/redux/thunks/callTool.ts @@ -1,10 +1,17 @@ import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; +import { ContextItem } from "core"; +import { BuiltInToolNames } from "core/tools/builtIn"; +import { EditToolArgs } from "core/tools/definitions/editFile"; +import { resolveRelativePathInDir } from "core/util/ideUtils"; +import { v4 as uuidv4 } from "uuid"; +import { IIdeMessenger } from "../../context/IdeMessenger"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; import { selectDefaultModel } from "../slices/configSlice"; import { acceptToolCall, cancelToolCall, setCalling, + setLastToolApplyStreamId, setToolCallOutput, } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; @@ -31,28 +38,46 @@ export const callTool = createAsyncThunk( dispatch(setCalling()); - const result = await extra.ideMessenger.request("tools/call", { - toolCall: toolCallState.toolCall, - selectedModelTitle: defaultModel.title, - }); + let errorMessage = ""; + let output: ContextItem[] | undefined = undefined; - if (result.status === "success") { - const contextItems = result.content.contextItems; - dispatch(setToolCallOutput(contextItems)); - dispatch(acceptToolCall()); - - // Send to the LLM to continue the conversation - const response = await dispatch( - streamResponseAfterToolCall({ - toolCallId: toolCallState.toolCall.id, - toolOutput: contextItems, - }), + if ( + toolCallState.toolCall.function.name === BuiltInToolNames.EditExistingFile + ) { + const newToolStreamId = uuidv4(); + dispatch(setLastToolApplyStreamId(newToolStreamId)); + const args = JSON.parse( + toolCallState.toolCall.function.arguments || "{}", ); - unwrapResult(response); + try { + await customGuiEditImpl( + args, + extra.ideMessenger, + newToolStreamId, + defaultModel.title, + ); + } catch (e) { + errorMessage = "Failed to call edit tool"; + if (e instanceof Error) { + errorMessage = e.message; + } + } } else { + const result = await extra.ideMessenger.request("tools/call", { + toolCall: toolCallState.toolCall, + selectedModelTitle: defaultModel.title, + }); + if (result.status === "error") { + errorMessage = result.error; + } else { + output = result.content.contextItems; + } + } + + if (errorMessage) { dispatch(cancelToolCall()); - const output = await dispatch( + const wrapped = await dispatch( streamResponseAfterToolCall({ toolCallId: toolCallState.toolCallId, toolOutput: [ @@ -60,34 +85,52 @@ export const callTool = createAsyncThunk( icon: "problems", name: "Tool Call Error", description: "Tool Call Failed", - content: `The tool call failed with the message:\n\n${result.error}\n\nPlease try something else or request further instructions.`, + content: `The tool call failed with the message:\n\n${errorMessage}\n\nPlease try something else or request further instructions.`, hidden: false, }, ], }), ); - unwrapResult(output); + unwrapResult(wrapped); + } else if (output) { + dispatch(setToolCallOutput(output)); + dispatch(acceptToolCall()); + + // Send to the LLM to continue the conversation + const wrapped = await dispatch( + streamResponseAfterToolCall({ + toolCallId: toolCallState.toolCall.id, + toolOutput: output, + }), + ); + unwrapResult(wrapped); } + // okay for a tool to have no output and need some GUI trigger or other to trigger response e.g. edit tool }, ); -// export const editFileImpl: ToolImpl = async (args, extras) => { -// const firstUriMatch = await resolveRelativePathInDir( -// args.filepath, -// extras.ide, -// ); -// if (!firstUriMatch) { -// throw new Error(`File ${args.filepath} does not exist.`); -// } -// if (!extras.applyToFile) { -// throw new Error("Failed to apply to file: invalid apply stream id"); -// } -// await extras.applyToFile(firstUriMatch, args.new_contents); -// return [ -// { -// name: "Edit results", -// description: `Edited ${args.filepath}`, -// content: `Applied edit diffs to ${args.filepath}. The user must manually reject/accept diffs. Prompt them to do so in your response`, -// }, -// ]; -// }; +async function customGuiEditImpl( + args: EditToolArgs, + ideMessenger: IIdeMessenger, + streamId: string, + modelTitle: string, +) { + const firstUriMatch = await resolveRelativePathInDir( + args.filepath, + ideMessenger.ide, + ); + if (!firstUriMatch) { + return [ + { + name: "Edit failure", + description: `editing ${args.filepath}`, + content: `Failed to edit ${args.filepath}: does not exist`, + }, + ]; + } + ideMessenger.post("applyToFile", { + streamId, + text: args.new_contents, + curSelectedModelTitle: modelTitle, + }); +}