Elffuss BraidHub

kikocis/continue public

59947ce7bb53 · 2.1 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 { searchReplaceToolImpl } from "./searchReplaceImpl";
8import { singleFindAndReplaceImpl } from "./singleFindAndReplaceImpl";
9
10export interface ClientToolExtras {
11 getState: () => RootState;
12 dispatch: AppThunkDispatch;
13 ideMessenger: IIdeMessenger;
14}
15
16export interface ClientToolOutput {
17 output: ContextItem[] | undefined;
18 respondImmediately: boolean;
19}
20
21export interface ClientToolResult extends ClientToolOutput {
22 errorMessage: string | undefined;
23}
24
25export type ClientToolImpl = (
26 args: any,
27 toolCallId: string,
28 extras: ClientToolExtras,
29) => Promise<ClientToolOutput>;
30
31export async function callClientTool(
32 toolCallState: ToolCallState,
33 extras: ClientToolExtras,
34): Promise<ClientToolResult> {
35 const { toolCall, parsedArgs } = toolCallState;
36 try {
37 let output: ClientToolOutput;
38 switch (toolCall.function.name) {
39 case BuiltInToolNames.EditExistingFile:
40 output = await editToolImpl(parsedArgs, toolCall.id, extras);
41 break;
42 case BuiltInToolNames.SearchAndReplaceInFile:
43 output = await searchReplaceToolImpl(parsedArgs, toolCall.id, extras);
44 break;
45 case BuiltInToolNames.SingleFindAndReplace:
46 output = await singleFindAndReplaceImpl(
47 parsedArgs,
48 toolCall.id,
49 extras,
50 );
51 break;
52 case BuiltInToolNames.MultiEdit:
53 output = await multiEditImpl(parsedArgs, toolCall.id, extras);
54 break;
55 default:
56 throw new Error(`Invalid client tool name ${toolCall.function.name}`);
57 }
58 return {
59 ...output,
60 errorMessage: undefined,
61 };
62 } catch (e) {
63 let errorMessage = `${e}`;
64 if (e instanceof Error) {
65 errorMessage = e.message;
66 }
67 return {
68 respondImmediately: true,
69 errorMessage,
70 output: undefined,
71 };
72 }
73}