@rivetkit/workflow-engine 2.1.0-rc.1

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.
@@ -0,0 +1,781 @@
1
+ // @generated - post-processed by compile-bare.ts
2
+ import * as bare from "@rivetkit/bare-ts"
3
+
4
+ const config = /* @__PURE__ */ bare.Config({})
5
+
6
+ export type u32 = number
7
+ export type u64 = bigint
8
+
9
+ export type Cbor = ArrayBuffer
10
+
11
+ export function readCbor(bc: bare.ByteCursor): Cbor {
12
+ return bare.readData(bc)
13
+ }
14
+
15
+ export function writeCbor(bc: bare.ByteCursor, x: Cbor): void {
16
+ bare.writeData(bc, x)
17
+ }
18
+
19
+ export type NameIndex = u32
20
+
21
+ export function readNameIndex(bc: bare.ByteCursor): NameIndex {
22
+ return bare.readU32(bc)
23
+ }
24
+
25
+ export function writeNameIndex(bc: bare.ByteCursor, x: NameIndex): void {
26
+ bare.writeU32(bc, x)
27
+ }
28
+
29
+ export type LoopIterationMarker = {
30
+ readonly loop: NameIndex,
31
+ readonly iteration: u32,
32
+ }
33
+
34
+ export function readLoopIterationMarker(bc: bare.ByteCursor): LoopIterationMarker {
35
+ return {
36
+ loop: readNameIndex(bc),
37
+ iteration: bare.readU32(bc),
38
+ }
39
+ }
40
+
41
+ export function writeLoopIterationMarker(bc: bare.ByteCursor, x: LoopIterationMarker): void {
42
+ writeNameIndex(bc, x.loop)
43
+ bare.writeU32(bc, x.iteration)
44
+ }
45
+
46
+ export type PathSegment =
47
+ | { readonly tag: "NameIndex", readonly val: NameIndex }
48
+ | { readonly tag: "LoopIterationMarker", readonly val: LoopIterationMarker }
49
+
50
+ export function readPathSegment(bc: bare.ByteCursor): PathSegment {
51
+ const offset = bc.offset
52
+ const tag = bare.readU8(bc)
53
+ switch (tag) {
54
+ case 0:
55
+ return { tag: "NameIndex", val: readNameIndex(bc) }
56
+ case 1:
57
+ return { tag: "LoopIterationMarker", val: readLoopIterationMarker(bc) }
58
+ default: {
59
+ bc.offset = offset
60
+ throw new bare.BareError(offset, "invalid tag")
61
+ }
62
+ }
63
+ }
64
+
65
+ export function writePathSegment(bc: bare.ByteCursor, x: PathSegment): void {
66
+ switch (x.tag) {
67
+ case "NameIndex": {
68
+ bare.writeU8(bc, 0)
69
+ writeNameIndex(bc, x.val)
70
+ break
71
+ }
72
+ case "LoopIterationMarker": {
73
+ bare.writeU8(bc, 1)
74
+ writeLoopIterationMarker(bc, x.val)
75
+ break
76
+ }
77
+ }
78
+ }
79
+
80
+ export type Location = readonly PathSegment[]
81
+
82
+ export function readLocation(bc: bare.ByteCursor): Location {
83
+ const len = bare.readUintSafe(bc)
84
+ if (len === 0) { return [] }
85
+ const result = [readPathSegment(bc)]
86
+ for (let i = 1; i < len; i++) {
87
+ result[i] = readPathSegment(bc)
88
+ }
89
+ return result
90
+ }
91
+
92
+ export function writeLocation(bc: bare.ByteCursor, x: Location): void {
93
+ bare.writeUintSafe(bc, x.length)
94
+ for (let i = 0; i < x.length; i++) {
95
+ writePathSegment(bc, x[i])
96
+ }
97
+ }
98
+
99
+ export enum EntryStatus {
100
+ PENDING = "PENDING",
101
+ RUNNING = "RUNNING",
102
+ COMPLETED = "COMPLETED",
103
+ FAILED = "FAILED",
104
+ EXHAUSTED = "EXHAUSTED",
105
+ }
106
+
107
+ export function readEntryStatus(bc: bare.ByteCursor): EntryStatus {
108
+ const offset = bc.offset
109
+ const tag = bare.readU8(bc)
110
+ switch (tag) {
111
+ case 0:
112
+ return EntryStatus.PENDING
113
+ case 1:
114
+ return EntryStatus.RUNNING
115
+ case 2:
116
+ return EntryStatus.COMPLETED
117
+ case 3:
118
+ return EntryStatus.FAILED
119
+ case 4:
120
+ return EntryStatus.EXHAUSTED
121
+ default: {
122
+ bc.offset = offset
123
+ throw new bare.BareError(offset, "invalid tag")
124
+ }
125
+ }
126
+ }
127
+
128
+ export function writeEntryStatus(bc: bare.ByteCursor, x: EntryStatus): void {
129
+ switch (x) {
130
+ case EntryStatus.PENDING: {
131
+ bare.writeU8(bc, 0)
132
+ break
133
+ }
134
+ case EntryStatus.RUNNING: {
135
+ bare.writeU8(bc, 1)
136
+ break
137
+ }
138
+ case EntryStatus.COMPLETED: {
139
+ bare.writeU8(bc, 2)
140
+ break
141
+ }
142
+ case EntryStatus.FAILED: {
143
+ bare.writeU8(bc, 3)
144
+ break
145
+ }
146
+ case EntryStatus.EXHAUSTED: {
147
+ bare.writeU8(bc, 4)
148
+ break
149
+ }
150
+ }
151
+ }
152
+
153
+ export enum SleepState {
154
+ PENDING = "PENDING",
155
+ COMPLETED = "COMPLETED",
156
+ INTERRUPTED = "INTERRUPTED",
157
+ }
158
+
159
+ export function readSleepState(bc: bare.ByteCursor): SleepState {
160
+ const offset = bc.offset
161
+ const tag = bare.readU8(bc)
162
+ switch (tag) {
163
+ case 0:
164
+ return SleepState.PENDING
165
+ case 1:
166
+ return SleepState.COMPLETED
167
+ case 2:
168
+ return SleepState.INTERRUPTED
169
+ default: {
170
+ bc.offset = offset
171
+ throw new bare.BareError(offset, "invalid tag")
172
+ }
173
+ }
174
+ }
175
+
176
+ export function writeSleepState(bc: bare.ByteCursor, x: SleepState): void {
177
+ switch (x) {
178
+ case SleepState.PENDING: {
179
+ bare.writeU8(bc, 0)
180
+ break
181
+ }
182
+ case SleepState.COMPLETED: {
183
+ bare.writeU8(bc, 1)
184
+ break
185
+ }
186
+ case SleepState.INTERRUPTED: {
187
+ bare.writeU8(bc, 2)
188
+ break
189
+ }
190
+ }
191
+ }
192
+
193
+ export enum BranchStatusType {
194
+ PENDING = "PENDING",
195
+ RUNNING = "RUNNING",
196
+ COMPLETED = "COMPLETED",
197
+ FAILED = "FAILED",
198
+ CANCELLED = "CANCELLED",
199
+ }
200
+
201
+ export function readBranchStatusType(bc: bare.ByteCursor): BranchStatusType {
202
+ const offset = bc.offset
203
+ const tag = bare.readU8(bc)
204
+ switch (tag) {
205
+ case 0:
206
+ return BranchStatusType.PENDING
207
+ case 1:
208
+ return BranchStatusType.RUNNING
209
+ case 2:
210
+ return BranchStatusType.COMPLETED
211
+ case 3:
212
+ return BranchStatusType.FAILED
213
+ case 4:
214
+ return BranchStatusType.CANCELLED
215
+ default: {
216
+ bc.offset = offset
217
+ throw new bare.BareError(offset, "invalid tag")
218
+ }
219
+ }
220
+ }
221
+
222
+ export function writeBranchStatusType(bc: bare.ByteCursor, x: BranchStatusType): void {
223
+ switch (x) {
224
+ case BranchStatusType.PENDING: {
225
+ bare.writeU8(bc, 0)
226
+ break
227
+ }
228
+ case BranchStatusType.RUNNING: {
229
+ bare.writeU8(bc, 1)
230
+ break
231
+ }
232
+ case BranchStatusType.COMPLETED: {
233
+ bare.writeU8(bc, 2)
234
+ break
235
+ }
236
+ case BranchStatusType.FAILED: {
237
+ bare.writeU8(bc, 3)
238
+ break
239
+ }
240
+ case BranchStatusType.CANCELLED: {
241
+ bare.writeU8(bc, 4)
242
+ break
243
+ }
244
+ }
245
+ }
246
+
247
+ function read0(bc: bare.ByteCursor): Cbor | null {
248
+ return bare.readBool(bc)
249
+ ? readCbor(bc)
250
+ : null
251
+ }
252
+
253
+ function write0(bc: bare.ByteCursor, x: Cbor | null): void {
254
+ bare.writeBool(bc, x !== null)
255
+ if (x !== null) {
256
+ writeCbor(bc, x)
257
+ }
258
+ }
259
+
260
+ function read1(bc: bare.ByteCursor): string | null {
261
+ return bare.readBool(bc)
262
+ ? bare.readString(bc)
263
+ : null
264
+ }
265
+
266
+ function write1(bc: bare.ByteCursor, x: string | null): void {
267
+ bare.writeBool(bc, x !== null)
268
+ if (x !== null) {
269
+ bare.writeString(bc, x)
270
+ }
271
+ }
272
+
273
+ export type StepEntry = {
274
+ readonly output: Cbor | null,
275
+ readonly error: string | null,
276
+ }
277
+
278
+ export function readStepEntry(bc: bare.ByteCursor): StepEntry {
279
+ return {
280
+ output: read0(bc),
281
+ error: read1(bc),
282
+ }
283
+ }
284
+
285
+ export function writeStepEntry(bc: bare.ByteCursor, x: StepEntry): void {
286
+ write0(bc, x.output)
287
+ write1(bc, x.error)
288
+ }
289
+
290
+ export type LoopEntry = {
291
+ readonly state: Cbor,
292
+ readonly iteration: u32,
293
+ readonly output: Cbor | null,
294
+ }
295
+
296
+ export function readLoopEntry(bc: bare.ByteCursor): LoopEntry {
297
+ return {
298
+ state: readCbor(bc),
299
+ iteration: bare.readU32(bc),
300
+ output: read0(bc),
301
+ }
302
+ }
303
+
304
+ export function writeLoopEntry(bc: bare.ByteCursor, x: LoopEntry): void {
305
+ writeCbor(bc, x.state)
306
+ bare.writeU32(bc, x.iteration)
307
+ write0(bc, x.output)
308
+ }
309
+
310
+ export type SleepEntry = {
311
+ readonly deadline: u64,
312
+ readonly state: SleepState,
313
+ }
314
+
315
+ export function readSleepEntry(bc: bare.ByteCursor): SleepEntry {
316
+ return {
317
+ deadline: bare.readU64(bc),
318
+ state: readSleepState(bc),
319
+ }
320
+ }
321
+
322
+ export function writeSleepEntry(bc: bare.ByteCursor, x: SleepEntry): void {
323
+ bare.writeU64(bc, x.deadline)
324
+ writeSleepState(bc, x.state)
325
+ }
326
+
327
+ export type MessageEntry = {
328
+ readonly name: string,
329
+ readonly messageData: Cbor,
330
+ }
331
+
332
+ export function readMessageEntry(bc: bare.ByteCursor): MessageEntry {
333
+ return {
334
+ name: bare.readString(bc),
335
+ messageData: readCbor(bc),
336
+ }
337
+ }
338
+
339
+ export function writeMessageEntry(bc: bare.ByteCursor, x: MessageEntry): void {
340
+ bare.writeString(bc, x.name)
341
+ writeCbor(bc, x.messageData)
342
+ }
343
+
344
+ export type RollbackCheckpointEntry = {
345
+ readonly name: string,
346
+ }
347
+
348
+ export function readRollbackCheckpointEntry(bc: bare.ByteCursor): RollbackCheckpointEntry {
349
+ return {
350
+ name: bare.readString(bc),
351
+ }
352
+ }
353
+
354
+ export function writeRollbackCheckpointEntry(bc: bare.ByteCursor, x: RollbackCheckpointEntry): void {
355
+ bare.writeString(bc, x.name)
356
+ }
357
+
358
+ export type BranchStatus = {
359
+ readonly status: BranchStatusType,
360
+ readonly output: Cbor | null,
361
+ readonly error: string | null,
362
+ }
363
+
364
+ export function readBranchStatus(bc: bare.ByteCursor): BranchStatus {
365
+ return {
366
+ status: readBranchStatusType(bc),
367
+ output: read0(bc),
368
+ error: read1(bc),
369
+ }
370
+ }
371
+
372
+ export function writeBranchStatus(bc: bare.ByteCursor, x: BranchStatus): void {
373
+ writeBranchStatusType(bc, x.status)
374
+ write0(bc, x.output)
375
+ write1(bc, x.error)
376
+ }
377
+
378
+ function read2(bc: bare.ByteCursor): ReadonlyMap<string, BranchStatus> {
379
+ const len = bare.readUintSafe(bc)
380
+ const result = new Map<string, BranchStatus>()
381
+ for (let i = 0; i < len; i++) {
382
+ const offset = bc.offset
383
+ const key = bare.readString(bc)
384
+ if (result.has(key)) {
385
+ bc.offset = offset
386
+ throw new bare.BareError(offset, "duplicated key")
387
+ }
388
+ result.set(key, readBranchStatus(bc))
389
+ }
390
+ return result
391
+ }
392
+
393
+ function write2(bc: bare.ByteCursor, x: ReadonlyMap<string, BranchStatus>): void {
394
+ bare.writeUintSafe(bc, x.size)
395
+ for(const kv of x) {
396
+ bare.writeString(bc, kv[0])
397
+ writeBranchStatus(bc, kv[1])
398
+ }
399
+ }
400
+
401
+ export type JoinEntry = {
402
+ readonly branches: ReadonlyMap<string, BranchStatus>,
403
+ }
404
+
405
+ export function readJoinEntry(bc: bare.ByteCursor): JoinEntry {
406
+ return {
407
+ branches: read2(bc),
408
+ }
409
+ }
410
+
411
+ export function writeJoinEntry(bc: bare.ByteCursor, x: JoinEntry): void {
412
+ write2(bc, x.branches)
413
+ }
414
+
415
+ export type RaceEntry = {
416
+ readonly winner: string | null,
417
+ readonly branches: ReadonlyMap<string, BranchStatus>,
418
+ }
419
+
420
+ export function readRaceEntry(bc: bare.ByteCursor): RaceEntry {
421
+ return {
422
+ winner: read1(bc),
423
+ branches: read2(bc),
424
+ }
425
+ }
426
+
427
+ export function writeRaceEntry(bc: bare.ByteCursor, x: RaceEntry): void {
428
+ write1(bc, x.winner)
429
+ write2(bc, x.branches)
430
+ }
431
+
432
+ export type RemovedEntry = {
433
+ readonly originalType: string,
434
+ readonly originalName: string | null,
435
+ }
436
+
437
+ export function readRemovedEntry(bc: bare.ByteCursor): RemovedEntry {
438
+ return {
439
+ originalType: bare.readString(bc),
440
+ originalName: read1(bc),
441
+ }
442
+ }
443
+
444
+ export function writeRemovedEntry(bc: bare.ByteCursor, x: RemovedEntry): void {
445
+ bare.writeString(bc, x.originalType)
446
+ write1(bc, x.originalName)
447
+ }
448
+
449
+ export type EntryKind =
450
+ | { readonly tag: "StepEntry", readonly val: StepEntry }
451
+ | { readonly tag: "LoopEntry", readonly val: LoopEntry }
452
+ | { readonly tag: "SleepEntry", readonly val: SleepEntry }
453
+ | { readonly tag: "MessageEntry", readonly val: MessageEntry }
454
+ | { readonly tag: "RollbackCheckpointEntry", readonly val: RollbackCheckpointEntry }
455
+ | { readonly tag: "JoinEntry", readonly val: JoinEntry }
456
+ | { readonly tag: "RaceEntry", readonly val: RaceEntry }
457
+ | { readonly tag: "RemovedEntry", readonly val: RemovedEntry }
458
+
459
+ export function readEntryKind(bc: bare.ByteCursor): EntryKind {
460
+ const offset = bc.offset
461
+ const tag = bare.readU8(bc)
462
+ switch (tag) {
463
+ case 0:
464
+ return { tag: "StepEntry", val: readStepEntry(bc) }
465
+ case 1:
466
+ return { tag: "LoopEntry", val: readLoopEntry(bc) }
467
+ case 2:
468
+ return { tag: "SleepEntry", val: readSleepEntry(bc) }
469
+ case 3:
470
+ return { tag: "MessageEntry", val: readMessageEntry(bc) }
471
+ case 4:
472
+ return { tag: "RollbackCheckpointEntry", val: readRollbackCheckpointEntry(bc) }
473
+ case 5:
474
+ return { tag: "JoinEntry", val: readJoinEntry(bc) }
475
+ case 6:
476
+ return { tag: "RaceEntry", val: readRaceEntry(bc) }
477
+ case 7:
478
+ return { tag: "RemovedEntry", val: readRemovedEntry(bc) }
479
+ default: {
480
+ bc.offset = offset
481
+ throw new bare.BareError(offset, "invalid tag")
482
+ }
483
+ }
484
+ }
485
+
486
+ export function writeEntryKind(bc: bare.ByteCursor, x: EntryKind): void {
487
+ switch (x.tag) {
488
+ case "StepEntry": {
489
+ bare.writeU8(bc, 0)
490
+ writeStepEntry(bc, x.val)
491
+ break
492
+ }
493
+ case "LoopEntry": {
494
+ bare.writeU8(bc, 1)
495
+ writeLoopEntry(bc, x.val)
496
+ break
497
+ }
498
+ case "SleepEntry": {
499
+ bare.writeU8(bc, 2)
500
+ writeSleepEntry(bc, x.val)
501
+ break
502
+ }
503
+ case "MessageEntry": {
504
+ bare.writeU8(bc, 3)
505
+ writeMessageEntry(bc, x.val)
506
+ break
507
+ }
508
+ case "RollbackCheckpointEntry": {
509
+ bare.writeU8(bc, 4)
510
+ writeRollbackCheckpointEntry(bc, x.val)
511
+ break
512
+ }
513
+ case "JoinEntry": {
514
+ bare.writeU8(bc, 5)
515
+ writeJoinEntry(bc, x.val)
516
+ break
517
+ }
518
+ case "RaceEntry": {
519
+ bare.writeU8(bc, 6)
520
+ writeRaceEntry(bc, x.val)
521
+ break
522
+ }
523
+ case "RemovedEntry": {
524
+ bare.writeU8(bc, 7)
525
+ writeRemovedEntry(bc, x.val)
526
+ break
527
+ }
528
+ }
529
+ }
530
+
531
+ export type Entry = {
532
+ readonly id: string,
533
+ readonly location: Location,
534
+ readonly kind: EntryKind,
535
+ }
536
+
537
+ export function readEntry(bc: bare.ByteCursor): Entry {
538
+ return {
539
+ id: bare.readString(bc),
540
+ location: readLocation(bc),
541
+ kind: readEntryKind(bc),
542
+ }
543
+ }
544
+
545
+ export function writeEntry(bc: bare.ByteCursor, x: Entry): void {
546
+ bare.writeString(bc, x.id)
547
+ writeLocation(bc, x.location)
548
+ writeEntryKind(bc, x.kind)
549
+ }
550
+
551
+ export function encodeEntry(x: Entry): Uint8Array {
552
+ const bc = new bare.ByteCursor(
553
+ new Uint8Array(config.initialBufferLength),
554
+ config
555
+ )
556
+ writeEntry(bc, x)
557
+ return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
558
+ }
559
+
560
+ export function decodeEntry(bytes: Uint8Array): Entry {
561
+ const bc = new bare.ByteCursor(bytes, config)
562
+ const result = readEntry(bc)
563
+ if (bc.offset < bc.view.byteLength) {
564
+ throw new bare.BareError(bc.offset, "remaining bytes")
565
+ }
566
+ return result
567
+ }
568
+
569
+ function read3(bc: bare.ByteCursor): u64 | null {
570
+ return bare.readBool(bc)
571
+ ? bare.readU64(bc)
572
+ : null
573
+ }
574
+
575
+ function write3(bc: bare.ByteCursor, x: u64 | null): void {
576
+ bare.writeBool(bc, x !== null)
577
+ if (x !== null) {
578
+ bare.writeU64(bc, x)
579
+ }
580
+ }
581
+
582
+ export type EntryMetadata = {
583
+ readonly status: EntryStatus,
584
+ readonly error: string | null,
585
+ readonly attempts: u32,
586
+ readonly lastAttemptAt: u64,
587
+ readonly createdAt: u64,
588
+ readonly completedAt: u64 | null,
589
+ readonly rollbackCompletedAt: u64 | null,
590
+ readonly rollbackError: string | null,
591
+ }
592
+
593
+ export function readEntryMetadata(bc: bare.ByteCursor): EntryMetadata {
594
+ return {
595
+ status: readEntryStatus(bc),
596
+ error: read1(bc),
597
+ attempts: bare.readU32(bc),
598
+ lastAttemptAt: bare.readU64(bc),
599
+ createdAt: bare.readU64(bc),
600
+ completedAt: read3(bc),
601
+ rollbackCompletedAt: read3(bc),
602
+ rollbackError: read1(bc),
603
+ }
604
+ }
605
+
606
+ export function writeEntryMetadata(bc: bare.ByteCursor, x: EntryMetadata): void {
607
+ writeEntryStatus(bc, x.status)
608
+ write1(bc, x.error)
609
+ bare.writeU32(bc, x.attempts)
610
+ bare.writeU64(bc, x.lastAttemptAt)
611
+ bare.writeU64(bc, x.createdAt)
612
+ write3(bc, x.completedAt)
613
+ write3(bc, x.rollbackCompletedAt)
614
+ write1(bc, x.rollbackError)
615
+ }
616
+
617
+ export function encodeEntryMetadata(x: EntryMetadata): Uint8Array {
618
+ const bc = new bare.ByteCursor(
619
+ new Uint8Array(config.initialBufferLength),
620
+ config
621
+ )
622
+ writeEntryMetadata(bc, x)
623
+ return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
624
+ }
625
+
626
+ export function decodeEntryMetadata(bytes: Uint8Array): EntryMetadata {
627
+ const bc = new bare.ByteCursor(bytes, config)
628
+ const result = readEntryMetadata(bc)
629
+ if (bc.offset < bc.view.byteLength) {
630
+ throw new bare.BareError(bc.offset, "remaining bytes")
631
+ }
632
+ return result
633
+ }
634
+
635
+ export type Message = {
636
+ readonly id: string,
637
+ readonly name: string,
638
+ readonly messageData: Cbor,
639
+ readonly sentAt: u64,
640
+ }
641
+
642
+ export function readMessage(bc: bare.ByteCursor): Message {
643
+ return {
644
+ id: bare.readString(bc),
645
+ name: bare.readString(bc),
646
+ messageData: readCbor(bc),
647
+ sentAt: bare.readU64(bc),
648
+ }
649
+ }
650
+
651
+ export function writeMessage(bc: bare.ByteCursor, x: Message): void {
652
+ bare.writeString(bc, x.id)
653
+ bare.writeString(bc, x.name)
654
+ writeCbor(bc, x.messageData)
655
+ bare.writeU64(bc, x.sentAt)
656
+ }
657
+
658
+ export function encodeMessage(x: Message): Uint8Array {
659
+ const bc = new bare.ByteCursor(
660
+ new Uint8Array(config.initialBufferLength),
661
+ config
662
+ )
663
+ writeMessage(bc, x)
664
+ return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
665
+ }
666
+
667
+ export function decodeMessage(bytes: Uint8Array): Message {
668
+ const bc = new bare.ByteCursor(bytes, config)
669
+ const result = readMessage(bc)
670
+ if (bc.offset < bc.view.byteLength) {
671
+ throw new bare.BareError(bc.offset, "remaining bytes")
672
+ }
673
+ return result
674
+ }
675
+
676
+ export enum WorkflowState {
677
+ PENDING = "PENDING",
678
+ RUNNING = "RUNNING",
679
+ SLEEPING = "SLEEPING",
680
+ FAILED = "FAILED",
681
+ COMPLETED = "COMPLETED",
682
+ ROLLING_BACK = "ROLLING_BACK",
683
+ }
684
+
685
+ export function readWorkflowState(bc: bare.ByteCursor): WorkflowState {
686
+ const offset = bc.offset
687
+ const tag = bare.readU8(bc)
688
+ switch (tag) {
689
+ case 0:
690
+ return WorkflowState.PENDING
691
+ case 1:
692
+ return WorkflowState.RUNNING
693
+ case 2:
694
+ return WorkflowState.SLEEPING
695
+ case 3:
696
+ return WorkflowState.FAILED
697
+ case 4:
698
+ return WorkflowState.COMPLETED
699
+ case 5:
700
+ return WorkflowState.ROLLING_BACK
701
+ default: {
702
+ bc.offset = offset
703
+ throw new bare.BareError(offset, "invalid tag")
704
+ }
705
+ }
706
+ }
707
+
708
+ export function writeWorkflowState(bc: bare.ByteCursor, x: WorkflowState): void {
709
+ switch (x) {
710
+ case WorkflowState.PENDING: {
711
+ bare.writeU8(bc, 0)
712
+ break
713
+ }
714
+ case WorkflowState.RUNNING: {
715
+ bare.writeU8(bc, 1)
716
+ break
717
+ }
718
+ case WorkflowState.SLEEPING: {
719
+ bare.writeU8(bc, 2)
720
+ break
721
+ }
722
+ case WorkflowState.FAILED: {
723
+ bare.writeU8(bc, 3)
724
+ break
725
+ }
726
+ case WorkflowState.COMPLETED: {
727
+ bare.writeU8(bc, 4)
728
+ break
729
+ }
730
+ case WorkflowState.ROLLING_BACK: {
731
+ bare.writeU8(bc, 5)
732
+ break
733
+ }
734
+ }
735
+ }
736
+
737
+ export type WorkflowMetadata = {
738
+ readonly state: WorkflowState,
739
+ readonly output: Cbor | null,
740
+ readonly error: string | null,
741
+ readonly version: string | null,
742
+ }
743
+
744
+ export function readWorkflowMetadata(bc: bare.ByteCursor): WorkflowMetadata {
745
+ return {
746
+ state: readWorkflowState(bc),
747
+ output: read0(bc),
748
+ error: read1(bc),
749
+ version: read1(bc),
750
+ }
751
+ }
752
+
753
+ export function writeWorkflowMetadata(bc: bare.ByteCursor, x: WorkflowMetadata): void {
754
+ writeWorkflowState(bc, x.state)
755
+ write0(bc, x.output)
756
+ write1(bc, x.error)
757
+ write1(bc, x.version)
758
+ }
759
+
760
+ export function encodeWorkflowMetadata(x: WorkflowMetadata): Uint8Array {
761
+ const bc = new bare.ByteCursor(
762
+ new Uint8Array(config.initialBufferLength),
763
+ config
764
+ )
765
+ writeWorkflowMetadata(bc, x)
766
+ return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
767
+ }
768
+
769
+ export function decodeWorkflowMetadata(bytes: Uint8Array): WorkflowMetadata {
770
+ const bc = new bare.ByteCursor(bytes, config)
771
+ const result = readWorkflowMetadata(bc)
772
+ if (bc.offset < bc.view.byteLength) {
773
+ throw new bare.BareError(bc.offset, "remaining bytes")
774
+ }
775
+ return result
776
+ }
777
+
778
+
779
+ function assert(condition: boolean, message?: string): asserts condition {
780
+ if (!condition) throw new Error(message ?? "Assertion failed")
781
+ }