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

orchestration.ts

1,443 lines47.7 KB

1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4import * as SchemaIssue from "effect/SchemaIssue";
5import * as SchemaTransformation from "effect/SchemaTransformation";
6import * as Struct from "effect/Struct";
7import { ProviderOptionSelections } from "./model.ts";
8import { RepositoryIdentity } from "./environment.ts";
9import {
10 ApprovalRequestId,
11 CheckpointRef,
12 CommandId,
13 EventId,
14 IsoDateTime,
15 MessageId,
16 NonNegativeInt,
17 ProjectId,
18 ProviderItemId,
19 ThreadId,
20 TrimmedNonEmptyString,
21 TurnId,
22} from "./baseSchemas.ts";
23import { ProviderInstanceId } from "./providerInstance.ts";
24​
25export const ORCHESTRATION_WS_METHODS = {
26 dispatchCommand: "orchestration.dispatchCommand",
27 getTurnDiff: "orchestration.getTurnDiff",
28 getFullThreadDiff: "orchestration.getFullThreadDiff",
29 replayEvents: "orchestration.replayEvents",
30 getArchivedShellSnapshot: "orchestration.getArchivedShellSnapshot",
31 subscribeShell: "orchestration.subscribeShell",
32 subscribeThread: "orchestration.subscribeThread",
33} as const;
34​
35export const ProviderApprovalPolicy = Schema.Literals([
36 "untrusted",
37 "on-failure",
38 "on-request",
39 "never",
40]);
41export type ProviderApprovalPolicy = typeof ProviderApprovalPolicy.Type;
42export const ProviderSandboxMode = Schema.Literals([
43 "read-only",
44 "workspace-write",
45 "danger-full-access",
46]);
47export type ProviderSandboxMode = typeof ProviderSandboxMode.Type;
48​
49/**
50 * `ModelSelection` — selection of a model on a configured provider instance.
51 *
52 * The routing key is `instanceId` (a user-defined slug identifying one
53 * configured provider instance). Drivers, credentials, working-directory
54 * bindings, and any other per-instance state are recovered from the
55 * runtime registry via the instance id.
56 *
57 * Wire legacy: persisted selections produced before the driver/instance
58 * split carried a `provider: <driver-id>` field instead. The schema absorbs
59 * that shape via a pre-decoding transform — `{provider, model}` is promoted
60 * to `{instanceId: defaultInstanceIdForDriver(provider), model}`. No
61 * post-decode compatibility code lives in the runtime; the transform is the
62 * only compat surface.
63 */
64const ModelSelectionWire = Schema.Struct({
65 instanceId: ProviderInstanceId,
66 model: TrimmedNonEmptyString,
67 options: Schema.optionalKey(ProviderOptionSelections),
68});
69​
70// Source shape for persisted legacy payloads. Fields are typed as
71// `Schema.Unknown` so malformed drafts still make it into the transform and
72// fail validation through the target schema (with proper error messages)
73// rather than at the source-struct layer where the error is less actionable.
74const ModelSelectionSource = Schema.Struct({
75 provider: Schema.optional(Schema.Unknown),
76 instanceId: Schema.optional(Schema.Unknown),
77 model: Schema.Unknown,
78 options: Schema.optional(Schema.Unknown),
79});
80​
81export const ModelSelection = ModelSelectionSource.pipe(
82 Schema.decodeTo(
83 ModelSelectionWire,
84 SchemaTransformation.transformOrFail({
85 decode: (raw) => {
86 // Resolve the routing key: prefer an explicit `instanceId`; fall
87 // back to promoting the legacy `provider` slug (the canonical
88 // `defaultInstanceIdForDriver` mapping) so persisted rollout-era
89 // payloads decode without data loss. The target schema brands the
90 // string as `ProviderInstanceId`.
91 const instanceIdSource =
92 raw.instanceId !== undefined
93 ? raw.instanceId
94 : typeof raw.provider === "string"
95 ? raw.provider
96 : undefined;
97 const base: Record<string, unknown> = {
98 instanceId: instanceIdSource,
99 model: raw.model,
100 };
101 if (raw.options !== undefined) base.options = raw.options;
102 return Effect.succeed(base as typeof ModelSelectionWire.Encoded);
103 },
104 encode: (value) => {
105 const base: Record<string, unknown> = {
106 model: value.model,
107 instanceId: value.instanceId,
108 };
109 if (value.options !== undefined) base.options = value.options;
110 return Effect.succeed(base as typeof ModelSelectionSource.Encoded);
111 },
112 }),
113 ),
114);
115export type ModelSelection = typeof ModelSelection.Type;
116​
117export const RuntimeMode = Schema.Literals([
118 "approval-required",
119 "auto-accept-edits",
120 "auto",
121 "full-access",
122]);
123export type RuntimeMode = typeof RuntimeMode.Type;
124export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
125export const ProviderInteractionMode = Schema.Literals(["default", "plan"]);
126export type ProviderInteractionMode = typeof ProviderInteractionMode.Type;
127export const DEFAULT_PROVIDER_INTERACTION_MODE: ProviderInteractionMode = "default";
128export const ProviderRequestKind = Schema.Literals(["command", "file-read", "file-change"]);
129export type ProviderRequestKind = typeof ProviderRequestKind.Type;
130export const AssistantDeliveryMode = Schema.Literals(["buffered", "streaming"]);
131export type AssistantDeliveryMode = typeof AssistantDeliveryMode.Type;
132export const ProviderApprovalDecision = Schema.Literals([
133 "accept",
134 "acceptForSession",
135 "decline",
136 "cancel",
137]);
138export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;
139export const ProviderUserInputAnswers = Schema.Record(Schema.String, Schema.Unknown);
140export type ProviderUserInputAnswers = typeof ProviderUserInputAnswers.Type;
141​
142export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
143export const PROVIDER_SEND_TURN_MAX_ATTACHMENTS = 8;
144export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
145const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
146const CHAT_ATTACHMENT_ID_MAX_CHARS = 128;
147// Correlation id is command id by design in this model.
148export const CorrelationId = CommandId;
149export type CorrelationId = typeof CorrelationId.Type;
150​
151const ChatAttachmentId = TrimmedNonEmptyString.check(
152 Schema.isMaxLength(CHAT_ATTACHMENT_ID_MAX_CHARS),
153 Schema.isPattern(/^[a-z0-9_-]+$/i),
154);
155export type ChatAttachmentId = typeof ChatAttachmentId.Type;
156​
157export const ChatImageAttachment = Schema.Struct({
158 type: Schema.Literal("image"),
159 id: ChatAttachmentId,
160 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
161 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
162 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
163});
164export type ChatImageAttachment = typeof ChatImageAttachment.Type;
165​
166const UploadChatImageAttachment = Schema.Struct({
167 type: Schema.Literal("image"),
168 name: TrimmedNonEmptyString.check(Schema.isMaxLength(255)),
169 mimeType: TrimmedNonEmptyString.check(Schema.isMaxLength(100), Schema.isPattern(/^image\//i)),
170 sizeBytes: NonNegativeInt.check(Schema.isLessThanOrEqualTo(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)),
171 dataUrl: TrimmedNonEmptyString.check(
172 Schema.isMaxLength(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
173 ),
174});
175export type UploadChatImageAttachment = typeof UploadChatImageAttachment.Type;
176​
177export const ChatAttachment = Schema.Union([ChatImageAttachment]);
178export type ChatAttachment = typeof ChatAttachment.Type;
179const UploadChatAttachment = Schema.Union([UploadChatImageAttachment]);
180export type UploadChatAttachment = typeof UploadChatAttachment.Type;
181​
182export const ProjectScriptIcon = Schema.Literals([
183 "play",
184 "test",
185 "lint",
186 "configure",
187 "build",
188 "debug",
189]);
190export type ProjectScriptIcon = typeof ProjectScriptIcon.Type;
191​
192export const ProjectScript = Schema.Struct({
193 id: TrimmedNonEmptyString,
194 name: TrimmedNonEmptyString,
195 command: TrimmedNonEmptyString,
196 icon: ProjectScriptIcon,
197 runOnWorktreeCreate: Schema.Boolean,
198 /**
199 * URL to open in the in-app browser preview when this script runs (or
200 * when the user explicitly requests a preview). Optional; only honored on
201 * the desktop build.
202 */
203 previewUrl: Schema.optional(TrimmedNonEmptyString),
204 /**
205 * When true, automatically open the preview panel pointed at `previewUrl`
206 * the moment this script starts. Ignored without `previewUrl` or on web.
207 */
208 autoOpenPreview: Schema.optional(Schema.Boolean),
209});
210export type ProjectScript = typeof ProjectScript.Type;
211​
212export const OrchestrationProject = Schema.Struct({
213 id: ProjectId,
214 title: TrimmedNonEmptyString,
215 workspaceRoot: TrimmedNonEmptyString,
216 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
217 defaultModelSelection: Schema.NullOr(ModelSelection),
218 scripts: Schema.Array(ProjectScript),
219 createdAt: IsoDateTime,
220 updatedAt: IsoDateTime,
221 deletedAt: Schema.NullOr(IsoDateTime),
222});
223export type OrchestrationProject = typeof OrchestrationProject.Type;
224​
225export const OrchestrationMessageRole = Schema.Literals(["user", "assistant", "system"]);
226export type OrchestrationMessageRole = typeof OrchestrationMessageRole.Type;
227​
228export const OrchestrationMessage = Schema.Struct({
229 id: MessageId,
230 role: OrchestrationMessageRole,
231 text: Schema.String,
232 attachments: Schema.optional(Schema.Array(ChatAttachment)),
233 turnId: Schema.NullOr(TurnId),
234 streaming: Schema.Boolean,
235 createdAt: IsoDateTime,
236 updatedAt: IsoDateTime,
237});
238export type OrchestrationMessage = typeof OrchestrationMessage.Type;
239​
240export const OrchestrationProposedPlanId = TrimmedNonEmptyString;
241export type OrchestrationProposedPlanId = typeof OrchestrationProposedPlanId.Type;
242​
243export const OrchestrationProposedPlan = Schema.Struct({
244 id: OrchestrationProposedPlanId,
245 turnId: Schema.NullOr(TurnId),
246 planMarkdown: TrimmedNonEmptyString,
247 implementedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
248 implementationThreadId: Schema.NullOr(ThreadId).pipe(
249 Schema.withDecodingDefault(Effect.succeed(null)),
250 ),
251 createdAt: IsoDateTime,
252 updatedAt: IsoDateTime,
253});
254export type OrchestrationProposedPlan = typeof OrchestrationProposedPlan.Type;
255​
256const SourceProposedPlanReference = Schema.Struct({
257 threadId: ThreadId,
258 planId: OrchestrationProposedPlanId,
259});
260​
261export const OrchestrationSessionStatus = Schema.Literals([
262 "idle",
263 "starting",
264 "running",
265 "ready",
266 "interrupted",
267 "stopped",
268 "error",
269]);
270export type OrchestrationSessionStatus = typeof OrchestrationSessionStatus.Type;
271​
272export const OrchestrationSession = Schema.Struct({
273 threadId: ThreadId,
274 status: OrchestrationSessionStatus,
275 providerName: Schema.NullOr(TrimmedNonEmptyString),
276 providerInstanceId: Schema.optional(ProviderInstanceId),
277 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
278 activeTurnId: Schema.NullOr(TurnId),
279 lastError: Schema.NullOr(TrimmedNonEmptyString),
280 updatedAt: IsoDateTime,
281});
282export type OrchestrationSession = typeof OrchestrationSession.Type;
283​
284export const OrchestrationCheckpointFile = Schema.Struct({
285 path: TrimmedNonEmptyString,
286 kind: TrimmedNonEmptyString,
287 additions: NonNegativeInt,
288 deletions: NonNegativeInt,
289});
290export type OrchestrationCheckpointFile = typeof OrchestrationCheckpointFile.Type;
291​
292export const OrchestrationCheckpointStatus = Schema.Literals(["ready", "missing", "error"]);
293export type OrchestrationCheckpointStatus = typeof OrchestrationCheckpointStatus.Type;
294​
295export const OrchestrationCheckpointSummary = Schema.Struct({
296 turnId: TurnId,
297 checkpointTurnCount: NonNegativeInt,
298 checkpointRef: CheckpointRef,
299 status: OrchestrationCheckpointStatus,
300 files: Schema.Array(OrchestrationCheckpointFile),
301 assistantMessageId: Schema.NullOr(MessageId),
302 completedAt: IsoDateTime,
303});
304export type OrchestrationCheckpointSummary = typeof OrchestrationCheckpointSummary.Type;
305​
306export const OrchestrationThreadActivityTone = Schema.Literals([
307 "info",
308 "tool",
309 "approval",
310 "error",
311]);
312export type OrchestrationThreadActivityTone = typeof OrchestrationThreadActivityTone.Type;
313​
314export const OrchestrationThreadActivity = Schema.Struct({
315 id: EventId,
316 tone: OrchestrationThreadActivityTone,
317 kind: TrimmedNonEmptyString,
318 summary: TrimmedNonEmptyString,
319 payload: Schema.Unknown,
320 turnId: Schema.NullOr(TurnId),
321 sequence: Schema.optional(NonNegativeInt),
322 createdAt: IsoDateTime,
323});
324export type OrchestrationThreadActivity = typeof OrchestrationThreadActivity.Type;
325​
326const OrchestrationLatestTurnState = Schema.Literals([
327 "running",
328 "interrupted",
329 "completed",
330 "error",
331]);
332export type OrchestrationLatestTurnState = typeof OrchestrationLatestTurnState.Type;
333​
334export const OrchestrationLatestTurn = Schema.Struct({
335 turnId: TurnId,
336 state: OrchestrationLatestTurnState,
337 requestedAt: IsoDateTime,
338 startedAt: Schema.NullOr(IsoDateTime),
339 completedAt: Schema.NullOr(IsoDateTime),
340 assistantMessageId: Schema.NullOr(MessageId),
341 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
342});
343export type OrchestrationLatestTurn = typeof OrchestrationLatestTurn.Type;
344​
345export const OrchestrationThread = Schema.Struct({
346 id: ThreadId,
347 projectId: ProjectId,
348 title: TrimmedNonEmptyString,
349 modelSelection: ModelSelection,
350 runtimeMode: RuntimeMode,
351 interactionMode: ProviderInteractionMode.pipe(
352 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
353 ),
354 branch: Schema.NullOr(TrimmedNonEmptyString),
355 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
356 latestTurn: Schema.NullOr(OrchestrationLatestTurn),
357 createdAt: IsoDateTime,
358 updatedAt: IsoDateTime,
359 archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
360 settledOverride: Schema.NullOr(Schema.Literals(["settled", "active"])).pipe(
361 Schema.withDecodingDefault(Effect.succeed(null)),
362 ),
363 settledAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
364 // Snooze is an overlay on the active lifecycle, not a fourth destination:
365 // a snoozed thread stays "active" in the model and is only suppressed from
366 // the inbox until snoozedUntil passes (or the thread raises its hand).
367 // Optional so payloads from pre-snooze servers still decode.
368 snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
369 snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
370 deletedAt: Schema.NullOr(IsoDateTime),
371 messages: Schema.Array(OrchestrationMessage),
372 proposedPlans: Schema.Array(OrchestrationProposedPlan).pipe(
373 Schema.withDecodingDefault(Effect.succeed([])),
374 ),
375 activities: Schema.Array(OrchestrationThreadActivity),
376 checkpoints: Schema.Array(OrchestrationCheckpointSummary),
377 session: Schema.NullOr(OrchestrationSession),
378});
379export type OrchestrationThread = typeof OrchestrationThread.Type;
380​
381export const OrchestrationReadModel = Schema.Struct({
382 snapshotSequence: NonNegativeInt,
383 projects: Schema.Array(OrchestrationProject),
384 threads: Schema.Array(OrchestrationThread),
385 updatedAt: IsoDateTime,
386});
387export type OrchestrationReadModel = typeof OrchestrationReadModel.Type;
388​
389export const OrchestrationProjectShell = Schema.Struct({
390 id: ProjectId,
391 title: TrimmedNonEmptyString,
392 workspaceRoot: TrimmedNonEmptyString,
393 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
394 defaultModelSelection: Schema.NullOr(ModelSelection),
395 scripts: Schema.Array(ProjectScript),
396 createdAt: IsoDateTime,
397 updatedAt: IsoDateTime,
398});
399export type OrchestrationProjectShell = typeof OrchestrationProjectShell.Type;
400​
401export const OrchestrationThreadShell = Schema.Struct({
402 id: ThreadId,
403 projectId: ProjectId,
404 title: TrimmedNonEmptyString,
405 modelSelection: ModelSelection,
406 runtimeMode: RuntimeMode,
407 interactionMode: ProviderInteractionMode.pipe(
408 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
409 ),
410 branch: Schema.NullOr(TrimmedNonEmptyString),
411 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
412 latestTurn: Schema.NullOr(OrchestrationLatestTurn),
413 createdAt: IsoDateTime,
414 updatedAt: IsoDateTime,
415 archivedAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
416 settledOverride: Schema.NullOr(Schema.Literals(["settled", "active"])).pipe(
417 Schema.withDecodingDefault(Effect.succeed(null)),
418 ),
419 settledAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
420 snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
421 snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
422 session: Schema.NullOr(OrchestrationSession),
423 latestUserMessageAt: Schema.NullOr(IsoDateTime),
424 hasPendingApprovals: Schema.Boolean,
425 hasPendingUserInput: Schema.Boolean,
426 hasActionableProposedPlan: Schema.Boolean,
427});
428export type OrchestrationThreadShell = typeof OrchestrationThreadShell.Type;
429​
430export const OrchestrationShellSnapshot = Schema.Struct({
431 snapshotSequence: NonNegativeInt,
432 projects: Schema.Array(OrchestrationProjectShell),
433 threads: Schema.Array(OrchestrationThreadShell),
434 updatedAt: IsoDateTime,
435});
436export type OrchestrationShellSnapshot = typeof OrchestrationShellSnapshot.Type;
437​
438export const OrchestrationShellStreamEvent = Schema.Union([
439 Schema.Struct({
440 kind: Schema.Literal("project-upserted"),
441 sequence: NonNegativeInt,
442 project: OrchestrationProjectShell,
443 }),
444 Schema.Struct({
445 kind: Schema.Literal("project-removed"),
446 sequence: NonNegativeInt,
447 projectId: ProjectId,
448 }),
449 Schema.Struct({
450 kind: Schema.Literal("thread-upserted"),
451 sequence: NonNegativeInt,
452 thread: OrchestrationThreadShell,
453 }),
454 Schema.Struct({
455 kind: Schema.Literal("thread-removed"),
456 sequence: NonNegativeInt,
457 threadId: ThreadId,
458 }),
459]);
460export type OrchestrationShellStreamEvent = typeof OrchestrationShellStreamEvent.Type;
461​
462export const OrchestrationShellStreamItem = Schema.Union([
463 Schema.Struct({
464 kind: Schema.Literal("synchronized"),
465 }),
466 Schema.Struct({
467 kind: Schema.Literal("snapshot"),
468 snapshot: OrchestrationShellSnapshot,
469 }),
470 OrchestrationShellStreamEvent,
471]);
472export type OrchestrationShellStreamItem = typeof OrchestrationShellStreamItem.Type;
473​
474export const OrchestrationSubscribeShellInput = Schema.Struct({
475 /**
476 * When provided, the server skips the initial full shell snapshot and instead
477 * replays shell events after this sequence before streaming live events.
478 * Clients that already hold a cached (or HTTP-loaded) shell snapshot pass its
479 * sequence here so the subscription resumes without re-sending the entire
480 * projects/threads list (overlapping events are deduped by sequence on the
481 * client).
482 */
483 afterSequence: Schema.optionalKey(NonNegativeInt),
484 /**
485 * Requests an explicit marker after the subscription has emitted its initial
486 * snapshot or catch-up replay and before it begins emitting live events.
487 */
488 requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
489});
490export type OrchestrationSubscribeShellInput = typeof OrchestrationSubscribeShellInput.Type;
491​
492export const OrchestrationSubscribeThreadInput = Schema.Struct({
493 threadId: ThreadId,
494 /**
495 * When provided, the server skips the initial snapshot frame and instead
496 * replays events after this sequence before streaming live events. Clients
497 * that load the snapshot over HTTP pass the snapshot's sequence here so the
498 * live subscription resumes without a gap (overlapping events are deduped by
499 * sequence on the client).
500 */
501 afterSequence: Schema.optionalKey(NonNegativeInt),
502 /**
503 * Requests an explicit marker after the subscription has emitted its initial
504 * snapshot or catch-up replay and before it begins emitting live events.
505 */
506 requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
507});
508export type OrchestrationSubscribeThreadInput = typeof OrchestrationSubscribeThreadInput.Type;
509​
510export const OrchestrationThreadDetailSnapshot = Schema.Struct({
511 snapshotSequence: NonNegativeInt,
512 thread: OrchestrationThread,
513});
514export type OrchestrationThreadDetailSnapshot = typeof OrchestrationThreadDetailSnapshot.Type;
515​
516export const ProjectCreateCommand = Schema.Struct({
517 type: Schema.Literal("project.create"),
518 commandId: CommandId,
519 projectId: ProjectId,
520 title: TrimmedNonEmptyString,
521 workspaceRoot: TrimmedNonEmptyString,
522 createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
523 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
524 createdAt: IsoDateTime,
525});
526​
527const ProjectMetaUpdateCommand = Schema.Struct({
528 type: Schema.Literal("project.meta.update"),
529 commandId: CommandId,
530 projectId: ProjectId,
531 title: Schema.optional(TrimmedNonEmptyString),
532 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
533 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
534 scripts: Schema.optional(Schema.Array(ProjectScript)),
535});
536​
537const ProjectDeleteCommand = Schema.Struct({
538 type: Schema.Literal("project.delete"),
539 commandId: CommandId,
540 projectId: ProjectId,
541 force: Schema.optional(Schema.Boolean),
542});
543​
544const ThreadCreateCommand = Schema.Struct({
545 type: Schema.Literal("thread.create"),
546 commandId: CommandId,
547 threadId: ThreadId,
548 projectId: ProjectId,
549 title: TrimmedNonEmptyString,
550 modelSelection: ModelSelection,
551 runtimeMode: RuntimeMode,
552 interactionMode: ProviderInteractionMode.pipe(
553 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
554 ),
555 branch: Schema.NullOr(TrimmedNonEmptyString),
556 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
557 createdAt: IsoDateTime,
558});
559​
560const ThreadDeleteCommand = Schema.Struct({
561 type: Schema.Literal("thread.delete"),
562 commandId: CommandId,
563 threadId: ThreadId,
564});
565​
566const ThreadArchiveCommand = Schema.Struct({
567 type: Schema.Literal("thread.archive"),
568 commandId: CommandId,
569 threadId: ThreadId,
570});
571​
572const ThreadUnarchiveCommand = Schema.Struct({
573 type: Schema.Literal("thread.unarchive"),
574 commandId: CommandId,
575 threadId: ThreadId,
576});
577​
578const ThreadSettleCommand = Schema.Struct({
579 type: Schema.Literal("thread.settle"),
580 commandId: CommandId,
581 threadId: ThreadId,
582});
583​
584const ThreadUnsettleCommand = Schema.Struct({
585 type: Schema.Literal("thread.unsettle"),
586 commandId: CommandId,
587 threadId: ThreadId,
588 // Commands only carry "user": activity un-settles are decided server-side
589 // (the decider emits thread.unsettled(reason: "activity") events directly,
590 // never through this command), so a client cannot forge the neutral reset.
591 reason: Schema.Literal("user"),
592});
593​
594const ThreadSnoozeCommand = Schema.Struct({
595 type: Schema.Literal("thread.snooze"),
596 commandId: CommandId,
597 threadId: ThreadId,
598 // The wake time. Event-based wake conditions (PR merged, review posted)
599 // will arrive as an optional condition field alongside this; time-based
600 // snooze is just the first kind of condition.
601 snoozedUntil: IsoDateTime,
602});
603​
604const ThreadUnsnoozeCommand = Schema.Struct({
605 type: Schema.Literal("thread.unsnooze"),
606 commandId: CommandId,
607 threadId: ThreadId,
608 // Commands only carry "user": activity wakes are decided server-side (the
609 // decider emits thread.unsnoozed(reason: "activity") directly), and timer
610 // wakes need no event at all — clients derive visibility from snoozedUntil,
611 // so a passed wake time simply stops classifying as snoozed.
612 reason: Schema.Literal("user"),
613});
614​
615const ThreadMetaUpdateCommand = Schema.Struct({
616 type: Schema.Literal("thread.meta.update"),
617 commandId: CommandId,
618 threadId: ThreadId,
619 title: Schema.optional(TrimmedNonEmptyString),
620 modelSelection: Schema.optional(ModelSelection),
621 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
622 expectedBranch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
623 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
624});
625​
626const ThreadRuntimeModeSetCommand = Schema.Struct({
627 type: Schema.Literal("thread.runtime-mode.set"),
628 commandId: CommandId,
629 threadId: ThreadId,
630 runtimeMode: RuntimeMode,
631 createdAt: IsoDateTime,
632});
633​
634const ThreadInteractionModeSetCommand = Schema.Struct({
635 type: Schema.Literal("thread.interaction-mode.set"),
636 commandId: CommandId,
637 threadId: ThreadId,
638 interactionMode: ProviderInteractionMode,
639 createdAt: IsoDateTime,
640});
641​
642const ThreadTurnStartBootstrapCreateThread = Schema.Struct({
643 projectId: ProjectId,
644 title: TrimmedNonEmptyString,
645 modelSelection: ModelSelection,
646 runtimeMode: RuntimeMode,
647 interactionMode: ProviderInteractionMode,
648 branch: Schema.NullOr(TrimmedNonEmptyString),
649 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
650 createdAt: IsoDateTime,
651});
652​
653const ThreadTurnStartBootstrapPrepareWorktree = Schema.Struct({
654 projectCwd: TrimmedNonEmptyString,
655 baseBranch: TrimmedNonEmptyString,
656 branch: Schema.optional(TrimmedNonEmptyString),
657 startFromOrigin: Schema.optional(Schema.Boolean),
658});
659​
660const ThreadTurnStartBootstrap = Schema.Struct({
661 createThread: Schema.optional(ThreadTurnStartBootstrapCreateThread),
662 prepareWorktree: Schema.optional(ThreadTurnStartBootstrapPrepareWorktree),
663 runSetupScript: Schema.optional(Schema.Boolean),
664});
665​
666export type ThreadTurnStartBootstrap = typeof ThreadTurnStartBootstrap.Type;
667​
668export const ThreadTurnStartCommand = Schema.Struct({
669 type: Schema.Literal("thread.turn.start"),
670 commandId: CommandId,
671 threadId: ThreadId,
672 message: Schema.Struct({
673 messageId: MessageId,
674 role: Schema.Literal("user"),
675 text: Schema.String,
676 attachments: Schema.Array(ChatAttachment),
677 }),
678 modelSelection: Schema.optional(ModelSelection),
679 titleSeed: Schema.optional(TrimmedNonEmptyString),
680 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
681 interactionMode: ProviderInteractionMode.pipe(
682 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
683 ),
684 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
685 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
686 createdAt: IsoDateTime,
687});
688​
689const ClientThreadTurnStartCommand = Schema.Struct({
690 type: Schema.Literal("thread.turn.start"),
691 commandId: CommandId,
692 threadId: ThreadId,
693 message: Schema.Struct({
694 messageId: MessageId,
695 role: Schema.Literal("user"),
696 text: Schema.String,
697 attachments: Schema.Array(UploadChatAttachment),
698 }),
699 modelSelection: Schema.optional(ModelSelection),
700 titleSeed: Schema.optional(TrimmedNonEmptyString),
701 runtimeMode: RuntimeMode,
702 interactionMode: ProviderInteractionMode,
703 bootstrap: Schema.optional(ThreadTurnStartBootstrap),
704 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
705 createdAt: IsoDateTime,
706});
707​
708const ThreadTurnInterruptCommand = Schema.Struct({
709 type: Schema.Literal("thread.turn.interrupt"),
710 commandId: CommandId,
711 threadId: ThreadId,
712 turnId: Schema.optional(TurnId),
713 createdAt: IsoDateTime,
714});
715​
716const ThreadApprovalRespondCommand = Schema.Struct({
717 type: Schema.Literal("thread.approval.respond"),
718 commandId: CommandId,
719 threadId: ThreadId,
720 requestId: ApprovalRequestId,
721 decision: ProviderApprovalDecision,
722 createdAt: IsoDateTime,
723});
724​
725const ThreadUserInputRespondCommand = Schema.Struct({
726 type: Schema.Literal("thread.user-input.respond"),
727 commandId: CommandId,
728 threadId: ThreadId,
729 requestId: ApprovalRequestId,
730 answers: ProviderUserInputAnswers,
731 createdAt: IsoDateTime,
732});
733​
734const ThreadCheckpointRevertCommand = Schema.Struct({
735 type: Schema.Literal("thread.checkpoint.revert"),
736 commandId: CommandId,
737 threadId: ThreadId,
738 turnCount: NonNegativeInt,
739 createdAt: IsoDateTime,
740});
741​
742const ThreadSessionStopCommand = Schema.Struct({
743 type: Schema.Literal("thread.session.stop"),
744 commandId: CommandId,
745 threadId: ThreadId,
746 createdAt: IsoDateTime,
747});
748​
749const DispatchableClientOrchestrationCommand = Schema.Union([
750 ProjectCreateCommand,
751 ProjectMetaUpdateCommand,
752 ProjectDeleteCommand,
753 ThreadCreateCommand,
754 ThreadDeleteCommand,
755 ThreadArchiveCommand,
756 ThreadUnarchiveCommand,
757 ThreadSettleCommand,
758 ThreadUnsettleCommand,
759 ThreadSnoozeCommand,
760 ThreadUnsnoozeCommand,
761 ThreadMetaUpdateCommand,
762 ThreadRuntimeModeSetCommand,
763 ThreadInteractionModeSetCommand,
764 ThreadTurnStartCommand,
765 ThreadTurnInterruptCommand,
766 ThreadApprovalRespondCommand,
767 ThreadUserInputRespondCommand,
768 ThreadCheckpointRevertCommand,
769 ThreadSessionStopCommand,
770]);
771export type DispatchableClientOrchestrationCommand =
772 typeof DispatchableClientOrchestrationCommand.Type;
773​
774export const ClientOrchestrationCommand = Schema.Union([
775 ProjectCreateCommand,
776 ProjectMetaUpdateCommand,
777 ProjectDeleteCommand,
778 ThreadCreateCommand,
779 ThreadDeleteCommand,
780 ThreadArchiveCommand,
781 ThreadUnarchiveCommand,
782 ThreadSettleCommand,
783 ThreadUnsettleCommand,
784 ThreadSnoozeCommand,
785 ThreadUnsnoozeCommand,
786 ThreadMetaUpdateCommand,
787 ThreadRuntimeModeSetCommand,
788 ThreadInteractionModeSetCommand,
789 ClientThreadTurnStartCommand,
790 ThreadTurnInterruptCommand,
791 ThreadApprovalRespondCommand,
792 ThreadUserInputRespondCommand,
793 ThreadCheckpointRevertCommand,
794 ThreadSessionStopCommand,
795]);
796export type ClientOrchestrationCommand = typeof ClientOrchestrationCommand.Type;
797​
798const ThreadSessionSetCommand = Schema.Struct({
799 type: Schema.Literal("thread.session.set"),
800 commandId: CommandId,
801 threadId: ThreadId,
802 session: OrchestrationSession,
803 createdAt: IsoDateTime,
804});
805​
806const ThreadMessageAssistantDeltaCommand = Schema.Struct({
807 type: Schema.Literal("thread.message.assistant.delta"),
808 commandId: CommandId,
809 threadId: ThreadId,
810 messageId: MessageId,
811 delta: Schema.String,
812 turnId: Schema.optional(TurnId),
813 createdAt: IsoDateTime,
814});
815​
816const ThreadMessageAssistantCompleteCommand = Schema.Struct({
817 type: Schema.Literal("thread.message.assistant.complete"),
818 commandId: CommandId,
819 threadId: ThreadId,
820 messageId: MessageId,
821 turnId: Schema.optional(TurnId),
822 createdAt: IsoDateTime,
823});
824​
825const ThreadProposedPlanUpsertCommand = Schema.Struct({
826 type: Schema.Literal("thread.proposed-plan.upsert"),
827 commandId: CommandId,
828 threadId: ThreadId,
829 proposedPlan: OrchestrationProposedPlan,
830 createdAt: IsoDateTime,
831});
832​
833const ThreadTurnDiffCompleteCommand = Schema.Struct({
834 type: Schema.Literal("thread.turn.diff.complete"),
835 commandId: CommandId,
836 threadId: ThreadId,
837 turnId: TurnId,
838 completedAt: IsoDateTime,
839 checkpointRef: CheckpointRef,
840 status: OrchestrationCheckpointStatus,
841 files: Schema.Array(OrchestrationCheckpointFile),
842 assistantMessageId: Schema.optional(MessageId),
843 checkpointTurnCount: NonNegativeInt,
844 createdAt: IsoDateTime,
845});
846​
847const ThreadActivityAppendCommand = Schema.Struct({
848 type: Schema.Literal("thread.activity.append"),
849 commandId: CommandId,
850 threadId: ThreadId,
851 activity: OrchestrationThreadActivity,
852 createdAt: IsoDateTime,
853});
854​
855const ThreadRevertCompleteCommand = Schema.Struct({
856 type: Schema.Literal("thread.revert.complete"),
857 commandId: CommandId,
858 threadId: ThreadId,
859 turnCount: NonNegativeInt,
860 createdAt: IsoDateTime,
861});
862​
863const InternalOrchestrationCommand = Schema.Union([
864 ThreadSessionSetCommand,
865 ThreadMessageAssistantDeltaCommand,
866 ThreadMessageAssistantCompleteCommand,
867 ThreadProposedPlanUpsertCommand,
868 ThreadTurnDiffCompleteCommand,
869 ThreadActivityAppendCommand,
870 ThreadRevertCompleteCommand,
871]);
872export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
873​
874export const OrchestrationCommand = Schema.Union([
875 DispatchableClientOrchestrationCommand,
876 InternalOrchestrationCommand,
877]);
878export type OrchestrationCommand = typeof OrchestrationCommand.Type;
879​
880export const OrchestrationEventType = Schema.Literals([
881 "project.created",
882 "project.meta-updated",
883 "project.deleted",
884 "thread.created",
885 "thread.deleted",
886 "thread.archived",
887 "thread.unarchived",
888 "thread.settled",
889 "thread.unsettled",
890 "thread.snoozed",
891 "thread.unsnoozed",
892 "thread.meta-updated",
893 "thread.runtime-mode-set",
894 "thread.interaction-mode-set",
895 "thread.message-sent",
896 "thread.turn-start-requested",
897 "thread.turn-interrupt-requested",
898 "thread.approval-response-requested",
899 "thread.user-input-response-requested",
900 "thread.checkpoint-revert-requested",
901 "thread.reverted",
902 "thread.session-stop-requested",
903 "thread.session-set",
904 "thread.proposed-plan-upserted",
905 "thread.turn-diff-completed",
906 "thread.activity-appended",
907]);
908export type OrchestrationEventType = typeof OrchestrationEventType.Type;
909​
910export const OrchestrationAggregateKind = Schema.Literals(["project", "thread"]);
911export type OrchestrationAggregateKind = typeof OrchestrationAggregateKind.Type;
912export const OrchestrationActorKind = Schema.Literals(["client", "server", "provider"]);
913​
914export const ProjectCreatedPayload = Schema.Struct({
915 projectId: ProjectId,
916 title: TrimmedNonEmptyString,
917 workspaceRoot: TrimmedNonEmptyString,
918 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
919 defaultModelSelection: Schema.NullOr(ModelSelection),
920 scripts: Schema.Array(ProjectScript),
921 createdAt: IsoDateTime,
922 updatedAt: IsoDateTime,
923});
924​
925export const ProjectMetaUpdatedPayload = Schema.Struct({
926 projectId: ProjectId,
927 title: Schema.optional(TrimmedNonEmptyString),
928 workspaceRoot: Schema.optional(TrimmedNonEmptyString),
929 repositoryIdentity: Schema.optional(Schema.NullOr(RepositoryIdentity)),
930 defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
931 scripts: Schema.optional(Schema.Array(ProjectScript)),
932 updatedAt: IsoDateTime,
933});
934​
935export const ProjectDeletedPayload = Schema.Struct({
936 projectId: ProjectId,
937 deletedAt: IsoDateTime,
938});
939​
940export const ThreadCreatedPayload = Schema.Struct({
941 threadId: ThreadId,
942 projectId: ProjectId,
943 title: TrimmedNonEmptyString,
944 modelSelection: ModelSelection,
945 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
946 interactionMode: ProviderInteractionMode.pipe(
947 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
948 ),
949 branch: Schema.NullOr(TrimmedNonEmptyString),
950 worktreePath: Schema.NullOr(TrimmedNonEmptyString),
951 createdAt: IsoDateTime,
952 updatedAt: IsoDateTime,
953});
954​
955export const ThreadDeletedPayload = Schema.Struct({
956 threadId: ThreadId,
957 deletedAt: IsoDateTime,
958});
959​
960export const ThreadArchivedPayload = Schema.Struct({
961 threadId: ThreadId,
962 archivedAt: IsoDateTime,
963 updatedAt: IsoDateTime,
964});
965​
966export const ThreadUnarchivedPayload = Schema.Struct({
967 threadId: ThreadId,
968 updatedAt: IsoDateTime,
969});
970​
971export const ThreadSettledPayload = Schema.Struct({
972 threadId: ThreadId,
973 settledAt: IsoDateTime,
974 updatedAt: IsoDateTime,
975});
976​
977export const ThreadUnsettledPayload = Schema.Struct({
978 threadId: ThreadId,
979 reason: Schema.Literals(["user", "activity"]),
980 updatedAt: IsoDateTime,
981});
982​
983export const ThreadSnoozedPayload = Schema.Struct({
984 threadId: ThreadId,
985 snoozedUntil: IsoDateTime,
986 snoozedAt: IsoDateTime,
987 updatedAt: IsoDateTime,
988});
989​
990export const ThreadUnsnoozedPayload = Schema.Struct({
991 threadId: ThreadId,
992 // user: explicit "wake now". activity: real work arrived (user message /
993 // session coming alive) and the decider cleared the snooze — mirrors
994 // thread.unsettled's activity resets. Timer wakes emit no event: clients
995 // derive them from snoozedUntil passing.
996 reason: Schema.Literals(["user", "activity"]),
997 updatedAt: IsoDateTime,
998});
999​
1000export const ThreadMetaUpdatedPayload = Schema.Struct({
1001 threadId: ThreadId,
1002 title: Schema.optional(TrimmedNonEmptyString),
1003 modelSelection: Schema.optional(ModelSelection),
1004 branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
1005 worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
1006 updatedAt: IsoDateTime,
1007});
1008​
1009export const ThreadRuntimeModeSetPayload = Schema.Struct({
1010 threadId: ThreadId,
1011 runtimeMode: RuntimeMode,
1012 updatedAt: IsoDateTime,
1013});
1014​
1015export const ThreadInteractionModeSetPayload = Schema.Struct({
1016 threadId: ThreadId,
1017 interactionMode: ProviderInteractionMode.pipe(
1018 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
1019 ),
1020 updatedAt: IsoDateTime,
1021});
1022​
1023export const ThreadMessageSentPayload = Schema.Struct({
1024 threadId: ThreadId,
1025 messageId: MessageId,
1026 role: OrchestrationMessageRole,
1027 text: Schema.String,
1028 attachments: Schema.optional(Schema.Array(ChatAttachment)),
1029 turnId: Schema.NullOr(TurnId),
1030 streaming: Schema.Boolean,
1031 createdAt: IsoDateTime,
1032 updatedAt: IsoDateTime,
1033});
1034​
1035export const ThreadTurnStartRequestedPayload = Schema.Struct({
1036 threadId: ThreadId,
1037 messageId: MessageId,
1038 modelSelection: Schema.optional(ModelSelection),
1039 titleSeed: Schema.optional(TrimmedNonEmptyString),
1040 runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_RUNTIME_MODE))),
1041 interactionMode: ProviderInteractionMode.pipe(
1042 Schema.withDecodingDefault(Effect.succeed(DEFAULT_PROVIDER_INTERACTION_MODE)),
1043 ),
1044 sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
1045 createdAt: IsoDateTime,
1046});
1047​
1048export const ThreadTurnInterruptRequestedPayload = Schema.Struct({
1049 threadId: ThreadId,
1050 turnId: Schema.optional(TurnId),
1051 createdAt: IsoDateTime,
1052});
1053​
1054export const ThreadApprovalResponseRequestedPayload = Schema.Struct({
1055 threadId: ThreadId,
1056 requestId: ApprovalRequestId,
1057 decision: ProviderApprovalDecision,
1058 createdAt: IsoDateTime,
1059});
1060​
1061const ThreadUserInputResponseRequestedPayload = Schema.Struct({
1062 threadId: ThreadId,
1063 requestId: ApprovalRequestId,
1064 answers: ProviderUserInputAnswers,
1065 createdAt: IsoDateTime,
1066});
1067​
1068export const ThreadCheckpointRevertRequestedPayload = Schema.Struct({
1069 threadId: ThreadId,
1070 turnCount: NonNegativeInt,
1071 createdAt: IsoDateTime,
1072});
1073​
1074export const ThreadRevertedPayload = Schema.Struct({
1075 threadId: ThreadId,
1076 turnCount: NonNegativeInt,
1077});
1078​
1079export const ThreadSessionStopRequestedPayload = Schema.Struct({
1080 threadId: ThreadId,
1081 createdAt: IsoDateTime,
1082});
1083​
1084export const ThreadSessionSetPayload = Schema.Struct({
1085 threadId: ThreadId,
1086 session: OrchestrationSession,
1087});
1088​
1089export const ThreadProposedPlanUpsertedPayload = Schema.Struct({
1090 threadId: ThreadId,
1091 proposedPlan: OrchestrationProposedPlan,
1092});
1093​
1094export const ThreadTurnDiffCompletedPayload = Schema.Struct({
1095 threadId: ThreadId,
1096 turnId: TurnId,
1097 checkpointTurnCount: NonNegativeInt,
1098 checkpointRef: CheckpointRef,
1099 status: OrchestrationCheckpointStatus,
1100 files: Schema.Array(OrchestrationCheckpointFile),
1101 assistantMessageId: Schema.NullOr(MessageId),
1102 completedAt: IsoDateTime,
1103});
1104​
1105export const ThreadActivityAppendedPayload = Schema.Struct({
1106 threadId: ThreadId,
1107 activity: OrchestrationThreadActivity,
1108});
1109​
1110export const OrchestrationEventMetadata = Schema.Struct({
1111 providerTurnId: Schema.optional(TrimmedNonEmptyString),
1112 providerItemId: Schema.optional(ProviderItemId),
1113 adapterKey: Schema.optional(TrimmedNonEmptyString),
1114 requestId: Schema.optional(ApprovalRequestId),
1115 ingestedAt: Schema.optional(IsoDateTime),
1116});
1117export type OrchestrationEventMetadata = typeof OrchestrationEventMetadata.Type;
1118​
1119const EventBaseFields = {
1120 sequence: NonNegativeInt,
1121 eventId: EventId,
1122 aggregateKind: OrchestrationAggregateKind,
1123 aggregateId: Schema.Union([ProjectId, ThreadId]),
1124 occurredAt: IsoDateTime,
1125 commandId: Schema.NullOr(CommandId),
1126 causationEventId: Schema.NullOr(EventId),
1127 correlationId: Schema.NullOr(CommandId),
1128 metadata: OrchestrationEventMetadata,
1129} as const;
1130​
1131export const OrchestrationEvent = Schema.Union([
1132 Schema.Struct({
1133 ...EventBaseFields,
1134 type: Schema.Literal("project.created"),
1135 payload: ProjectCreatedPayload,
1136 }),
1137 Schema.Struct({
1138 ...EventBaseFields,
1139 type: Schema.Literal("project.meta-updated"),
1140 payload: ProjectMetaUpdatedPayload,
1141 }),
1142 Schema.Struct({
1143 ...EventBaseFields,
1144 type: Schema.Literal("project.deleted"),
1145 payload: ProjectDeletedPayload,
1146 }),
1147 Schema.Struct({
1148 ...EventBaseFields,
1149 type: Schema.Literal("thread.created"),
1150 payload: ThreadCreatedPayload,
1151 }),
1152 Schema.Struct({
1153 ...EventBaseFields,
1154 type: Schema.Literal("thread.deleted"),
1155 payload: ThreadDeletedPayload,
1156 }),
1157 Schema.Struct({
1158 ...EventBaseFields,
1159 type: Schema.Literal("thread.archived"),
1160 payload: ThreadArchivedPayload,
1161 }),
1162 Schema.Struct({
1163 ...EventBaseFields,
1164 type: Schema.Literal("thread.unarchived"),
1165 payload: ThreadUnarchivedPayload,
1166 }),
1167 Schema.Struct({
1168 ...EventBaseFields,
1169 type: Schema.Literal("thread.settled"),
1170 payload: ThreadSettledPayload,
1171 }),
1172 Schema.Struct({
1173 ...EventBaseFields,
1174 type: Schema.Literal("thread.unsettled"),
1175 payload: ThreadUnsettledPayload,
1176 }),
1177 Schema.Struct({
1178 ...EventBaseFields,
1179 type: Schema.Literal("thread.snoozed"),
1180 payload: ThreadSnoozedPayload,
1181 }),
1182 Schema.Struct({
1183 ...EventBaseFields,
1184 type: Schema.Literal("thread.unsnoozed"),
1185 payload: ThreadUnsnoozedPayload,
1186 }),
1187 Schema.Struct({
1188 ...EventBaseFields,
1189 type: Schema.Literal("thread.meta-updated"),
1190 payload: ThreadMetaUpdatedPayload,
1191 }),
1192 Schema.Struct({
1193 ...EventBaseFields,
1194 type: Schema.Literal("thread.runtime-mode-set"),
1195 payload: ThreadRuntimeModeSetPayload,
1196 }),
1197 Schema.Struct({
1198 ...EventBaseFields,
1199 type: Schema.Literal("thread.interaction-mode-set"),
1200 payload: ThreadInteractionModeSetPayload,
1201 }),
1202 Schema.Struct({
1203 ...EventBaseFields,
1204 type: Schema.Literal("thread.message-sent"),
1205 payload: ThreadMessageSentPayload,
1206 }),
1207 Schema.Struct({
1208 ...EventBaseFields,
1209 type: Schema.Literal("thread.turn-start-requested"),
1210 payload: ThreadTurnStartRequestedPayload,
1211 }),
1212 Schema.Struct({
1213 ...EventBaseFields,
1214 type: Schema.Literal("thread.turn-interrupt-requested"),
1215 payload: ThreadTurnInterruptRequestedPayload,
1216 }),
1217 Schema.Struct({
1218 ...EventBaseFields,
1219 type: Schema.Literal("thread.approval-response-requested"),
1220 payload: ThreadApprovalResponseRequestedPayload,
1221 }),
1222 Schema.Struct({
1223 ...EventBaseFields,
1224 type: Schema.Literal("thread.user-input-response-requested"),
1225 payload: ThreadUserInputResponseRequestedPayload,
1226 }),
1227 Schema.Struct({
1228 ...EventBaseFields,
1229 type: Schema.Literal("thread.checkpoint-revert-requested"),
1230 payload: ThreadCheckpointRevertRequestedPayload,
1231 }),
1232 Schema.Struct({
1233 ...EventBaseFields,
1234 type: Schema.Literal("thread.reverted"),
1235 payload: ThreadRevertedPayload,
1236 }),
1237 Schema.Struct({
1238 ...EventBaseFields,
1239 type: Schema.Literal("thread.session-stop-requested"),
1240 payload: ThreadSessionStopRequestedPayload,
1241 }),
1242 Schema.Struct({
1243 ...EventBaseFields,
1244 type: Schema.Literal("thread.session-set"),
1245 payload: ThreadSessionSetPayload,
1246 }),
1247 Schema.Struct({
1248 ...EventBaseFields,
1249 type: Schema.Literal("thread.proposed-plan-upserted"),
1250 payload: ThreadProposedPlanUpsertedPayload,
1251 }),
1252 Schema.Struct({
1253 ...EventBaseFields,
1254 type: Schema.Literal("thread.turn-diff-completed"),
1255 payload: ThreadTurnDiffCompletedPayload,
1256 }),
1257 Schema.Struct({
1258 ...EventBaseFields,
1259 type: Schema.Literal("thread.activity-appended"),
1260 payload: ThreadActivityAppendedPayload,
1261 }),
1262]);
1263export type OrchestrationEvent = typeof OrchestrationEvent.Type;
1264​
1265export const OrchestrationThreadStreamItem = Schema.Union([
1266 Schema.Struct({
1267 kind: Schema.Literal("synchronized"),
1268 }),
1269 Schema.Struct({
1270 kind: Schema.Literal("snapshot"),
1271 snapshot: OrchestrationThreadDetailSnapshot,
1272 }),
1273 Schema.Struct({
1274 kind: Schema.Literal("event"),
1275 event: OrchestrationEvent,
1276 }),
1277]);
1278export type OrchestrationThreadStreamItem = typeof OrchestrationThreadStreamItem.Type;
1279​
1280export const OrchestrationCommandReceiptStatus = Schema.Literals(["accepted", "rejected"]);
1281export type OrchestrationCommandReceiptStatus = typeof OrchestrationCommandReceiptStatus.Type;
1282​
1283export const TurnCountRange = Schema.Struct({
1284 fromTurnCount: NonNegativeInt,
1285 toTurnCount: NonNegativeInt,
1286}).check(
1287 Schema.makeFilter(
1288 (input) =>
1289 input.fromTurnCount <= input.toTurnCount ||
1290 new SchemaIssue.InvalidValue(Option.some(input.fromTurnCount), {
1291 message: "fromTurnCount must be less than or equal to toTurnCount",
1292 }),
1293 { identifier: "OrchestrationTurnDiffRange" },
1294 ),
1296​
1297export const ThreadTurnDiff = TurnCountRange.mapFields(
1298 Struct.assign({
1299 threadId: ThreadId,
1300 diff: Schema.String,
1301 }),
1302 { unsafePreserveChecks: true },
1304​
1305export const ProviderSessionRuntimeStatus = Schema.Literals([
1306 "starting",
1307 "running",
1308 "stopped",
1309 "error",
1310]);
1311export type ProviderSessionRuntimeStatus = typeof ProviderSessionRuntimeStatus.Type;
1312​
1313const ProjectionThreadTurnStatus = Schema.Literals([
1314 "running",
1315 "completed",
1316 "interrupted",
1317 "error",
1318]);
1319export type ProjectionThreadTurnStatus = typeof ProjectionThreadTurnStatus.Type;
1320​
1321const ProjectionCheckpointRow = Schema.Struct({
1322 threadId: ThreadId,
1323 turnId: TurnId,
1324 checkpointTurnCount: NonNegativeInt,
1325 checkpointRef: CheckpointRef,
1326 status: OrchestrationCheckpointStatus,
1327 files: Schema.Array(OrchestrationCheckpointFile),
1328 assistantMessageId: Schema.NullOr(MessageId),
1329 completedAt: IsoDateTime,
1330});
1331export type ProjectionCheckpointRow = typeof ProjectionCheckpointRow.Type;
1332​
1333export const ProjectionPendingApprovalStatus = Schema.Literals(["pending", "resolved"]);
1334export type ProjectionPendingApprovalStatus = typeof ProjectionPendingApprovalStatus.Type;
1335​
1336export const ProjectionPendingApprovalDecision = Schema.NullOr(ProviderApprovalDecision);
1337export type ProjectionPendingApprovalDecision = typeof ProjectionPendingApprovalDecision.Type;
1338​
1339export const DispatchResult = Schema.Struct({
1340 sequence: NonNegativeInt,
1341});
1342export type DispatchResult = typeof DispatchResult.Type;
1343​
1344export const OrchestrationGetTurnDiffInput = TurnCountRange.mapFields(
1345 Struct.assign({
1346 threadId: ThreadId,
1347 ignoreWhitespace: Schema.optionalKey(Schema.Boolean),
1348 }),
1349 { unsafePreserveChecks: true },
1351export type OrchestrationGetTurnDiffInput = typeof OrchestrationGetTurnDiffInput.Type;
1352​
1353export const OrchestrationGetTurnDiffResult = ThreadTurnDiff;
1354export type OrchestrationGetTurnDiffResult = typeof OrchestrationGetTurnDiffResult.Type;
1355​
1356export const OrchestrationGetFullThreadDiffInput = Schema.Struct({
1357 threadId: ThreadId,
1358 toTurnCount: NonNegativeInt,
1359 ignoreWhitespace: Schema.optionalKey(Schema.Boolean),
1360});
1361export type OrchestrationGetFullThreadDiffInput = typeof OrchestrationGetFullThreadDiffInput.Type;
1362​
1363export const OrchestrationGetFullThreadDiffResult = ThreadTurnDiff;
1364export type OrchestrationGetFullThreadDiffResult = typeof OrchestrationGetFullThreadDiffResult.Type;
1365​
1366export const OrchestrationReplayEventsInput = Schema.Struct({
1367 fromSequenceExclusive: NonNegativeInt,
1368});
1369export type OrchestrationReplayEventsInput = typeof OrchestrationReplayEventsInput.Type;
1370​
1371const OrchestrationReplayEventsResult = Schema.Array(OrchestrationEvent);
1372export type OrchestrationReplayEventsResult = typeof OrchestrationReplayEventsResult.Type;
1373​
1374export const OrchestrationRpcSchemas = {
1375 dispatchCommand: {
1376 input: ClientOrchestrationCommand,
1377 output: DispatchResult,
1378 },
1379 getTurnDiff: {
1380 input: OrchestrationGetTurnDiffInput,
1381 output: OrchestrationGetTurnDiffResult,
1382 },
1383 getFullThreadDiff: {
1384 input: OrchestrationGetFullThreadDiffInput,
1385 output: OrchestrationGetFullThreadDiffResult,
1386 },
1387 replayEvents: {
1388 input: OrchestrationReplayEventsInput,
1389 output: OrchestrationReplayEventsResult,
1390 },
1391 getArchivedShellSnapshot: {
1392 input: Schema.Struct({}),
1393 output: OrchestrationShellSnapshot,
1394 },
1395 subscribeThread: {
1396 input: OrchestrationSubscribeThreadInput,
1397 output: OrchestrationThreadStreamItem,
1398 },
1399 subscribeShell: {
1400 input: OrchestrationSubscribeShellInput,
1401 output: OrchestrationShellStreamItem,
1402 },
1403} as const;
1404​
1405export class OrchestrationGetSnapshotError extends Schema.TaggedErrorClass<OrchestrationGetSnapshotError>()(
1406 "OrchestrationGetSnapshotError",
1408 message: TrimmedNonEmptyString,
1409 cause: Schema.optional(Schema.Defect()),
1410 },
1411) {}
1412​
1413export class OrchestrationDispatchCommandError extends Schema.TaggedErrorClass<OrchestrationDispatchCommandError>()(
1414 "OrchestrationDispatchCommandError",
1416 message: TrimmedNonEmptyString,
1417 cause: Schema.optional(Schema.Defect()),
1418 },
1419) {}
1420​
1421export class OrchestrationGetTurnDiffError extends Schema.TaggedErrorClass<OrchestrationGetTurnDiffError>()(
1422 "OrchestrationGetTurnDiffError",
1424 message: TrimmedNonEmptyString,
1425 cause: Schema.optional(Schema.Defect()),
1426 },
1427) {}
1428​
1429export class OrchestrationGetFullThreadDiffError extends Schema.TaggedErrorClass<OrchestrationGetFullThreadDiffError>()(
1430 "OrchestrationGetFullThreadDiffError",
1432 message: TrimmedNonEmptyString,
1433 cause: Schema.optional(Schema.Defect()),
1434 },
1435) {}
1436​
1437export class OrchestrationReplayEventsError extends Schema.TaggedErrorClass<OrchestrationReplayEventsError>()(
1438 "OrchestrationReplayEventsError",
1440 message: TrimmedNonEmptyString,
1441 cause: Schema.optional(Schema.Defect()),
1442 },
1443) {}