forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestration.ts
More file actions
1498 lines (1367 loc) · 50.2 KB
/
Copy pathorchestration.ts
File metadata and controls
1498 lines (1367 loc) · 50.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as SchemaIssue from "effect/SchemaIssue";
import * as SchemaTransformation from "effect/SchemaTransformation";
import * as Struct from "effect/Struct";
import { ProviderOptionSelections } from "./model.ts";
import { RepositoryIdentity } from "./environment.ts";
import {
ApprovalRequestId,
CheckpointRef,
CommandId,
EventId,
IsoDateTime,
MessageId,
NonNegativeInt,
ProjectId,
ProviderItemId,
ThreadId,
TrimmedNonEmptyString,
TrimmedString,
TurnId,
} from "./baseSchemas.ts";
import { ProviderInstanceId } from "./providerInstance.ts";
export const ORCHESTRATION_WS_METHODS = {
dispatchCommand: "orchestration.dispatchCommand",
getTurnDiff: "orchestration.getTurnDiff",
getFullThreadDiff: "orchestration.getFullThreadDiff",
searchThreads: "orchestration.searchThreads",
getArchivedShellSnapshot: "orchestration.getArchivedShellSnapshot",
subscribeShell: "orchestration.subscribeShell",
subscribeThread: "orchestration.subscribeThread",
} as const;
export const ProviderApprovalPolicy = Schema.Literals([
"untrusted",
"on-failure",
"on-request",
"never",
]);
export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;
export const ProviderSandboxMode = Schema.Literals([
"read-only",
"workspace-write",
"danger-full-access",
]);
export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;
/**
* `ModelSelection` — selection of a model on a configured provider instance.
*
* The routing key is `instanceId` (a user-defined slug identifying one
* configured provider instance). Drivers, credentials, working-directory
* bindings, and any other per-instance state are recovered from the
* runtime registry via the instance id.
*
* Wire legacy: persisted selections produced before the driver/instance
* split carried a `provider: <driver-id>` field instead. The schema absorbs
* that shape via a pre-decoding transform — `{provider, model}` is promoted
* to `{instanceId: defaultInstanceIdForDriver(provider), model}`. No
* post-decode compatibility code lives in the runtime; the transform is the
* only compat surface.
*/
const ModelSelectionWire = Schema.Struct({
instanceId: ProviderInstanceId,
model: TrimmedNonEmptyString,
options: Schema.optionalKey(ProviderOptionSelections),
});
// Source shape for persisted legacy payloads. Fields are typed as
// `Schema.Unknown` so malformed drafts still make it into the transform and
// fail validation through the target schema (with proper error messages)
// rather than at the source-struct layer where the error is less actionable.
const ModelSelectionSource = Schema.Struct({
provider: Schema.optional(Schema.Unknown),
instanceId: Schema.optional(Schema.Unknown),
model: Schema.Unknown,
options: Schema.optional(Schema.Unknown),
});
export const ModelSelection = ModelSelectionSource.pipe(
Schema.decodeTo(
ModelSelectionWire,
SchemaTransformation.transformOrFail({
decode: (raw) => {
// Resolve the routing key: prefer an explicit `instanceId`; fall
// back to promoting the legacy `provider` slug (the canonical
// `defaultInstanceIdForDriver` mapping) so persisted rollout-era
// payloads decode without data loss. The target schema brands the
// string as `ProviderInstanceId`.
const instanceIdSource =
raw.instanceId !== undefined
? raw.instanceId
: typeof raw.provider === "string"
? raw.provider
: undefined;
const base: Record<string, unknown> = {
instanceId: instanceIdSource,
model: raw.model,
};
if (raw.options !== undefined) base.options = raw.options;
return Effect.succeed(base as typeof ModelSelectionWire.Encoded);
},
encode: (value) => {
const base: Record<string, unknown> = {
model: value.model,
instanceId: value.instanceId,
};
if (value.options !== undefined) base.options = value.options;
return Effect.succeed(base as typeof ModelSelectionSource.Encoded);
},
}),
),
);
export type ModelSelection = typeof ModelSelection.Type;
export const RuntimeMode = Schema.Literals([
"approval-required",
"auto-accept-edits",
"auto",
"full-access",
]);
export type RuntimeMode = typeof RuntimeMode.Type;
export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
export const ProviderInteractionMode = Schema.Literals(["default", "plan"]);
export type ProviderInteractionMode = typeof ProviderInteractionMode.Type;
export const DEFAULT_PROVIDER_INTERACTION_MODE: ProviderInteractionMode = "default";
export const ProviderRequestKind = Schema.Literals(["command", "file-read", "file-change"]);
export type ProviderRequestKind = typeof ProviderRequestKind.Type;
export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);
export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;
export const ProviderApprovalDecision = Schema.Literals([
"accept",
"acceptForSession",
"decline",
"cancel",
]);
export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;
export const ProviderUserInputAnswers = Schema.Record(Schema.String, Schema.Unknown);
export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type;
export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
// Correlation id is command id by design in this model.
export const CorrelationId = CommandId;
export type CorrelationId = typeof CorrelationId.Type;
const ChatAttachmentId = TrimmedNonEmptyString.check(
Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
Schema.isPattern(/^[a-z0-9_-]+$/i),
);
export type ChatAttachmentId = typeof ChatAttachmentId.Type;
export const ChatImageAttachment = Schema.Struct({
type: Schema.Literal("image"),
id: ChatAttachmentId,
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
});
export type ChatImageAttachment = typeof ChatImageAttachment.Type;
const UploadChatImageAttachment = Schema.Struct({
type: Schema.Literal("image"),
name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
dataUrl: TrimmedNonEmptyString.check(
Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
),
});
export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
export const ChatAttachment = Schema.Union([ChatImageAttachment]);
export type ChatAttachment = typeof ChatAttachment.Type;
const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
export type UploadChatAttachment = typeof UploadChatAttachment.Type;
export const ProjectScriptIcon = Schema.Literals([
"play",
"test",
"lint",
"configure",
"build",
"debug",
]);
export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
export const ProjectScript = Schema.Struct({
id: TrimmedNonEmptyString,
name: TrimmedNonEmptyString,
command: TrimmedNonEmptyString,
icon: ProjectScriptIcon,
runOnWorktreeCreate: Schema.Boolean,
/**
* URL to open in the in-app browser preview when this script runs (or
* when the user explicitly requests a preview). Optional; only honored on
* the desktop build.
*/
previewUrl: Schema.optional(TrimmedNonEmptyString),
/**
* When true, automatically open the preview panel pointed at `previewUrl`
* the moment this script starts. Ignored without `previewUrl` or on web.
*/
autoOpenPreview: Schema.optional(Schema.Boolean),
});
export type ProjectScript = typeof ProjectScript.Type;
export const OrchestrationProject = Schema.Struct({
id: ProjectId,
title: TrimmedNonEmptyString,
workspaceRoot: TrimmedNonEmptyString,
repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
defaultModelSelection: Schema.NullOr(ModelSelection),
scripts: Schema.Array(ProjectScript),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
deletedAt: Schema.NullOr(IsoDateTime),
});
export type OrchestrationProject = typeof OrchestrationProject.Type;
export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);
export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type;
export const OrchestrationMessage = Schema.Struct({
id: MessageId,
role: OrchestrationMessageRole,
text: Schema.String,
attachments: Schema.optional(Schema.Array(ChatAttachment)),
turnId: Schema.NullOr(TurnId),
streaming: Schema.Boolean,
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export type OrchestrationMessage = typeof OrchestrationMessage.Type;
export const OrchestrationProposedPlanId = TrimmedNonEmptyString;
export type OrchestrationProposedPlanId = typeof OrchestrationProposedPlanId.Type;
export const OrchestrationProposedPlan = Schema.Struct({
id: OrchestrationProposedPlanId,
turnId: Schema.NullOr(TurnId),
planMarkdown: TrimmedNonEmptyString,
implementedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
implementationThreadId: Schema.NullOr(ThreadId).pipe(
Schema.withDecodingDefault(Effect.succeed(null)),
),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type;
const SourceProposedPlanReference = Schema.Struct({
threadId: ThreadId,
planId: OrchestrationProposedPlanId,
});
export const OrchestrationSessionStatus = Schema.Literals([
"idle",
"starting",
"running",
"ready",
"interrupted",
"stopped",
"error",
]);
export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type;
export const OrchestrationSession = Schema.Struct({
threadId: ThreadId,
status: OrchestrationSessionStatus,
providerName: Schema.NullOr(TrimmedNonEmptyString),
providerInstanceId: Schema.optional(ProviderInstanceId),
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
activeTurnId: Schema.NullOr(TurnId),
lastError: Schema.NullOr(TrimmedNonEmptyString),
updatedAt: IsoDateTime,
});
export type OrchestrationSession = typeof OrchestrationSession.Type;
export const OrchestrationCheckpointFile = Schema.Struct({
path: TrimmedNonEmptyString,
kind: TrimmedNonEmptyString,
additions: NonNegativeInt,
deletions: NonNegativeInt,
});
export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type;
export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);
export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type;
export const OrchestrationCheckpointSummary = Schema.Struct({
turnId: TurnId,
checkpointTurnCount: NonNegativeInt,
checkpointRef: CheckpointRef,
status: OrchestrationCheckpointStatus,
files: Schema.Array(OrchestrationCheckpointFile),
assistantMessageId: Schema.NullOr(MessageId),
completedAt: IsoDateTime,
});
export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type;
export const OrchestrationThreadActivityTone = Schema.Literals([
"info",
"tool",
"approval",
"error",
]);
export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type;
export const OrchestrationThreadActivity = Schema.Struct({
id: EventId,
tone: OrchestrationThreadActivityTone,
kind: TrimmedNonEmptyString,
summary: TrimmedNonEmptyString,
payload: Schema.Unknown,
turnId: Schema.NullOr(TurnId),
sequence: Schema.optional(NonNegativeInt),
createdAt: IsoDateTime,
});
export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type;
const OrchestrationLatestTurnState = Schema.Literals([
"running",
"interrupted",
"completed",
"error",
]);
export type OrchestrationLatestTurnState = typeof OrchestrationLatestTurnState.Type;
export const OrchestrationLatestTurn = Schema.Struct({
turnId: TurnId,
state: OrchestrationLatestTurnState,
requestedAt: IsoDateTime,
startedAt: Schema.NullOr(IsoDateTime),
completedAt: Schema.NullOr(IsoDateTime),
assistantMessageId: Schema.NullOr(MessageId),
sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
});
export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type;
export const ThreadTitleRegeneration = Schema.Struct({
requestId: CommandId,
startedAt: IsoDateTime,
});
export type ThreadTitleRegeneration = typeof ThreadTitleRegeneration.Type;
export const OrchestrationThread = Schema.Struct({
id: ThreadId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
modelSelection: ModelSelection,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
),
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
parentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
latestTurn: Schema.NullOr(OrchestrationLatestTurn),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
settledOverride: Schema.NullOr(Schema.Literals(["settled", "active"])).pipe(
Schema.withDecodingDefault(Effect.succeed(null)),
),
settledAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
// Snooze is an overlay on the active lifecycle, not a fourth destination:
// a snoozed thread stays "active" in the model and is only suppressed from
// the inbox until snoozedUntil passes (or the thread raises its hand).
// Optional so payloads from pre-snooze servers still decode.
snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
// Pending-only state. Optional so older servers remain compatible.
titleRegeneration: Schema.optional(Schema.NullOr(ThreadTitleRegeneration)),
deletedAt: Schema.NullOr(IsoDateTime),
messages: Schema.Array(OrchestrationMessage),
proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe(
Schema.withDecodingDefault(Effect.succeed([])),
),
activities: Schema.Array(OrchestrationThreadActivity),
checkpoints: Schema.Array(OrchestrationCheckpointSummary),
session: Schema.NullOr(OrchestrationSession),
});
export type OrchestrationThread = typeof OrchestrationThread.Type;
export const OrchestrationReadModel = Schema.Struct({
snapshotSequence: NonNegativeInt,
projects: Schema.Array(OrchestrationProject),
threads: Schema.Array(OrchestrationThread),
updatedAt: IsoDateTime,
});
export type OrchestrationReadModel = typeof OrchestrationReadModel.Type;
export const OrchestrationProjectShell = Schema.Struct({
id: ProjectId,
title: TrimmedNonEmptyString,
workspaceRoot: TrimmedNonEmptyString,
repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
defaultModelSelection: Schema.NullOr(ModelSelection),
scripts: Schema.Array(ProjectScript),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export type OrchestrationProjectShell = typeof OrchestrationProjectShell.Type;
export const OrchestrationThreadShell = Schema.Struct({
id: ThreadId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
modelSelection: ModelSelection,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
),
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
parentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
latestTurn: Schema.NullOr(OrchestrationLatestTurn),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
settledOverride: Schema.NullOr(Schema.Literals(["settled", "active"])).pipe(
Schema.withDecodingDefault(Effect.succeed(null)),
),
settledAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
titleRegeneration: Schema.optional(Schema.NullOr(ThreadTitleRegeneration)),
session: Schema.NullOr(OrchestrationSession),
latestUserMessageAt: Schema.NullOr(IsoDateTime),
hasPendingApprovals: Schema.Boolean,
hasPendingUserInput: Schema.Boolean,
hasActionableProposedPlan: Schema.Boolean,
});
export type OrchestrationThreadShell = typeof OrchestrationThreadShell.Type;
export const OrchestrationShellSnapshot = Schema.Struct({
snapshotSequence: NonNegativeInt,
projects: Schema.Array(OrchestrationProjectShell),
threads: Schema.Array(OrchestrationThreadShell),
updatedAt: IsoDateTime,
});
export type OrchestrationShellSnapshot = typeof OrchestrationShellSnapshot.Type;
export const OrchestrationShellStreamEvent = Schema.Union([
Schema.Struct({
kind: Schema.Literal("project-upserted"),
sequence: NonNegativeInt,
project: OrchestrationProjectShell,
}),
Schema.Struct({
kind: Schema.Literal("project-removed"),
sequence: NonNegativeInt,
projectId: ProjectId,
}),
Schema.Struct({
kind: Schema.Literal("thread-upserted"),
sequence: NonNegativeInt,
thread: OrchestrationThreadShell,
}),
Schema.Struct({
kind: Schema.Literal("thread-removed"),
sequence: NonNegativeInt,
threadId: ThreadId,
}),
]);
export type OrchestrationShellStreamEvent = typeof OrchestrationShellStreamEvent.Type;
export const OrchestrationShellStreamItem = Schema.Union([
Schema.Struct({
kind: Schema.Literal("synchronized"),
}),
Schema.Struct({
kind: Schema.Literal("snapshot"),
snapshot: OrchestrationShellSnapshot,
}),
OrchestrationShellStreamEvent,
]);
export type OrchestrationShellStreamItem = typeof OrchestrationShellStreamItem.Type;
export const OrchestrationSubscribeShellInput = Schema.Struct({
/**
* When provided, the server skips the initial full shell snapshot and instead
* replays shell events after this sequence before streaming live events.
* Clients that already hold a cached (or HTTP-loaded) shell snapshot pass its
* sequence here so the subscription resumes without re-sending the entire
* projects/threads list (overlapping events are deduped by sequence on the
* client).
*/
afterSequence: Schema.optionalKey(NonNegativeInt),
/**
* Requests an explicit marker after the subscription has emitted its initial
* snapshot or catch-up replay and before it begins emitting live events.
*/
requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
});
export type OrchestrationSubscribeShellInput = typeof OrchestrationSubscribeShellInput.Type;
export const OrchestrationSubscribeThreadInput = Schema.Struct({
threadId: ThreadId,
/**
* When provided, the server skips the initial snapshot frame and instead
* replays events after this sequence before streaming live events. Clients
* that load the snapshot over HTTP pass the snapshot's sequence here so the
* live subscription resumes without a gap (overlapping events are deduped by
* sequence on the client).
*/
afterSequence: Schema.optionalKey(NonNegativeInt),
/**
* Requests an explicit marker after the subscription has emitted its initial
* snapshot or catch-up replay and before it begins emitting live events.
*/
requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
});
export type OrchestrationSubscribeThreadInput = typeof OrchestrationSubscribeThreadInput.Type;
export const OrchestrationThreadDetailSnapshot = Schema.Struct({
snapshotSequence: NonNegativeInt,
thread: OrchestrationThread,
});
export type OrchestrationThreadDetailSnapshot = typeof OrchestrationThreadDetailSnapshot.Type;
export const ProjectCreateCommand = Schema.Struct({
type: Schema.Literal("project.create"),
commandId: CommandId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
workspaceRoot: TrimmedNonEmptyString,
createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
createdAt: IsoDateTime,
});
const ProjectMetaUpdateCommand = Schema.Struct({
type: Schema.Literal("project.meta.update"),
commandId: CommandId,
projectId: ProjectId,
title: Schema.optional(TrimmedNonEmptyString),
workspaceRoot: Schema.optional(TrimmedNonEmptyString),
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
scripts: Schema.optional(Schema.Array(ProjectScript)),
});
const ProjectDeleteCommand = Schema.Struct({
type: Schema.Literal("project.delete"),
commandId: CommandId,
projectId: ProjectId,
force: Schema.optional(Schema.Boolean),
});
const ThreadCreateCommand = Schema.Struct({
type: Schema.Literal("thread.create"),
commandId: CommandId,
threadId: ThreadId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
modelSelection: ModelSelection,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
),
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
parentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
createdAt: IsoDateTime,
});
const ThreadDeleteCommand = Schema.Struct({
type: Schema.Literal("thread.delete"),
commandId: CommandId,
threadId: ThreadId,
});
const ThreadArchiveCommand = Schema.Struct({
type: Schema.Literal("thread.archive"),
commandId: CommandId,
threadId: ThreadId,
});
const ThreadUnarchiveCommand = Schema.Struct({
type: Schema.Literal("thread.unarchive"),
commandId: CommandId,
threadId: ThreadId,
});
const ThreadSettleCommand = Schema.Struct({
type: Schema.Literal("thread.settle"),
commandId: CommandId,
threadId: ThreadId,
});
const ThreadUnsettleCommand = Schema.Struct({
type: Schema.Literal("thread.unsettle"),
commandId: CommandId,
threadId: ThreadId,
// Commands only carry "user": activity un-settles are decided server-side
// (the decider emits thread.unsettled(reason: "activity") events directly,
// never through this command), so a client cannot forge the neutral reset.
reason: Schema.Literal("user"),
});
const ThreadSnoozeCommand = Schema.Struct({
type: Schema.Literal("thread.snooze"),
commandId: CommandId,
threadId: ThreadId,
// The wake time. Event-based wake conditions (PR merged, review posted)
// will arrive as an optional condition field alongside this; time-based
// snooze is just the first kind of condition.
snoozedUntil: IsoDateTime,
});
const ThreadUnsnoozeCommand = Schema.Struct({
type: Schema.Literal("thread.unsnooze"),
commandId: CommandId,
threadId: ThreadId,
// Commands only carry "user": activity wakes are decided server-side (the
// decider emits thread.unsnoozed(reason: "activity") directly), and timer
// wakes need no event at all — clients derive visibility from snoozedUntil,
// so a passed wake time simply stops classifying as snoozed.
reason: Schema.Literal("user"),
});
const ThreadMetaUpdateCommand = Schema.Struct({
type: Schema.Literal("thread.meta.update"),
commandId: CommandId,
threadId: ThreadId,
title: Schema.optional(TrimmedNonEmptyString),
regenerateTitle: Schema.optional(Schema.Literal(true)),
modelSelection: Schema.optional(ModelSelection),
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
expectedBranch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
}).check(
Schema.makeFilter(
(input) =>
!(input.title !== undefined && input.regenerateTitle === true) ||
"title and regenerateTitle cannot be specified together",
),
);
const ThreadRuntimeModeSetCommand = Schema.Struct({
type: Schema.Literal("thread.runtime-mode.set"),
commandId: CommandId,
threadId: ThreadId,
runtimeMode: RuntimeMode,
createdAt: IsoDateTime,
});
const ThreadInteractionModeSetCommand = Schema.Struct({
type: Schema.Literal("thread.interaction-mode.set"),
commandId: CommandId,
threadId: ThreadId,
interactionMode: ProviderInteractionMode,
createdAt: IsoDateTime,
});
const ThreadTurnStartBootstrapCreateThread = Schema.Struct({
projectId: ProjectId,
title: TrimmedNonEmptyString,
modelSelection: ModelSelection,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
parentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
createdAt: IsoDateTime,
});
const ThreadTurnStartBootstrapPrepareWorktree = Schema.Struct({
projectCwd: TrimmedNonEmptyString,
baseBranch: TrimmedNonEmptyString,
branch: Schema.optional(TrimmedNonEmptyString),
startFromOrigin: Schema.optional(Schema.Boolean),
});
const ThreadTurnStartBootstrap = Schema.Struct({
createThread: Schema.optional(ThreadTurnStartBootstrapCreateThread),
prepareWorktree: Schema.optional(ThreadTurnStartBootstrapPrepareWorktree),
runSetupScript: Schema.optional(Schema.Boolean),
});
export type ThreadTurnStartBootstrap = typeof ThreadTurnStartBootstrap.Type;
export const ThreadTurnStartCommand = Schema.Struct({
type: Schema.Literal("thread.turn.start"),
commandId: CommandId,
threadId: ThreadId,
message: Schema.Struct({
messageId: MessageId,
role: Schema.Literal("user"),
text: Schema.String,
attachments: Schema.Array(ChatAttachment),
}),
modelSelection: Schema.optional(ModelSelection),
titleSeed: Schema.optional(TrimmedNonEmptyString),
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
),
bootstrap: Schema.optional(ThreadTurnStartBootstrap),
sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
createdAt: IsoDateTime,
});
const ClientThreadTurnStartCommand = Schema.Struct({
type: Schema.Literal("thread.turn.start"),
commandId: CommandId,
threadId: ThreadId,
message: Schema.Struct({
messageId: MessageId,
role: Schema.Literal("user"),
text: Schema.String,
attachments: Schema.Array(UploadChatAttachment),
}),
modelSelection: Schema.optional(ModelSelection),
titleSeed: Schema.optional(TrimmedNonEmptyString),
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
bootstrap: Schema.optional(ThreadTurnStartBootstrap),
sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
createdAt: IsoDateTime,
});
const ThreadTurnInterruptCommand = Schema.Struct({
type: Schema.Literal("thread.turn.interrupt"),
commandId: CommandId,
threadId: ThreadId,
turnId: Schema.optional(TurnId),
createdAt: IsoDateTime,
});
const ThreadApprovalRespondCommand = Schema.Struct({
type: Schema.Literal("thread.approval.respond"),
commandId: CommandId,
threadId: ThreadId,
requestId: ApprovalRequestId,
decision: ProviderApprovalDecision,
createdAt: IsoDateTime,
});
const ThreadUserInputRespondCommand = Schema.Struct({
type: Schema.Literal("thread.user-input.respond"),
commandId: CommandId,
threadId: ThreadId,
requestId: ApprovalRequestId,
answers: ProviderUserInputAnswers,
createdAt: IsoDateTime,
});
const ThreadCheckpointRevertCommand = Schema.Struct({
type: Schema.Literal("thread.checkpoint.revert"),
commandId: CommandId,
threadId: ThreadId,
turnCount: NonNegativeInt,
createdAt: IsoDateTime,
});
const ThreadSessionStopCommand = Schema.Struct({
type: Schema.Literal("thread.session.stop"),
commandId: CommandId,
threadId: ThreadId,
createdAt: IsoDateTime,
});
const DispatchableClientOrchestrationCommand = Schema.Union([
ProjectCreateCommand,
ProjectMetaUpdateCommand,
ProjectDeleteCommand,
ThreadCreateCommand,
ThreadDeleteCommand,
ThreadArchiveCommand,
ThreadUnarchiveCommand,
ThreadSettleCommand,
ThreadUnsettleCommand,
ThreadSnoozeCommand,
ThreadUnsnoozeCommand,
ThreadMetaUpdateCommand,
ThreadRuntimeModeSetCommand,
ThreadInteractionModeSetCommand,
ThreadTurnStartCommand,
ThreadTurnInterruptCommand,
ThreadApprovalRespondCommand,
ThreadUserInputRespondCommand,
ThreadCheckpointRevertCommand,
ThreadSessionStopCommand,
]);
export type DispatchableClientOrchestrationCommand =
typeof DispatchableClientOrchestrationCommand.Type;
export const ClientOrchestrationCommand = Schema.Union([
ProjectCreateCommand,
ProjectMetaUpdateCommand,
ProjectDeleteCommand,
ThreadCreateCommand,
ThreadDeleteCommand,
ThreadArchiveCommand,
ThreadUnarchiveCommand,
ThreadSettleCommand,
ThreadUnsettleCommand,
ThreadSnoozeCommand,
ThreadUnsnoozeCommand,
ThreadMetaUpdateCommand,
ThreadRuntimeModeSetCommand,
ThreadInteractionModeSetCommand,
ClientThreadTurnStartCommand,
ThreadTurnInterruptCommand,
ThreadApprovalRespondCommand,
ThreadUserInputRespondCommand,
ThreadCheckpointRevertCommand,
ThreadSessionStopCommand,
]);
export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type;
const ThreadSessionSetCommand = Schema.Struct({
type: Schema.Literal("thread.session.set"),
commandId: CommandId,
threadId: ThreadId,
session: OrchestrationSession,
createdAt: IsoDateTime,
});
const ThreadMessageAssistantDeltaCommand = Schema.Struct({
type: Schema.Literal("thread.message.assistant.delta"),
commandId: CommandId,
threadId: ThreadId,
messageId: MessageId,
delta: Schema.String,
turnId: Schema.optional(TurnId),
createdAt: IsoDateTime,
});
const ThreadMessageAssistantCompleteCommand = Schema.Struct({
type: Schema.Literal("thread.message.assistant.complete"),
commandId: CommandId,
threadId: ThreadId,
messageId: MessageId,
turnId: Schema.optional(TurnId),
createdAt: IsoDateTime,
});
const ThreadProposedPlanUpsertCommand = Schema.Struct({
type: Schema.Literal("thread.proposed-plan.upsert"),
commandId: CommandId,
threadId: ThreadId,
proposedPlan: OrchestrationProposedPlan,
createdAt: IsoDateTime,
});
const ThreadTurnDiffCompleteCommand = Schema.Struct({
type: Schema.Literal("thread.turn.diff.complete"),
commandId: CommandId,
threadId: ThreadId,
turnId: TurnId,
completedAt: IsoDateTime,
checkpointRef: CheckpointRef,
status: OrchestrationCheckpointStatus,
files: Schema.Array(OrchestrationCheckpointFile),
assistantMessageId: Schema.optional(MessageId),
checkpointTurnCount: NonNegativeInt,
createdAt: IsoDateTime,
});
const ThreadActivityAppendCommand = Schema.Struct({
type: Schema.Literal("thread.activity.append"),
commandId: CommandId,
threadId: ThreadId,
activity: OrchestrationThreadActivity,
createdAt: IsoDateTime,
});
const ThreadRevertCompleteCommand = Schema.Struct({
type: Schema.Literal("thread.revert.complete"),
commandId: CommandId,
threadId: ThreadId,
turnCount: NonNegativeInt,
createdAt: IsoDateTime,
});
const ThreadTitleRegenerationCompleteCommand = Schema.Struct({
type: Schema.Literal("thread.title.regeneration.complete"),
commandId: CommandId,
threadId: ThreadId,
requestId: CommandId,
title: Schema.optional(TrimmedNonEmptyString),
});
const InternalOrchestrationCommand = Schema.Union([
ThreadSessionSetCommand,
ThreadMessageAssistantDeltaCommand,
ThreadMessageAssistantCompleteCommand,
ThreadProposedPlanUpsertCommand,
ThreadTurnDiffCompleteCommand,
ThreadActivityAppendCommand,
ThreadRevertCompleteCommand,
ThreadTitleRegenerationCompleteCommand,
]);
export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
export const OrchestrationCommand = Schema.Union([
DispatchableClientOrchestrationCommand,
InternalOrchestrationCommand,
]);
export type OrchestrationCommand = typeof OrchestrationCommand.Type;
export const OrchestrationEventType = Schema.Literals([
"project.created",
"project.meta-updated",
"project.deleted",
"thread.created",
"thread.deleted",
"thread.archived",
"thread.unarchived",
"thread.settled",
"thread.unsettled",
"thread.snoozed",
"thread.unsnoozed",
"thread.meta-updated",
"thread.runtime-mode-set",
"thread.interaction-mode-set",
"thread.message-sent",
"thread.turn-start-requested",
"thread.turn-interrupt-requested",
"thread.approval-response-requested",
"thread.user-input-response-requested",
"thread.checkpoint-revert-requested",
"thread.reverted",
"thread.session-stop-requested",
"thread.session-set",
"thread.proposed-plan-upserted",
"thread.turn-diff-completed",
"thread.activity-appended",
]);
export type OrchestrationEventType = typeof OrchestrationEventType.Type;
export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
export const ProjectCreatedPayload = Schema.Struct({
projectId: ProjectId,
title: TrimmedNonEmptyString,
workspaceRoot: TrimmedNonEmptyString,
repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
defaultModelSelection: Schema.NullOr(ModelSelection),
scripts: Schema.Array(ProjectScript),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export const ProjectMetaUpdatedPayload = Schema.Struct({
projectId: ProjectId,
title: Schema.optional(TrimmedNonEmptyString),
workspaceRoot: Schema.optional(TrimmedNonEmptyString),
repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
scripts: Schema.optional(Schema.Array(ProjectScript)),
updatedAt: IsoDateTime,
});
export const ProjectDeletedPayload = Schema.Struct({
projectId: ProjectId,
deletedAt: IsoDateTime,
});
export const ThreadCreatedPayload = Schema.Struct({
threadId: ThreadId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
modelSelection: ModelSelection,
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
),
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
parentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export const ThreadDeletedPayload = Schema.Struct({
threadId: ThreadId,
deletedAt: IsoDateTime,
});
export const ThreadArchivedPayload = Schema.Struct({
threadId: ThreadId,
archivedAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export const ThreadUnarchivedPayload = Schema.Struct({
threadId: ThreadId,
updatedAt: IsoDateTime,
});