Elffuss BraidHub

kikocis/continue public

6c0ea8c4e384 · 1.7 KiBRawPermalink
1import { ContextItem, ToolCallState } from "core";
2import { BuiltInToolNames } from "core/tools/builtIn";
3import { IIdeMessenger } from "../../context/IdeMessenger";
4import { AppThunkDispatch, RootState } from "../../redux/store";
5import { editToolImpl } from "./editImpl";
6import { searchReplaceToolImpl } from "./searchReplaceImpl";
7
8export interface ClientToolExtras {
9 getState: () => RootState;
10 dispatch: AppThunkDispatch;
11 ideMessenger: IIdeMessenger;
12 streamId?: string;
13}
14
15export interface ClientToolOutput {
16 output: ContextItem[] | undefined;
17 respondImmediately: boolean;
18}
19
20export interface ClientToolResult extends ClientToolOutput {
21 errorMessage: string | undefined;
22}
23
24export type ClientToolImpl = (
25 args: any,
26 toolCallId: string,
27 extras: ClientToolExtras,
28) => Promise<ClientToolOutput>;
29
30export async function callClientTool(
31 toolCallState: ToolCallState,
32 extras: ClientToolExtras,
33): Promise<ClientToolResult> {
34 const { toolCall, parsedArgs } = toolCallState;
35 try {
36 let output: ClientToolOutput;
37 switch (toolCall.function.name) {
38 case BuiltInToolNames.EditExistingFile:
39 output = await editToolImpl(parsedArgs, toolCall.id, extras);
40 break;
41 case BuiltInToolNames.SearchAndReplaceInFile:
42 output = await searchReplaceToolImpl(parsedArgs, toolCall.id, extras);
43 break;
44 default:
45 throw new Error(`Invalid client tool name ${toolCall.function.name}`);
46 }
47 return {
48 ...output,
49 errorMessage: undefined,
50 };
51 } catch (e) {
52 let errorMessage = `${e}`;
53 if (e instanceof Error) {
54 errorMessage = e.message;
55 }
56 return {
57 respondImmediately: true,
58 errorMessage,
59 output: undefined,
60 };
61 }
62}