import type { InferToolContext, ModelMessage, ToolSet, } from '@ai-sdk/provider-utils'; import type { Callback } from '../util/callback'; import type { DynamicToolCall, StaticToolCall } from './tool-call'; import type { ToolOutput } from './tool-output'; import type { ValueOf } from '../util/value-of'; /** * Resolves a single tool's context type, falling back to `undefined` when the * tool does not declare a `contextSchema`. */ type ToolContextFor = [ InferToolContext, ] extends [never] ? undefined : InferToolContext; type BaseToolExecutionStartFields = { /** Unique identifier for this generation call, used to correlate events. */ readonly callId: string; /** * Messages that were sent to the language model to initiate the response that contained the tool call. * The messages **do not** include the system prompt nor the assistant response that contained the tool call. */ readonly messages: ModelMessage[]; }; /** * Precise start event union for statically known tools. * * Each union member ties a specific `toolCall.toolName` to that tool's * validated `toolContext` type. */ type StaticToolExecutionStartEvent = ValueOf<{ [NAME in keyof TOOLS]: BaseToolExecutionStartFields & { readonly toolCall: Extract, { toolName: NAME }>; readonly toolContext: ToolContextFor; }; }>; /** * Start event shape for dynamic or untyped tool calls. */ type DynamicToolExecutionStartEvent = BaseToolExecutionStartFields & { readonly toolCall: DynamicToolCall; readonly toolContext: unknown; }; /** * Broad start event shape used for the default `ToolSet` specialization. * * This keeps generic collectors ergonomic when the caller is not working with * a concrete tool set and therefore cannot benefit from per-tool narrowing. */ type WidenedToolExecutionStartEvent = BaseToolExecutionStartFields & { readonly toolCall: StaticToolCall | DynamicToolCall; readonly toolContext: unknown; }; /** * Event passed to the `onToolExecutionStart` callback. * * Called when a tool execution begins, before the tool's `execute` function is invoked. */ export type ToolExecutionStartEvent = [ ToolSet, ] extends [TOOLS] ? WidenedToolExecutionStartEvent : StaticToolExecutionStartEvent | DynamicToolExecutionStartEvent; type BaseToolExecutionEndFields = { /** Unique identifier for this generation call, used to correlate events. */ readonly callId: string; /** Execution time of the tool call in milliseconds. */ readonly toolExecutionMs: number; /** * Messages that were sent to the language model to initiate the response that contained the tool call. * The messages **do not** include the system prompt nor the assistant response that contained the tool call. */ readonly messages: ModelMessage[]; }; /** * Precise end event union for statically known tools. * * Each union member preserves the link between `toolCall.toolName`, the * corresponding validated `toolContext`, and the tool execution result. */ type StaticToolExecutionEndEvent = ValueOf<{ [NAME in keyof TOOLS]: BaseToolExecutionEndFields & { readonly toolCall: Extract, { toolName: NAME }>; readonly toolContext: ToolContextFor; readonly toolOutput: ToolOutput; }; }>; /** * End event shape for dynamic or untyped tool calls. */ type DynamicToolExecutionEndEvent = BaseToolExecutionEndFields & { readonly toolCall: DynamicToolCall; readonly toolContext: unknown; readonly toolOutput: ToolOutput; }; /** * Broad end event shape used for the default `ToolSet` specialization. * * This provides an assignable catch-all event type for generic consumers while * the concrete-tool specialization retains full per-tool narrowing. */ type WidenedToolExecutionEndEvent = BaseToolExecutionEndFields & { readonly toolCall: StaticToolCall | DynamicToolCall; readonly toolContext: unknown; readonly toolOutput: ToolOutput; }; /** * Event passed to the `onToolExecutionEnd` callback. * * Called when a tool execution completes, either successfully or with an error. * Uses the `toolOutput.type` discriminator to distinguish success and error. */ export type ToolExecutionEndEvent = [ ToolSet, ] extends [TOOLS] ? WidenedToolExecutionEndEvent : StaticToolExecutionEndEvent | DynamicToolExecutionEndEvent; /** * Callback that is set using the `onToolExecutionStart` option. * * Called when a tool execution begins, before the tool's `execute` function is invoked. * Use this for logging tool invocations, tracking tool usage, or pre-execution validation. * * @param event - The event object containing tool call information. */ export type OnToolExecutionStartCallback = Callback>; /** * Callback that is set using the `onToolExecutionEnd` option. * * Called when a tool execution completes, either successfully or with an error. * Use this for logging tool results, tracking execution time, or error handling. * * The event uses a discriminated union on `toolOutput.type`: * - When `toolOutput.type === 'tool-result'`: `toolOutput.output` contains the tool result. * - When `toolOutput.type === 'tool-error'`: `toolOutput.error` contains the error. * * @param event - The event object containing tool call result information. */ export type OnToolExecutionEndCallback = Callback>; /** @deprecated Use `ToolExecutionStartEvent` instead. */ export type OnToolCallStartEvent = ToolExecutionStartEvent; /** @deprecated Use `ToolExecutionEndEvent` instead. */ export type OnToolCallFinishEvent = ToolExecutionEndEvent;