@machinemetrics/agent-event-protocol 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +274 -0
- package/dist/chunk-RSG7CUUT.js +220 -0
- package/dist/chunk-RSG7CUUT.js.map +1 -0
- package/dist/chunk-WZD2VBMM.js +180 -0
- package/dist/chunk-WZD2VBMM.js.map +1 -0
- package/dist/chunk-ZINGYSGU.js +19 -0
- package/dist/chunk-ZINGYSGU.js.map +1 -0
- package/dist/core.cjs +241 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +463 -0
- package/dist/core.d.ts +463 -0
- package/dist/core.js +47 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +458 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/sse.cjs +404 -0
- package/dist/sse.cjs.map +1 -0
- package/dist/sse.d.cts +56 -0
- package/dist/sse.d.ts +56 -0
- package/dist/sse.js +10 -0
- package/dist/sse.js.map +1 -0
- package/dist/types-CttB5bnC.d.cts +323 -0
- package/dist/types-CttB5bnC.d.ts +323 -0
- package/package.json +69 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content block for user input messages.
|
|
3
|
+
* Currently supports text only; extensible for multimodal content (images, files, audio).
|
|
4
|
+
*/
|
|
5
|
+
type UserContentBlock = {
|
|
6
|
+
type: 'text';
|
|
7
|
+
/** The text content from the user. */
|
|
8
|
+
text: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Content block variants in assistant responses.
|
|
12
|
+
* Discriminated union on `type` field.
|
|
13
|
+
*/
|
|
14
|
+
type AssistantContentBlock = {
|
|
15
|
+
type: 'text';
|
|
16
|
+
/** The text output from the agent. */
|
|
17
|
+
text: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'thinking';
|
|
20
|
+
/** The reasoning/thinking content from the model. */
|
|
21
|
+
text: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'tool_use';
|
|
24
|
+
/** Unique identifier for this tool call, used to correlate with tool_result. */
|
|
25
|
+
toolCallId: string;
|
|
26
|
+
/** Name of the tool being invoked. */
|
|
27
|
+
tool: string;
|
|
28
|
+
/** Arguments passed to the tool. */
|
|
29
|
+
args: Record<string, unknown>;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'tool_result';
|
|
32
|
+
/** Identifier of the tool call this result corresponds to. */
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
/** Name of the tool that was executed. */
|
|
35
|
+
tool: string;
|
|
36
|
+
/** Whether the tool execution succeeded or failed. */
|
|
37
|
+
status: 'success' | 'error';
|
|
38
|
+
/** Optional human-readable summary of the result. Full payloads are not included. */
|
|
39
|
+
summary?: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A single message containing user input.
|
|
43
|
+
* Messages contain an array of content blocks for future multimodal support.
|
|
44
|
+
*/
|
|
45
|
+
type InputMessage = {
|
|
46
|
+
/** Array of content blocks in this message. */
|
|
47
|
+
content: UserContentBlock[];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Result from a client-side action triggered by a continuation event.
|
|
51
|
+
* Sent back to the server in the next request's context.
|
|
52
|
+
*/
|
|
53
|
+
type ActionResult = {
|
|
54
|
+
/** Identifier matching the actionId from the continuation event. */
|
|
55
|
+
actionId: string;
|
|
56
|
+
/** Action type, matching the kind from the continuation event. */
|
|
57
|
+
kind: string;
|
|
58
|
+
/** Whether the action completed successfully. */
|
|
59
|
+
status: 'success' | 'error';
|
|
60
|
+
/** Result data on success. Schema depends on the action kind. */
|
|
61
|
+
data?: unknown;
|
|
62
|
+
/** Error message on failure. */
|
|
63
|
+
error?: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Request payload for agent streaming endpoints.
|
|
67
|
+
* Only `messages` is required; threadId enables conversation continuity.
|
|
68
|
+
*/
|
|
69
|
+
type AgentRequest = {
|
|
70
|
+
/** New user input messages. Server loads prior history via threadId. */
|
|
71
|
+
messages: InputMessage[];
|
|
72
|
+
/** Optional thread identifier to resume an existing conversation. */
|
|
73
|
+
threadId?: string;
|
|
74
|
+
/** Optional context including action results and passthrough data. */
|
|
75
|
+
context?: {
|
|
76
|
+
/** Results from continuation events in the previous response. */
|
|
77
|
+
actionResults?: ActionResult[];
|
|
78
|
+
/** Additional passthrough data for the agent. */
|
|
79
|
+
[key: string]: unknown;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Lifecycle stage for streaming content blocks.
|
|
84
|
+
* Content events progress through start → delta(s) → stop.
|
|
85
|
+
*/
|
|
86
|
+
type EventStage = 'start' | 'delta' | 'stop';
|
|
87
|
+
/**
|
|
88
|
+
* Error codes for protocol error events.
|
|
89
|
+
* Maps to specific HTTP status codes and retry guidance.
|
|
90
|
+
*/
|
|
91
|
+
type ErrorCode =
|
|
92
|
+
/** Authentication failed (401). */
|
|
93
|
+
'auth_failed'
|
|
94
|
+
/** Thread does not exist (404). */
|
|
95
|
+
| 'thread_not_found'
|
|
96
|
+
/** Thread ownership violation (403). */
|
|
97
|
+
| 'thread_access_denied'
|
|
98
|
+
/** Invalid request format or missing required fields (400). */
|
|
99
|
+
| 'invalid_message_format'
|
|
100
|
+
/** Message exceeds size limit (413). */
|
|
101
|
+
| 'message_too_large'
|
|
102
|
+
/** Rate limit exceeded (429). */
|
|
103
|
+
| 'rate_limit_exceeded'
|
|
104
|
+
/** Stream timed out (504). */
|
|
105
|
+
| 'stream_timeout'
|
|
106
|
+
/** Agent execution error (500). */
|
|
107
|
+
| 'agent_error'
|
|
108
|
+
/** Tool execution error (500). */
|
|
109
|
+
| 'tool_error'
|
|
110
|
+
/** Internal server error (500). */
|
|
111
|
+
| 'internal_error'
|
|
112
|
+
/** Action ID not found or already resolved (400). */
|
|
113
|
+
| 'invalid_action_id';
|
|
114
|
+
/**
|
|
115
|
+
* Reason why the stream finished.
|
|
116
|
+
* Determines whether additional action is needed from the client.
|
|
117
|
+
*/
|
|
118
|
+
type FinishReason =
|
|
119
|
+
/** Generation completed normally. */
|
|
120
|
+
'stop'
|
|
121
|
+
/** Generation stopped due to max tokens. */
|
|
122
|
+
| 'length'
|
|
123
|
+
/** Generation stopped due to an error. */
|
|
124
|
+
| 'error'
|
|
125
|
+
/** Generation paused waiting for client action (see continuation event). */
|
|
126
|
+
| 'action_required';
|
|
127
|
+
/**
|
|
128
|
+
* First event in a stream, providing request and thread identifiers.
|
|
129
|
+
* Always emitted before any content events.
|
|
130
|
+
*/
|
|
131
|
+
type MetadataEvent = {
|
|
132
|
+
event: 'metadata';
|
|
133
|
+
/** Unique identifier for this request, present on all events. */
|
|
134
|
+
requestId: string;
|
|
135
|
+
/** Thread identifier for conversation continuity. */
|
|
136
|
+
threadId: string;
|
|
137
|
+
/** Identifier of the agent handling this request. */
|
|
138
|
+
agentId: string;
|
|
139
|
+
/** ISO 8601 timestamp. */
|
|
140
|
+
timestamp: string;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Streaming text content from the agent.
|
|
144
|
+
* Uses the stage lifecycle: start → delta(s) → stop.
|
|
145
|
+
*/
|
|
146
|
+
type TextEvent = {
|
|
147
|
+
event: 'text';
|
|
148
|
+
/** Request identifier for correlation. */
|
|
149
|
+
requestId: string;
|
|
150
|
+
/** Message identifier within this request. */
|
|
151
|
+
messageId: string;
|
|
152
|
+
/** Unique identifier for this content block. */
|
|
153
|
+
blockId: string;
|
|
154
|
+
/** Index of this block within the message (on start only). */
|
|
155
|
+
blockIndex?: number;
|
|
156
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
157
|
+
stage: EventStage;
|
|
158
|
+
/** Text content chunk (on delta only). */
|
|
159
|
+
delta?: string;
|
|
160
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
161
|
+
deltaIndex?: number;
|
|
162
|
+
/** ISO 8601 timestamp. */
|
|
163
|
+
timestamp: string;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Streaming thinking/reasoning content from the model.
|
|
167
|
+
* Structure mirrors TextEvent; represents internal reasoning.
|
|
168
|
+
*/
|
|
169
|
+
type ThinkingEvent = {
|
|
170
|
+
event: 'thinking';
|
|
171
|
+
/** Request identifier for correlation. */
|
|
172
|
+
requestId: string;
|
|
173
|
+
/** Message identifier within this request. */
|
|
174
|
+
messageId: string;
|
|
175
|
+
/** Unique identifier for this content block. */
|
|
176
|
+
blockId: string;
|
|
177
|
+
/** Index of this block within the message (on start only). */
|
|
178
|
+
blockIndex?: number;
|
|
179
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
180
|
+
stage: EventStage;
|
|
181
|
+
/** Thinking content chunk (on delta only). */
|
|
182
|
+
delta?: string;
|
|
183
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
184
|
+
deltaIndex?: number;
|
|
185
|
+
/** ISO 8601 timestamp. */
|
|
186
|
+
timestamp: string;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Tool invocation event with streaming argument construction.
|
|
190
|
+
* Arguments may stream as JSON fragments in delta events.
|
|
191
|
+
*/
|
|
192
|
+
type ToolCallEvent = {
|
|
193
|
+
event: 'tool_call';
|
|
194
|
+
/** Request identifier for correlation. */
|
|
195
|
+
requestId: string;
|
|
196
|
+
/** Message identifier within this request. */
|
|
197
|
+
messageId: string;
|
|
198
|
+
/** Unique identifier for this content block. */
|
|
199
|
+
blockId: string;
|
|
200
|
+
/** Index of this block within the message (on start only). */
|
|
201
|
+
blockIndex?: number;
|
|
202
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
203
|
+
stage: EventStage;
|
|
204
|
+
/** Unique identifier for this tool call (on start and stop). */
|
|
205
|
+
toolCallId?: string;
|
|
206
|
+
/** Name of the tool being invoked (on start and stop). */
|
|
207
|
+
tool?: string;
|
|
208
|
+
/** JSON fragment of arguments (on delta only). */
|
|
209
|
+
delta?: string;
|
|
210
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
211
|
+
deltaIndex?: number;
|
|
212
|
+
/** Complete parsed arguments (on stop only). */
|
|
213
|
+
args?: Record<string, unknown>;
|
|
214
|
+
/** ISO 8601 timestamp. */
|
|
215
|
+
timestamp: string;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Tool execution result event.
|
|
219
|
+
* Contains status and optional summary; full payloads are not included.
|
|
220
|
+
*/
|
|
221
|
+
type ToolResultEvent = {
|
|
222
|
+
event: 'tool_result';
|
|
223
|
+
/** Request identifier for correlation. */
|
|
224
|
+
requestId: string;
|
|
225
|
+
/** Message identifier within this request. */
|
|
226
|
+
messageId: string;
|
|
227
|
+
/** Unique identifier for this content block. */
|
|
228
|
+
blockId: string;
|
|
229
|
+
/** Index of this block within the message (on start only). */
|
|
230
|
+
blockIndex?: number;
|
|
231
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
232
|
+
stage: EventStage;
|
|
233
|
+
/** Identifier of the tool call this result corresponds to. */
|
|
234
|
+
toolCallId?: string;
|
|
235
|
+
/** Name of the tool that was executed. */
|
|
236
|
+
tool?: string;
|
|
237
|
+
/** Whether the tool execution succeeded or failed (on stop only). */
|
|
238
|
+
status?: 'success' | 'error';
|
|
239
|
+
/** Optional human-readable summary of the result (on stop only). */
|
|
240
|
+
summary?: string;
|
|
241
|
+
/** ISO 8601 timestamp. */
|
|
242
|
+
timestamp: string;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* Terminal event indicating stream completion.
|
|
246
|
+
* Contains the complete content array and usage statistics.
|
|
247
|
+
*/
|
|
248
|
+
type DoneEvent = {
|
|
249
|
+
event: 'done';
|
|
250
|
+
/** Request identifier for correlation. */
|
|
251
|
+
requestId: string;
|
|
252
|
+
/** Message identifier within this request. */
|
|
253
|
+
messageId: string;
|
|
254
|
+
/** Thread identifier for conversation continuity. */
|
|
255
|
+
threadId: string;
|
|
256
|
+
/** All completed content blocks for this response. */
|
|
257
|
+
content: AssistantContentBlock[];
|
|
258
|
+
/** Token usage statistics. */
|
|
259
|
+
usage?: {
|
|
260
|
+
promptTokens: number;
|
|
261
|
+
completionTokens: number;
|
|
262
|
+
};
|
|
263
|
+
/** Reason why generation finished. */
|
|
264
|
+
finishReason: FinishReason;
|
|
265
|
+
/** Optional trace identifier for debugging. */
|
|
266
|
+
traceId?: string;
|
|
267
|
+
/** ISO 8601 timestamp. */
|
|
268
|
+
timestamp: string;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Terminal event indicating an error condition.
|
|
272
|
+
* Includes error code and retry guidance.
|
|
273
|
+
*/
|
|
274
|
+
type ErrorEvent = {
|
|
275
|
+
event: 'error';
|
|
276
|
+
/** Request identifier for correlation. */
|
|
277
|
+
requestId: string;
|
|
278
|
+
/** Human-readable error message. */
|
|
279
|
+
message: string;
|
|
280
|
+
/** Structured error code for programmatic handling. */
|
|
281
|
+
code?: ErrorCode;
|
|
282
|
+
/** Whether the client should retry this request. */
|
|
283
|
+
retryable?: boolean;
|
|
284
|
+
/** ISO 8601 timestamp. */
|
|
285
|
+
timestamp: string;
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Terminal event acknowledging client cancellation.
|
|
289
|
+
* Sent when the client aborts the stream.
|
|
290
|
+
*/
|
|
291
|
+
type AbortedEvent = {
|
|
292
|
+
event: 'aborted';
|
|
293
|
+
/** Request identifier for correlation. */
|
|
294
|
+
requestId: string;
|
|
295
|
+
/** Message identifier if abort occurred mid-message. */
|
|
296
|
+
messageId?: string;
|
|
297
|
+
/** ISO 8601 timestamp. */
|
|
298
|
+
timestamp: string;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Event requesting client-side action before continuing.
|
|
302
|
+
* Stream pauses with finishReason 'action_required' until client responds.
|
|
303
|
+
*/
|
|
304
|
+
type ContinuationEvent = {
|
|
305
|
+
event: 'continuation';
|
|
306
|
+
/** Request identifier for correlation. */
|
|
307
|
+
requestId: string;
|
|
308
|
+
/** Unique identifier for this action, used in ActionResult. */
|
|
309
|
+
actionId: string;
|
|
310
|
+
/** Action type identifier (e.g., 'tool', 'approval', 'input', 'oauth'). */
|
|
311
|
+
kind: string;
|
|
312
|
+
/** Action-specific payload. Schema depends on the kind. */
|
|
313
|
+
payload: Record<string, unknown>;
|
|
314
|
+
/** ISO 8601 timestamp. */
|
|
315
|
+
timestamp: string;
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Union of all protocol event types.
|
|
319
|
+
* Discriminated on the `event` field.
|
|
320
|
+
*/
|
|
321
|
+
type AgentEvent = MetadataEvent | TextEvent | ThinkingEvent | ToolCallEvent | ToolResultEvent | DoneEvent | ErrorEvent | AbortedEvent | ContinuationEvent;
|
|
322
|
+
|
|
323
|
+
export type { AssistantContentBlock as A, ContinuationEvent as C, DoneEvent as D, EventStage as E, FinishReason as F, InputMessage as I, MetadataEvent as M, TextEvent as T, UserContentBlock as U, ActionResult as a, AgentRequest as b, ErrorCode as c, ThinkingEvent as d, ToolCallEvent as e, ToolResultEvent as f, ErrorEvent as g, AbortedEvent as h, AgentEvent as i };
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content block for user input messages.
|
|
3
|
+
* Currently supports text only; extensible for multimodal content (images, files, audio).
|
|
4
|
+
*/
|
|
5
|
+
type UserContentBlock = {
|
|
6
|
+
type: 'text';
|
|
7
|
+
/** The text content from the user. */
|
|
8
|
+
text: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Content block variants in assistant responses.
|
|
12
|
+
* Discriminated union on `type` field.
|
|
13
|
+
*/
|
|
14
|
+
type AssistantContentBlock = {
|
|
15
|
+
type: 'text';
|
|
16
|
+
/** The text output from the agent. */
|
|
17
|
+
text: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'thinking';
|
|
20
|
+
/** The reasoning/thinking content from the model. */
|
|
21
|
+
text: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'tool_use';
|
|
24
|
+
/** Unique identifier for this tool call, used to correlate with tool_result. */
|
|
25
|
+
toolCallId: string;
|
|
26
|
+
/** Name of the tool being invoked. */
|
|
27
|
+
tool: string;
|
|
28
|
+
/** Arguments passed to the tool. */
|
|
29
|
+
args: Record<string, unknown>;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'tool_result';
|
|
32
|
+
/** Identifier of the tool call this result corresponds to. */
|
|
33
|
+
toolCallId: string;
|
|
34
|
+
/** Name of the tool that was executed. */
|
|
35
|
+
tool: string;
|
|
36
|
+
/** Whether the tool execution succeeded or failed. */
|
|
37
|
+
status: 'success' | 'error';
|
|
38
|
+
/** Optional human-readable summary of the result. Full payloads are not included. */
|
|
39
|
+
summary?: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A single message containing user input.
|
|
43
|
+
* Messages contain an array of content blocks for future multimodal support.
|
|
44
|
+
*/
|
|
45
|
+
type InputMessage = {
|
|
46
|
+
/** Array of content blocks in this message. */
|
|
47
|
+
content: UserContentBlock[];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Result from a client-side action triggered by a continuation event.
|
|
51
|
+
* Sent back to the server in the next request's context.
|
|
52
|
+
*/
|
|
53
|
+
type ActionResult = {
|
|
54
|
+
/** Identifier matching the actionId from the continuation event. */
|
|
55
|
+
actionId: string;
|
|
56
|
+
/** Action type, matching the kind from the continuation event. */
|
|
57
|
+
kind: string;
|
|
58
|
+
/** Whether the action completed successfully. */
|
|
59
|
+
status: 'success' | 'error';
|
|
60
|
+
/** Result data on success. Schema depends on the action kind. */
|
|
61
|
+
data?: unknown;
|
|
62
|
+
/** Error message on failure. */
|
|
63
|
+
error?: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Request payload for agent streaming endpoints.
|
|
67
|
+
* Only `messages` is required; threadId enables conversation continuity.
|
|
68
|
+
*/
|
|
69
|
+
type AgentRequest = {
|
|
70
|
+
/** New user input messages. Server loads prior history via threadId. */
|
|
71
|
+
messages: InputMessage[];
|
|
72
|
+
/** Optional thread identifier to resume an existing conversation. */
|
|
73
|
+
threadId?: string;
|
|
74
|
+
/** Optional context including action results and passthrough data. */
|
|
75
|
+
context?: {
|
|
76
|
+
/** Results from continuation events in the previous response. */
|
|
77
|
+
actionResults?: ActionResult[];
|
|
78
|
+
/** Additional passthrough data for the agent. */
|
|
79
|
+
[key: string]: unknown;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Lifecycle stage for streaming content blocks.
|
|
84
|
+
* Content events progress through start → delta(s) → stop.
|
|
85
|
+
*/
|
|
86
|
+
type EventStage = 'start' | 'delta' | 'stop';
|
|
87
|
+
/**
|
|
88
|
+
* Error codes for protocol error events.
|
|
89
|
+
* Maps to specific HTTP status codes and retry guidance.
|
|
90
|
+
*/
|
|
91
|
+
type ErrorCode =
|
|
92
|
+
/** Authentication failed (401). */
|
|
93
|
+
'auth_failed'
|
|
94
|
+
/** Thread does not exist (404). */
|
|
95
|
+
| 'thread_not_found'
|
|
96
|
+
/** Thread ownership violation (403). */
|
|
97
|
+
| 'thread_access_denied'
|
|
98
|
+
/** Invalid request format or missing required fields (400). */
|
|
99
|
+
| 'invalid_message_format'
|
|
100
|
+
/** Message exceeds size limit (413). */
|
|
101
|
+
| 'message_too_large'
|
|
102
|
+
/** Rate limit exceeded (429). */
|
|
103
|
+
| 'rate_limit_exceeded'
|
|
104
|
+
/** Stream timed out (504). */
|
|
105
|
+
| 'stream_timeout'
|
|
106
|
+
/** Agent execution error (500). */
|
|
107
|
+
| 'agent_error'
|
|
108
|
+
/** Tool execution error (500). */
|
|
109
|
+
| 'tool_error'
|
|
110
|
+
/** Internal server error (500). */
|
|
111
|
+
| 'internal_error'
|
|
112
|
+
/** Action ID not found or already resolved (400). */
|
|
113
|
+
| 'invalid_action_id';
|
|
114
|
+
/**
|
|
115
|
+
* Reason why the stream finished.
|
|
116
|
+
* Determines whether additional action is needed from the client.
|
|
117
|
+
*/
|
|
118
|
+
type FinishReason =
|
|
119
|
+
/** Generation completed normally. */
|
|
120
|
+
'stop'
|
|
121
|
+
/** Generation stopped due to max tokens. */
|
|
122
|
+
| 'length'
|
|
123
|
+
/** Generation stopped due to an error. */
|
|
124
|
+
| 'error'
|
|
125
|
+
/** Generation paused waiting for client action (see continuation event). */
|
|
126
|
+
| 'action_required';
|
|
127
|
+
/**
|
|
128
|
+
* First event in a stream, providing request and thread identifiers.
|
|
129
|
+
* Always emitted before any content events.
|
|
130
|
+
*/
|
|
131
|
+
type MetadataEvent = {
|
|
132
|
+
event: 'metadata';
|
|
133
|
+
/** Unique identifier for this request, present on all events. */
|
|
134
|
+
requestId: string;
|
|
135
|
+
/** Thread identifier for conversation continuity. */
|
|
136
|
+
threadId: string;
|
|
137
|
+
/** Identifier of the agent handling this request. */
|
|
138
|
+
agentId: string;
|
|
139
|
+
/** ISO 8601 timestamp. */
|
|
140
|
+
timestamp: string;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Streaming text content from the agent.
|
|
144
|
+
* Uses the stage lifecycle: start → delta(s) → stop.
|
|
145
|
+
*/
|
|
146
|
+
type TextEvent = {
|
|
147
|
+
event: 'text';
|
|
148
|
+
/** Request identifier for correlation. */
|
|
149
|
+
requestId: string;
|
|
150
|
+
/** Message identifier within this request. */
|
|
151
|
+
messageId: string;
|
|
152
|
+
/** Unique identifier for this content block. */
|
|
153
|
+
blockId: string;
|
|
154
|
+
/** Index of this block within the message (on start only). */
|
|
155
|
+
blockIndex?: number;
|
|
156
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
157
|
+
stage: EventStage;
|
|
158
|
+
/** Text content chunk (on delta only). */
|
|
159
|
+
delta?: string;
|
|
160
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
161
|
+
deltaIndex?: number;
|
|
162
|
+
/** ISO 8601 timestamp. */
|
|
163
|
+
timestamp: string;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Streaming thinking/reasoning content from the model.
|
|
167
|
+
* Structure mirrors TextEvent; represents internal reasoning.
|
|
168
|
+
*/
|
|
169
|
+
type ThinkingEvent = {
|
|
170
|
+
event: 'thinking';
|
|
171
|
+
/** Request identifier for correlation. */
|
|
172
|
+
requestId: string;
|
|
173
|
+
/** Message identifier within this request. */
|
|
174
|
+
messageId: string;
|
|
175
|
+
/** Unique identifier for this content block. */
|
|
176
|
+
blockId: string;
|
|
177
|
+
/** Index of this block within the message (on start only). */
|
|
178
|
+
blockIndex?: number;
|
|
179
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
180
|
+
stage: EventStage;
|
|
181
|
+
/** Thinking content chunk (on delta only). */
|
|
182
|
+
delta?: string;
|
|
183
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
184
|
+
deltaIndex?: number;
|
|
185
|
+
/** ISO 8601 timestamp. */
|
|
186
|
+
timestamp: string;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Tool invocation event with streaming argument construction.
|
|
190
|
+
* Arguments may stream as JSON fragments in delta events.
|
|
191
|
+
*/
|
|
192
|
+
type ToolCallEvent = {
|
|
193
|
+
event: 'tool_call';
|
|
194
|
+
/** Request identifier for correlation. */
|
|
195
|
+
requestId: string;
|
|
196
|
+
/** Message identifier within this request. */
|
|
197
|
+
messageId: string;
|
|
198
|
+
/** Unique identifier for this content block. */
|
|
199
|
+
blockId: string;
|
|
200
|
+
/** Index of this block within the message (on start only). */
|
|
201
|
+
blockIndex?: number;
|
|
202
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
203
|
+
stage: EventStage;
|
|
204
|
+
/** Unique identifier for this tool call (on start and stop). */
|
|
205
|
+
toolCallId?: string;
|
|
206
|
+
/** Name of the tool being invoked (on start and stop). */
|
|
207
|
+
tool?: string;
|
|
208
|
+
/** JSON fragment of arguments (on delta only). */
|
|
209
|
+
delta?: string;
|
|
210
|
+
/** Index of this delta within the block for ordering (on delta only). */
|
|
211
|
+
deltaIndex?: number;
|
|
212
|
+
/** Complete parsed arguments (on stop only). */
|
|
213
|
+
args?: Record<string, unknown>;
|
|
214
|
+
/** ISO 8601 timestamp. */
|
|
215
|
+
timestamp: string;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Tool execution result event.
|
|
219
|
+
* Contains status and optional summary; full payloads are not included.
|
|
220
|
+
*/
|
|
221
|
+
type ToolResultEvent = {
|
|
222
|
+
event: 'tool_result';
|
|
223
|
+
/** Request identifier for correlation. */
|
|
224
|
+
requestId: string;
|
|
225
|
+
/** Message identifier within this request. */
|
|
226
|
+
messageId: string;
|
|
227
|
+
/** Unique identifier for this content block. */
|
|
228
|
+
blockId: string;
|
|
229
|
+
/** Index of this block within the message (on start only). */
|
|
230
|
+
blockIndex?: number;
|
|
231
|
+
/** Lifecycle stage: start, delta, or stop. */
|
|
232
|
+
stage: EventStage;
|
|
233
|
+
/** Identifier of the tool call this result corresponds to. */
|
|
234
|
+
toolCallId?: string;
|
|
235
|
+
/** Name of the tool that was executed. */
|
|
236
|
+
tool?: string;
|
|
237
|
+
/** Whether the tool execution succeeded or failed (on stop only). */
|
|
238
|
+
status?: 'success' | 'error';
|
|
239
|
+
/** Optional human-readable summary of the result (on stop only). */
|
|
240
|
+
summary?: string;
|
|
241
|
+
/** ISO 8601 timestamp. */
|
|
242
|
+
timestamp: string;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* Terminal event indicating stream completion.
|
|
246
|
+
* Contains the complete content array and usage statistics.
|
|
247
|
+
*/
|
|
248
|
+
type DoneEvent = {
|
|
249
|
+
event: 'done';
|
|
250
|
+
/** Request identifier for correlation. */
|
|
251
|
+
requestId: string;
|
|
252
|
+
/** Message identifier within this request. */
|
|
253
|
+
messageId: string;
|
|
254
|
+
/** Thread identifier for conversation continuity. */
|
|
255
|
+
threadId: string;
|
|
256
|
+
/** All completed content blocks for this response. */
|
|
257
|
+
content: AssistantContentBlock[];
|
|
258
|
+
/** Token usage statistics. */
|
|
259
|
+
usage?: {
|
|
260
|
+
promptTokens: number;
|
|
261
|
+
completionTokens: number;
|
|
262
|
+
};
|
|
263
|
+
/** Reason why generation finished. */
|
|
264
|
+
finishReason: FinishReason;
|
|
265
|
+
/** Optional trace identifier for debugging. */
|
|
266
|
+
traceId?: string;
|
|
267
|
+
/** ISO 8601 timestamp. */
|
|
268
|
+
timestamp: string;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Terminal event indicating an error condition.
|
|
272
|
+
* Includes error code and retry guidance.
|
|
273
|
+
*/
|
|
274
|
+
type ErrorEvent = {
|
|
275
|
+
event: 'error';
|
|
276
|
+
/** Request identifier for correlation. */
|
|
277
|
+
requestId: string;
|
|
278
|
+
/** Human-readable error message. */
|
|
279
|
+
message: string;
|
|
280
|
+
/** Structured error code for programmatic handling. */
|
|
281
|
+
code?: ErrorCode;
|
|
282
|
+
/** Whether the client should retry this request. */
|
|
283
|
+
retryable?: boolean;
|
|
284
|
+
/** ISO 8601 timestamp. */
|
|
285
|
+
timestamp: string;
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Terminal event acknowledging client cancellation.
|
|
289
|
+
* Sent when the client aborts the stream.
|
|
290
|
+
*/
|
|
291
|
+
type AbortedEvent = {
|
|
292
|
+
event: 'aborted';
|
|
293
|
+
/** Request identifier for correlation. */
|
|
294
|
+
requestId: string;
|
|
295
|
+
/** Message identifier if abort occurred mid-message. */
|
|
296
|
+
messageId?: string;
|
|
297
|
+
/** ISO 8601 timestamp. */
|
|
298
|
+
timestamp: string;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Event requesting client-side action before continuing.
|
|
302
|
+
* Stream pauses with finishReason 'action_required' until client responds.
|
|
303
|
+
*/
|
|
304
|
+
type ContinuationEvent = {
|
|
305
|
+
event: 'continuation';
|
|
306
|
+
/** Request identifier for correlation. */
|
|
307
|
+
requestId: string;
|
|
308
|
+
/** Unique identifier for this action, used in ActionResult. */
|
|
309
|
+
actionId: string;
|
|
310
|
+
/** Action type identifier (e.g., 'tool', 'approval', 'input', 'oauth'). */
|
|
311
|
+
kind: string;
|
|
312
|
+
/** Action-specific payload. Schema depends on the kind. */
|
|
313
|
+
payload: Record<string, unknown>;
|
|
314
|
+
/** ISO 8601 timestamp. */
|
|
315
|
+
timestamp: string;
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Union of all protocol event types.
|
|
319
|
+
* Discriminated on the `event` field.
|
|
320
|
+
*/
|
|
321
|
+
type AgentEvent = MetadataEvent | TextEvent | ThinkingEvent | ToolCallEvent | ToolResultEvent | DoneEvent | ErrorEvent | AbortedEvent | ContinuationEvent;
|
|
322
|
+
|
|
323
|
+
export type { AssistantContentBlock as A, ContinuationEvent as C, DoneEvent as D, EventStage as E, FinishReason as F, InputMessage as I, MetadataEvent as M, TextEvent as T, UserContentBlock as U, ActionResult as a, AgentRequest as b, ErrorCode as c, ThinkingEvent as d, ToolCallEvent as e, ToolResultEvent as f, ErrorEvent as g, AbortedEvent as h, AgentEvent as i };
|