11import { Message , Task , TaskStatusUpdateEvent , TaskArtifactUpdateEvent } from '../../index.js' ;
22
33/**
4- * Discriminant values for {@link AgentExecutionEvent}. Mirror
5- * `StreamResponse.payload.$case` values for trivial conversion.
4+ * Discriminant values for {@link AgentExecutionEvent}. The wire-mapped
5+ * kinds (`message`, `task`, `statusUpdate`, `artifactUpdate`) mirror
6+ * `StreamResponse.payload.$case` values for trivial conversion. The
7+ * `error` kind is internal to the server pipeline: it is published on
8+ * the bus by the framework when the {@link AgentExecutor} rejects, and
9+ * consumed by the drain loop to persist a FAILED status and propagate
10+ * the exception to the transport. It never crosses the wire.
611 */
7- export type AgentExecutionEventKind = 'message' | 'task' | 'statusUpdate' | 'artifactUpdate' ;
12+ export type AgentExecutionEventKind =
13+ | 'message'
14+ | 'task'
15+ | 'statusUpdate'
16+ | 'artifactUpdate'
17+ | 'error' ;
818
919/**
1020 * Discriminated union wrapper for agent execution events. The `kind`
1121 * property is the TypeScript discriminant, enabling exhaustive
1222 * `switch`/`case` narrowing without unsafe casts.
23+ *
24+ * The `error` variant carries the raw error thrown by the
25+ * {@link AgentExecutor}. It is an internal, server-side signalling
26+ * event: the framework publishes it in the `.catch` handler of
27+ * `AgentExecutor.execute()` and the drain loop consumes it to persist
28+ * a FAILED task status and propagate the exception to the transport
29+ * layer. Executors MUST NOT publish `error` events directly — use
30+ * regular termination (return normally or publish a FAILED status).
1331 */
1432export type AgentExecutionEvent =
1533 | { kind : 'message' ; data : Message }
1634 | { kind : 'task' ; data : Task }
1735 | { kind : 'statusUpdate' ; data : TaskStatusUpdateEvent }
18- | { kind : 'artifactUpdate' ; data : TaskArtifactUpdateEvent } ;
36+ | { kind : 'artifactUpdate' ; data : TaskArtifactUpdateEvent }
37+ | { kind : 'error' ; data : unknown } ;
1938
2039/**
2140 * Factory functions for type-safe {@link AgentExecutionEvent} wrappers.
@@ -38,6 +57,13 @@ export const AgentEvent = {
3857 kind : 'artifactUpdate' ,
3958 data,
4059 } ) ,
60+ /**
61+ * Wraps a raw executor error for in-band propagation on the event bus.
62+ * See the `'error'` variant of {@link AgentExecutionEvent} for the
63+ * lifecycle contract — do not publish this from user-facing executor
64+ * code; the framework does so automatically.
65+ */
66+ error : ( data : unknown ) : AgentExecutionEvent => ( { kind : 'error' , data } ) ,
4167} as const ;
4268
4369/**
0 commit comments