From 4e55b7e06ea6aecf6067745251a6fff73d57d754 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 22 Apr 2025 12:08:26 -0700 Subject: [PATCH] client tool rearchitecture --- core/core.ts | 30 +++------ gui/src/redux/store.ts | 17 ++++- gui/src/redux/thunks/callTool.ts | 77 +++++----------------- gui/src/util/clientTools/callClientTool.ts | 45 +++++++++++++ gui/src/util/clientTools/editImpl.ts | 41 ++++++++++++ 5 files changed, 125 insertions(+), 85 deletions(-) create mode 100644 gui/src/util/clientTools/callClientTool.ts create mode 100644 gui/src/util/clientTools/editImpl.ts diff --git a/core/core.ts b/core/core.ts index c3a2d25b9..27edd993a 100644 --- a/core/core.ts +++ b/core/core.ts @@ -696,15 +696,6 @@ export class Core { throw new Error("No chat model selected"); } -<<<<<<< HEAD - const contextItems = await callTool(tool, toolCall.function.arguments, { - ide: this.ide, - llm, - fetch: (url, init) => - fetchwithRequestOptions(url, init, config.requestOptions), - tool, - }); -======= // Define a callback for streaming output updates const onPartialOutput = (params: { toolCallId: string; @@ -713,26 +704,21 @@ export class Core { this.messenger.send("toolCallPartialOutput", params); }; - const contextItems = await callTool( + const contextItems = await callTool(tool, toolCall.function.arguments, { + ide: this.ide, + llm: config.selectedModelByRole.chat, + fetch: (url, init) => + fetchwithRequestOptions(url, init, config.requestOptions), tool, - JSON.parse(toolCall.function.arguments || "{}"), - { - ide: this.ide, - llm: config.selectedModelByRole.chat, - fetch: (url, init) => - fetchwithRequestOptions(url, init, config.requestOptions), - tool, - toolCallId: toolCall.id, - onPartialOutput, - }, - ); + toolCallId: toolCall.id, + onPartialOutput, + }); if (tool.faviconUrl) { contextItems.forEach((item) => { item.icon = tool.faviconUrl; }); } ->>>>>>> b8c92f26cad325ff79eca6b5d01fd4ac47b4a88f return { contextItems }; }, diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index c69e65315..4a4d939c0 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -1,4 +1,9 @@ -import { combineReducers, configureStore } from "@reduxjs/toolkit"; +import { + combineReducers, + configureStore, + ThunkDispatch, + UnknownAction, +} from "@reduxjs/toolkit"; import { createLogger } from "redux-logger"; import { createMigrate, @@ -130,11 +135,19 @@ export function setupStore() { }); } +export type ThunkExtrasType = { ideMessenger: IIdeMessenger }; + export type ThunkApiType = { state: RootState; - extra: { ideMessenger: IIdeMessenger }; + extra: ThunkExtrasType; }; +export type AppThunkDispatch = ThunkDispatch< + RootState, + ThunkExtrasType, + UnknownAction +>; + export const store = setupStore(); export type RootState = ReturnType; diff --git a/gui/src/redux/thunks/callTool.ts b/gui/src/redux/thunks/callTool.ts index 99ac82bab..afd44362f 100644 --- a/gui/src/redux/thunks/callTool.ts +++ b/gui/src/redux/thunks/callTool.ts @@ -1,9 +1,7 @@ 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 { IIdeMessenger } from "../../context/IdeMessenger"; +import { CLIENT_TOOLS } from "core/tools/builtIn"; +import { callClientTool } from "../../util/clientTools/callClientTool"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; import { selectSelectedChatModel } from "../slices/configSlice"; import { @@ -11,7 +9,6 @@ import { cancelToolCall, setCalling, setToolCallOutput, - updateApplyState, } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; @@ -38,43 +35,25 @@ export const callTool = createAsyncThunk( dispatch(setCalling()); - let errorMessage = ""; + let errorMessage: string | undefined = ""; let output: ContextItem[] | undefined = undefined; if ( - toolCallState.toolCall.function.name === BuiltInToolNames.EditExistingFile + CLIENT_TOOLS.find( + (toolName) => toolName === toolCallState.toolCall.function.name, + ) ) { - const args = JSON.parse( - toolCallState.toolCall.function.arguments || "{}", - ); - try { - if (!state.session.activeToolStreamId) { - throw new Error("Invalid apply state"); - } - await customGuiEditImpl( - args, - extra.ideMessenger, - state.session.activeToolStreamId[0], - toolCallState.toolCallId, - ); - } catch (e) { - errorMessage = `Failed to call ${toolCallState.toolCall.function.name} tool`; - if (e instanceof Error) { - errorMessage = e.message; - } - if (state.session.activeToolStreamId?.[0]) { - dispatch( - updateApplyState({ - streamId: state.session.activeToolStreamId[0], - status: "closed", - toolCallId: toolCallState.toolCallId, - numDiffs: 0, - filepath: args.filepath, - }), - ); - } - } + // Tool is called on client side + const { errorMessage: clientErrorMessage, output: clientOutput } = + await callClientTool(toolCallState.toolCall, { + dispatch, + ideMessenger: extra.ideMessenger, + activeToolStreamId: state.session.activeToolStreamId?.[0], + }); + output = clientOutput; + errorMessage = clientErrorMessage; } else { + // Tool is called on core side const result = await extra.ideMessenger.request("tools/call", { toolCall: toolCallState.toolCall, selectedModelTitle: selectedChatModel.title, @@ -112,27 +91,3 @@ export const callTool = createAsyncThunk( // okay for a tool to have no output and need some GUI trigger or other to trigger response e.g. edit tool }, ); - -async function customGuiEditImpl( - args: EditToolArgs, - ideMessenger: IIdeMessenger, - streamId: string, - toolCallId: string, -) { - const firstUriMatch = await resolveRelativePathInDir( - args.filepath, - ideMessenger.ide, - ); - if (!firstUriMatch) { - throw new Error(`${args.filepath} does not exist`); - } - const apply = await ideMessenger.request("applyToFile", { - streamId, - text: args.new_contents, - toolCallId, - filepath: firstUriMatch, - }); - if (apply.status === "error") { - throw new Error(apply.error); - } -} diff --git a/gui/src/util/clientTools/callClientTool.ts b/gui/src/util/clientTools/callClientTool.ts new file mode 100644 index 000000000..44bb7aadc --- /dev/null +++ b/gui/src/util/clientTools/callClientTool.ts @@ -0,0 +1,45 @@ +import { ContextItem, ToolCall } from "core"; +import { BuiltInToolNames } from "core/tools/builtIn"; +import { IIdeMessenger } from "../../context/IdeMessenger"; +import { AppThunkDispatch } from "../../redux/store"; +import { editToolImpl } from "./editImpl"; + +export interface ClientToolExtras { + dispatch: AppThunkDispatch; + ideMessenger: IIdeMessenger; + activeToolStreamId?: string; +} + +export interface ClientToolOutput { + output?: ContextItem[]; + errorMessage?: string; +} + +export type ClientToolImpl = ( + args: any, + toolCallId: string, + extras: ClientToolExtras, +) => Promise; + +export async function callClientTool( + toolCall: ToolCall, + extras: ClientToolExtras, +): Promise { + const args = JSON.parse(toolCall.function.arguments || "{}"); + let output: ContextItem[] | undefined = undefined; + try { + switch (toolCall.function.name) { + case BuiltInToolNames.EditExistingFile: + output = await editToolImpl(args, toolCall.id, extras); + default: + throw new Error(`Invalid client tool name ${toolCall.function.name}`); + } + return { output }; + } catch (e) { + let errorMessage = `Failed to call ${toolCall.function.name} tool`; + if (e instanceof Error) { + errorMessage = e.message; + } + return { errorMessage }; + } +} diff --git a/gui/src/util/clientTools/editImpl.ts b/gui/src/util/clientTools/editImpl.ts new file mode 100644 index 000000000..f4956e87e --- /dev/null +++ b/gui/src/util/clientTools/editImpl.ts @@ -0,0 +1,41 @@ +import { resolveRelativePathInDir } from "core/util/ideUtils"; +import { updateApplyState } from "../../redux/slices/sessionSlice"; +import { ClientToolImpl } from "./callClientTool"; + +export const editToolImpl: ClientToolImpl = async ( + args, + toolCallId, + extras, +) => { + if (!extras.activeToolStreamId) { + throw new Error("Invalid apply state"); + } + const firstUriMatch = await resolveRelativePathInDir( + args.filepath, + extras.ideMessenger.ide, + ); + if (!firstUriMatch) { + throw new Error(`${args.filepath} does not exist`); + } + const apply = await extras.ideMessenger.request("applyToFile", { + streamId: extras.activeToolStreamId, + text: args.new_contents, + toolCallId, + filepath: firstUriMatch, + }); + if (apply.status === "error") { + throw new Error(apply.error); + } + if (extras.activeToolStreamId) { + extras.dispatch( + updateApplyState({ + streamId: extras.activeToolStreamId, + status: "closed", + toolCallId, + numDiffs: 0, + filepath: args.filepath, + }), + ); + } + return undefined; +};