diff --git a/core/core.ts b/core/core.ts index 270668b17..0d1b5ffba 100644 --- a/core/core.ts +++ b/core/core.ts @@ -83,6 +83,7 @@ import { NextEditProvider } from "./nextEdit/NextEditProvider"; import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; import { OnboardingModes } from "./protocol/core"; import type { IMessenger, Message } from "./protocol/messenger"; +import { ContinueError, ContinueErrorReason } from "./util/errors"; import { shareSession } from "./util/historyUtils"; import { Logger } from "./util/Logger.js"; @@ -1131,12 +1132,27 @@ export class Core { if (!tool) { throw new Error(`Tool ${toolName} not found`); } - const preprocessedArgs = await tool.preprocessArgs?.(args, { - ide: this.ide, - }); - return { - preprocessedArgs, - }; + + try { + const preprocessedArgs = await tool.preprocessArgs?.(args, { + ide: this.ide, + }); + return { + preprocessedArgs, + }; + } catch (e) { + let errorReason = + e instanceof ContinueError ? e.reason : ContinueErrorReason.Unknown; + let errorMessage = + e instanceof Error + ? e.message + : `Error preprocessing tool call args for ${toolName}\n${JSON.stringify(args)}`; + return { + preprocessedArgs: undefined, + errorReason, + errorMessage, + }; + } }); on("isItemTooBig", async ({ data: { item } }) => { diff --git a/core/protocol/core.ts b/core/protocol/core.ts index ea8bb388a..c559d9f64 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -54,6 +54,7 @@ import { } from "../control-plane/client"; import { ProcessedItem } from "../nextEdit/NextEditPrefetchQueue"; import { NextEditOutcome } from "../nextEdit/types"; +import { ContinueErrorReason } from "../util/errors"; export enum OnboardingModes { API_KEY = "API Key", @@ -317,7 +318,11 @@ export type ToCoreFromIdeOrWebviewProtocol = { ]; "tools/preprocessArgs": [ { toolName: string; args: Record }, - { preprocessedArgs?: Record }, + { + preprocessedArgs?: Record; + errorReason?: ContinueErrorReason; + errorMessage?: string; + }, ]; "clipboardCache/add": [{ content: string }, void]; "controlPlane/openUrl": [{ path: string; orgSlug?: string }, void]; diff --git a/gui/src/redux/thunks/preprocessToolCallArgs.ts b/gui/src/redux/thunks/preprocessToolCallArgs.ts index f6b973323..fd6a9e52d 100644 --- a/gui/src/redux/thunks/preprocessToolCallArgs.ts +++ b/gui/src/redux/thunks/preprocessToolCallArgs.ts @@ -1,4 +1,6 @@ import { ToolCallState } from "core"; +import { ContinueErrorReason } from "core/util/errors"; +import posthog from "posthog-js"; import { IIdeMessenger } from "../../context/IdeMessenger"; import { errorToolCall, @@ -15,25 +17,29 @@ export async function preprocessToolCalls( // Tool call pre-processing await Promise.all( generatedToolCalls.map(async (tcState) => { - try { - const result = await ideMessenger.request("tools/preprocessArgs", { + let errorReason: ContinueErrorReason | undefined = undefined; + let errorMessage: string | undefined = undefined; + let preprocessedArgs: Record | undefined = undefined; + const result = await ideMessenger.request("tools/preprocessArgs", { + toolName: tcState.toolCall.function.name, + args: tcState.parsedArgs, + }); + if (result.status === "success") { + preprocessedArgs = result.content.preprocessedArgs; + errorMessage = result.content.errorMessage; + errorReason = result.content.errorReason; + } else { + errorMessage = result.error; + errorReason = ContinueErrorReason.Unknown; + } + if (errorReason) { + posthog.capture("tool_call_outcome", { + // model: , TODO + succeeded: false, toolName: tcState.toolCall.function.name, - args: tcState.parsedArgs, + errorReason, + duration_ms: 0, // preprocessing is more or less instantaneous }); - if (result.status === "success") { - if (result.content.preprocessedArgs) { - dispatch( - setToolCallArgs({ - toolCallId: tcState.toolCallId, - newArgs: result.content.preprocessedArgs, - }), - ); - } - } else { - throw new Error(result.error); - } - } catch (e) { - let errorMessage = e instanceof Error ? e.message : `Unknown error`; dispatch( errorToolCall({ toolCallId: tcState.toolCallId, @@ -53,6 +59,13 @@ export async function preprocessToolCalls( ], }), ); + } else if (preprocessedArgs) { + dispatch( + setToolCallArgs({ + toolCallId: tcState.toolCallId, + newArgs: preprocessedArgs, + }), + ); } }), );