Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 3.8 KiBRawPermalink
1import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
2import { ContextItem } from "core";
3import { CLIENT_TOOLS } from "core/tools/builtIn";
4import { callClientTool } from "../../util/clientTools/callClientTool";
5import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall";
6import { selectSelectedChatModel } from "../slices/configSlice";
7import {
8 acceptToolCall,
9 errorToolCall,
10 setToolCallCalling,
11 updateToolCallOutput,
12} from "../slices/sessionSlice";
13import { ThunkApiType } from "../store";
14import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
15
16export const callCurrentTool = createAsyncThunk<void, undefined, ThunkApiType>(
17 "chat/callTool",
18 async (_, { dispatch, extra, getState }) => {
19 const state = getState();
20 const toolCallState = selectCurrentToolCall(state);
21
22 if (!toolCallState) {
23 return;
24 }
25
26 if (toolCallState.status !== "generated") {
27 return;
28 }
29
30 const selectedChatModel = selectSelectedChatModel(state);
31
32 if (!selectedChatModel) {
33 throw new Error("No model selected");
34 }
35
36 const { toolCallId } = toolCallState;
37
38 dispatch(
39 setToolCallCalling({
40 toolCallId,
41 }),
42 );
43
44 let output: ContextItem[] | undefined = undefined;
45 let errorMessage: string | undefined = undefined;
46 let streamResponse: boolean;
47
48 // IMPORTANT:
49 // Errors that occur while calling tool call implementations
50 // Are caught and passed in output as context items
51 // Errors that occur outside specifically calling the tool
52 // Should not be caught here - should be handled as normal stream errors
53 if (
54 CLIENT_TOOLS.find(
55 (toolName) => toolName === toolCallState.toolCall.function.name,
56 )
57 ) {
58 // Tool is called on client side
59 const {
60 output: clientToolOuput,
61 respondImmediately,
62 errorMessage: clientToolError,
63 } = await callClientTool(toolCallState.toolCall, {
64 dispatch,
65 ideMessenger: extra.ideMessenger,
66 streamId: state.session.codeBlockApplyStates.states.find(
67 (state) =>
68 state.toolCallId && state.toolCallId === toolCallState.toolCallId,
69 )?.streamId,
70 getState,
71 });
72 output = clientToolOuput;
73 errorMessage = clientToolError;
74 streamResponse = respondImmediately;
75 } else {
76 // Tool is called on core side
77 const result = await extra.ideMessenger.request("tools/call", {
78 toolCall: toolCallState.toolCall,
79 });
80 if (result.status === "error") {
81 throw new Error(result.error);
82 } else {
83 output = result.content.contextItems;
84 errorMessage = result.content.errorMessage;
85 }
86 streamResponse = true;
87 }
88
89 if (errorMessage) {
90 dispatch(
91 updateToolCallOutput({
92 toolCallId,
93 contextItems: [
94 {
95 icon: "problems",
96 name: "Tool Call Error",
97 description: "Tool Call Failed",
98 content: `${toolCallState.toolCall.function.name} failed with the message: ${errorMessage}\n\nPlease try something else or request further instructions.`,
99 hidden: false,
100 },
101 ],
102 }),
103 );
104 } else if (output?.length) {
105 dispatch(
106 updateToolCallOutput({
107 toolCallId,
108 contextItems: output,
109 }),
110 );
111 }
112
113 if (streamResponse) {
114 if (errorMessage) {
115 dispatch(
116 errorToolCall({
117 toolCallId,
118 }),
119 );
120 } else {
121 dispatch(
122 acceptToolCall({
123 toolCallId,
124 }),
125 );
126 }
127
128 // Send to the LLM to continue the conversation
129 const wrapped = await dispatch(
130 streamResponseAfterToolCall({
131 toolCallId,
132 }),
133 );
134 unwrapResult(wrapped);
135 }
136 },
137);