@moxt-ai/mobius-protocol 0.0.3

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.
Files changed (49) hide show
  1. package/dist/bot.d.ts +398 -0
  2. package/dist/bot.d.ts.map +1 -0
  3. package/dist/bot.js +901 -0
  4. package/dist/bot.js.map +1 -0
  5. package/dist/codecs/messages.d.ts +35 -0
  6. package/dist/codecs/messages.d.ts.map +1 -0
  7. package/dist/codecs/messages.js +466 -0
  8. package/dist/codecs/messages.js.map +1 -0
  9. package/dist/control-messages.d.ts +662 -0
  10. package/dist/control-messages.d.ts.map +1 -0
  11. package/dist/control-messages.js +494 -0
  12. package/dist/control-messages.js.map +1 -0
  13. package/dist/conversation-title.d.ts +2 -0
  14. package/dist/conversation-title.d.ts.map +1 -0
  15. package/dist/conversation-title.js +28 -0
  16. package/dist/conversation-title.js.map +1 -0
  17. package/dist/index.d.ts +12 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +12 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/messages.d.ts +135 -0
  22. package/dist/messages.d.ts.map +1 -0
  23. package/dist/messages.js +25 -0
  24. package/dist/messages.js.map +1 -0
  25. package/dist/mobius-api.d.ts +15 -0
  26. package/dist/mobius-api.d.ts.map +1 -0
  27. package/dist/mobius-api.js +28 -0
  28. package/dist/mobius-api.js.map +1 -0
  29. package/dist/native-sessions.d.ts +262 -0
  30. package/dist/native-sessions.d.ts.map +1 -0
  31. package/dist/native-sessions.js +153 -0
  32. package/dist/native-sessions.js.map +1 -0
  33. package/dist/prompt-usage.d.ts +76 -0
  34. package/dist/prompt-usage.d.ts.map +1 -0
  35. package/dist/prompt-usage.js +88 -0
  36. package/dist/prompt-usage.js.map +1 -0
  37. package/dist/resource-model.d.ts +945 -0
  38. package/dist/resource-model.d.ts.map +1 -0
  39. package/dist/resource-model.js +716 -0
  40. package/dist/resource-model.js.map +1 -0
  41. package/dist/session-events.d.ts +97 -0
  42. package/dist/session-events.d.ts.map +1 -0
  43. package/dist/session-events.js +160 -0
  44. package/dist/session-events.js.map +1 -0
  45. package/dist/session-history.d.ts +69 -0
  46. package/dist/session-history.d.ts.map +1 -0
  47. package/dist/session-history.js +84 -0
  48. package/dist/session-history.js.map +1 -0
  49. package/package.json +36 -0
@@ -0,0 +1,716 @@
1
+ import { z } from "zod";
2
+ import { ProtocolMessageError } from "./codecs/messages.js";
3
+ import { elicitationFieldSchema, permissionOptionSchema } from "./control-messages.js";
4
+ export const MAX_LOCAL_AGENT_DIRECTORY_ENTRIES = 32;
5
+ const IDENTIFIER_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/;
6
+ const DECIMAL_SEQUENCE_PATTERN = /^(?:0|[1-9][0-9]*)$/;
7
+ const SHA256_PATTERN = /^[a-f0-9]{64}$/;
8
+ const identifierSchema = z.string().min(1).max(128).regex(IDENTIFIER_PATTERN);
9
+ const timestampSchema = z.string().datetime({ offset: true });
10
+ const decimalSequenceSchema = z.string().min(1).max(40).regex(DECIMAL_SEQUENCE_PATTERN);
11
+ const digestSchema = z.string().length(64).regex(SHA256_PATTERN);
12
+ const recordingPolicySchema = z
13
+ .strictObject({
14
+ contentLimitBytes: z.number().int().positive().safe(),
15
+ hotEventLimit: z.number().int().positive().safe(),
16
+ idempotencyRetentionMilliseconds: z.number().int().positive().safe(),
17
+ level: z.enum(["full", "summary"]),
18
+ retentionMilliseconds: z.number().int().positive().safe(),
19
+ })
20
+ .readonly();
21
+ const tenantLifecycleSchema = z.discriminatedUnion("kind", [
22
+ z.strictObject({ kind: z.literal("active") }).readonly(),
23
+ z
24
+ .strictObject({
25
+ kind: z.literal("suspended"),
26
+ suspendedAt: timestampSchema,
27
+ })
28
+ .readonly(),
29
+ z.strictObject({ deletedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
30
+ ]);
31
+ const tenantSchema = z
32
+ .strictObject({
33
+ lifecycle: tenantLifecycleSchema,
34
+ quotaPolicyRevision: z.number().int().nonnegative().safe(),
35
+ recordingPolicy: recordingPolicySchema,
36
+ tenantId: identifierSchema,
37
+ })
38
+ .readonly();
39
+ const projectLifecycleSchema = z.discriminatedUnion("kind", [
40
+ z.strictObject({ kind: z.literal("active") }).readonly(),
41
+ z.strictObject({ archivedAt: timestampSchema, kind: z.literal("archived") }).readonly(),
42
+ z.strictObject({ deletedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
43
+ ]);
44
+ const projectSchema = z
45
+ .strictObject({
46
+ displayName: z.string().min(1).max(120),
47
+ lifecycle: projectLifecycleSchema,
48
+ projectId: identifierSchema,
49
+ tenantId: identifierSchema,
50
+ })
51
+ .readonly();
52
+ const runtimeLifecycleSchema = z.discriminatedUnion("kind", [
53
+ z.strictObject({ kind: z.literal("registered") }).readonly(),
54
+ z.strictObject({ disabledAt: timestampSchema, kind: z.literal("disabled") }).readonly(),
55
+ z.strictObject({ deletedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
56
+ ]);
57
+ const runtimeCapabilitySchema = z
58
+ .strictObject({
59
+ name: identifierSchema,
60
+ revision: z.number().int().nonnegative().safe(),
61
+ })
62
+ .readonly();
63
+ const runtimeSchema = z
64
+ .strictObject({
65
+ capabilities: z.array(runtimeCapabilitySchema).max(100).readonly(),
66
+ displayName: z.string().min(1).max(120),
67
+ lifecycle: runtimeLifecycleSchema,
68
+ runtimeId: identifierSchema,
69
+ tenantId: identifierSchema,
70
+ })
71
+ .readonly();
72
+ const channelStateSchema = z.discriminatedUnion("kind", [
73
+ z
74
+ .strictObject({
75
+ connectionEpoch: z.number().int().nonnegative().safe(),
76
+ connectedAt: timestampSchema,
77
+ connectionId: identifierSchema,
78
+ kind: z.literal("online"),
79
+ })
80
+ .readonly(),
81
+ z
82
+ .strictObject({
83
+ connectionEpoch: z.number().int().nonnegative().safe(),
84
+ disconnectedAt: timestampSchema,
85
+ kind: z.literal("offline"),
86
+ })
87
+ .readonly(),
88
+ ]);
89
+ const channelSchema = z
90
+ .strictObject({
91
+ channelId: identifierSchema,
92
+ runtimeId: identifierSchema,
93
+ state: channelStateSchema,
94
+ })
95
+ .readonly();
96
+ const sessionLifecycleSchema = z.discriminatedUnion("kind", [
97
+ z.strictObject({ kind: z.literal("ready") }).readonly(),
98
+ z.strictObject({ kind: z.literal("deleting"), requestedAt: timestampSchema }).readonly(),
99
+ z.strictObject({ completedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
100
+ ]);
101
+ const sessionSchema = z
102
+ .strictObject({
103
+ createdAt: timestampSchema,
104
+ createdBy: identifierSchema,
105
+ defaultBindingId: identifierSchema,
106
+ lastCommittedSeq: decimalSequenceSchema,
107
+ lifecycle: sessionLifecycleSchema,
108
+ projectId: identifierSchema,
109
+ sessionId: identifierSchema,
110
+ tenantId: identifierSchema,
111
+ })
112
+ .readonly();
113
+ const sessionBindingSchema = z
114
+ .strictObject({
115
+ agentRef: identifierSchema,
116
+ bindingId: identifierSchema,
117
+ configRevision: z.number().int().nonnegative().safe(),
118
+ createdAt: timestampSchema,
119
+ runtimeId: identifierSchema,
120
+ sessionId: identifierSchema,
121
+ workspaceRef: identifierSchema,
122
+ })
123
+ .readonly();
124
+ const domainErrorSchema = z
125
+ .strictObject({
126
+ code: identifierSchema,
127
+ message: z.string().min(1).max(2_000),
128
+ retryable: z.boolean(),
129
+ })
130
+ .readonly();
131
+ const executionResultSchema = z
132
+ .strictObject({
133
+ kind: z.literal("agent_result"),
134
+ stopReason: z.string().min(1).max(128),
135
+ })
136
+ .readonly();
137
+ const reconciliationSchema = z
138
+ .strictObject({
139
+ detectedAt: timestampSchema,
140
+ reason: z.enum(["connection_lost", "dispatch_indeterminate", "runtime_restarted"]),
141
+ })
142
+ .readonly();
143
+ const turnStateSchema = z.discriminatedUnion("kind", [
144
+ z.strictObject({ kind: z.literal("queued") }).readonly(),
145
+ z.strictObject({ kind: z.literal("running"), startedAt: timestampSchema }).readonly(),
146
+ z
147
+ .strictObject({
148
+ completedAt: timestampSchema,
149
+ kind: z.literal("succeeded"),
150
+ result: executionResultSchema,
151
+ })
152
+ .readonly(),
153
+ z
154
+ .strictObject({
155
+ error: domainErrorSchema,
156
+ failedAt: timestampSchema,
157
+ kind: z.literal("failed"),
158
+ })
159
+ .readonly(),
160
+ z
161
+ .strictObject({
162
+ cancelledAt: timestampSchema,
163
+ kind: z.literal("cancelled"),
164
+ })
165
+ .readonly(),
166
+ z
167
+ .strictObject({
168
+ kind: z.literal("indeterminate"),
169
+ reconciliation: reconciliationSchema,
170
+ })
171
+ .readonly(),
172
+ ]);
173
+ const turnRequestSchema = z.discriminatedUnion("kind", [
174
+ z
175
+ .strictObject({
176
+ content: z.string().max(100_000),
177
+ kind: z.literal("prompt"),
178
+ })
179
+ .readonly(),
180
+ ]);
181
+ const turnSchema = z
182
+ .strictObject({
183
+ acceptedAt: timestampSchema,
184
+ bindingId: identifierSchema,
185
+ idempotencyKey: identifierSchema,
186
+ request: turnRequestSchema,
187
+ sessionId: identifierSchema,
188
+ state: turnStateSchema,
189
+ turnId: identifierSchema,
190
+ })
191
+ .readonly();
192
+ const attemptStateSchema = z.discriminatedUnion("kind", [
193
+ z
194
+ .strictObject({
195
+ dispatchedAt: timestampSchema,
196
+ kind: z.literal("dispatching"),
197
+ })
198
+ .readonly(),
199
+ z.strictObject({ kind: z.literal("running"), startedAt: timestampSchema }).readonly(),
200
+ z
201
+ .strictObject({
202
+ interactionIds: z.array(identifierSchema).min(1).max(100).readonly(),
203
+ kind: z.literal("waiting_input"),
204
+ startedAt: timestampSchema,
205
+ })
206
+ .readonly(),
207
+ z
208
+ .strictObject({
209
+ completedAt: timestampSchema,
210
+ kind: z.literal("succeeded"),
211
+ result: executionResultSchema,
212
+ startedAt: timestampSchema,
213
+ })
214
+ .readonly(),
215
+ z
216
+ .strictObject({
217
+ error: domainErrorSchema,
218
+ failedAt: timestampSchema,
219
+ kind: z.literal("failed"),
220
+ startedAt: timestampSchema,
221
+ })
222
+ .readonly(),
223
+ z
224
+ .strictObject({
225
+ cancelledAt: timestampSchema,
226
+ kind: z.literal("cancelled"),
227
+ startedAt: timestampSchema,
228
+ })
229
+ .readonly(),
230
+ z
231
+ .strictObject({
232
+ kind: z.literal("indeterminate"),
233
+ reconciliation: reconciliationSchema,
234
+ startedAt: timestampSchema,
235
+ })
236
+ .readonly(),
237
+ ]);
238
+ const attemptSchema = z
239
+ .strictObject({
240
+ attemptId: identifierSchema,
241
+ bindingId: identifierSchema,
242
+ sessionId: identifierSchema,
243
+ state: attemptStateSchema,
244
+ turnId: identifierSchema,
245
+ })
246
+ .readonly();
247
+ const permissionRequestSchema = z
248
+ .strictObject({
249
+ kind: z.literal("permission"),
250
+ options: z.array(permissionOptionSchema).min(1).max(20).readonly(),
251
+ title: z.string().min(1).max(500),
252
+ toolCallId: z.string().min(1).max(255),
253
+ toolKind: z.string().max(64),
254
+ })
255
+ .readonly();
256
+ const elicitationRequestSchema = z
257
+ .strictObject({
258
+ elicitationId: z.string().max(255),
259
+ fields: z.array(elicitationFieldSchema).max(100).readonly(),
260
+ kind: z.literal("elicitation"),
261
+ message: z.string().min(1).max(2_000),
262
+ mode: z.enum(["form", "url"]),
263
+ url: z.string().max(4_096),
264
+ })
265
+ .readonly();
266
+ const interactionRequestSchema = z.discriminatedUnion("kind", [permissionRequestSchema, elicitationRequestSchema]);
267
+ const interactionResponseSchema = z
268
+ .strictObject({
269
+ content: z.string().max(100_000),
270
+ kind: z.enum(["permission", "elicitation"]),
271
+ })
272
+ .readonly();
273
+ const interactionStateSchema = z.discriminatedUnion("kind", [
274
+ z.strictObject({ kind: z.literal("pending") }).readonly(),
275
+ z
276
+ .strictObject({
277
+ kind: z.literal("responded"),
278
+ respondedAt: timestampSchema,
279
+ responderId: identifierSchema,
280
+ response: interactionResponseSchema,
281
+ })
282
+ .readonly(),
283
+ z
284
+ .strictObject({
285
+ kind: z.literal("resolved"),
286
+ resolution: z.discriminatedUnion("kind", [
287
+ z.strictObject({ kind: z.literal("without_response") }).readonly(),
288
+ z
289
+ .strictObject({
290
+ kind: z.literal("response_applied"),
291
+ respondedAt: timestampSchema,
292
+ responderId: identifierSchema,
293
+ response: interactionResponseSchema,
294
+ })
295
+ .readonly(),
296
+ ]),
297
+ resolvedAt: timestampSchema,
298
+ })
299
+ .readonly(),
300
+ z.strictObject({ expiredAt: timestampSchema, kind: z.literal("expired") }).readonly(),
301
+ z
302
+ .strictObject({
303
+ cancelledAt: timestampSchema,
304
+ kind: z.literal("cancelled"),
305
+ })
306
+ .readonly(),
307
+ ]);
308
+ const interactionSchema = z
309
+ .strictObject({
310
+ attemptId: identifierSchema,
311
+ interactionId: identifierSchema,
312
+ request: interactionRequestSchema,
313
+ sessionId: identifierSchema,
314
+ state: interactionStateSchema,
315
+ turnId: identifierSchema,
316
+ })
317
+ .readonly();
318
+ const recordingCompletenessSchema = z.discriminatedUnion("kind", [
319
+ z.strictObject({ kind: z.literal("complete") }).readonly(),
320
+ z
321
+ .strictObject({
322
+ capturedBytes: z.number().int().nonnegative().safe(),
323
+ kind: z.literal("truncated"),
324
+ originalBytes: z.number().int().positive().safe(),
325
+ })
326
+ .readonly(),
327
+ z.strictObject({ kind: z.literal("redacted"), reason: identifierSchema }).readonly(),
328
+ z.strictObject({ kind: z.literal("uncaptured") }).readonly(),
329
+ ]);
330
+ const messageViewStateSchema = z.discriminatedUnion("kind", [
331
+ z.strictObject({ kind: z.literal("building") }).readonly(),
332
+ z
333
+ .strictObject({
334
+ completeness: recordingCompletenessSchema,
335
+ kind: z.literal("available"),
336
+ })
337
+ .readonly(),
338
+ z.strictObject({ deletedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
339
+ ]);
340
+ const messageViewCommon = {
341
+ contentArtifactIds: z.array(identifierSchema).max(100).readonly(),
342
+ lastAppliedSeq: decimalSequenceSchema,
343
+ messageId: identifierSchema,
344
+ preview: z.string().max(4_000),
345
+ sessionId: identifierSchema,
346
+ state: messageViewStateSchema,
347
+ };
348
+ const messageViewSchema = z.discriminatedUnion("role", [
349
+ z
350
+ .strictObject({
351
+ ...messageViewCommon,
352
+ role: z.literal("user"),
353
+ source: z.strictObject({ kind: z.literal("turn"), turnId: identifierSchema }).readonly(),
354
+ })
355
+ .readonly(),
356
+ z
357
+ .strictObject({
358
+ ...messageViewCommon,
359
+ role: z.literal("agent"),
360
+ source: z
361
+ .strictObject({
362
+ attemptId: identifierSchema,
363
+ kind: z.literal("attempt"),
364
+ turnId: identifierSchema,
365
+ })
366
+ .readonly(),
367
+ })
368
+ .readonly(),
369
+ z
370
+ .strictObject({
371
+ ...messageViewCommon,
372
+ role: z.literal("tool"),
373
+ source: z
374
+ .strictObject({
375
+ attemptId: identifierSchema,
376
+ kind: z.literal("tool_call"),
377
+ toolCallId: identifierSchema,
378
+ turnId: identifierSchema,
379
+ })
380
+ .readonly(),
381
+ })
382
+ .readonly(),
383
+ z
384
+ .strictObject({
385
+ ...messageViewCommon,
386
+ role: z.literal("system"),
387
+ source: z.strictObject({ kind: z.literal("system") }).readonly(),
388
+ })
389
+ .readonly(),
390
+ ]);
391
+ const toolCallDetailSchema = z.discriminatedUnion("kind", [
392
+ z
393
+ .strictObject({
394
+ artifactId: identifierSchema,
395
+ kind: z.literal("available"),
396
+ })
397
+ .readonly(),
398
+ z
399
+ .strictObject({
400
+ capturedBytes: z.number().int().nonnegative().safe(),
401
+ kind: z.literal("truncated"),
402
+ originalBytes: z.number().int().positive().safe(),
403
+ })
404
+ .readonly(),
405
+ z.strictObject({ kind: z.literal("uncaptured") }).readonly(),
406
+ z.strictObject({ deletedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
407
+ ]);
408
+ const toolCallViewStateSchema = z.discriminatedUnion("kind", [
409
+ z.strictObject({ kind: z.literal("pending") }).readonly(),
410
+ z.strictObject({ kind: z.literal("running"), startedAt: timestampSchema }).readonly(),
411
+ z
412
+ .strictObject({
413
+ completedAt: timestampSchema,
414
+ kind: z.literal("succeeded"),
415
+ })
416
+ .readonly(),
417
+ z
418
+ .strictObject({
419
+ error: domainErrorSchema,
420
+ failedAt: timestampSchema,
421
+ kind: z.literal("failed"),
422
+ })
423
+ .readonly(),
424
+ z
425
+ .strictObject({
426
+ cancelledAt: timestampSchema,
427
+ kind: z.literal("cancelled"),
428
+ })
429
+ .readonly(),
430
+ ]);
431
+ const toolCallViewSchema = z
432
+ .strictObject({
433
+ attemptId: identifierSchema,
434
+ detail: toolCallDetailSchema,
435
+ inputPreview: z.string().max(4_000),
436
+ lastAppliedSeq: decimalSequenceSchema,
437
+ name: z.string().max(128),
438
+ outputPreview: z.string().max(4_000),
439
+ state: toolCallViewStateSchema,
440
+ title: z.string().min(1).max(500),
441
+ toolCallId: identifierSchema,
442
+ toolKind: identifierSchema,
443
+ })
444
+ .readonly();
445
+ const sessionListEntrySchema = z
446
+ .strictObject({
447
+ lastAppliedSeq: decimalSequenceSchema,
448
+ preview: z.string().max(4_000),
449
+ projectId: identifierSchema,
450
+ projectedAttemptState: z.enum(["idle", "dispatching", "running", "waiting_input", "indeterminate"]),
451
+ sessionId: identifierSchema,
452
+ tenantId: identifierSchema,
453
+ title: z.string().max(500),
454
+ updatedAt: timestampSchema,
455
+ })
456
+ .readonly();
457
+ const artifactStateSchema = z.discriminatedUnion("kind", [
458
+ z
459
+ .strictObject({
460
+ expectedBytes: z.number().int().nonnegative().safe(),
461
+ expiresAt: timestampSchema,
462
+ kind: z.literal("uploading"),
463
+ uploadId: identifierSchema,
464
+ })
465
+ .readonly(),
466
+ z
467
+ .strictObject({
468
+ availableAt: timestampSchema,
469
+ byteLength: z.number().int().nonnegative().safe(),
470
+ kind: z.literal("available"),
471
+ sha256: digestSchema,
472
+ })
473
+ .readonly(),
474
+ z.strictObject({ kind: z.literal("deleting"), requestedAt: timestampSchema }).readonly(),
475
+ z.strictObject({ completedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
476
+ z
477
+ .strictObject({
478
+ error: domainErrorSchema,
479
+ failedAt: timestampSchema,
480
+ kind: z.literal("failed"),
481
+ })
482
+ .readonly(),
483
+ z.strictObject({ expiredAt: timestampSchema, kind: z.literal("expired") }).readonly(),
484
+ ]);
485
+ const artifactSchema = z
486
+ .strictObject({
487
+ artifactId: identifierSchema,
488
+ completeness: recordingCompletenessSchema,
489
+ contentKind: identifierSchema,
490
+ mediaType: z.string().min(1).max(128),
491
+ projectId: identifierSchema,
492
+ provenance: z.discriminatedUnion("kind", [
493
+ z.strictObject({ kind: z.literal("user_upload") }).readonly(),
494
+ z
495
+ .strictObject({
496
+ attemptId: identifierSchema,
497
+ kind: z.literal("tool_call"),
498
+ toolCallId: identifierSchema,
499
+ turnId: identifierSchema,
500
+ })
501
+ .readonly(),
502
+ z
503
+ .strictObject({
504
+ attemptId: identifierSchema,
505
+ kind: z.literal("agent_generated"),
506
+ turnId: identifierSchema,
507
+ })
508
+ .readonly(),
509
+ ]),
510
+ sessionId: identifierSchema,
511
+ state: artifactStateSchema,
512
+ tenantId: identifierSchema,
513
+ })
514
+ .readonly();
515
+ const archiveSegmentStateSchema = z.discriminatedUnion("kind", [
516
+ z.strictObject({ kind: z.literal("pending"), queuedAt: timestampSchema }).readonly(),
517
+ z
518
+ .strictObject({
519
+ byteLength: z.number().int().nonnegative().safe(),
520
+ kind: z.literal("uploaded"),
521
+ sha256: digestSchema,
522
+ uploadedAt: timestampSchema,
523
+ })
524
+ .readonly(),
525
+ z
526
+ .strictObject({
527
+ byteLength: z.number().int().nonnegative().safe(),
528
+ kind: z.literal("published"),
529
+ publishedAt: timestampSchema,
530
+ sha256: digestSchema,
531
+ })
532
+ .readonly(),
533
+ z.strictObject({ kind: z.literal("deleting"), requestedAt: timestampSchema }).readonly(),
534
+ z.strictObject({ completedAt: timestampSchema, kind: z.literal("deleted") }).readonly(),
535
+ ]);
536
+ const archiveSegmentSchema = z
537
+ .strictObject({
538
+ compression: z.literal("gzip"),
539
+ eventCount: z.number().int().positive().safe(),
540
+ firstSeq: decimalSequenceSchema,
541
+ lastSeq: decimalSequenceSchema,
542
+ segmentId: identifierSchema,
543
+ sessionId: identifierSchema,
544
+ state: archiveSegmentStateSchema,
545
+ })
546
+ .readonly();
547
+ const pendingWorkStateSchema = z.discriminatedUnion("kind", [
548
+ z.strictObject({ kind: z.literal("pending"), queuedAt: timestampSchema }).readonly(),
549
+ z
550
+ .strictObject({
551
+ attempt: z.number().int().positive().safe(),
552
+ kind: z.literal("running"),
553
+ startedAt: timestampSchema,
554
+ })
555
+ .readonly(),
556
+ z
557
+ .strictObject({
558
+ completedAt: timestampSchema,
559
+ kind: z.literal("completed"),
560
+ })
561
+ .readonly(),
562
+ z
563
+ .strictObject({
564
+ error: domainErrorSchema,
565
+ kind: z.literal("retryable"),
566
+ nextAttemptAt: timestampSchema,
567
+ })
568
+ .readonly(),
569
+ z
570
+ .strictObject({
571
+ error: domainErrorSchema,
572
+ failedAt: timestampSchema,
573
+ kind: z.literal("failed"),
574
+ })
575
+ .readonly(),
576
+ ]);
577
+ const pendingWorkSchema = z
578
+ .strictObject({
579
+ kind: z.enum(["archive", "delete", "dispatch", "interaction_response", "projection"]),
580
+ projectId: identifierSchema,
581
+ sessionId: identifierSchema,
582
+ state: pendingWorkStateSchema,
583
+ workId: identifierSchema,
584
+ })
585
+ .readonly();
586
+ const commandReceiptSchema = z
587
+ .strictObject({
588
+ acceptedAt: timestampSchema,
589
+ commandKind: identifierSchema,
590
+ expiresAt: timestampSchema,
591
+ idempotencyKey: identifierSchema,
592
+ projectId: identifierSchema,
593
+ requestHash: digestSchema,
594
+ resultId: identifierSchema,
595
+ sessionId: identifierSchema,
596
+ })
597
+ .readonly();
598
+ export class ResourceTransitionError extends Error {
599
+ code = "invalid_resource_transition";
600
+ constructor(resource, from, to) {
601
+ super(`Invalid ${resource} transition from ${from} to ${to}`);
602
+ this.name = "ResourceTransitionError";
603
+ }
604
+ }
605
+ function canTransitionSession(from, to) {
606
+ switch (from) {
607
+ case "ready":
608
+ return to === "deleting";
609
+ case "deleting":
610
+ return to === "deleted";
611
+ case "deleted":
612
+ return false;
613
+ }
614
+ }
615
+ function canTransitionTurn(from, to) {
616
+ switch (from) {
617
+ case "queued":
618
+ return to === "running" || to === "failed" || to === "cancelled";
619
+ case "running":
620
+ return to === "succeeded" || to === "failed" || to === "cancelled" || to === "indeterminate";
621
+ case "indeterminate":
622
+ return to === "running" || to === "succeeded" || to === "failed" || to === "cancelled";
623
+ case "succeeded":
624
+ case "failed":
625
+ case "cancelled":
626
+ return false;
627
+ }
628
+ }
629
+ function canTransitionAttempt(from, to) {
630
+ switch (from) {
631
+ case "dispatching":
632
+ return to === "running" || to === "failed" || to === "cancelled" || to === "indeterminate";
633
+ case "running":
634
+ return (to === "waiting_input" ||
635
+ to === "succeeded" ||
636
+ to === "failed" ||
637
+ to === "cancelled" ||
638
+ to === "indeterminate");
639
+ case "waiting_input":
640
+ return (to === "running" ||
641
+ to === "succeeded" ||
642
+ to === "failed" ||
643
+ to === "cancelled" ||
644
+ to === "indeterminate");
645
+ case "indeterminate":
646
+ return to === "running" || to === "succeeded" || to === "failed" || to === "cancelled";
647
+ case "succeeded":
648
+ case "failed":
649
+ case "cancelled":
650
+ return false;
651
+ }
652
+ }
653
+ function canTransitionInteraction(from, to) {
654
+ switch (from) {
655
+ case "pending":
656
+ return to === "responded" || to === "resolved" || to === "expired" || to === "cancelled";
657
+ case "responded":
658
+ return to === "resolved" || to === "expired" || to === "cancelled";
659
+ case "resolved":
660
+ case "expired":
661
+ case "cancelled":
662
+ return false;
663
+ }
664
+ }
665
+ export function transitionSession(session, lifecycle) {
666
+ if (!canTransitionSession(session.lifecycle.kind, lifecycle.kind)) {
667
+ throw new ResourceTransitionError("Session", session.lifecycle.kind, lifecycle.kind);
668
+ }
669
+ return { ...session, lifecycle };
670
+ }
671
+ export function transitionTurn(turn, state) {
672
+ if (!canTransitionTurn(turn.state.kind, state.kind)) {
673
+ throw new ResourceTransitionError("Turn", turn.state.kind, state.kind);
674
+ }
675
+ return { ...turn, state };
676
+ }
677
+ export function transitionAttempt(attempt, state) {
678
+ if (!canTransitionAttempt(attempt.state.kind, state.kind)) {
679
+ throw new ResourceTransitionError("Attempt", attempt.state.kind, state.kind);
680
+ }
681
+ return { ...attempt, state };
682
+ }
683
+ export function transitionInteraction(interaction, state) {
684
+ if (!canTransitionInteraction(interaction.state.kind, state.kind)) {
685
+ throw new ResourceTransitionError("Interaction", interaction.state.kind, state.kind);
686
+ }
687
+ return { ...interaction, state };
688
+ }
689
+ function decodeResource(value, schema) {
690
+ const decoded = schema.safeParse(value);
691
+ if (!decoded.success) {
692
+ throw new ProtocolMessageError("Resource does not match its schema", {
693
+ cause: decoded.error,
694
+ });
695
+ }
696
+ return decoded.data;
697
+ }
698
+ export const decodeTenant = (value) => decodeResource(value, tenantSchema);
699
+ export const decodeProject = (value) => decodeResource(value, projectSchema);
700
+ export const decodeRuntime = (value) => decodeResource(value, runtimeSchema);
701
+ export const decodeChannel = (value) => decodeResource(value, channelSchema);
702
+ export const decodeSession = (value) => decodeResource(value, sessionSchema);
703
+ export const decodeSessionBinding = (value) => decodeResource(value, sessionBindingSchema);
704
+ export const decodeTurn = (value) => decodeResource(value, turnSchema);
705
+ export const decodeAttempt = (value) => decodeResource(value, attemptSchema);
706
+ export const decodeInteraction = (value) => decodeResource(value, interactionSchema);
707
+ export const decodeInteractionRequest = (value) => decodeResource(value, interactionRequestSchema);
708
+ export const decodeInteractionResponse = (value) => decodeResource(value, interactionResponseSchema);
709
+ export const decodeMessageView = (value) => decodeResource(value, messageViewSchema);
710
+ export const decodeToolCallView = (value) => decodeResource(value, toolCallViewSchema);
711
+ export const decodeSessionListEntry = (value) => decodeResource(value, sessionListEntrySchema);
712
+ export const decodeArtifact = (value) => decodeResource(value, artifactSchema);
713
+ export const decodeArchiveSegment = (value) => decodeResource(value, archiveSegmentSchema);
714
+ export const decodePendingWork = (value) => decodeResource(value, pendingWorkSchema);
715
+ export const decodeCommandReceipt = (value) => decodeResource(value, commandReceiptSchema);
716
+ //# sourceMappingURL=resource-model.js.map