Elffuss BraidHub

kikocis/continue public

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