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

orchestration.ts

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