Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestration.ts

879 lines27.6 KB

1import { Option, Schema, SchemaIssue, Struct } from "effect";
2import {
3 ApprovalRequestId,
4 CheckpointRef,
5 CommandId,
6 EventId,
7 IsoDateTime,
8 MessageId,
9 NonNegativeInt,
10 ProjectId,
11 ProviderItemId,
12 ProviderSessionId,
13 ProviderThreadId,
14 ProviderTurnId,
15 ThreadId,
16 TrimmedNonEmptyString,
17 TurnId,
18} from "./baseSchemas";
19​
20export const ORCHESTRATION_WS_METHODS = {
21 getSnapshot: "orchestration.getSnapshot",
22 dispatchCommand: "orchestration.dispatchCommand",
23 getTurnDiff: "orchestration.getTurnDiff",
24 getFullThreadDiff: "orchestration.getFullThreadDiff",
25 replayEvents: "orchestration.replayEvents",
26} as const;
27​
28export const ORCHESTRATION_WS_CHANNELS = {
29 domainEvent: "orchestration.domainEvent",
30} as const;
31​
32export const ProviderKind = Schema.Literals(["codex", "claudeCode"]);
33export type ProviderKind = typeof ProviderKind.Type;
34export const ProviderApprovalPolicy = Schema.Literals([
35 "untrusted",
36 "on-failure",
37 "on-request",
38 "never",
39]);
40export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;
41export const ProviderSandboxMode = Schema.Literals([
42 "read-only",
43 "workspace-write",
44 "danger-full-access",
45]);
46export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;
47export const ProviderRequestKind = Schema.Literals(["command", "file-change"]);
48export type ProviderRequestKind = typeof ProviderRequestKind.Type;
49export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);
50export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;
51export const ProviderApprovalDecision = Schema.Literals([
52 "accept",
53 "acceptForSession",
54 "decline",
55 "cancel",
56]);
57export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;
58​
59export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
60export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
61export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
62export const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
63​
64// Correlation id is command id by design in this model.
65export const CorrelationId = CommandId;
66export type CorrelationId = typeof CorrelationId.Type;
67​
68export const ChatImageAttachment = Schema.Struct({
69 type: Schema.Literal("image"),
70 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
71 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
72 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
73 dataUrl: TrimmedNonEmptyString.check(
74 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
75 ),
76});
77export type ChatImageAttachment = typeof ChatImageAttachment.Type;
78​
79export const ChatAttachment = Schema.Union([ChatImageAttachment]);
80export type ChatAttachment = typeof ChatAttachment.Type;
81​
82export const ProjectScriptIcon = Schema.Literals([
83 "play",
84 "test",
85 "lint",
86 "configure",
87 "build",
88 "debug",
89]);
90export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
91​
92export const ProjectScript = Schema.Struct({
93 id: TrimmedNonEmptyString,
94 name: TrimmedNonEmptyString,
95 command: TrimmedNonEmptyString,
96 icon: ProjectScriptIcon,
97 runOnWorktreeCreate: Schema.Boolean,
98});
99export type ProjectScript = typeof ProjectScript.Type;
100​
101export const OrchestrationProject = Schema.Struct({
102 id: ProjectId,
103 title: TrimmedNonEmptyString,
104 workspaceRoot: TrimmedNonEmptyString,
105 defaultModel: Schema.NullOr(TrimmedNonEmptyString),
106 scripts: Schema.Array(ProjectScript),
107 createdAt: IsoDateTime,
108 updatedAt: IsoDateTime,
109 deletedAt: Schema.NullOr(IsoDateTime),
110});
111export type OrchestrationProject = typeof OrchestrationProject.Type;
112​
113export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);
114export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type;
115​
116export const OrchestrationMessage = Schema.Struct({
117 id: MessageId,
118 role: OrchestrationMessageRole,
119 text: Schema.String,
120 attachments: Schema.optional(Schema.Array(ChatAttachment)),
121 turnId: Schema.NullOr(TurnId),
122 streaming: Schema.Boolean,
123 createdAt: IsoDateTime,
124 updatedAt: IsoDateTime,
125});
126export type OrchestrationMessage = typeof OrchestrationMessage.Type;
127​
128export const OrchestrationSessionStatus = Schema.Literals([
129 "idle",
130 "starting",
131 "running",
132 "ready",
133 "interrupted",
134 "stopped",
135 "error",
136]);
137export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type;
138​
139export const OrchestrationSession = Schema.Struct({
140 threadId: ThreadId,
141 status: OrchestrationSessionStatus,
142 providerName: Schema.NullOr(TrimmedNonEmptyString),
143 providerSessionId: Schema.NullOr(ProviderSessionId),
144 providerThreadId: Schema.NullOr(ProviderThreadId),
145 approvalPolicy: ProviderApprovalPolicy.pipe(Schema.withDecodingDefault(() => "on-failure")),
146 sandboxMode: ProviderSandboxMode.pipe(Schema.withDecodingDefault(() => "workspace-write")),
147 activeTurnId: Schema.NullOr(TurnId),
148 lastError: Schema.NullOr(TrimmedNonEmptyString),
149 updatedAt: IsoDateTime,
150});
151export type OrchestrationSession = typeof OrchestrationSession.Type;
152​
153export const OrchestrationCheckpointFile = Schema.Struct({
154 path: TrimmedNonEmptyString,
155 kind: TrimmedNonEmptyString,
156 additions: NonNegativeInt,
157 deletions: NonNegativeInt,
158});
159export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type;
160​
161export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);
162export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type;
163​
164export const OrchestrationCheckpointSummary = Schema.Struct({
165 turnId: TurnId,
166 checkpointTurnCount: NonNegativeInt,
167 checkpointRef: CheckpointRef,
168 status: OrchestrationCheckpointStatus,
169 files: Schema.Array(OrchestrationCheckpointFile),
170 assistantMessageId: Schema.NullOr(MessageId),
171 completedAt: IsoDateTime,
172});
173export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type;
174​
175export const OrchestrationThreadActivityTone = Schema.Literals([
176 "info",
177 "tool",
178 "approval",
179 "error",
180]);
181export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type;
182​
183export const OrchestrationThreadActivity = Schema.Struct({
184 id: EventId,
185 tone: OrchestrationThreadActivityTone,
186 kind: TrimmedNonEmptyString,
187 summary: TrimmedNonEmptyString,
188 payload: Schema.Unknown,
189 turnId: Schema.NullOr(TurnId),
190 createdAt: IsoDateTime,
191});
192export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type;
193​
194export const OrchestrationThread = Schema.Struct({
195 id: ThreadId,
196 projectId: ProjectId,
197 title: TrimmedNonEmptyString,
198 model: TrimmedNonEmptyString,
199 branch: Schema.NullOr(TrimmedNonEmptyString),
200 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
201 latestTurnId: Schema.NullOr(TurnId),
202 createdAt: IsoDateTime,
203 updatedAt: IsoDateTime,
204 deletedAt: Schema.NullOr(IsoDateTime),
205 messages: Schema.Array(OrchestrationMessage),
206 activities: Schema.Array(OrchestrationThreadActivity),
207 checkpoints: Schema.Array(OrchestrationCheckpointSummary),
208 session: Schema.NullOr(OrchestrationSession),
209});
210export type OrchestrationThread = typeof OrchestrationThread.Type;
211​
212export const OrchestrationReadModel = Schema.Struct({
213 snapshotSequence: NonNegativeInt,
214 projects: Schema.Array(OrchestrationProject),
215 threads: Schema.Array(OrchestrationThread),
216 updatedAt: IsoDateTime,
217});
218export type OrchestrationReadModel = typeof OrchestrationReadModel.Type;
219​
220export const ProjectCreateCommand = Schema.Struct({
221 type: Schema.Literal("project.create"),
222 commandId: CommandId,
223 projectId: ProjectId,
224 title: TrimmedNonEmptyString,
225 workspaceRoot: TrimmedNonEmptyString,
226 defaultModel: Schema.optional(TrimmedNonEmptyString),
227 createdAt: IsoDateTime,
228});
229​
230export const ProjectMetaUpdateCommand = Schema.Struct({
231 type: Schema.Literal("project.meta.update"),
232 commandId: CommandId,
233 projectId: ProjectId,
234 title: Schema.optional(TrimmedNonEmptyString),
235 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
236 defaultModel: Schema.optional(TrimmedNonEmptyString),
237 scripts: Schema.optional(Schema.Array(ProjectScript)),
238});
239​
240export const ProjectDeleteCommand = Schema.Struct({
241 type: Schema.Literal("project.delete"),
242 commandId: CommandId,
243 projectId: ProjectId,
244});
245​
246export const ThreadCreateCommand = Schema.Struct({
247 type: Schema.Literal("thread.create"),
248 commandId: CommandId,
249 threadId: ThreadId,
250 projectId: ProjectId,
251 title: TrimmedNonEmptyString,
252 model: TrimmedNonEmptyString,
253 branch: Schema.NullOr(TrimmedNonEmptyString),
254 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
255 createdAt: IsoDateTime,
256});
257​
258export const ThreadDeleteCommand = Schema.Struct({
259 type: Schema.Literal("thread.delete"),
260 commandId: CommandId,
261 threadId: ThreadId,
262});
263​
264export const ThreadMetaUpdateCommand = Schema.Struct({
265 type: Schema.Literal("thread.meta.update"),
266 commandId: CommandId,
267 threadId: ThreadId,
268 title: Schema.optional(TrimmedNonEmptyString),
269 model: Schema.optional(TrimmedNonEmptyString),
270 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
271 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
272});
273​
274export const ThreadTurnStartCommand = Schema.Struct({
275 type: Schema.Literal("thread.turn.start"),
276 commandId: CommandId,
277 threadId: ThreadId,
278 message: Schema.Struct({
279 messageId: MessageId,
280 role: Schema.Literal("user"),
281 text: Schema.String,
282 attachments: Schema.Array(ChatAttachment),
283 }),
284 model: Schema.optional(TrimmedNonEmptyString),
285 effort: Schema.optional(TrimmedNonEmptyString),
286 assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),
287 approvalPolicy: ProviderApprovalPolicy,
288 sandboxMode: ProviderSandboxMode,
289 createdAt: IsoDateTime,
290});
291​
292export const ThreadTurnInterruptCommand = Schema.Struct({
293 type: Schema.Literal("thread.turn.interrupt"),
294 commandId: CommandId,
295 threadId: ThreadId,
296 turnId: Schema.optional(TurnId),
297 createdAt: IsoDateTime,
298});
299​
300export const ThreadApprovalRespondCommand = Schema.Struct({
301 type: Schema.Literal("thread.approval.respond"),
302 commandId: CommandId,
303 threadId: ThreadId,
304 requestId: ApprovalRequestId,
305 decision: ProviderApprovalDecision,
306 createdAt: IsoDateTime,
307});
308​
309export const ThreadCheckpointRevertCommand = Schema.Struct({
310 type: Schema.Literal("thread.checkpoint.revert"),
311 commandId: CommandId,
312 threadId: ThreadId,
313 turnCount: NonNegativeInt,
314 createdAt: IsoDateTime,
315});
316​
317export const ThreadSessionStopCommand = Schema.Struct({
318 type: Schema.Literal("thread.session.stop"),
319 commandId: CommandId,
320 threadId: ThreadId,
321 createdAt: IsoDateTime,
322});
323​
324export const ClientOrchestrationCommand = Schema.Union([
325 ProjectCreateCommand,
326 ProjectMetaUpdateCommand,
327 ProjectDeleteCommand,
328 ThreadCreateCommand,
329 ThreadDeleteCommand,
330 ThreadMetaUpdateCommand,
331 ThreadTurnStartCommand,
332 ThreadTurnInterruptCommand,
333 ThreadApprovalRespondCommand,
334 ThreadCheckpointRevertCommand,
335 ThreadSessionStopCommand,
336]);
337export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type;
338​
339export const ThreadSessionSetCommand = Schema.Struct({
340 type: Schema.Literal("thread.session.set"),
341 commandId: CommandId,
342 threadId: ThreadId,
343 session: OrchestrationSession,
344 createdAt: IsoDateTime,
345});
346​
347export const ThreadMessageAssistantDeltaCommand = Schema.Struct({
348 type: Schema.Literal("thread.message.assistant.delta"),
349 commandId: CommandId,
350 threadId: ThreadId,
351 messageId: MessageId,
352 delta: Schema.String,
353 turnId: Schema.optional(TurnId),
354 createdAt: IsoDateTime,
355});
356​
357export const ThreadMessageAssistantCompleteCommand = Schema.Struct({
358 type: Schema.Literal("thread.message.assistant.complete"),
359 commandId: CommandId,
360 threadId: ThreadId,
361 messageId: MessageId,
362 turnId: Schema.optional(TurnId),
363 createdAt: IsoDateTime,
364});
365​
366export const ThreadTurnDiffCompleteCommand = Schema.Struct({
367 type: Schema.Literal("thread.turn.diff.complete"),
368 commandId: CommandId,
369 threadId: ThreadId,
370 turnId: TurnId,
371 completedAt: IsoDateTime,
372 checkpointRef: CheckpointRef,
373 status: OrchestrationCheckpointStatus,
374 files: Schema.Array(OrchestrationCheckpointFile),
375 assistantMessageId: Schema.optional(MessageId),
376 checkpointTurnCount: NonNegativeInt,
377 createdAt: IsoDateTime,
378});
379​
380export const ThreadActivityAppendCommand = Schema.Struct({
381 type: Schema.Literal("thread.activity.append"),
382 commandId: CommandId,
383 threadId: ThreadId,
384 activity: OrchestrationThreadActivity,
385 createdAt: IsoDateTime,
386});
387​
388export const ThreadRevertCompleteCommand = Schema.Struct({
389 type: Schema.Literal("thread.revert.complete"),
390 commandId: CommandId,
391 threadId: ThreadId,
392 turnCount: NonNegativeInt,
393 createdAt: IsoDateTime,
394});
395​
396export const InternalOrchestrationCommand = Schema.Union([
397 ThreadSessionSetCommand,
398 ThreadMessageAssistantDeltaCommand,
399 ThreadMessageAssistantCompleteCommand,
400 ThreadTurnDiffCompleteCommand,
401 ThreadActivityAppendCommand,
402 ThreadRevertCompleteCommand,
403]);
404export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
405​
406export const OrchestrationCommand = Schema.Union([
407 ClientOrchestrationCommand,
408 InternalOrchestrationCommand,
409]);
410export type OrchestrationCommand = typeof OrchestrationCommand.Type;
411​
412export const OrchestrationEventType = Schema.Literals([
413 "project.created",
414 "project.meta-updated",
415 "project.deleted",
416 "thread.created",
417 "thread.deleted",
418 "thread.meta-updated",
419 "thread.message-sent",
420 "thread.turn-start-requested",
421 "thread.turn-interrupt-requested",
422 "thread.approval-response-requested",
423 "thread.checkpoint-revert-requested",
424 "thread.reverted",
425 "thread.session-stop-requested",
426 "thread.session-set",
427 "thread.turn-diff-completed",
428 "thread.activity-appended",
429]);
430export type OrchestrationEventType = typeof OrchestrationEventType.Type;
431​
432export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
433export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
434export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
435​
436export const ProjectCreatedPayload = Schema.Struct({
437 projectId: ProjectId,
438 title: TrimmedNonEmptyString,
439 workspaceRoot: TrimmedNonEmptyString,
440 defaultModel: Schema.NullOr(TrimmedNonEmptyString),
441 scripts: Schema.Array(ProjectScript),
442 createdAt: IsoDateTime,
443 updatedAt: IsoDateTime,
444});
445​
446export const ProjectMetaUpdatedPayload = Schema.Struct({
447 projectId: ProjectId,
448 title: Schema.optional(TrimmedNonEmptyString),
449 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
450 defaultModel: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
451 scripts: Schema.optional(Schema.Array(ProjectScript)),
452 updatedAt: IsoDateTime,
453});
454​
455export const ProjectDeletedPayload = Schema.Struct({
456 projectId: ProjectId,
457 deletedAt: IsoDateTime,
458});
459​
460export const ThreadCreatedPayload = Schema.Struct({
461 threadId: ThreadId,
462 projectId: ProjectId,
463 title: TrimmedNonEmptyString,
464 model: TrimmedNonEmptyString,
465 branch: Schema.NullOr(TrimmedNonEmptyString),
466 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
467 createdAt: IsoDateTime,
468 updatedAt: IsoDateTime,
469});
470​
471export const ThreadDeletedPayload = Schema.Struct({
472 threadId: ThreadId,
473 deletedAt: IsoDateTime,
474});
475​
476export const ThreadMetaUpdatedPayload = Schema.Struct({
477 threadId: ThreadId,
478 title: Schema.optional(TrimmedNonEmptyString),
479 model: Schema.optional(TrimmedNonEmptyString),
480 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
481 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
482 updatedAt: IsoDateTime,
483});
484​
485export const ThreadMessageSentPayload = Schema.Struct({
486 threadId: ThreadId,
487 messageId: MessageId,
488 role: OrchestrationMessageRole,
489 text: Schema.String,
490 attachments: Schema.optional(Schema.Array(ChatAttachment)),
491 turnId: Schema.NullOr(TurnId),
492 streaming: Schema.Boolean,
493 createdAt: IsoDateTime,
494 updatedAt: IsoDateTime,
495});
496​
497export const ThreadTurnStartRequestedPayload = Schema.Struct({
498 threadId: ThreadId,
499 messageId: MessageId,
500 model: Schema.optional(TrimmedNonEmptyString),
501 effort: Schema.optional(TrimmedNonEmptyString),
502 assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),
503 approvalPolicy: ProviderApprovalPolicy.pipe(Schema.withDecodingDefault(() => "on-failure")),
504 sandboxMode: ProviderSandboxMode.pipe(Schema.withDecodingDefault(() => "workspace-write")),
505 createdAt: IsoDateTime,
506});
507​
508export const ThreadTurnInterruptRequestedPayload = Schema.Struct({
509 threadId: ThreadId,
510 turnId: Schema.optional(TurnId),
511 createdAt: IsoDateTime,
512});
513​
514export const ThreadApprovalResponseRequestedPayload = Schema.Struct({
515 threadId: ThreadId,
516 requestId: ApprovalRequestId,
517 decision: ProviderApprovalDecision,
518 createdAt: IsoDateTime,
519});
520​
521export const ThreadCheckpointRevertRequestedPayload = Schema.Struct({
522 threadId: ThreadId,
523 turnCount: NonNegativeInt,
524 createdAt: IsoDateTime,
525});
526​
527export const ThreadRevertedPayload = Schema.Struct({
528 threadId: ThreadId,
529 turnCount: NonNegativeInt,
530});
531​
532export const ThreadSessionStopRequestedPayload = Schema.Struct({
533 threadId: ThreadId,
534 createdAt: IsoDateTime,
535});
536​
537export const ThreadSessionSetPayload = Schema.Struct({
538 threadId: ThreadId,
539 session: OrchestrationSession,
540});
541​
542export const ThreadTurnDiffCompletedPayload = Schema.Struct({
543 threadId: ThreadId,
544 turnId: TurnId,
545 checkpointTurnCount: NonNegativeInt,
546 checkpointRef: CheckpointRef,
547 status: OrchestrationCheckpointStatus,
548 files: Schema.Array(OrchestrationCheckpointFile),
549 assistantMessageId: Schema.NullOr(MessageId),
550 completedAt: IsoDateTime,
551});
552​
553export const ThreadActivityAppendedPayload = Schema.Struct({
554 threadId: ThreadId,
555 activity: OrchestrationThreadActivity,
556});
557​
558export const OrchestrationEventMetadata = Schema.Struct({
559 providerSessionId: Schema.optional(ProviderSessionId),
560 providerThreadId: Schema.optional(ProviderThreadId),
561 providerTurnId: Schema.optional(ProviderTurnId),
562 providerItemId: Schema.optional(ProviderItemId),
563 adapterKey: Schema.optional(TrimmedNonEmptyString),
564 requestId: Schema.optional(ApprovalRequestId),
565 ingestedAt: Schema.optional(IsoDateTime),
566});
567export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;
568​
569const EventBaseFields = {
570 sequence: NonNegativeInt,
571 eventId: EventId,
572 aggregateKind: OrchestrationAggregateKind,
573 aggregateId: Schema.Union([ProjectId, ThreadId]),
574 occurredAt: IsoDateTime,
575 commandId: Schema.NullOr(CommandId),
576 causationEventId: Schema.NullOr(EventId),
577 correlationId: Schema.NullOr(CommandId),
578 metadata: OrchestrationEventMetadata,
579} as const;
580​
581const PersistedEventBaseFields = {
582 sequence: NonNegativeInt,
583 eventId: EventId,
584 aggregateKind: OrchestrationAggregateKind,
585 streamId: Schema.Union([ProjectId, ThreadId]),
586 streamVersion: NonNegativeInt,
587 occurredAt: IsoDateTime,
588 commandId: Schema.NullOr(CommandId),
589 causationEventId: Schema.NullOr(EventId),
590 correlationId: Schema.NullOr(CommandId),
591 actorKind: OrchestrationActorKind,
592 metadata: OrchestrationEventMetadata,
593} as const;
594​
595export const OrchestrationEvent = Schema.Union([
596 Schema.Struct({
597 ...EventBaseFields,
598 type: Schema.Literal("project.created"),
599 payload: ProjectCreatedPayload,
600 }),
601 Schema.Struct({
602 ...EventBaseFields,
603 type: Schema.Literal("project.meta-updated"),
604 payload: ProjectMetaUpdatedPayload,
605 }),
606 Schema.Struct({
607 ...EventBaseFields,
608 type: Schema.Literal("project.deleted"),
609 payload: ProjectDeletedPayload,
610 }),
611 Schema.Struct({
612 ...EventBaseFields,
613 type: Schema.Literal("thread.created"),
614 payload: ThreadCreatedPayload,
615 }),
616 Schema.Struct({
617 ...EventBaseFields,
618 type: Schema.Literal("thread.deleted"),
619 payload: ThreadDeletedPayload,
620 }),
621 Schema.Struct({
622 ...EventBaseFields,
623 type: Schema.Literal("thread.meta-updated"),
624 payload: ThreadMetaUpdatedPayload,
625 }),
626 Schema.Struct({
627 ...EventBaseFields,
628 type: Schema.Literal("thread.message-sent"),
629 payload: ThreadMessageSentPayload,
630 }),
631 Schema.Struct({
632 ...EventBaseFields,
633 type: Schema.Literal("thread.turn-start-requested"),
634 payload: ThreadTurnStartRequestedPayload,
635 }),
636 Schema.Struct({
637 ...EventBaseFields,
638 type: Schema.Literal("thread.turn-interrupt-requested"),
639 payload: ThreadTurnInterruptRequestedPayload,
640 }),
641 Schema.Struct({
642 ...EventBaseFields,
643 type: Schema.Literal("thread.approval-response-requested"),
644 payload: ThreadApprovalResponseRequestedPayload,
645 }),
646 Schema.Struct({
647 ...EventBaseFields,
648 type: Schema.Literal("thread.checkpoint-revert-requested"),
649 payload: ThreadCheckpointRevertRequestedPayload,
650 }),
651 Schema.Struct({
652 ...EventBaseFields,
653 type: Schema.Literal("thread.reverted"),
654 payload: ThreadRevertedPayload,
655 }),
656 Schema.Struct({
657 ...EventBaseFields,
658 type: Schema.Literal("thread.session-stop-requested"),
659 payload: ThreadSessionStopRequestedPayload,
660 }),
661 Schema.Struct({
662 ...EventBaseFields,
663 type: Schema.Literal("thread.session-set"),
664 payload: ThreadSessionSetPayload,
665 }),
666 Schema.Struct({
667 ...EventBaseFields,
668 type: Schema.Literal("thread.turn-diff-completed"),
669 payload: ThreadTurnDiffCompletedPayload,
670 }),
671 Schema.Struct({
672 ...EventBaseFields,
673 type: Schema.Literal("thread.activity-appended"),
674 payload: ThreadActivityAppendedPayload,
675 }),
676]);
677export type OrchestrationEvent = typeof OrchestrationEvent.Type;
678​
679export const OrchestrationPersistedEvent = Schema.Union([
680 Schema.Struct({
681 ...PersistedEventBaseFields,
682 eventType: Schema.Literal("project.created"),
683 payload: ProjectCreatedPayload,
684 }),
685 Schema.Struct({
686 ...PersistedEventBaseFields,
687 eventType: Schema.Literal("project.meta-updated"),
688 payload: ProjectMetaUpdatedPayload,
689 }),
690 Schema.Struct({
691 ...PersistedEventBaseFields,
692 eventType: Schema.Literal("project.deleted"),
693 payload: ProjectDeletedPayload,
694 }),
695 Schema.Struct({
696 ...PersistedEventBaseFields,
697 eventType: Schema.Literal("thread.created"),
698 payload: ThreadCreatedPayload,
699 }),
700 Schema.Struct({
701 ...PersistedEventBaseFields,
702 eventType: Schema.Literal("thread.deleted"),
703 payload: ThreadDeletedPayload,
704 }),
705 Schema.Struct({
706 ...PersistedEventBaseFields,
707 eventType: Schema.Literal("thread.meta-updated"),
708 payload: ThreadMetaUpdatedPayload,
709 }),
710 Schema.Struct({
711 ...PersistedEventBaseFields,
712 eventType: Schema.Literal("thread.message-sent"),
713 payload: ThreadMessageSentPayload,
714 }),
715 Schema.Struct({
716 ...PersistedEventBaseFields,
717 eventType: Schema.Literal("thread.turn-start-requested"),
718 payload: ThreadTurnStartRequestedPayload,
719 }),
720 Schema.Struct({
721 ...PersistedEventBaseFields,
722 eventType: Schema.Literal("thread.turn-interrupt-requested"),
723 payload: ThreadTurnInterruptRequestedPayload,
724 }),
725 Schema.Struct({
726 ...PersistedEventBaseFields,
727 eventType: Schema.Literal("thread.approval-response-requested"),
728 payload: ThreadApprovalResponseRequestedPayload,
729 }),
730 Schema.Struct({
731 ...PersistedEventBaseFields,
732 eventType: Schema.Literal("thread.checkpoint-revert-requested"),
733 payload: ThreadCheckpointRevertRequestedPayload,
734 }),
735 Schema.Struct({
736 ...PersistedEventBaseFields,
737 eventType: Schema.Literal("thread.reverted"),
738 payload: ThreadRevertedPayload,
739 }),
740 Schema.Struct({
741 ...PersistedEventBaseFields,
742 eventType: Schema.Literal("thread.session-stop-requested"),
743 payload: ThreadSessionStopRequestedPayload,
744 }),
745 Schema.Struct({
746 ...PersistedEventBaseFields,
747 eventType: Schema.Literal("thread.session-set"),
748 payload: ThreadSessionSetPayload,
749 }),
750 Schema.Struct({
751 ...PersistedEventBaseFields,
752 eventType: Schema.Literal("thread.turn-diff-completed"),
753 payload: ThreadTurnDiffCompletedPayload,
754 }),
755 Schema.Struct({
756 ...PersistedEventBaseFields,
757 eventType: Schema.Literal("thread.activity-appended"),
758 payload: ThreadActivityAppendedPayload,
759 }),
760]);
761export type OrchestrationPersistedEvent = typeof OrchestrationPersistedEvent.Type;
762​
763export const OrchestrationCommandReceiptStatus = Schema.Literals(["accepted", "rejected"]);
764export type OrchestrationCommandReceiptStatus = typeof OrchestrationCommandReceiptStatus.Type;
765​
766export const TurnCountRange = Schema.Struct({
767 fromTurnCount: NonNegativeInt,
768 toTurnCount: NonNegativeInt,
769}).check(
770 Schema.makeFilter(
771 (input) =>
772 input.fromTurnCount <= input.toTurnCount ||
773 new SchemaIssue.InvalidValue(Option.some(input.fromTurnCount), {
774 message: "fromTurnCount must be less than or equal to toTurnCount",
775 }),
776 { identifier: "OrchestrationTurnDiffRange" },
777 ),
778);
779​
780export const ThreadTurnDiff = TurnCountRange.mapFields(
781 Struct.assign({
782 threadId: ThreadId,
783 diff: Schema.String,
784 }),
785 { unsafePreserveChecks: true },
786);
787​
788export const ProviderSessionRuntimeStatus = Schema.Literals([
789 "starting",
790 "running",
791 "stopped",
792 "error",
793]);
794export type ProviderSessionRuntimeStatus = typeof ProviderSessionRuntimeStatus.Type;
795​
796export const ProjectionThreadTurnStatus = Schema.Literals([
797 "running",
798 "completed",
799 "interrupted",
800 "error",
801]);
802export type ProjectionThreadTurnStatus = typeof ProjectionThreadTurnStatus.Type;
803​
804export const ProjectionCheckpointRow = Schema.Struct({
805 threadId: ThreadId,
806 turnId: TurnId,
807 checkpointTurnCount: NonNegativeInt,
808 checkpointRef: CheckpointRef,
809 status: OrchestrationCheckpointStatus,
810 files: Schema.Array(OrchestrationCheckpointFile),
811 assistantMessageId: Schema.NullOr(MessageId),
812 completedAt: IsoDateTime,
813});
814export type ProjectionCheckpointRow = typeof ProjectionCheckpointRow.Type;
815​
816export const ProjectionPendingApprovalStatus = Schema.Literals(["pending", "resolved"]);
817export type ProjectionPendingApprovalStatus = typeof ProjectionPendingApprovalStatus.Type;
818​
819export const ProjectionPendingApprovalDecision = Schema.NullOr(ProviderApprovalDecision);
820export type ProjectionPendingApprovalDecision = typeof ProjectionPendingApprovalDecision.Type;
821​
822export const DispatchResult = Schema.Struct({
823 sequence: NonNegativeInt,
824});
825export type DispatchResult = typeof DispatchResult.Type;
826​
827export const OrchestrationGetSnapshotInput = Schema.Struct({});
828export type OrchestrationGetSnapshotInput = typeof OrchestrationGetSnapshotInput.Type;
829export const OrchestrationGetSnapshotResult = OrchestrationReadModel;
830export type OrchestrationGetSnapshotResult = typeof OrchestrationGetSnapshotResult.Type;
831​
832export const OrchestrationGetTurnDiffInput = TurnCountRange.mapFields(
833 Struct.assign({ threadId: ThreadId }),
834 { unsafePreserveChecks: true },
835);
836export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput.Type;
837​
838export const OrchestrationGetTurnDiffResult = ThreadTurnDiff;
839export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type;
840​
841export const OrchestrationGetFullThreadDiffInput = Schema.Struct({
842 threadId: ThreadId,
843 toTurnCount: NonNegativeInt,
844});
845export type OrchestrationGetFullThreadDiffInput = typeof OrchestrationGetFullThreadDiffInput.Type;
846​
847export const OrchestrationGetFullThreadDiffResult = ThreadTurnDiff;
848export type OrchestrationGetFullThreadDiffResult = typeof OrchestrationGetFullThreadDiffResult.Type;
849​
850export const OrchestrationReplayEventsInput = Schema.Struct({
851 fromSequenceExclusive: NonNegativeInt,
852});
853export type OrchestrationReplayEventsInput = typeof OrchestrationReplayEventsInput.Type;
854​
855export const OrchestrationReplayEventsResult = Schema.Array(OrchestrationEvent);
856export type OrchestrationReplayEventsResult = typeof OrchestrationReplayEventsResult.Type;
857​
858export const OrchestrationRpcSchemas = {
859 getSnapshot: {
860 input: OrchestrationGetSnapshotInput,
861 output: OrchestrationGetSnapshotResult,
862 },
863 dispatchCommand: {
864 input: ClientOrchestrationCommand,
865 output: DispatchResult,
866 },
867 getTurnDiff: {
868 input: OrchestrationGetTurnDiffInput,
869 output: OrchestrationGetTurnDiffResult,
870 },
871 getFullThreadDiff: {
872 input: OrchestrationGetFullThreadDiffInput,
873 output: OrchestrationGetFullThreadDiffResult,
874 },
875 replayEvents: {
876 input: OrchestrationReplayEventsInput,
877 output: OrchestrationReplayEventsResult,
878 },
879} as const;