Elffuss BraidHub

kikocis/continue public

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