import type { LanguageModelUsage, ModelMessage, StepResult, ToolSet } from 'ai'; export type { LanguageModelUsage }; /** * Simplified tool call type for ContractSpec usage. * Compatible with AI SDK v6 TypedToolCall. */ export interface ToolCallInfo { type: 'tool-call'; toolCallId: string; toolName: string; args: unknown; } /** * Simplified tool result type for ContractSpec usage. * Compatible with AI SDK v6 TypedToolResult. */ export interface ToolResultInfo { type: 'tool-result'; toolCallId: string; toolName: string; output: unknown; } /** * Extended message type that adds metadata support to ModelMessage. * Used for session memory tracking. */ export interface AgentMessage { role: 'user' | 'assistant' | 'system' | 'tool'; content: string | { type: string; text?: string; [key: string]: unknown; }[]; metadata?: Record; } export type AgentStatus = 'idle' | 'running' | 'waiting' | 'completed' | 'failed' | 'escalated'; export type AgentEventName = 'agent.session.created' | 'agent.session.updated' | 'agent.step.started' | 'agent.step.completed' | 'agent.tool.called' | 'agent.tool.completed' | 'agent.tool.failed' | 'agent.tool.approval_requested' | 'agent.escalated' | 'agent.completed' | 'agent.failed'; export type AgentFinishReason = 'stop' | 'tool-calls' | 'length' | 'content-filter' | 'error' | 'other' | 'unknown'; export type AgentExecutionErrorKind = 'fatal' | 'retryable' | 'timeout' | 'guard_rejected' | 'policy_blocked'; export interface AgentExecutionError { kind: AgentExecutionErrorKind; message: string; code?: string; retryAfterMs?: number; } export interface AgentEventPayload { sessionId: string; agentId: string; tenantId?: string; workflowId?: string; traceId?: string; checkpointId?: string; stepIndex?: number; toolName?: string; metadata?: Record; } /** * Runtime context passed to agent calls via AI SDK v6 callOptionsSchema. * Maps to ContractSpec's tenant/actor system. */ export interface AgentCallOptions { /** Tenant scoping for guardrails and data isolation */ tenantId?: string; /** Unique end-user identifier (for personalization) */ actorId?: string; /** Session to resume; new session created when omitted */ sessionId?: string; /** Parent workflow execution id when the agent runs inside a workflow. */ workflowId?: string; /** External thread id used by checkpoint-capable runtimes. */ threadId?: string; /** Arbitrary metadata forwarded to events and tool handlers */ metadata?: Record; /** Locale override for this call */ locale?: string; /** Model selection context for ranking-driven model routing */ selectionContext?: import('@contractspec/lib.ai-providers/selector-types').ModelSelectionContext; /** Transport preference for provider calls. */ transport?: 'rest' | 'mcp' | 'sdk'; /** Auth method to use for provider calls. */ authMethod?: 'api-key' | 'oauth2' | 'bearer'; /** Custom auth headers for provider calls. */ authHeaders?: Record; } export interface AgentSessionState { sessionId: string; agentId: string; tenantId?: string; actorId?: string; workflowId?: string; threadId?: string; traceId?: string; checkpointId?: string; /** Active locale for this session */ locale?: string; status: AgentStatus; messages: ModelMessage[]; steps: StepResult[]; pendingApprovalRequestId?: string; lastError?: AgentExecutionError; createdAt: Date; updatedAt: Date; metadata?: Record; } export interface AgentGenerateParams { /** User prompt (required when messages is not provided) */ prompt?: string; /** Full conversation history (for subagent passConversationHistory; when set, prompt is ignored) */ messages?: { role: string; content: string | unknown[]; }[]; /** System prompt override (appended to agent instructions) */ systemOverride?: string; /** Runtime context options */ options?: AgentCallOptions; /** Maximum number of steps/iterations */ maxSteps?: number; /** Abort signal for cancellation */ signal?: AbortSignal; } export interface AgentStreamParams extends AgentGenerateParams { /** Called when a step completes */ onStepFinish?: (step: StepResult) => void | Promise; } export interface AgentGenerateResult { /** The final text response */ text: string; /** Structured output if configured */ output?: TOutput; /** All steps taken during generation */ steps: StepResult[]; /** All tool calls made during generation */ toolCalls: ToolCallInfo[]; /** All tool results */ toolResults: ToolResultInfo[]; /** Reason generation finished */ finishReason: AgentFinishReason; /** Token usage statistics */ usage?: LanguageModelUsage; /** Updated session state */ session?: AgentSessionState; /** Whether approval is pending for a tool call */ pendingApproval?: { toolName: string; toolCallId: string; args: unknown; }; } /** * Context provided to tool handlers during execution. */ export interface ToolExecutionContext { agentId: string; sessionId: string; tenantId?: string; actorId?: string; /** Locale for i18n (BCP 47). Falls back to 'en' when unset. */ locale?: string; metadata?: Record; signal?: AbortSignal; } /** * Handler function for a tool. * May return a Promise (single result) or AsyncGenerator (streaming preliminary results). */ export type ToolHandler = (input: TInput, context: ToolExecutionContext) => Promise | TOutput | AsyncGenerator; export interface AgentStepMetrics { agentId: string; stepIndex: number; toolCalls: { toolName: string; durationMs?: number; success: boolean; error?: string; }[]; finishReason: string; usage?: LanguageModelUsage; timestamp: Date; } export type AgentEventEmitter = (event: AgentEventName, payload: AgentEventPayload) => void | Promise;