Elffuss BraidHub

kikocis/continue public

f840a2b4e8a4 · 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 { 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 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: clientToolOutput,
65 respondImmediately,
66 errorMessage: clientToolError,
67 } = await callClientTool(toolCallState, {
68 dispatch,
69 ideMessenger: extra.ideMessenger,
70 getState,
71 });
72 output = clientToolOutput;
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 // Because we don't have access to use hooks, we check `allowAnonymousTelemetry`
114 // directly rather than using `CustomPostHogProvider`
115 if (allowAnonymousTelemetry) {
116 // Capture telemetry for tool calls
117 posthog.capture("gui_tool_call_outcome", {
118 succeeded: errorMessage === undefined,
119 toolName: toolCallState.toolCall.function.name,
120 errorMessage: errorMessage,
121 });
122 }
123
124 if (streamResponse) {
125 if (errorMessage) {
126 logToolUsage(toolCallState, false, false, extra.ideMessenger, output);
127 dispatch(
128 errorToolCall({
129 toolCallId,
130 }),
131 );
132 } else {
133 logToolUsage(toolCallState, true, true, extra.ideMessenger, output);
134 dispatch(
135 acceptToolCall({
136 toolCallId,
137 }),
138 );
139 }
140
141 // Send to the LLM to continue the conversation
142 const wrapped = await dispatch(
143 streamResponseAfterToolCall({
144 toolCallId,
145 }),
146 );
147 unwrapResult(wrapped);
148 }
149 },
150);