Elffuss BraidHub

kikocis/continue public

dfc814e4318e · 1.9 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 { multiEditImpl } from "./multiEditImpl";
7import { singleFindAndReplaceImpl } from "./singleFindAndReplaceImpl";
8
9export interface ClientToolExtras {
10 getState: () => RootState;
11 dispatch: AppThunkDispatch;
12 ideMessenger: IIdeMessenger;
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.SingleFindAndReplace:
42 output = await singleFindAndReplaceImpl(
43 parsedArgs,
44 toolCall.id,
45 extras,
46 );
47 break;
48 case BuiltInToolNames.MultiEdit:
49 output = await multiEditImpl(parsedArgs, toolCall.id, extras);
50 break;
51 default:
52 throw new Error(`Invalid client tool name ${toolCall.function.name}`);
53 }
54 return {
55 ...output,
56 errorMessage: undefined,
57 };
58 } catch (e) {
59 let errorMessage = `${e}`;
60 if (e instanceof Error) {
61 errorMessage = e.message;
62 }
63 return {
64 respondImmediately: true,
65 errorMessage,
66 output: undefined,
67 };
68 }
69}