@roll-agent/protocol 0.0.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/dist/index.js ADDED
@@ -0,0 +1,506 @@
1
+ import { z } from "zod/v4";
2
+ export const RUNTIME_PROTOCOL_VERSION = "1.0";
3
+ export const RUNTIME_EVENT_NOTIFICATION = "runtime.event";
4
+ export const RUNTIME_METHODS = {
5
+ initialize: "initialize",
6
+ threadList: "thread.list",
7
+ threadCreate: "thread.create",
8
+ threadOpen: "thread.open",
9
+ threadSnapshot: "thread.snapshot",
10
+ threadRename: "thread.rename",
11
+ threadDelete: "thread.delete",
12
+ threadDetach: "thread.detach",
13
+ threadCapabilities: "thread.capabilities",
14
+ turnStart: "turn.start",
15
+ turnCancel: "turn.cancel",
16
+ approvalRespond: "approval.respond",
17
+ operationGet: "operation.get",
18
+ };
19
+ export const RUNTIME_FEATURES = [
20
+ "thread-management",
21
+ "snapshots",
22
+ "turns",
23
+ "approvals",
24
+ "tool-streaming",
25
+ "reasoning-summary",
26
+ "operation-projection",
27
+ "process-local-sequence",
28
+ ];
29
+ export const RUNTIME_ERROR_CODES = {
30
+ protocolVersionUnsupported: "PROTOCOL_VERSION_UNSUPPORTED",
31
+ initializeRequired: "INITIALIZE_REQUIRED",
32
+ invalidParams: "INVALID_PARAMS",
33
+ threadNotFound: "THREAD_NOT_FOUND",
34
+ threadBusy: "THREAD_BUSY",
35
+ turnNotFound: "TURN_NOT_FOUND",
36
+ turnAlreadyActive: "TURN_ALREADY_ACTIVE",
37
+ approvalNotFound: "APPROVAL_NOT_FOUND",
38
+ capabilityUnavailable: "CAPABILITY_UNAVAILABLE",
39
+ runtimeClosing: "RUNTIME_CLOSING",
40
+ outcomeUnknown: "OUTCOME_UNKNOWN",
41
+ internalError: "INTERNAL_ERROR",
42
+ };
43
+ export const TOOL_OUTCOME_KINDS = [
44
+ "success",
45
+ "user_rejected",
46
+ "policy_denied",
47
+ "invalid_input",
48
+ "cancelled",
49
+ "tool_failed",
50
+ ];
51
+ export const threadIdSchema = z.string().uuid().brand();
52
+ export const turnIdSchema = z.string().uuid().brand();
53
+ export const approvalIdSchema = z.string().uuid().brand();
54
+ export const runtimeInstanceIdSchema = z.string().uuid().brand();
55
+ export const requestIdSchema = z.string().uuid().brand();
56
+ export const streamIdSchema = z.string().uuid().brand();
57
+ export const operationIdSchema = z.string().uuid().brand();
58
+ export const timestampSchema = z.string().datetime({ offset: true });
59
+ export const jsonValueSchema = z.json();
60
+ const runtimeClientInfoFields = {
61
+ name: z.string().trim().min(1).max(100),
62
+ version: z.string().trim().min(1).max(100),
63
+ };
64
+ export const runtimeClientInfoSchema = z.object(runtimeClientInfoFields).strict().readonly();
65
+ export const runtimeServerInfoSchema = z
66
+ .object({
67
+ ...runtimeClientInfoFields,
68
+ runtimeVersion: z.string().trim().min(1).max(100),
69
+ })
70
+ .strict()
71
+ .readonly();
72
+ export const runtimeLimitsSchema = z
73
+ .object({
74
+ maxFrameBytes: z.number().int().positive(),
75
+ maxPageSize: z.number().int().positive(),
76
+ eventReplay: z.literal(false),
77
+ idempotencyCacheEntries: z.number().int().positive(),
78
+ })
79
+ .strict()
80
+ .readonly();
81
+ export const initializeParamsSchema = z
82
+ .object({
83
+ protocolVersions: z.array(z.string().min(1)).min(1),
84
+ client: runtimeClientInfoSchema,
85
+ })
86
+ .strict()
87
+ .readonly();
88
+ export const initializeResultSchema = z
89
+ .object({
90
+ protocolVersion: z.literal(RUNTIME_PROTOCOL_VERSION),
91
+ runtimeInstanceId: runtimeInstanceIdSchema,
92
+ server: runtimeServerInfoSchema,
93
+ features: z.array(z.enum(RUNTIME_FEATURES)),
94
+ limits: runtimeLimitsSchema,
95
+ })
96
+ .strict()
97
+ .readonly();
98
+ export const threadSummarySchema = z
99
+ .object({
100
+ id: threadIdSchema,
101
+ title: z.string().optional(),
102
+ model: z.string().optional(),
103
+ createdAt: timestampSchema,
104
+ updatedAt: timestampSchema,
105
+ messageCount: z.number().int().nonnegative(),
106
+ })
107
+ .strict()
108
+ .readonly();
109
+ export const uiMessagePartSchema = z
110
+ .object({
111
+ type: z.literal("text"),
112
+ text: z.string(),
113
+ })
114
+ .strict()
115
+ .readonly();
116
+ export const uiMessageSchema = z
117
+ .object({
118
+ sequence: z.number().int().nonnegative(),
119
+ role: z.enum(["user", "assistant"]),
120
+ createdAt: timestampSchema,
121
+ parts: z.array(uiMessagePartSchema),
122
+ })
123
+ .strict()
124
+ .readonly();
125
+ export const toolOutcomeSchema = z
126
+ .object({
127
+ kind: z.enum(TOOL_OUTCOME_KINDS),
128
+ reason: z.string().optional(),
129
+ })
130
+ .strict()
131
+ .readonly();
132
+ export const operationViewSchema = z
133
+ .object({
134
+ id: operationIdSchema,
135
+ sequence: z.number().int().nonnegative(),
136
+ toolCallId: z.string().min(1),
137
+ agentName: z.string().min(1),
138
+ toolName: z.string().min(1),
139
+ createdAt: timestampSchema,
140
+ outcome: toolOutcomeSchema,
141
+ display: jsonValueSchema,
142
+ })
143
+ .strict()
144
+ .readonly();
145
+ export const activeTurnSchema = z
146
+ .object({
147
+ id: turnIdSchema,
148
+ status: z.enum(["running", "cancelling"]),
149
+ startedAt: timestampSchema,
150
+ })
151
+ .strict()
152
+ .readonly();
153
+ export const pendingApprovalSchema = z
154
+ .object({
155
+ id: approvalIdSchema,
156
+ turnId: turnIdSchema,
157
+ agentName: z.string().min(1),
158
+ toolName: z.string().min(1),
159
+ preview: jsonValueSchema,
160
+ reason: z.string().optional(),
161
+ })
162
+ .strict()
163
+ .readonly();
164
+ export const messagePageSchema = z
165
+ .object({
166
+ items: z.array(uiMessageSchema),
167
+ nextBeforeSequence: z.number().int().nonnegative().nullable(),
168
+ })
169
+ .strict()
170
+ .readonly();
171
+ export const operationPageSchema = z
172
+ .object({
173
+ items: z.array(operationViewSchema),
174
+ nextBeforeSequence: z.number().int().nonnegative().nullable(),
175
+ })
176
+ .strict()
177
+ .readonly();
178
+ export const threadSnapshotSchema = z
179
+ .object({
180
+ thread: threadSummarySchema,
181
+ messages: messagePageSchema,
182
+ operations: operationPageSchema,
183
+ activeTurn: activeTurnSchema.optional(),
184
+ pendingApprovals: z.array(pendingApprovalSchema),
185
+ transcriptCompleteness: z.enum(["complete", "legacy_snapshot"]),
186
+ })
187
+ .strict()
188
+ .readonly();
189
+ const pageSizeSchema = z.number().int().min(1).max(500).default(100);
190
+ const requestFields = { requestId: requestIdSchema };
191
+ const threadRequestFields = { threadId: threadIdSchema };
192
+ export const threadListParamsSchema = z
193
+ .object({
194
+ cursor: z.string().regex(/^\d+$/u).optional(),
195
+ limit: pageSizeSchema,
196
+ })
197
+ .strict()
198
+ .readonly();
199
+ export const threadListResultSchema = z
200
+ .object({
201
+ items: z.array(threadSummarySchema),
202
+ nextCursor: z.string().regex(/^\d+$/u).nullable(),
203
+ })
204
+ .strict()
205
+ .readonly();
206
+ export const threadCreateParamsSchema = z
207
+ .object({
208
+ ...requestFields,
209
+ title: z.string().trim().min(1).max(200).optional(),
210
+ })
211
+ .strict()
212
+ .readonly();
213
+ export const threadCreateResultSchema = z
214
+ .object({
215
+ thread: threadSummarySchema,
216
+ })
217
+ .strict()
218
+ .readonly();
219
+ export const threadOpenParamsSchema = z.object(threadRequestFields).strict().readonly();
220
+ export const threadOpenResultSchema = threadSnapshotSchema;
221
+ export const threadSnapshotParamsSchema = z
222
+ .object({
223
+ ...threadRequestFields,
224
+ messageBeforeSequence: z.number().int().nonnegative().optional(),
225
+ operationBeforeSequence: z.number().int().nonnegative().optional(),
226
+ limit: pageSizeSchema,
227
+ })
228
+ .strict()
229
+ .readonly();
230
+ export const threadSnapshotResultSchema = threadSnapshotSchema;
231
+ export const threadRenameParamsSchema = z
232
+ .object({
233
+ ...requestFields,
234
+ ...threadRequestFields,
235
+ title: z.string().trim().min(1).max(200),
236
+ })
237
+ .strict()
238
+ .readonly();
239
+ export const threadRenameResultSchema = z
240
+ .object({
241
+ thread: threadSummarySchema,
242
+ })
243
+ .strict()
244
+ .readonly();
245
+ export const threadDeleteParamsSchema = z
246
+ .object({ ...requestFields, ...threadRequestFields })
247
+ .strict()
248
+ .readonly();
249
+ export const threadDeleteResultSchema = z
250
+ .object({ deleted: z.literal(true) })
251
+ .strict()
252
+ .readonly();
253
+ export const threadDetachParamsSchema = z
254
+ .object({ ...requestFields, ...threadRequestFields })
255
+ .strict()
256
+ .readonly();
257
+ export const threadDetachResultSchema = z.object({ detached: z.boolean() }).strict().readonly();
258
+ export const threadCapabilitiesParamsSchema = z.object(threadRequestFields).strict().readonly();
259
+ export const threadCapabilitiesResultSchema = z
260
+ .object({
261
+ manifest: jsonValueSchema,
262
+ })
263
+ .strict()
264
+ .readonly();
265
+ export const turnStartParamsSchema = z
266
+ .object({
267
+ ...requestFields,
268
+ ...threadRequestFields,
269
+ turnId: turnIdSchema,
270
+ input: z
271
+ .object({
272
+ text: z.string().min(1),
273
+ })
274
+ .strict()
275
+ .readonly(),
276
+ })
277
+ .strict()
278
+ .readonly();
279
+ export const turnStartResultSchema = z
280
+ .object({
281
+ accepted: z.literal(true),
282
+ turnId: turnIdSchema,
283
+ })
284
+ .strict()
285
+ .readonly();
286
+ export const turnCancelParamsSchema = z
287
+ .object({
288
+ ...requestFields,
289
+ ...threadRequestFields,
290
+ turnId: turnIdSchema,
291
+ })
292
+ .strict()
293
+ .readonly();
294
+ export const turnCancelResultSchema = z.object({ cancelling: z.boolean() }).strict().readonly();
295
+ export const approvalRespondParamsSchema = z
296
+ .object({
297
+ ...requestFields,
298
+ ...threadRequestFields,
299
+ turnId: turnIdSchema,
300
+ approvalId: approvalIdSchema,
301
+ decision: z.enum(["approve", "reject"]),
302
+ reason: z.string().optional(),
303
+ })
304
+ .strict()
305
+ .readonly();
306
+ export const approvalRespondResultSchema = z
307
+ .object({ resolved: z.literal(true) })
308
+ .strict()
309
+ .readonly();
310
+ export const operationGetParamsSchema = z
311
+ .object({
312
+ ...threadRequestFields,
313
+ operationId: operationIdSchema,
314
+ })
315
+ .strict()
316
+ .readonly();
317
+ export const operationGetResultSchema = z
318
+ .object({
319
+ operation: operationViewSchema.nullable(),
320
+ })
321
+ .strict()
322
+ .readonly();
323
+ export const runtimeEventSchema = z.discriminatedUnion("type", [
324
+ z
325
+ .object({ type: z.literal("turn.started") })
326
+ .strict()
327
+ .readonly(),
328
+ z
329
+ .object({
330
+ type: z.literal("message.started"),
331
+ streamId: streamIdSchema,
332
+ })
333
+ .strict()
334
+ .readonly(),
335
+ z
336
+ .object({
337
+ type: z.literal("message.delta"),
338
+ streamId: streamIdSchema,
339
+ delta: z.string(),
340
+ })
341
+ .strict()
342
+ .readonly(),
343
+ z
344
+ .object({
345
+ type: z.literal("message.completed"),
346
+ streamId: streamIdSchema,
347
+ text: z.string(),
348
+ })
349
+ .strict()
350
+ .readonly(),
351
+ z
352
+ .object({
353
+ type: z.literal("reasoning.summary.delta"),
354
+ reasoningId: z.string().min(1),
355
+ delta: z.string(),
356
+ })
357
+ .strict()
358
+ .readonly(),
359
+ z
360
+ .object({
361
+ type: z.literal("tool.started"),
362
+ toolCallId: z.string().min(1),
363
+ agentName: z.string().min(1),
364
+ toolName: z.string().min(1),
365
+ input: jsonValueSchema,
366
+ })
367
+ .strict()
368
+ .readonly(),
369
+ z
370
+ .object({
371
+ type: z.literal("tool.output"),
372
+ toolCallId: z.string().min(1),
373
+ agentName: z.string().min(1),
374
+ toolName: z.string().min(1),
375
+ stream: z.enum(["stdout", "stderr"]),
376
+ delta: z.string(),
377
+ })
378
+ .strict()
379
+ .readonly(),
380
+ z
381
+ .object({
382
+ type: z.literal("tool.completed"),
383
+ toolCallId: z.string().min(1),
384
+ agentName: z.string().min(1),
385
+ toolName: z.string().min(1),
386
+ operationId: operationIdSchema.optional(),
387
+ outcome: toolOutcomeSchema.optional(),
388
+ display: jsonValueSchema,
389
+ })
390
+ .strict()
391
+ .readonly(),
392
+ z
393
+ .object({
394
+ type: z.literal("approval.required"),
395
+ approval: pendingApprovalSchema,
396
+ })
397
+ .strict()
398
+ .readonly(),
399
+ z
400
+ .object({ type: z.literal("turn.completed") })
401
+ .strict()
402
+ .readonly(),
403
+ z
404
+ .object({
405
+ type: z.literal("turn.cancelled"),
406
+ reason: z.string(),
407
+ message: z.string(),
408
+ })
409
+ .strict()
410
+ .readonly(),
411
+ z
412
+ .object({
413
+ type: z.literal("turn.failed"),
414
+ stage: z.enum(["bootstrap", "plan", "execute"]),
415
+ message: z.string(),
416
+ })
417
+ .strict()
418
+ .readonly(),
419
+ z
420
+ .object({ type: z.literal("capabilities.changed") })
421
+ .strict()
422
+ .readonly(),
423
+ ]);
424
+ export const runtimeEventEnvelopeSchema = z
425
+ .object({
426
+ protocolVersion: z.literal(RUNTIME_PROTOCOL_VERSION),
427
+ runtimeInstanceId: runtimeInstanceIdSchema,
428
+ sequence: z.number().int().nonnegative(),
429
+ timestamp: timestampSchema,
430
+ threadId: threadIdSchema,
431
+ turnId: turnIdSchema.optional(),
432
+ event: runtimeEventSchema,
433
+ })
434
+ .strict()
435
+ .readonly();
436
+ export const runtimeProtocolErrorDataSchema = z
437
+ .object({
438
+ rollCode: z.enum(Object.values(RUNTIME_ERROR_CODES)),
439
+ retryable: z.boolean(),
440
+ details: jsonValueSchema.optional(),
441
+ })
442
+ .strict()
443
+ .readonly();
444
+ export const runtimeMethodSchemas = {
445
+ [RUNTIME_METHODS.initialize]: {
446
+ params: initializeParamsSchema,
447
+ result: initializeResultSchema,
448
+ },
449
+ [RUNTIME_METHODS.threadList]: {
450
+ params: threadListParamsSchema,
451
+ result: threadListResultSchema,
452
+ },
453
+ [RUNTIME_METHODS.threadCreate]: {
454
+ params: threadCreateParamsSchema,
455
+ result: threadCreateResultSchema,
456
+ },
457
+ [RUNTIME_METHODS.threadOpen]: {
458
+ params: threadOpenParamsSchema,
459
+ result: threadOpenResultSchema,
460
+ },
461
+ [RUNTIME_METHODS.threadSnapshot]: {
462
+ params: threadSnapshotParamsSchema,
463
+ result: threadSnapshotResultSchema,
464
+ },
465
+ [RUNTIME_METHODS.threadRename]: {
466
+ params: threadRenameParamsSchema,
467
+ result: threadRenameResultSchema,
468
+ },
469
+ [RUNTIME_METHODS.threadDelete]: {
470
+ params: threadDeleteParamsSchema,
471
+ result: threadDeleteResultSchema,
472
+ },
473
+ [RUNTIME_METHODS.threadDetach]: {
474
+ params: threadDetachParamsSchema,
475
+ result: threadDetachResultSchema,
476
+ },
477
+ [RUNTIME_METHODS.threadCapabilities]: {
478
+ params: threadCapabilitiesParamsSchema,
479
+ result: threadCapabilitiesResultSchema,
480
+ },
481
+ [RUNTIME_METHODS.turnStart]: {
482
+ params: turnStartParamsSchema,
483
+ result: turnStartResultSchema,
484
+ },
485
+ [RUNTIME_METHODS.turnCancel]: {
486
+ params: turnCancelParamsSchema,
487
+ result: turnCancelResultSchema,
488
+ },
489
+ [RUNTIME_METHODS.approvalRespond]: {
490
+ params: approvalRespondParamsSchema,
491
+ result: approvalRespondResultSchema,
492
+ },
493
+ [RUNTIME_METHODS.operationGet]: {
494
+ params: operationGetParamsSchema,
495
+ result: operationGetResultSchema,
496
+ },
497
+ };
498
+ export function isRuntimeMethod(value) {
499
+ return Object.hasOwn(runtimeMethodSchemas, value);
500
+ }
501
+ export function parseRuntimeMethodParams(method, value) {
502
+ return runtimeMethodSchemas[method].params.parse(value);
503
+ }
504
+ export function parseRuntimeMethodResult(method, value) {
505
+ return runtimeMethodSchemas[method].result.parse(value);
506
+ }