diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 6803947fb..50ff37320 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -18,7 +18,9 @@ import { RuleWithSource, Session, SessionMetadata, + ToolCallState, } from "core"; +import { safeParseToolCallArgs } from "core/tools/parseArgs"; import { NEW_SESSION_TITLE } from "core/util/constants"; import { renderChatMessage } from "core/util/messageContent"; import { findUriInDirs, getUriPathBasename } from "core/util/uri"; @@ -28,6 +30,25 @@ import { RootState } from "../store"; import { streamResponseThunk } from "../thunks/streamResponse"; import { findCurrentToolCall, findToolCall } from "../util"; +const getIdeMessenger = () => (window as any).ideMessenger; + +const logToolUsage = (toolCallState: ToolCallState, success: boolean) => { + const ideMessenger = getIdeMessenger(); + if (ideMessenger) { + ideMessenger.post("devdata/log", { + name: "toolUsage", + data: { + toolCallId: toolCallState.toolCallId, + functionName: toolCallState.toolCall.function.name, + functionArgs: toolCallState.toolCall.function.arguments, + parsedArgs: safeParseToolCallArgs(toolCallState.toolCall), + succeeded: success, + output: toolCallState.output || [], + }, + }); + } +}; + // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. // The proper fix is adding a UUID to all chat messages, but this is the temp workaround. export type ChatHistoryItemWithMessageId = ChatHistoryItem & { @@ -559,6 +580,7 @@ export const sessionSlice = createSlice({ ); if (toolCallState) { toolCallState.status = "errored"; + logToolUsage(toolCallState, false); } }, acceptToolCall: ( @@ -573,6 +595,7 @@ export const sessionSlice = createSlice({ ); if (toolCallState) { toolCallState.status = "done"; + logToolUsage(toolCallState, true); } }, setToolCallCalling: ( diff --git a/packages/config-yaml/src/schemas/data/index.ts b/packages/config-yaml/src/schemas/data/index.ts index 7251e12a7..679227c52 100644 --- a/packages/config-yaml/src/schemas/data/index.ts +++ b/packages/config-yaml/src/schemas/data/index.ts @@ -42,6 +42,11 @@ import { tokensGeneratedEventSchema_0_2_0, tokensGeneratedEventSchema_0_2_0_noCode, } from "./tokensGenerated/v0.2.0.js"; +import { toolUsageEventAllSchema } from "./toolUsage/index.js"; +import { + toolUsageEventSchema_0_2_0, + toolUsageEventSchema_0_2_0_noCode, +} from "./toolUsage/v0.2.0.js"; const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9A-Za-z-][0-9A-Za-z-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/; @@ -72,6 +77,7 @@ const devEventAllVersionDataSchemas = z.object({ tokensGenerated: tokensGeneratedEventAllSchema, chatInteraction: chatInteractionEventAllSchema, editInteraction: editInteractionEventAllSchema, + toolUsage: toolUsageEventAllSchema, }); // Version and level specific schemas are organized here @@ -97,6 +103,7 @@ export const devDataVersionedSchemas = { tokensGenerated: tokensGeneratedEventSchema_0_2_0, chatInteraction: chatInteractionEventSchema_0_2_0, editInteraction: editInteractionEventSchema_0_2_0, + toolUsage: toolUsageEventSchema_0_2_0, }, noCode: { autocomplete: autocompleteEventSchema_0_2_0_noCode, @@ -104,6 +111,7 @@ export const devDataVersionedSchemas = { tokensGenerated: tokensGeneratedEventSchema_0_2_0_noCode, chatInteraction: chatInteractionEventSchema_0_2_0_noCode, editInteraction: editInteractionEventSchema_0_2_0_noCode, + toolUsage: toolUsageEventSchema_0_2_0_noCode, }, }, }; diff --git a/packages/config-yaml/src/schemas/data/toolUsage/index.ts b/packages/config-yaml/src/schemas/data/toolUsage/index.ts new file mode 100644 index 000000000..8119366f3 --- /dev/null +++ b/packages/config-yaml/src/schemas/data/toolUsage/index.ts @@ -0,0 +1,11 @@ +import { z } from "zod"; +import { baseDevDataAllSchema } from "../base.js"; + +export const toolUsageEventAllSchema = baseDevDataAllSchema.extend({ + toolCallId: z.string(), + functionName: z.string(), + functionArgs: z.string(), + parsedArgs: z.any(), + succeeded: z.boolean(), + output: z.array(z.any()).optional(), +}); diff --git a/packages/config-yaml/src/schemas/data/toolUsage/v0.2.0.ts b/packages/config-yaml/src/schemas/data/toolUsage/v0.2.0.ts new file mode 100644 index 000000000..533e33218 --- /dev/null +++ b/packages/config-yaml/src/schemas/data/toolUsage/v0.2.0.ts @@ -0,0 +1,26 @@ +import { toolUsageEventAllSchema } from "./index.js"; + +export const toolUsageEventSchema_0_2_0 = toolUsageEventAllSchema.pick({ + // base + timestamp: true, + userId: true, + userAgent: true, + selectedProfileId: true, + eventName: true, + schema: true, + + // tool-usage-specific + toolCallId: true, + functionName: true, + functionArgs: true, + parsedArgs: true, + succeeded: true, + output: true, +}); + +export const toolUsageEventSchema_0_2_0_noCode = + toolUsageEventSchema_0_2_0.omit({ + functionArgs: true, + parsedArgs: true, + output: true, + });