Elffuss BraidHub

kikocis/continue public

54bc82ab2ee9 · 4.2 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 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 // Check if telemetry is enabled
49 const allowAnonymousTelemetry = state.config.config.allowAnonymousTelemetry;
50
51 // IMPORTANT:
52 // Errors that occur while calling tool call implementations
53 // Are caught and passed in output as context items
54 // Errors that occur outside specifically calling the tool
55 // Should not be caught here - should be handled as normal stream errors
56 if (
57 CLIENT_TOOLS_IMPLS.find(
58 (toolName) => toolName === toolCallState.toolCall.function.name,
59 )
60 ) {
61 // Tool is called on client side
62 const {
63 output: clientToolOutput,
64 respondImmediately,
65 errorMessage: clientToolError,
66 } = await callClientTool(toolCallState, {
67 dispatch,
68 ideMessenger: extra.ideMessenger,
69 getState,
70 });
71 output = clientToolOutput;
72 errorMessage = clientToolError;
73 streamResponse = respondImmediately;
74 } else {
75 // Tool is called on core side
76 const result = await extra.ideMessenger.request("tools/call", {
77 toolCall: toolCallState.toolCall,
78 });
79 if (result.status === "error") {
80 throw new Error(result.error);
81 } else {
82 output = result.content.contextItems;
83 errorMessage = result.content.errorMessage;
84 }
85 streamResponse = true;
86 }
87
88 if (errorMessage) {
89 dispatch(
90 updateToolCallOutput({
91 toolCallId,
92 contextItems: [
93 {
94 icon: "problems",
95 name: "Tool Call Error",
96 description: "Tool Call Failed",
97 content: `${toolCallState.toolCall.function.name} failed with the message: ${errorMessage}\n\nPlease try something else or request further instructions.`,
98 hidden: false,
99 },
100 ],
101 }),
102 );
103 } else if (output?.length) {
104 dispatch(
105 updateToolCallOutput({
106 toolCallId,
107 contextItems: output,
108 }),
109 );
110 }
111
112 // Because we don't have access to use hooks, we check `allowAnonymousTelemetry`
113 // directly rather than using `CustomPostHogProvider`
114 if (allowAnonymousTelemetry) {
115 // Capture telemetry for tool calls
116 posthog.capture("gui_tool_call_outcome", {
117 succeeded: errorMessage === undefined,
118 toolName: toolCallState.toolCall.function.name,
119 errorMessage: errorMessage,
120 });
121 }
122
123 if (streamResponse) {
124 if (errorMessage) {
125 dispatch(
126 errorToolCall({
127 toolCallId,
128 }),
129 );
130 } else {
131 dispatch(
132 acceptToolCall({
133 toolCallId,
134 }),
135 );
136 }
137
138 // Send to the LLM to continue the conversation
139 const wrapped = await dispatch(
140 streamResponseAfterToolCall({
141 toolCallId,
142 }),
143 );
144 unwrapResult(wrapped);
145 }
146 },
147);