| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { ContextItem } from "core"; |
| 3 | import { CLIENT_TOOLS_IMPLS } from "core/tools/builtIn"; |
| 4 | import { ContinueError, ContinueErrorReason } from "core/util/errors"; |
| 5 | import posthog from "posthog-js"; |
| 6 | import { callClientTool } from "../../util/clientTools/callClientTool"; |
| 7 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 8 | import { |
| 9 | acceptToolCall, |
| 10 | errorToolCall, |
| 11 | setInactive, |
| 12 | setToolCallCalling, |
| 13 | updateToolCallOutput, |
| 14 | } from "../slices/sessionSlice"; |
| 15 | import { DEFAULT_TOOL_SETTING } from "../slices/uiSlice"; |
| 16 | import { ThunkApiType } from "../store"; |
| 17 | import { findToolCallById, logToolUsage } from "../util"; |
| 18 | import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; |
| 19 | |
| 20 | export const callToolById = createAsyncThunk< |
| 21 | void, |
| 22 | { toolCallId: string }, |
| 23 | ThunkApiType |
| 24 | >("chat/callTool", async ({ toolCallId }, { dispatch, extra, getState }) => { |
| 25 | const state = getState(); |
| 26 | const toolCallState = findToolCallById(state.session.history, toolCallId); |
| 27 | if (!toolCallState) { |
| 28 | console.warn(`Tool call with ID ${toolCallId} not found`); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (toolCallState.status !== "generated") { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // Track tool call acceptance and start timing |
| 37 | const startTime = Date.now(); |
| 38 | |
| 39 | // Check if this is an auto-approved tool |
| 40 | const toolSettings = state.ui.toolSettings; |
| 41 | const toolPolicy = |
| 42 | toolSettings[toolCallState.toolCall.function.name] ?? |
| 43 | toolCallState.tool?.defaultToolPolicy ?? |
| 44 | DEFAULT_TOOL_SETTING; |
| 45 | const isAutoApproved = toolPolicy === "allowedWithoutPermission"; |
| 46 | |
| 47 | const selectedChatModel = selectSelectedChatModel(state); |
| 48 | |
| 49 | posthog.capture("tool_call_decision", { |
| 50 | model: selectedChatModel, |
| 51 | decision: isAutoApproved ? "auto_accept" : "accept", |
| 52 | toolName: toolCallState.toolCall.function.name, |
| 53 | toolCallId: toolCallId, |
| 54 | }); |
| 55 | |
| 56 | if (!selectedChatModel) { |
| 57 | throw new Error("No model selected"); |
| 58 | } |
| 59 | |
| 60 | dispatch( |
| 61 | setToolCallCalling({ |
| 62 | toolCallId, |
| 63 | }), |
| 64 | ); |
| 65 | |
| 66 | let output: ContextItem[] | undefined = undefined; |
| 67 | let error: ContinueError | undefined = undefined; |
| 68 | let streamResponse: boolean; |
| 69 | |
| 70 | // IMPORTANT: |
| 71 | // Errors that occur while calling tool call implementations |
| 72 | // Are caught and passed in output as context items |
| 73 | // Errors that occur outside specifically calling the tool |
| 74 | // Should not be caught here - should be handled as normal stream errors |
| 75 | if ( |
| 76 | CLIENT_TOOLS_IMPLS.find( |
| 77 | (toolName) => toolName === toolCallState.toolCall.function.name, |
| 78 | ) |
| 79 | ) { |
| 80 | // Tool is called on client side |
| 81 | const { |
| 82 | output: clientToolOutput, |
| 83 | respondImmediately, |
| 84 | error: clientToolError, |
| 85 | } = await callClientTool(toolCallState, { |
| 86 | dispatch, |
| 87 | ideMessenger: extra.ideMessenger, |
| 88 | getState, |
| 89 | }); |
| 90 | output = clientToolOutput; |
| 91 | error = clientToolError; |
| 92 | streamResponse = respondImmediately; |
| 93 | } else { |
| 94 | // Tool is called on core side |
| 95 | const result = await extra.ideMessenger.request("tools/call", { |
| 96 | toolCall: toolCallState.toolCall, |
| 97 | }); |
| 98 | if (result.status === "error") { |
| 99 | throw new Error(result.error); |
| 100 | } else { |
| 101 | output = result.content.contextItems; |
| 102 | error = result.content.errorMessage |
| 103 | ? new ContinueError( |
| 104 | ContinueErrorReason.Unspecified, |
| 105 | result.content.errorMessage, |
| 106 | ) |
| 107 | : undefined; |
| 108 | } |
| 109 | streamResponse = true; |
| 110 | } |
| 111 | |
| 112 | if (error) { |
| 113 | dispatch( |
| 114 | updateToolCallOutput({ |
| 115 | toolCallId, |
| 116 | contextItems: [ |
| 117 | { |
| 118 | icon: "problems", |
| 119 | name: "Tool Call Error", |
| 120 | description: "Tool Call Failed", |
| 121 | content: `${toolCallState.toolCall.function.name} failed with the message: ${error.message}\n\nPlease try something else or request further instructions.`, |
| 122 | hidden: false, |
| 123 | }, |
| 124 | ], |
| 125 | }), |
| 126 | ); |
| 127 | } else if (output?.length) { |
| 128 | dispatch( |
| 129 | updateToolCallOutput({ |
| 130 | toolCallId, |
| 131 | contextItems: output, |
| 132 | }), |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | // Capture telemetry for tool call execution outcome with duration |
| 137 | const duration_ms = Date.now() - startTime; |
| 138 | posthog.capture("tool_call_outcome", { |
| 139 | model: selectedChatModel, |
| 140 | succeeded: !error, |
| 141 | toolName: toolCallState.toolCall.function.name, |
| 142 | errorReason: error?.reason, |
| 143 | duration_ms: duration_ms, |
| 144 | }); |
| 145 | |
| 146 | if (streamResponse) { |
| 147 | if (error) { |
| 148 | logToolUsage(toolCallState, false, false, extra.ideMessenger, output); |
| 149 | dispatch( |
| 150 | errorToolCall({ |
| 151 | toolCallId, |
| 152 | }), |
| 153 | ); |
| 154 | } else { |
| 155 | logToolUsage(toolCallState, true, true, extra.ideMessenger, output); |
| 156 | dispatch( |
| 157 | acceptToolCall({ |
| 158 | toolCallId, |
| 159 | }), |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | // Send to the LLM to continue the conversation |
| 164 | const wrapped = await dispatch( |
| 165 | streamResponseAfterToolCall({ |
| 166 | toolCallId, |
| 167 | }), |
| 168 | ); |
| 169 | unwrapResult(wrapped); |
| 170 | } else { |
| 171 | dispatch(setInactive()); |
| 172 | } |
| 173 | }); |