| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { ContextItem } from "core"; |
| 3 | import { CLIENT_TOOLS_IMPLS } from "core/tools/builtIn"; |
| 4 | import posthog from "posthog-js"; |
| 5 | import { callClientTool } from "../../util/clientTools/callClientTool"; |
| 6 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 7 | import { |
| 8 | acceptToolCall, |
| 9 | errorToolCall, |
| 10 | setInactive, |
| 11 | setToolCallCalling, |
| 12 | updateToolCallOutput, |
| 13 | } from "../slices/sessionSlice"; |
| 14 | import { ThunkApiType } from "../store"; |
| 15 | import { findToolCallById, logToolUsage } from "../util"; |
| 16 | import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; |
| 17 | |
| 18 | export const callToolById = createAsyncThunk< |
| 19 | void, |
| 20 | { toolCallId: string }, |
| 21 | ThunkApiType |
| 22 | >("chat/callTool", async ({ toolCallId }, { dispatch, extra, getState }) => { |
| 23 | const state = getState(); |
| 24 | const toolCallState = findToolCallById(state.session.history, toolCallId); |
| 25 | if (!toolCallState) { |
| 26 | console.warn(`Tool call with ID ${toolCallId} not found`); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if (toolCallState.status !== "generated") { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | const selectedChatModel = selectSelectedChatModel(state); |
| 35 | |
| 36 | if (!selectedChatModel) { |
| 37 | throw new Error("No model selected"); |
| 38 | } |
| 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: clientToolOutput, |
| 66 | respondImmediately, |
| 67 | errorMessage: clientToolError, |
| 68 | } = await callClientTool(toolCallState, { |
| 69 | dispatch, |
| 70 | ideMessenger: extra.ideMessenger, |
| 71 | getState, |
| 72 | }); |
| 73 | output = clientToolOutput; |
| 74 | errorMessage = clientToolError; |
| 75 | streamResponse = respondImmediately; |
| 76 | } else { |
| 77 | // Tool is called on core side |
| 78 | const result = await extra.ideMessenger.request("tools/call", { |
| 79 | toolCall: toolCallState.toolCall, |
| 80 | }); |
| 81 | if (result.status === "error") { |
| 82 | throw new Error(result.error); |
| 83 | } else { |
| 84 | output = result.content.contextItems; |
| 85 | errorMessage = result.content.errorMessage; |
| 86 | } |
| 87 | streamResponse = true; |
| 88 | } |
| 89 | |
| 90 | if (errorMessage) { |
| 91 | dispatch( |
| 92 | updateToolCallOutput({ |
| 93 | toolCallId, |
| 94 | contextItems: [ |
| 95 | { |
| 96 | icon: "problems", |
| 97 | name: "Tool Call Error", |
| 98 | description: "Tool Call Failed", |
| 99 | content: `${toolCallState.toolCall.function.name} failed with the message: ${errorMessage}\n\nPlease try something else or request further instructions.`, |
| 100 | hidden: false, |
| 101 | }, |
| 102 | ], |
| 103 | }), |
| 104 | ); |
| 105 | } else if (output?.length) { |
| 106 | dispatch( |
| 107 | updateToolCallOutput({ |
| 108 | toolCallId, |
| 109 | contextItems: output, |
| 110 | }), |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | // Because we don't have access to use hooks, we check `allowAnonymousTelemetry` |
| 115 | // directly rather than using `CustomPostHogProvider` |
| 116 | if (allowAnonymousTelemetry) { |
| 117 | // Capture telemetry for tool calls |
| 118 | posthog.capture("gui_tool_call_outcome", { |
| 119 | succeeded: errorMessage === undefined, |
| 120 | toolName: toolCallState.toolCall.function.name, |
| 121 | errorMessage: errorMessage, |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | if (streamResponse) { |
| 126 | if (errorMessage) { |
| 127 | logToolUsage(toolCallState, false, false, extra.ideMessenger, output); |
| 128 | dispatch( |
| 129 | errorToolCall({ |
| 130 | toolCallId, |
| 131 | }), |
| 132 | ); |
| 133 | } else { |
| 134 | logToolUsage(toolCallState, true, true, extra.ideMessenger, output); |
| 135 | dispatch( |
| 136 | acceptToolCall({ |
| 137 | toolCallId, |
| 138 | }), |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | // Send to the LLM to continue the conversation |
| 143 | const wrapped = await dispatch( |
| 144 | streamResponseAfterToolCall({ |
| 145 | toolCallId, |
| 146 | }), |
| 147 | ); |
| 148 | unwrapResult(wrapped); |
| 149 | } else { |
| 150 | dispatch(setInactive()); |
| 151 | } |
| 152 | }); |