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

orchestration.ts

1,114 lines35.6 KB

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