import type { Telemetry } from "ai"; type TelemetryEvent = Parameters>[0]; /** Stable eve identity for one actual model attempt. */ export interface InstrumentationAttemptScope { readonly attemptId: string; readonly attemptIndex: number; readonly functionId?: string; readonly rootSessionId?: string; readonly sessionId: string; readonly stepIndex: number; readonly turnId: string; } export interface InstrumentationAttemptStartedEvent { readonly type: "attempt.started"; readonly scope: InstrumentationAttemptScope; readonly operation: TelemetryEvent<"onStart">; readonly step: TelemetryEvent<"onStepStart">; } export interface InstrumentationSessionStartedEvent { readonly type: "session.started"; readonly agentName?: string; readonly channelKind?: string; readonly parentTraceContext?: InstrumentationTraceContext; readonly rootSessionId: string; readonly sessionId: string; } export interface InstrumentationTraceContext { readonly spanId: string; readonly traceFlags: number; readonly traceId: string; } /** * Which tool call dispatched a subagent child. The trace structure alone * cannot say: one turn's children all parent to the same window. */ export interface InstrumentationParentLineage { readonly callId: string; readonly sessionId: string; readonly subagentName?: string; readonly turnId: string; } export interface InstrumentationSessionTransitionEvent { readonly type: "session.completed" | "session.failed" | "session.waiting"; readonly error?: unknown; readonly sessionId: string; readonly turnId?: string; } export interface InstrumentationTurnStartedEvent { readonly type: "turn.started"; readonly parentLineage?: InstrumentationParentLineage; readonly parentTraceContext?: InstrumentationTraceContext; readonly rootSessionId: string; readonly sequence: number; readonly sessionId: string; readonly turnId: string; } export interface InstrumentationTurnTerminalEvent { readonly type: "turn.cancelled" | "turn.completed" | "turn.failed"; readonly error?: unknown; readonly sessionId: string; readonly turnId: string; } export interface InstrumentationAttemptTerminalEvent { readonly type: "attempt.completed" | "attempt.failed"; readonly error?: unknown; readonly scope: InstrumentationAttemptScope; } /** * Provider metadata for one completed step, as reported by the AI SDK * (`StepResult.providerMetadata`). Carries Vercel AI Gateway cost data when * the request went through the gateway; absent for other providers. */ export interface InstrumentationAttemptMetadataEvent { readonly type: "attempt.metadata"; readonly scope: InstrumentationAttemptScope; readonly providerMetadata: Readonly>; } export interface InstrumentationModelCallStartedEvent { readonly type: "model.call.started"; readonly id: string; readonly scope: InstrumentationAttemptScope; readonly source: TelemetryEvent<"onLanguageModelCallStart">; } export interface InstrumentationModelCallCompletedEvent { readonly type: "model.call.completed"; readonly id: string; readonly scope: InstrumentationAttemptScope; readonly source: TelemetryEvent<"onLanguageModelCallEnd">; } export interface InstrumentationModelCallFailedEvent { readonly type: "model.call.failed"; readonly error: unknown; readonly id: string; readonly scope: InstrumentationAttemptScope; } export type InstrumentationModelCallTerminalEvent = InstrumentationModelCallCompletedEvent | InstrumentationModelCallFailedEvent; export interface InstrumentationToolCallStartedEvent { readonly type: "tool.call.started"; readonly id: string; readonly scope: InstrumentationAttemptScope; readonly source: TelemetryEvent<"onToolExecutionStart">; } export interface InstrumentationToolCallCompletedEvent { readonly type: "tool.call.completed"; readonly id: string; readonly scope: InstrumentationAttemptScope; readonly source: TelemetryEvent<"onToolExecutionEnd">; } export interface InstrumentationToolCallFailedEvent { readonly type: "tool.call.failed"; readonly error: unknown; readonly id: string; readonly scope: InstrumentationAttemptScope; } export type InstrumentationToolCallTerminalEvent = InstrumentationToolCallCompletedEvent | InstrumentationToolCallFailedEvent; export interface RelatedLifecycleHook { readonly before?: (event: TStart) => unknown | PromiseLike; readonly after?: (event: TTerminal, state: unknown) => void | PromiseLike; } /** Internal provider shape mirrored by the future public hook contract. */ export interface InstrumentationProviderDefinition { readonly events?: { readonly "model.call"?: RelatedLifecycleHook; readonly "attempt.started"?: (event: InstrumentationAttemptStartedEvent) => void | PromiseLike; readonly "attempt.completed"?: (event: InstrumentationAttemptTerminalEvent) => void | PromiseLike; readonly "attempt.failed"?: (event: InstrumentationAttemptTerminalEvent) => void | PromiseLike; readonly "attempt.metadata"?: (event: InstrumentationAttemptMetadataEvent) => void | PromiseLike; readonly "session.completed"?: (event: InstrumentationSessionTransitionEvent) => void | PromiseLike; readonly "session.failed"?: (event: InstrumentationSessionTransitionEvent) => void | PromiseLike; readonly "session.started"?: (event: InstrumentationSessionStartedEvent) => void | PromiseLike; readonly "session.waiting"?: (event: InstrumentationSessionTransitionEvent) => void | PromiseLike; readonly "tool.call"?: RelatedLifecycleHook; readonly "turn.cancelled"?: (event: InstrumentationTurnTerminalEvent) => void | PromiseLike; readonly "turn.completed"?: (event: InstrumentationTurnTerminalEvent) => void | PromiseLike; readonly "turn.failed"?: (event: InstrumentationTurnTerminalEvent) => void | PromiseLike; readonly "turn.started"?: (event: InstrumentationTurnStartedEvent) => void | PromiseLike; }; } export interface InstrumentationRelatedEventMap { readonly "model.call": { readonly start: InstrumentationModelCallStartedEvent; readonly terminal: InstrumentationModelCallTerminalEvent; }; readonly "tool.call": { readonly start: InstrumentationToolCallStartedEvent; readonly terminal: InstrumentationToolCallTerminalEvent; }; } export type InstrumentationRelatedEventName = keyof InstrumentationRelatedEventMap; export type InstrumentationPointEvent = InstrumentationAttemptStartedEvent | InstrumentationAttemptMetadataEvent | InstrumentationAttemptTerminalEvent | InstrumentationSessionStartedEvent | InstrumentationSessionTransitionEvent | InstrumentationTurnStartedEvent | InstrumentationTurnTerminalEvent; /** Trusted framework operation for activating context around AI SDK execution. */ export type InstrumentationContextRunner = (operation: InstrumentationExecutionOperation, execute: () => PromiseLike) => PromiseLike; /** Stable identity supplied only to a trusted framework context runner. */ export type InstrumentationExecutionOperation = { readonly id: string; readonly scope: InstrumentationAttemptScope; readonly type: "model.call"; } | { readonly id: string; readonly scope: InstrumentationAttemptScope; readonly type: "tool.call"; }; /** Provider-neutral hook operations consumed by the AI SDK bridge. */ export interface InstrumentationHooks { after(name: TKey, event: InstrumentationRelatedEventMap[TKey]["terminal"]): Promise; before(name: TKey, event: InstrumentationRelatedEventMap[TKey]["start"]): Promise; publish(event: InstrumentationPointEvent): Promise; } /** Creates failure-isolated hooks backed by an ordered provider list. */ export declare function createInstrumentationHooks(providers: readonly InstrumentationProviderDefinition[]): InstrumentationHooks; export {};