/** * Agent run state, as a reducer over events. * * AgentActivity, ApprovalGate, StreamingText and AgentStop each render one * state well, and none of them knows about the others. What turns "the agent * started step 3" into the right props on all four — one step active at a * time, a stopped run keeping what it did, an unanswered approval never * counting as a yes — was left to every product, and every product got a * different edge wrong. This file is that logic, once. * * It is plain data in, plain data out: no React, no timers. The same events * drive a live run (from a stream, a socket, a poll) and a recorded one * (`replayAgentRun`), and `agentRunFrom` rebuilds a finished run for a history * page without playing it. `useAgentRun` wraps it for components. * * Events are JSON-serialisable on purpose, so a recording can be stored as a * run log and a backend can emit them as-is. */ /** Where a run is. The last four are ends; nothing changes a run after one. */ export type AgentRunPhase = 'idle' | 'running' | 'awaiting' | 'finished' | 'failed' | 'stopped' | 'expired'; /** The same five statuses AgentActivityStep and ToolCall take. */ export type AgentRunStepStatus = 'pending' | 'active' | 'done' | 'failed' | 'skipped'; export interface AgentRunStep { id: string; status: AgentRunStepStatus; label?: string; /** Why it failed, from `step-failed`. */ error?: string; /** Whether it ever began. A planned step the run never reached is not in `log`. */ started: boolean; } export interface AgentRunApproval { /** The step the approval belongs to. */ stepId: string; status: 'pending' | 'approved' | 'rejected' | 'expired'; /** What happened, in words — ApprovalGate's `resolution`. */ resolution?: string; /** A decision is on its way to the server. */ isSubmitting: boolean; /** The decision did not arrive. The approval is still pending. */ error?: string; } export interface AgentRunState { phase: AgentRunPhase; /** Every step known so far, in the order it was planned or started. */ steps: AgentRunStep[]; /** The latest approval, or null if the run has not asked for one. */ approval: AgentRunApproval | null; output: string; isStreaming: boolean; /** Stop was requested and the run has not confirmed it yet. */ isStopping: boolean; /** From `run-failed`, when the failure is the run's and not one step's. */ error?: string; } /** * What a run reports. A producer — a backend stream, a script, a recording — * emits these; it never computes a status itself. */ export type AgentRunEvent = /** Optionally with the plan, so later steps can be listed as not done. */ { type: 'run-started'; steps?: { id: string; label?: string; }[]; } | { type: 'step-started'; id: string; label?: string; } | { type: 'step-done'; id: string; } | { type: 'step-failed'; id: string; error?: string; } | { type: 'step-skipped'; id: string; } | { type: 'approval-requested'; id: string; } | { type: 'approval-resolved'; id: string; decision: 'approved' | 'rejected' | 'expired'; resolution?: string; } /** A chunk of output, appended. */ | { type: 'output'; text: string; } | { type: 'output-done'; } | { type: 'run-finished'; } | { type: 'run-failed'; error?: string; } /** `resolution` explains an approval the stop left undecided. */ | { type: 'run-stopped'; resolution?: string; } | { type: 'stop-requested'; } | { type: 'stop-failed'; } | { type: 'reset'; } | { type: 'decision-submitted'; id: string; } | { type: 'decision-failed'; id: string; error: string; }; export declare const initialAgentRunState: AgentRunState; /** Whether the run has reached an end — finished, failed, stopped or expired. */ export declare const agentRunHasEnded: (state: AgentRunState) => boolean; /** * The whole policy. Every rule here is one the AgentRun or HumanApproval * pattern states; the comment says which. */ export declare function agentRunReducer(state: AgentRunState, event: AgentRunEvent): AgentRunState; /** A finished run rebuilt from its events, without playing it — for a history page. */ export declare const agentRunFrom: (events: AgentRunEvent[]) => AgentRunState; /** Steps that began, in order — what AgentActivity lists. Never planned-but-unreached ones. */ export declare const agentRunLog: (state: AgentRunState) => AgentRunStep[]; /** A recording: each event with the milliseconds to wait before it. */ export type AgentRunRecording = { delay: number; event: AgentRunEvent; }[]; export interface ReplayAgentRunOptions { /** 2 plays twice as fast. Default 1. */ speed?: number; } /** * Plays a recording into `dispatch` on timers. Returns a function that cancels * the rest — call it on unmount, or on stop before dispatching `run-stopped`. */ export declare function replayAgentRun(recording: AgentRunRecording, dispatch: (event: AgentRunEvent) => void, { speed }?: ReplayAgentRunOptions): () => void; //# sourceMappingURL=agent-run.d.ts.map