/** * ProviderRuntime event union — the canonical contract every harness driver * emits into. * * Pattern adapted from pingdotgg/t3code (`packages/contracts/src/providerRuntime.ts`). * Every event carries the same base envelope so consumers (UI, telemetry, * snapshot store, journal, RPC subscribers) can treat the stream uniformly. */ import { z } from "zod"; import type { EventId, HarnessId, ProviderInstanceId, SessionId, ThreadId, ToolCallId, TurnId } from "./ids.js"; export type SessionState = "idle" | "starting" | "running" | "ready" | "interrupted" | "stopped" | "error"; export type TurnState = "pending" | "running" | "done" | "failed"; export type AuthStatus = "missing" | "needs_login" | "signed_in" | "ready" | "not_required"; export interface ProviderRuntimeEventBase { readonly eventId: EventId; readonly seq: number; readonly harnessId: HarnessId; readonly providerInstanceId: ProviderInstanceId; readonly sessionId: SessionId; readonly threadId?: ThreadId; readonly turnId?: TurnId; readonly createdAt: string; } export type ProviderRuntimeEvent = SessionLifecycleEvent | TurnLifecycleEvent | MessageEvent | ReasoningEvent | ToolEvent | ApprovalEvent | AuthEvent | RateLimitEvent | UsageEvent | McpEvent | DiagnosticEvent | StreamEvent; export type SessionLifecycleEvent = ProviderRuntimeEventBase & ({ type: "session.created"; harnessConfigSummary: { harness: HarnessId; model?: string; effort?: string; }; } | { type: "session.state.changed"; from: SessionState; to: SessionState; reason?: string; } | { type: "session.shutdown"; reason: "user" | "error" | "system"; }); export type TurnLifecycleEvent = ProviderRuntimeEventBase & ({ type: "turn.created"; promptPreview: string; } | { type: "turn.state.changed"; from: TurnState; to: TurnState; } | { type: "turn.completed"; outcome: "success" | "cancelled" | "error"; error?: string; }); export type MessageEvent = ProviderRuntimeEventBase & ({ type: "message.user"; text: string; } | { type: "message.assistant.delta"; delta: string; } | { type: "message.assistant.complete"; text: string; }); export type ReasoningEvent = ProviderRuntimeEventBase & ({ type: "reasoning.delta"; delta: string; effort?: string; } | { type: "reasoning.complete"; text: string; }); export type ToolEvent = ProviderRuntimeEventBase & ({ type: "tool.call.started"; toolCallId: ToolCallId; tool: string; args: unknown; } | { type: "tool.call.output"; toolCallId: ToolCallId; chunk: string; stream: "stdout" | "stderr"; } | { type: "tool.call.completed"; toolCallId: ToolCallId; ok: boolean; result?: unknown; error?: string; elapsedMs: number; }); export type ApprovalEvent = ProviderRuntimeEventBase & ({ type: "approval.requested"; approvalId: string; tool: string; args: unknown; reason: string; } | { type: "approval.resolved"; approvalId: string; decision: "approved" | "denied"; reason?: string; }); export type AuthEvent = ProviderRuntimeEventBase & ({ type: "auth.status.updated"; status: AuthStatus; message?: string; }); export type RateLimitEvent = ProviderRuntimeEventBase & ({ type: "rate_limit.updated"; state: "ok" | "warning" | "limited" | "unknown"; retryAfterMs?: number; remainingTokens?: number; }); export type UsageEvent = ProviderRuntimeEventBase & ({ type: "usage.updated"; inputTokens: number; outputTokens: number; reasoningTokens?: number; estimatedCostUsd?: number; }); export type McpEvent = ProviderRuntimeEventBase & ({ type: "mcp.status.updated"; serverName: string; status: "connecting" | "ready" | "error" | "disconnected"; message?: string; } | { type: "mcp.tool.registered"; serverName: string; toolName: string; description?: string; }); export type DiagnosticEvent = ProviderRuntimeEventBase & ({ type: "diagnostic.warn"; message: string; data?: unknown; } | { type: "diagnostic.error"; message: string; data?: unknown; }); export type StreamEvent = ProviderRuntimeEventBase & ({ type: "stream.heartbeat"; }); export type ProviderRuntimeEventType = ProviderRuntimeEvent["type"]; export declare const PROVIDER_RUNTIME_EVENT_TYPES: readonly ProviderRuntimeEventType[]; export declare const providerRuntimeEventSchema: z.ZodEffects; }, "strip", z.ZodTypeAny, { type: string; }, { type: string; }>, z.ZodObject<{ eventId: z.ZodString; seq: z.ZodNumber; harnessId: z.ZodString; providerInstanceId: z.ZodString; sessionId: z.ZodString; threadId: z.ZodOptional; turnId: z.ZodOptional; createdAt: z.ZodEffects; }, "strip", z.ZodTypeAny, { createdAt: string; sessionId: string; eventId: string; seq: number; harnessId: string; providerInstanceId: string; threadId?: string | undefined; turnId?: string | undefined; }, { createdAt: string; sessionId: string; eventId: string; seq: number; harnessId: string; providerInstanceId: string; threadId?: string | undefined; turnId?: string | undefined; }>>, z.ZodRecord>, { type: string; } & { createdAt: string; sessionId: string; eventId: string; seq: number; harnessId: string; providerInstanceId: string; threadId?: string | undefined; turnId?: string | undefined; } & Record, { type: string; } & { createdAt: string; sessionId: string; eventId: string; seq: number; harnessId: string; providerInstanceId: string; threadId?: string | undefined; turnId?: string | undefined; } & Record>; export declare function parseProviderRuntimeEvent(raw: unknown): ProviderRuntimeEvent; export declare function safeParseProviderRuntimeEvent(raw: unknown): { ok: true; event: ProviderRuntimeEvent; } | { ok: false; error: string; };