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

orchestration.ts

1,110 lines35.5 KB

1import { Option, Schema, SchemaIssue, Struct } from "effect";
2import { ClaudeModelOptions, CodexModelOptions } from "./model";
3import { RepositoryIdentity } from "./environment";
4import {
5 ApprovalRequestId,
6 CheckpointRef,
7 CommandId,
8 EventId,
9 IsoDateTime,
10 MessageId,
11 NonNegativeInt,
12 ProjectId,
13 ProviderItemId,
14 ThreadId,
15 TrimmedNonEmptyString,
16 TurnId,
17} from "./baseSchemas";
18​
19export const ORCHESTRATION_WS_METHODS = {
20 getSnapshot: "orchestration.getSnapshot",
21 dispatchCommand: "orchestration.dispatchCommand",
22 getTurnDiff: "orchestration.getTurnDiff",
23 getFullThreadDiff: "orchestration.getFullThreadDiff",
24 replayEvents: "orchestration.replayEvents",
25} as const;
26​
27export const ProviderKind = Schema.Literals(["codex", "claudeAgent"]);
28export type ProviderKind = typeof ProviderKind.Type;
29export const ProviderApprovalPolicy = Schema.Literals([
30 "untrusted",
31 "on-failure",
32 "on-request",
33 "never",
34]);
35export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;
36export const ProviderSandboxMode = Schema.Literals([
37 "read-only",
38 "workspace-write",
39 "danger-full-access",
40]);
41export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;
42​
43export const DEFAULT_PROVIDER_KIND: ProviderKind = "codex";
44​
45export const CodexModelSelection = Schema.Struct({
46 provider: Schema.Literal("codex"),
47 model: TrimmedNonEmptyString,
48 options: Schema.optionalKey(CodexModelOptions),
49});
50export type CodexModelSelection = typeof CodexModelSelection.Type;
51​
52export const ClaudeModelSelection = Schema.Struct({
53 provider: Schema.Literal("claudeAgent"),
54 model: TrimmedNonEmptyString,
55 options: Schema.optionalKey(ClaudeModelOptions),
56});
57export type ClaudeModelSelection = typeof ClaudeModelSelection.Type;
58​
59export const ModelSelection = Schema.Union([CodexModelSelection, ClaudeModelSelection]);
60export type ModelSelection = typeof ModelSelection.Type;
61​
62export const RuntimeMode = Schema.Literals([
63 "approval-required",
64 "auto-accept-edits",
65 "full-access",
66]);
67export type RuntimeMode = typeof RuntimeMode.Type;
68export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
69export const ProviderInteractionMode = Schema.Literals(["default", "plan"]);
70export type ProviderInteractionMode = typeof ProviderInteractionMode.Type;
71export const DEFAULT_PROVIDER_INTERACTION_MODE: ProviderInteractionMode = "default";
72export const ProviderRequestKind = Schema.Literals(["command", "file-read", "file-change"]);
73export type ProviderRequestKind = typeof ProviderRequestKind.Type;
74export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);
75export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;
76export const ProviderApprovalDecision = Schema.Literals([
77 "accept",
78 "acceptForSession",
79 "decline",
80 "cancel",
81]);
82export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;
83export const ProviderUserInputAnswers = Schema.Record(Schema.String, Schema.Unknown);
84export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type;
85​
86export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
87export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
88export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
89const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
90const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
91// Correlation id is command id by design in this model.
92export const CorrelationId = CommandId;
93export type CorrelationId = typeof CorrelationId.Type;
94​
95const ChatAttachmentId = TrimmedNonEmptyString.check(
96 Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
97 Schema.isPattern(/^[a-z0-9_-]+$/i),
98);
99export type ChatAttachmentId = typeof ChatAttachmentId.Type;
100​
101export const ChatImageAttachment = Schema.Struct({
102 type: Schema.Literal("image"),
103 id: ChatAttachmentId,
104 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
105 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
106 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
107});
108export type ChatImageAttachment = typeof ChatImageAttachment.Type;
109​
110const UploadChatImageAttachment = Schema.Struct({
111 type: Schema.Literal("image"),
112 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
113 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
114 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
115 dataUrl: TrimmedNonEmptyString.check(
116 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
117 ),
118});
119export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
120​
121export const ChatAttachment = Schema.Union([ChatImageAttachment]);
122export type ChatAttachment = typeof ChatAttachment.Type;
123const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
124export type UploadChatAttachment = typeof UploadChatAttachment.Type;
125​
126export const ProjectScriptIcon = Schema.Literals([
127 "play",
128 "test",
129 "lint",
130 "configure",
131 "build",
132 "debug",
133]);
134export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
135​
136export const ProjectScript = Schema.Struct({
137 id: TrimmedNonEmptyString,
138 name: TrimmedNonEmptyString,
139 command: TrimmedNonEmptyString,
140 icon: ProjectScriptIcon,
141 runOnWorktreeCreate: Schema.Boolean,
142});
143export type ProjectScript = typeof ProjectScript.Type;
144​
145export const OrchestrationProject = Schema.Struct({
146 id: ProjectId,
147 title: TrimmedNonEmptyString,
148 workspaceRoot: TrimmedNonEmptyString,
149 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
150 defaultModelSelection: Schema.NullOr(ModelSelection),
151 scripts: Schema.Array(ProjectScript),
152 createdAt: IsoDateTime,
153 updatedAt: IsoDateTime,
154 deletedAt: Schema.NullOr(IsoDateTime),
155});
156export type OrchestrationProject = typeof OrchestrationProject.Type;
157​
158export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);
159export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type;
160​
161export const OrchestrationMessage = Schema.Struct({
162 id: MessageId,
163 role: OrchestrationMessageRole,
164 text: Schema.String,
165 attachments: Schema.optional(Schema.Array(ChatAttachment)),
166 turnId: Schema.NullOr(TurnId),
167 streaming: Schema.Boolean,
168 createdAt: IsoDateTime,
169 updatedAt: IsoDateTime,
170});
171export type OrchestrationMessage = typeof OrchestrationMessage.Type;
172​
173export const OrchestrationProposedPlanId = TrimmedNonEmptyString;
174export type OrchestrationProposedPlanId = typeof OrchestrationProposedPlanId.Type;
175​
176export const OrchestrationProposedPlan = Schema.Struct({
177 id: OrchestrationProposedPlanId,
178 turnId: Schema.NullOr(TurnId),
179 planMarkdown: TrimmedNonEmptyString,
180 implementedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(() => null)),
181 implementationThreadId: Schema.NullOr(ThreadId).pipe(Schema.withDecodingDefault(() => null)),
182 createdAt: IsoDateTime,
183 updatedAt: IsoDateTime,
184});
185export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type;
186​
187const SourceProposedPlanReference = Schema.Struct({
188 threadId: ThreadId,
189 planId: OrchestrationProposedPlanId,
190});
191​
192export const OrchestrationSessionStatus = Schema.Literals([
193 "idle",
194 "starting",
195 "running",
196 "ready",
197 "interrupted",
198 "stopped",
199 "error",
200]);
201export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type;
202​
203export const OrchestrationSession = Schema.Struct({
204 threadId: ThreadId,
205 status: OrchestrationSessionStatus,
206 providerName: Schema.NullOr(TrimmedNonEmptyString),
207 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
208 activeTurnId: Schema.NullOr(TurnId),
209 lastError: Schema.NullOr(TrimmedNonEmptyString),
210 updatedAt: IsoDateTime,
211});
212export type OrchestrationSession = typeof OrchestrationSession.Type;
213​
214export const OrchestrationCheckpointFile = Schema.Struct({
215 path: TrimmedNonEmptyString,
216 kind: TrimmedNonEmptyString,
217 additions: NonNegativeInt,
218 deletions: NonNegativeInt,
219});
220export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type;
221​
222export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);
223export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type;
224​
225export const OrchestrationCheckpointSummary = Schema.Struct({
226 turnId: TurnId,
227 checkpointTurnCount: NonNegativeInt,
228 checkpointRef: CheckpointRef,
229 status: OrchestrationCheckpointStatus,
230 files: Schema.Array(OrchestrationCheckpointFile),
231 assistantMessageId: Schema.NullOr(MessageId),
232 completedAt: IsoDateTime,
233});
234export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type;
235​
236export const OrchestrationThreadActivityTone = Schema.Literals([
237 "info",
238 "tool",
239 "approval",
240 "error",
241]);
242export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type;
243​
244export const OrchestrationThreadActivity = Schema.Struct({
245 id: EventId,
246 tone: OrchestrationThreadActivityTone,
247 kind: TrimmedNonEmptyString,
248 summary: TrimmedNonEmptyString,
249 payload: Schema.Unknown,
250 turnId: Schema.NullOr(TurnId),
251 sequence: Schema.optional(NonNegativeInt),
252 createdAt: IsoDateTime,
253});
254export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type;
255​
256const OrchestrationLatestTurnState = Schema.Literals([
257 "running",
258 "interrupted",
259 "completed",
260 "error",
261]);
262export type OrchestrationLatestTurnState = typeof OrchestrationLatestTurnState.Type;
263​
264export const OrchestrationLatestTurn = Schema.Struct({
265 turnId: TurnId,
266 state: OrchestrationLatestTurnState,
267 requestedAt: IsoDateTime,
268 startedAt: Schema.NullOr(IsoDateTime),
269 completedAt: Schema.NullOr(IsoDateTime),
270 assistantMessageId: Schema.NullOr(MessageId),
271 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
272});
273export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type;
274​
275export const OrchestrationThread = Schema.Struct({
276 id: ThreadId,
277 projectId: ProjectId,
278 title: TrimmedNonEmptyString,
279 modelSelection: ModelSelection,
280 runtimeMode: RuntimeMode,
281 interactionMode: ProviderInteractionMode.pipe(
282 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
283 ),
284 branch: Schema.NullOr(TrimmedNonEmptyString),
285 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
286 latestTurn: Schema.NullOr(OrchestrationLatestTurn),
287 createdAt: IsoDateTime,
288 updatedAt: IsoDateTime,
289 archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(() => null)),
290 deletedAt: Schema.NullOr(IsoDateTime),
291 messages: Schema.Array(OrchestrationMessage),
292 proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe(Schema.withDecodingDefault(() => [])),
293 activities: Schema.Array(OrchestrationThreadActivity),
294 checkpoints: Schema.Array(OrchestrationCheckpointSummary),
295 session: Schema.NullOr(OrchestrationSession),
296});
297export type OrchestrationThread = typeof OrchestrationThread.Type;
298​
299export const OrchestrationReadModel = Schema.Struct({
300 snapshotSequence: NonNegativeInt,
301 projects: Schema.Array(OrchestrationProject),
302 threads: Schema.Array(OrchestrationThread),
303 updatedAt: IsoDateTime,
304});
305export type OrchestrationReadModel = typeof OrchestrationReadModel.Type;
306​
307export const ProjectCreateCommand = Schema.Struct({
308 type: Schema.Literal("project.create"),
309 commandId: CommandId,
310 projectId: ProjectId,
311 title: TrimmedNonEmptyString,
312 workspaceRoot: TrimmedNonEmptyString,
313 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
314 createdAt: IsoDateTime,
315});
316​
317const ProjectMetaUpdateCommand = Schema.Struct({
318 type: Schema.Literal("project.meta.update"),
319 commandId: CommandId,
320 projectId: ProjectId,
321 title: Schema.optional(TrimmedNonEmptyString),
322 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
323 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
324 scripts: Schema.optional(Schema.Array(ProjectScript)),
325});
326​
327const ProjectDeleteCommand = Schema.Struct({
328 type: Schema.Literal("project.delete"),
329 commandId: CommandId,
330 projectId: ProjectId,
331});
332​
333const ThreadCreateCommand = Schema.Struct({
334 type: Schema.Literal("thread.create"),
335 commandId: CommandId,
336 threadId: ThreadId,
337 projectId: ProjectId,
338 title: TrimmedNonEmptyString,
339 modelSelection: ModelSelection,
340 runtimeMode: RuntimeMode,
341 interactionMode: ProviderInteractionMode.pipe(
342 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
343 ),
344 branch: Schema.NullOr(TrimmedNonEmptyString),
345 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
346 createdAt: IsoDateTime,
347});
348​
349const ThreadDeleteCommand = Schema.Struct({
350 type: Schema.Literal("thread.delete"),
351 commandId: CommandId,
352 threadId: ThreadId,
353});
354​
355const ThreadArchiveCommand = Schema.Struct({
356 type: Schema.Literal("thread.archive"),
357 commandId: CommandId,
358 threadId: ThreadId,
359});
360​
361const ThreadUnarchiveCommand = Schema.Struct({
362 type: Schema.Literal("thread.unarchive"),
363 commandId: CommandId,
364 threadId: ThreadId,
365});
366​
367const ThreadMetaUpdateCommand = Schema.Struct({
368 type: Schema.Literal("thread.meta.update"),
369 commandId: CommandId,
370 threadId: ThreadId,
371 title: Schema.optional(TrimmedNonEmptyString),
372 modelSelection: Schema.optional(ModelSelection),
373 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
374 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
375});
376​
377const ThreadRuntimeModeSetCommand = Schema.Struct({
378 type: Schema.Literal("thread.runtime-mode.set"),
379 commandId: CommandId,
380 threadId: ThreadId,
381 runtimeMode: RuntimeMode,
382 createdAt: IsoDateTime,
383});
384​
385const ThreadInteractionModeSetCommand = Schema.Struct({
386 type: Schema.Literal("thread.interaction-mode.set"),
387 commandId: CommandId,
388 threadId: ThreadId,
389 interactionMode: ProviderInteractionMode,
390 createdAt: IsoDateTime,
391});
392​
393const ThreadTurnStartBootstrapCreateThread = Schema.Struct({
394 projectId: ProjectId,
395 title: TrimmedNonEmptyString,
396 modelSelection: ModelSelection,
397 runtimeMode: RuntimeMode,
398 interactionMode: ProviderInteractionMode,
399 branch: Schema.NullOr(TrimmedNonEmptyString),
400 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
401 createdAt: IsoDateTime,
402});
403​
404const ThreadTurnStartBootstrapPrepareWorktree = Schema.Struct({
405 projectCwd: TrimmedNonEmptyString,
406 baseBranch: TrimmedNonEmptyString,
407 branch: Schema.optional(TrimmedNonEmptyString),
408});
409​
410const ThreadTurnStartBootstrap = Schema.Struct({
411 createThread: Schema.optional(ThreadTurnStartBootstrapCreateThread),
412 prepareWorktree: Schema.optional(ThreadTurnStartBootstrapPrepareWorktree),
413 runSetupScript: Schema.optional(Schema.Boolean),
414});
415​
416export type ThreadTurnStartBootstrap = typeof ThreadTurnStartBootstrap.Type;
417​
418export const ThreadTurnStartCommand = Schema.Struct({
419 type: Schema.Literal("thread.turn.start"),
420 commandId: CommandId,
421 threadId: ThreadId,
422 message: Schema.Struct({
423 messageId: MessageId,
424 role: Schema.Literal("user"),
425 text: Schema.String,
426 attachments: Schema.Array(ChatAttachment),
427 }),
428 modelSelection: Schema.optional(ModelSelection),
429 titleSeed: Schema.optional(TrimmedNonEmptyString),
430 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
431 interactionMode: ProviderInteractionMode.pipe(
432 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
433 ),
434 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
435 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
436 createdAt: IsoDateTime,
437});
438​
439const ClientThreadTurnStartCommand = Schema.Struct({
440 type: Schema.Literal("thread.turn.start"),
441 commandId: CommandId,
442 threadId: ThreadId,
443 message: Schema.Struct({
444 messageId: MessageId,
445 role: Schema.Literal("user"),
446 text: Schema.String,
447 attachments: Schema.Array(UploadChatAttachment),
448 }),
449 modelSelection: Schema.optional(ModelSelection),
450 titleSeed: Schema.optional(TrimmedNonEmptyString),
451 runtimeMode: RuntimeMode,
452 interactionMode: ProviderInteractionMode,
453 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
454 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
455 createdAt: IsoDateTime,
456});
457​
458const ThreadTurnInterruptCommand = Schema.Struct({
459 type: Schema.Literal("thread.turn.interrupt"),
460 commandId: CommandId,
461 threadId: ThreadId,
462 turnId: Schema.optional(TurnId),
463 createdAt: IsoDateTime,
464});
465​
466const ThreadApprovalRespondCommand = Schema.Struct({
467 type: Schema.Literal("thread.approval.respond"),
468 commandId: CommandId,
469 threadId: ThreadId,
470 requestId: ApprovalRequestId,
471 decision: ProviderApprovalDecision,
472 createdAt: IsoDateTime,
473});
474​
475const ThreadUserInputRespondCommand = Schema.Struct({
476 type: Schema.Literal("thread.user-input.respond"),
477 commandId: CommandId,
478 threadId: ThreadId,
479 requestId: ApprovalRequestId,
480 answers: ProviderUserInputAnswers,
481 createdAt: IsoDateTime,
482});
483​
484const ThreadCheckpointRevertCommand = Schema.Struct({
485 type: Schema.Literal("thread.checkpoint.revert"),
486 commandId: CommandId,
487 threadId: ThreadId,
488 turnCount: NonNegativeInt,
489 createdAt: IsoDateTime,
490});
491​
492const ThreadSessionStopCommand = Schema.Struct({
493 type: Schema.Literal("thread.session.stop"),
494 commandId: CommandId,
495 threadId: ThreadId,
496 createdAt: IsoDateTime,
497});
498​
499const DispatchableClientOrchestrationCommand = Schema.Union([
500 ProjectCreateCommand,
501 ProjectMetaUpdateCommand,
502 ProjectDeleteCommand,
503 ThreadCreateCommand,
504 ThreadDeleteCommand,
505 ThreadArchiveCommand,
506 ThreadUnarchiveCommand,
507 ThreadMetaUpdateCommand,
508 ThreadRuntimeModeSetCommand,
509 ThreadInteractionModeSetCommand,
510 ThreadTurnStartCommand,
511 ThreadTurnInterruptCommand,
512 ThreadApprovalRespondCommand,
513 ThreadUserInputRespondCommand,
514 ThreadCheckpointRevertCommand,
515 ThreadSessionStopCommand,
516]);
517export type DispatchableClientOrchestrationCommand =
518 typeof DispatchableClientOrchestrationCommand.Type;
519​
520export const ClientOrchestrationCommand = Schema.Union([
521 ProjectCreateCommand,
522 ProjectMetaUpdateCommand,
523 ProjectDeleteCommand,
524 ThreadCreateCommand,
525 ThreadDeleteCommand,
526 ThreadArchiveCommand,
527 ThreadUnarchiveCommand,
528 ThreadMetaUpdateCommand,
529 ThreadRuntimeModeSetCommand,
530 ThreadInteractionModeSetCommand,
531 ClientThreadTurnStartCommand,
532 ThreadTurnInterruptCommand,
533 ThreadApprovalRespondCommand,
534 ThreadUserInputRespondCommand,
535 ThreadCheckpointRevertCommand,
536 ThreadSessionStopCommand,
537]);
538export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type;
539​
540const ThreadSessionSetCommand = Schema.Struct({
541 type: Schema.Literal("thread.session.set"),
542 commandId: CommandId,
543 threadId: ThreadId,
544 session: OrchestrationSession,
545 createdAt: IsoDateTime,
546});
547​
548const ThreadMessageAssistantDeltaCommand = Schema.Struct({
549 type: Schema.Literal("thread.message.assistant.delta"),
550 commandId: CommandId,
551 threadId: ThreadId,
552 messageId: MessageId,
553 delta: Schema.String,
554 turnId: Schema.optional(TurnId),
555 createdAt: IsoDateTime,
556});
557​
558const ThreadMessageAssistantCompleteCommand = Schema.Struct({
559 type: Schema.Literal("thread.message.assistant.complete"),
560 commandId: CommandId,
561 threadId: ThreadId,
562 messageId: MessageId,
563 turnId: Schema.optional(TurnId),
564 createdAt: IsoDateTime,
565});
566​
567const ThreadProposedPlanUpsertCommand = Schema.Struct({
568 type: Schema.Literal("thread.proposed-plan.upsert"),
569 commandId: CommandId,
570 threadId: ThreadId,
571 proposedPlan: OrchestrationProposedPlan,
572 createdAt: IsoDateTime,
573});
574​
575const ThreadTurnDiffCompleteCommand = Schema.Struct({
576 type: Schema.Literal("thread.turn.diff.complete"),
577 commandId: CommandId,
578 threadId: ThreadId,
579 turnId: TurnId,
580 completedAt: IsoDateTime,
581 checkpointRef: CheckpointRef,
582 status: OrchestrationCheckpointStatus,
583 files: Schema.Array(OrchestrationCheckpointFile),
584 assistantMessageId: Schema.optional(MessageId),
585 checkpointTurnCount: NonNegativeInt,
586 createdAt: IsoDateTime,
587});
588​
589const ThreadActivityAppendCommand = Schema.Struct({
590 type: Schema.Literal("thread.activity.append"),
591 commandId: CommandId,
592 threadId: ThreadId,
593 activity: OrchestrationThreadActivity,
594 createdAt: IsoDateTime,
595});
596​
597const ThreadRevertCompleteCommand = Schema.Struct({
598 type: Schema.Literal("thread.revert.complete"),
599 commandId: CommandId,
600 threadId: ThreadId,
601 turnCount: NonNegativeInt,
602 createdAt: IsoDateTime,
603});
604​
605const InternalOrchestrationCommand = Schema.Union([
606 ThreadSessionSetCommand,
607 ThreadMessageAssistantDeltaCommand,
608 ThreadMessageAssistantCompleteCommand,
609 ThreadProposedPlanUpsertCommand,
610 ThreadTurnDiffCompleteCommand,
611 ThreadActivityAppendCommand,
612 ThreadRevertCompleteCommand,
613]);
614export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
615​
616export const OrchestrationCommand = Schema.Union([
617 DispatchableClientOrchestrationCommand,
618 InternalOrchestrationCommand,
619]);
620export type OrchestrationCommand = typeof OrchestrationCommand.Type;
621​
622export const OrchestrationEventType = Schema.Literals([
623 "project.created",
624 "project.meta-updated",
625 "project.deleted",
626 "thread.created",
627 "thread.deleted",
628 "thread.archived",
629 "thread.unarchived",
630 "thread.meta-updated",
631 "thread.runtime-mode-set",
632 "thread.interaction-mode-set",
633 "thread.message-sent",
634 "thread.turn-start-requested",
635 "thread.turn-interrupt-requested",
636 "thread.approval-response-requested",
637 "thread.user-input-response-requested",
638 "thread.checkpoint-revert-requested",
639 "thread.reverted",
640 "thread.session-stop-requested",
641 "thread.session-set",
642 "thread.proposed-plan-upserted",
643 "thread.turn-diff-completed",
644 "thread.activity-appended",
645]);
646export type OrchestrationEventType = typeof OrchestrationEventType.Type;
647​
648export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
649export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
650export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
651​
652export const ProjectCreatedPayload = Schema.Struct({
653 projectId: ProjectId,
654 title: TrimmedNonEmptyString,
655 workspaceRoot: TrimmedNonEmptyString,
656 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
657 defaultModelSelection: Schema.NullOr(ModelSelection),
658 scripts: Schema.Array(ProjectScript),
659 createdAt: IsoDateTime,
660 updatedAt: IsoDateTime,
661});
662​
663export const ProjectMetaUpdatedPayload = Schema.Struct({
664 projectId: ProjectId,
665 title: Schema.optional(TrimmedNonEmptyString),
666 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
667 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
668 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
669 scripts: Schema.optional(Schema.Array(ProjectScript)),
670 updatedAt: IsoDateTime,
671});
672​
673export const ProjectDeletedPayload = Schema.Struct({
674 projectId: ProjectId,
675 deletedAt: IsoDateTime,
676});
677​
678export const ThreadCreatedPayload = Schema.Struct({
679 threadId: ThreadId,
680 projectId: ProjectId,
681 title: TrimmedNonEmptyString,
682 modelSelection: ModelSelection,
683 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
684 interactionMode: ProviderInteractionMode.pipe(
685 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
686 ),
687 branch: Schema.NullOr(TrimmedNonEmptyString),
688 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
689 createdAt: IsoDateTime,
690 updatedAt: IsoDateTime,
691});
692​
693export const ThreadDeletedPayload = Schema.Struct({
694 threadId: ThreadId,
695 deletedAt: IsoDateTime,
696});
697​
698export const ThreadArchivedPayload = Schema.Struct({
699 threadId: ThreadId,
700 archivedAt: IsoDateTime,
701 updatedAt: IsoDateTime,
702});
703​
704export const ThreadUnarchivedPayload = Schema.Struct({
705 threadId: ThreadId,
706 updatedAt: IsoDateTime,
707});
708​
709export const ThreadMetaUpdatedPayload = Schema.Struct({
710 threadId: ThreadId,
711 title: Schema.optional(TrimmedNonEmptyString),
712 modelSelection: Schema.optional(ModelSelection),
713 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
714 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
715 updatedAt: IsoDateTime,
716});
717​
718export const ThreadRuntimeModeSetPayload = Schema.Struct({
719 threadId: ThreadId,
720 runtimeMode: RuntimeMode,
721 updatedAt: IsoDateTime,
722});
723​
724export const ThreadInteractionModeSetPayload = Schema.Struct({
725 threadId: ThreadId,
726 interactionMode: ProviderInteractionMode.pipe(
727 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
728 ),
729 updatedAt: IsoDateTime,
730});
731​
732export const ThreadMessageSentPayload = Schema.Struct({
733 threadId: ThreadId,
734 messageId: MessageId,
735 role: OrchestrationMessageRole,
736 text: Schema.String,
737 attachments: Schema.optional(Schema.Array(ChatAttachment)),
738 turnId: Schema.NullOr(TurnId),
739 streaming: Schema.Boolean,
740 createdAt: IsoDateTime,
741 updatedAt: IsoDateTime,
742});
743​
744export const ThreadTurnStartRequestedPayload = Schema.Struct({
745 threadId: ThreadId,
746 messageId: MessageId,
747 modelSelection: Schema.optional(ModelSelection),
748 titleSeed: Schema.optional(TrimmedNonEmptyString),
749 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
750 interactionMode: ProviderInteractionMode.pipe(
751 Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
752 ),
753 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
754 createdAt: IsoDateTime,
755});
756​
757export const ThreadTurnInterruptRequestedPayload = Schema.Struct({
758 threadId: ThreadId,
759 turnId: Schema.optional(TurnId),
760 createdAt: IsoDateTime,
761});
762​
763export const ThreadApprovalResponseRequestedPayload = Schema.Struct({
764 threadId: ThreadId,
765 requestId: ApprovalRequestId,
766 decision: ProviderApprovalDecision,
767 createdAt: IsoDateTime,
768});
769​
770const ThreadUserInputResponseRequestedPayload = Schema.Struct({
771 threadId: ThreadId,
772 requestId: ApprovalRequestId,
773 answers: ProviderUserInputAnswers,
774 createdAt: IsoDateTime,
775});
776​
777export const ThreadCheckpointRevertRequestedPayload = Schema.Struct({
778 threadId: ThreadId,
779 turnCount: NonNegativeInt,
780 createdAt: IsoDateTime,
781});
782​
783export const ThreadRevertedPayload = Schema.Struct({
784 threadId: ThreadId,
785 turnCount: NonNegativeInt,
786});
787​
788export const ThreadSessionStopRequestedPayload = Schema.Struct({
789 threadId: ThreadId,
790 createdAt: IsoDateTime,
791});
792​
793export const ThreadSessionSetPayload = Schema.Struct({
794 threadId: ThreadId,
795 session: OrchestrationSession,
796});
797​
798export const ThreadProposedPlanUpsertedPayload = Schema.Struct({
799 threadId: ThreadId,
800 proposedPlan: OrchestrationProposedPlan,
801});
802​
803export const ThreadTurnDiffCompletedPayload = Schema.Struct({
804 threadId: ThreadId,
805 turnId: TurnId,
806 checkpointTurnCount: NonNegativeInt,
807 checkpointRef: CheckpointRef,
808 status: OrchestrationCheckpointStatus,
809 files: Schema.Array(OrchestrationCheckpointFile),
810 assistantMessageId: Schema.NullOr(MessageId),
811 completedAt: IsoDateTime,
812});
813​
814export const ThreadActivityAppendedPayload = Schema.Struct({
815 threadId: ThreadId,
816 activity: OrchestrationThreadActivity,
817});
818​
819export const OrchestrationEventMetadata = Schema.Struct({
820 providerTurnId: Schema.optional(TrimmedNonEmptyString),
821 providerItemId: Schema.optional(ProviderItemId),
822 adapterKey: Schema.optional(TrimmedNonEmptyString),
823 requestId: Schema.optional(ApprovalRequestId),
824 ingestedAt: Schema.optional(IsoDateTime),
825});
826export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;
827​
828const EventBaseFields = {
829 sequence: NonNegativeInt,
830 eventId: EventId,
831 aggregateKind: OrchestrationAggregateKind,
832 aggregateId: Schema.Union([ProjectId, ThreadId]),
833 occurredAt: IsoDateTime,
834 commandId: Schema.NullOr(CommandId),
835 causationEventId: Schema.NullOr(EventId),
836 correlationId: Schema.NullOr(CommandId),
837 metadata: OrchestrationEventMetadata,
838} as const;
839​
840export const OrchestrationEvent = Schema.Union([
841 Schema.Struct({
842 ...EventBaseFields,
843 type: Schema.Literal("project.created"),
844 payload: ProjectCreatedPayload,
845 }),
846 Schema.Struct({
847 ...EventBaseFields,
848 type: Schema.Literal("project.meta-updated"),
849 payload: ProjectMetaUpdatedPayload,
850 }),
851 Schema.Struct({
852 ...EventBaseFields,
853 type: Schema.Literal("project.deleted"),
854 payload: ProjectDeletedPayload,
855 }),
856 Schema.Struct({
857 ...EventBaseFields,
858 type: Schema.Literal("thread.created"),
859 payload: ThreadCreatedPayload,
860 }),
861 Schema.Struct({
862 ...EventBaseFields,
863 type: Schema.Literal("thread.deleted"),
864 payload: ThreadDeletedPayload,
865 }),
866 Schema.Struct({
867 ...EventBaseFields,
868 type: Schema.Literal("thread.archived"),
869 payload: ThreadArchivedPayload,
870 }),
871 Schema.Struct({
872 ...EventBaseFields,
873 type: Schema.Literal("thread.unarchived"),
874 payload: ThreadUnarchivedPayload,
875 }),
876 Schema.Struct({
877 ...EventBaseFields,
878 type: Schema.Literal("thread.meta-updated"),
879 payload: ThreadMetaUpdatedPayload,
880 }),
881 Schema.Struct({
882 ...EventBaseFields,
883 type: Schema.Literal("thread.runtime-mode-set"),
884 payload: ThreadRuntimeModeSetPayload,
885 }),
886 Schema.Struct({
887 ...EventBaseFields,
888 type: Schema.Literal("thread.interaction-mode-set"),
889 payload: ThreadInteractionModeSetPayload,
890 }),
891 Schema.Struct({
892 ...EventBaseFields,
893 type: Schema.Literal("thread.message-sent"),
894 payload: ThreadMessageSentPayload,
895 }),
896 Schema.Struct({
897 ...EventBaseFields,
898 type: Schema.Literal("thread.turn-start-requested"),
899 payload: ThreadTurnStartRequestedPayload,
900 }),
901 Schema.Struct({
902 ...EventBaseFields,
903 type: Schema.Literal("thread.turn-interrupt-requested"),
904 payload: ThreadTurnInterruptRequestedPayload,
905 }),
906 Schema.Struct({
907 ...EventBaseFields,
908 type: Schema.Literal("thread.approval-response-requested"),
909 payload: ThreadApprovalResponseRequestedPayload,
910 }),
911 Schema.Struct({
912 ...EventBaseFields,
913 type: Schema.Literal("thread.user-input-response-requested"),
914 payload: ThreadUserInputResponseRequestedPayload,
915 }),
916 Schema.Struct({
917 ...EventBaseFields,
918 type: Schema.Literal("thread.checkpoint-revert-requested"),
919 payload: ThreadCheckpointRevertRequestedPayload,
920 }),
921 Schema.Struct({
922 ...EventBaseFields,
923 type: Schema.Literal("thread.reverted"),
924 payload: ThreadRevertedPayload,
925 }),
926 Schema.Struct({
927 ...EventBaseFields,
928 type: Schema.Literal("thread.session-stop-requested"),
929 payload: ThreadSessionStopRequestedPayload,
930 }),
931 Schema.Struct({
932 ...EventBaseFields,
933 type: Schema.Literal("thread.session-set"),
934 payload: ThreadSessionSetPayload,
935 }),
936 Schema.Struct({
937 ...EventBaseFields,
938 type: Schema.Literal("thread.proposed-plan-upserted"),
939 payload: ThreadProposedPlanUpsertedPayload,
940 }),
941 Schema.Struct({
942 ...EventBaseFields,
943 type: Schema.Literal("thread.turn-diff-completed"),
944 payload: ThreadTurnDiffCompletedPayload,
945 }),
946 Schema.Struct({
947 ...EventBaseFields,
948 type: Schema.Literal("thread.activity-appended"),
949 payload: ThreadActivityAppendedPayload,
950 }),
951]);
952export type OrchestrationEvent = typeof OrchestrationEvent.Type;
953​
954export const OrchestrationCommandReceiptStatus = Schema.Literals(["accepted", "rejected"]);
955export type OrchestrationCommandReceiptStatus = typeof OrchestrationCommandReceiptStatus.Type;
956​
957export const TurnCountRange = Schema.Struct({
958 fromTurnCount: NonNegativeInt,
959 toTurnCount: NonNegativeInt,
960}).check(
961 Schema.makeFilter(
962 (input) =>
963 input.fromTurnCount <= input.toTurnCount ||
964 new SchemaIssue.InvalidValue(Option.some(input.fromTurnCount), {
965 message: "fromTurnCount must be less than or equal to toTurnCount",
966 }),
967 { identifier: "OrchestrationTurnDiffRange" },
968 ),
969);
970​
971export const ThreadTurnDiff = TurnCountRange.mapFields(
972 Struct.assign({
973 threadId: ThreadId,
974 diff: Schema.String,
975 }),
976 { unsafePreserveChecks: true },
977);
978​
979export const ProviderSessionRuntimeStatus = Schema.Literals([
980 "starting",
981 "running",
982 "stopped",
983 "error",
984]);
985export type ProviderSessionRuntimeStatus = typeof ProviderSessionRuntimeStatus.Type;
986​
987const ProjectionThreadTurnStatus = Schema.Literals([
988 "running",
989 "completed",
990 "interrupted",
991 "error",
992]);
993export type ProjectionThreadTurnStatus = typeof ProjectionThreadTurnStatus.Type;
994​
995const ProjectionCheckpointRow = Schema.Struct({
996 threadId: ThreadId,
997 turnId: TurnId,
998 checkpointTurnCount: NonNegativeInt,
999 checkpointRef: CheckpointRef,
1000 status: OrchestrationCheckpointStatus,
1001 files: Schema.Array(OrchestrationCheckpointFile),
1002 assistantMessageId: Schema.NullOr(MessageId),
1003 completedAt: IsoDateTime,
1004});
1005export type ProjectionCheckpointRow = typeof ProjectionCheckpointRow.Type;
1006​
1007export const ProjectionPendingApprovalStatus = Schema.Literals(["pending", "resolved"]);
1008export type ProjectionPendingApprovalStatus = typeof ProjectionPendingApprovalStatus.Type;
1009​
1010export const ProjectionPendingApprovalDecision = Schema.NullOr(ProviderApprovalDecision);
1011export type ProjectionPendingApprovalDecision = typeof ProjectionPendingApprovalDecision.Type;
1012​
1013export const DispatchResult = Schema.Struct({
1014 sequence: NonNegativeInt,
1015});
1016export type DispatchResult = typeof DispatchResult.Type;
1017​
1018export const OrchestrationGetSnapshotInput = Schema.Struct({});
1019export type OrchestrationGetSnapshotInput = typeof OrchestrationGetSnapshotInput.Type;
1020const OrchestrationGetSnapshotResult = OrchestrationReadModel;
1021export type OrchestrationGetSnapshotResult = typeof OrchestrationGetSnapshotResult.Type;
1022​
1023export const OrchestrationGetTurnDiffInput = TurnCountRange.mapFields(
1024 Struct.assign({ threadId: ThreadId }),
1025 { unsafePreserveChecks: true },
1027export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput.Type;
1028​
1029export const OrchestrationGetTurnDiffResult = ThreadTurnDiff;
1030export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type;
1031​
1032export const OrchestrationGetFullThreadDiffInput = Schema.Struct({
1033 threadId: ThreadId,
1034 toTurnCount: NonNegativeInt,
1035});
1036export type OrchestrationGetFullThreadDiffInput = typeof OrchestrationGetFullThreadDiffInput.Type;
1037​
1038export const OrchestrationGetFullThreadDiffResult = ThreadTurnDiff;
1039export type OrchestrationGetFullThreadDiffResult = typeof OrchestrationGetFullThreadDiffResult.Type;
1040​
1041export const OrchestrationReplayEventsInput = Schema.Struct({
1042 fromSequenceExclusive: NonNegativeInt,
1043});
1044export type OrchestrationReplayEventsInput = typeof OrchestrationReplayEventsInput.Type;
1045​
1046const OrchestrationReplayEventsResult = Schema.Array(OrchestrationEvent);
1047export type OrchestrationReplayEventsResult = typeof OrchestrationReplayEventsResult.Type;
1048​
1049export const OrchestrationRpcSchemas = {
1050 getSnapshot: {
1051 input: OrchestrationGetSnapshotInput,
1052 output: OrchestrationGetSnapshotResult,
1053 },
1054 dispatchCommand: {
1055 input: ClientOrchestrationCommand,
1056 output: DispatchResult,
1057 },
1058 getTurnDiff: {
1059 input: OrchestrationGetTurnDiffInput,
1060 output: OrchestrationGetTurnDiffResult,
1061 },
1062 getFullThreadDiff: {
1063 input: OrchestrationGetFullThreadDiffInput,
1064 output: OrchestrationGetFullThreadDiffResult,
1065 },
1066 replayEvents: {
1067 input: OrchestrationReplayEventsInput,
1068 output: OrchestrationReplayEventsResult,
1069 },
1070} as const;
1071​
1072export class OrchestrationGetSnapshotError extends Schema.TaggedErrorClass<OrchestrationGetSnapshotError>()(
1073 "OrchestrationGetSnapshotError",
1075 message: TrimmedNonEmptyString,
1076 cause: Schema.optional(Schema.Defect),
1077 },
1078) {}
1079​
1080export class OrchestrationDispatchCommandError extends Schema.TaggedErrorClass<OrchestrationDispatchCommandError>()(
1081 "OrchestrationDispatchCommandError",
1083 message: TrimmedNonEmptyString,
1084 cause: Schema.optional(Schema.Defect),
1085 },
1086) {}
1087​
1088export class OrchestrationGetTurnDiffError extends Schema.TaggedErrorClass<OrchestrationGetTurnDiffError>()(
1089 "OrchestrationGetTurnDiffError",
1091 message: TrimmedNonEmptyString,
1092 cause: Schema.optional(Schema.Defect),
1093 },
1094) {}
1095​
1096export class OrchestrationGetFullThreadDiffError extends Schema.TaggedErrorClass<OrchestrationGetFullThreadDiffError>()(
1097 "OrchestrationGetFullThreadDiffError",
1099 message: TrimmedNonEmptyString,
1100 cause: Schema.optional(Schema.Defect),
1101 },
1102) {}
1103​
1104export class OrchestrationReplayEventsError extends Schema.TaggedErrorClass<OrchestrationReplayEventsError>()(
1105 "OrchestrationReplayEventsError",
1107 message: TrimmedNonEmptyString,
1108 cause: Schema.optional(Schema.Defect),
1109 },
1110) {}