client tool rearchitecture
This commit is contained in:
parent
3cd77bf9ab
commit
4e55b7e06e
5 changed files with 125 additions and 85 deletions
30
core/core.ts
30
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 };
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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<typeof rootReducer>;
|
||||
|
|
|
|||
|
|
@ -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<void, undefined, ThunkApiType>(
|
|||
|
||||
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<void, undefined, ThunkApiType>(
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
45
gui/src/util/clientTools/callClientTool.ts
Normal file
45
gui/src/util/clientTools/callClientTool.ts
Normal file
|
|
@ -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<ContextItem[] | undefined>;
|
||||
|
||||
export async function callClientTool(
|
||||
toolCall: ToolCall,
|
||||
extras: ClientToolExtras,
|
||||
): Promise<ClientToolOutput> {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
41
gui/src/util/clientTools/editImpl.ts
Normal file
41
gui/src/util/clientTools/editImpl.ts
Normal file
|
|
@ -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;
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue