import type { AgentProfile } from './agents.js'; import type { PermissionDecision, RuntimePermissionRequest } from './host.js'; import { type TranscriptState } from './session-data.js'; import type { RuntimeSessionEvent } from './session.js'; import { type RuntimeEventHandlers } from './runtime-event.js'; export interface RuntimeContext { tenantId?: string; userId?: string; workspaceId?: string; requestId?: string; [key: string]: unknown; } export interface RuntimeObservationBase { type: string; at: number; runtimeId: string; agentId: string; context?: RuntimeContext; } export type RuntimeObservation = (RuntimeObservationBase & { type: 'runtime.connect.started'; }) | (RuntimeObservationBase & { type: 'runtime.connect.completed'; durationMs: number; }) | (RuntimeObservationBase & { type: 'runtime.connect.failed'; durationMs: number; error: string; }) | (RuntimeObservationBase & { type: 'runtime.shutdown.started'; }) | (RuntimeObservationBase & { type: 'runtime.shutdown.completed'; durationMs: number; }) | (RuntimeObservationBase & { type: 'session.created' | 'session.loaded' | 'session.disposed'; sessionId: string; cwd?: string; }) | (RuntimeObservationBase & { type: 'session.status.changed'; sessionId: string; status: string; previousStatus: string | null; }) | (RuntimeObservationBase & { type: 'turn.started'; sessionId: string; turnId: string; }) | (RuntimeObservationBase & { type: 'turn.completed'; sessionId: string; turnId: string; stopReason: string | null; }) | (RuntimeObservationBase & { type: 'turn.failed' | 'turn.cancelled'; sessionId: string; turnId: string; error?: string; reason?: string; }) | (RuntimeObservationBase & { type: 'tool.started'; sessionId: string; turnId?: string; toolCallId: string; toolName: string; title?: string; }) | (RuntimeObservationBase & { type: 'tool.updated'; sessionId: string; turnId?: string; toolCallId: string; status: string; }) | (RuntimeObservationBase & { type: 'tool.completed'; sessionId: string; turnId?: string; toolCallId: string; status: string; }) | (RuntimeObservationBase & { type: 'permission.requested'; sessionId: string; toolCallId: string; toolName: string; title: string; }) | (RuntimeObservationBase & { type: 'permission.decided'; sessionId: string; toolCallId: string; toolName: string; decision: PermissionDecision; }) | (RuntimeObservationBase & { type: 'permission.failed'; sessionId: string; toolCallId: string; toolName: string; error: string; }) | (RuntimeObservationBase & { type: 'approval.queued'; sessionId: string; toolCallId: string; toolName: string; approvalId: string; }) | (RuntimeObservationBase & { type: 'approval.decided'; sessionId: string; toolCallId: string; toolName: string; approvalId: string; decision: PermissionDecision; }) | (RuntimeObservationBase & { type: 'session.error'; sessionId: string; turnId?: string; error: string; }) | (RuntimeObservationBase & { type: 'error'; sessionId?: string; turnId?: string; error: string; }); export type RuntimeObservationSink = (observation: RuntimeObservation) => void | Promise; export interface RuntimeObservabilityOptions { sink?: RuntimeObservationSink; } export interface RuntimeEventStoreQuery { runtimeId?: string; sessionId?: string; kind?: RuntimeStoreEntry['kind']; } export type RuntimeStoreEntry = { kind: 'observation'; at: number; runtimeId: string; agentId: string; context?: RuntimeContext; observation: RuntimeObservation; } | { kind: 'session.event'; at: number; runtimeId: string; agentId: string; context?: RuntimeContext; sessionId: string; event: RuntimeSessionEvent; } | { kind: 'transcript.snapshot'; at: number; runtimeId: string; agentId: string; context?: RuntimeContext; sessionId: string; snapshot: TranscriptState; }; export interface RuntimeEventStore { append(entry: RuntimeStoreEntry): void | Promise; load?(query?: RuntimeEventStoreQuery): Promise; } export interface RuntimeApprovalRequest extends RuntimePermissionRequest { approvalId: string; runtimeId: string; agentId: string; context?: RuntimeContext; requestedAt: number; } export interface RuntimeApprovalTicket { approvalId: string; [key: string]: unknown; } export interface RuntimeApprovalQueue { request(request: RuntimeApprovalRequest): RuntimeApprovalTicket | string | void | Promise; waitForDecision(ticket: RuntimeApprovalTicket): PermissionDecision | Promise; } export interface RuntimeReplay { events: RuntimeSessionEvent[]; transcript: TranscriptState; replay(handlers: RuntimeEventHandlers): Array; } export declare function replayRuntimeEvents(events: Iterable, handlers: RuntimeEventHandlers): Array; export declare function buildTranscriptFromRuntimeEvents(events: Iterable): TranscriptState; export declare function createRuntimeReplay(events: Iterable): RuntimeReplay; export declare function loadRuntimeReplay(store: RuntimeEventStore, query: RuntimeEventStoreQuery): Promise; export declare function sessionEventToObservation(params: { event: RuntimeSessionEvent; runtimeId: string; agent: AgentProfile; context?: RuntimeContext; }): RuntimeObservation | null;