Open History to see the last commit
1import {
2 type ChatAttachment,
3 type CommandId,
4 MessageId,
5 type ModelSelection,
6 type OrchestrationV2Actor,
7 type OrchestrationV2Command,
8 type OrchestrationV2ConversationMessage,
9 type OrchestrationV2CreationSource,
10 type OrchestrationV2Run,
11 type OrchestrationV2ThreadShellSnapshot,
12 type OrchestrationV2ThreadProjection,
13 type OrchestrationV2ThreadShell,
14 type OrchestrationV2TurnItem,
15 ProjectId,
16 RunId,
17 ThreadId,
18} from "@t3tools/contracts";
19import * as Context from "effect/Context";
20import * as DateTime from "effect/DateTime";
21import * as Duration from "effect/Duration";
22import * as Effect from "effect/Effect";
23import * as Layer from "effect/Layer";
24import * as Option from "effect/Option";
25import * as Schema from "effect/Schema";
26
27import {
28 OrchestratorDispatchError,
29 OrchestratorProjectionError,
30 OrchestratorV2,
31 type OrchestratorV2DispatchResult,
32 type OrchestratorV2Error,
33} from "./Orchestrator.ts";
34import {
35 LegacyV1ThreadImporter,
36 type LegacyV1ThreadImportError,
37} from "./LegacyV1ThreadImporter.ts";
38
39export type ThreadManagementSendMode = "auto" | "queue" | "steer" | "restart";
40
41export interface ThreadManagementProvenance {
42 readonly createdBy: OrchestrationV2Actor;
43 readonly creationSource: OrchestrationV2CreationSource;
44}
45
46export function withCreationProvenance(
47 command: OrchestrationV2Command,
48 provenance: ThreadManagementProvenance,
49): OrchestrationV2Command {
50 switch (command.type) {
51 case "thread.create":
52 case "message.dispatch":
53 case "thread.fork":
54 case "thread.merge_back":
55 case "delegated_task.request":
56 return { ...command, ...provenance };
57 default:
58 return command;
59 }
60}
61
62export function existingThreadIdsForCommand(
63 command: OrchestrationV2Command,
64): ReadonlyArray<ThreadId> {
65 switch (command.type) {
66 case "thread.create":
67 return [];
68 // Read-state commands only rewrite the thread payload's visited/unread
69 // watermark; they never touch messages, so they do not need the imported
70 // v1 transcript hydrated first. Visits fire on every thread-activity bump
71 // while a thread is open, so keeping them off the import path matters.
72 case "thread.visit":
73 case "thread.mark-unread":
74 return [];
75 case "thread.fork":
76 return [command.sourceThreadId];
77 case "thread.merge_back":
78 return command.sourceThreadId === command.targetThreadId
79 ? [command.sourceThreadId]
80 : [command.sourceThreadId, command.targetThreadId];
81 case "delegated_task.request":
82 case "delegated_task.wake-policy":
83 case "delegated_task.completion-delivery.acknowledge":
84 case "delegated_task.completion-delivery.dispose":
85 return [command.parentThreadId];
86 case "thread.created.record":
87 return command.parentThreadId === command.targetThreadId
88 ? [command.parentThreadId]
89 : [command.parentThreadId, command.targetThreadId];
90 default:
91 return [command.threadId];
92 }
93}
94
95export type ThreadManagementTerminalRunStatus = Extract<
96 OrchestrationV2Run["status"],
97 "completed" | "failed" | "cancelled" | "interrupted" | "rolled_back"
98>;
99
100export interface ThreadManagementSendInput {
101 readonly projectId: ProjectId;
102 readonly commandId: CommandId;
103 readonly threadId: ThreadId;
104 readonly messageId: MessageId;
105 readonly text: string;
106 readonly attachments: ReadonlyArray<ChatAttachment>;
107 readonly modelSelection?: ModelSelection;
108 readonly mode: ThreadManagementSendMode;
109 readonly createdBy: OrchestrationV2Actor;
110 readonly creationSource: OrchestrationV2CreationSource;
111}
112
113export interface ThreadManagementSendResult {
114 readonly dispatch: OrchestratorV2DispatchResult;
115 readonly projection: OrchestrationV2ThreadProjection;
116 readonly message: OrchestrationV2ConversationMessage;
117 readonly run: OrchestrationV2Run;
118 /** Null for queued sends: the user turn item materializes when the queued turn starts. */
119 readonly turnItem: Extract<OrchestrationV2TurnItem, { readonly type: "user_message" }> | null;
120 readonly delivery: "started" | "queued" | "steered" | "restarted";
121}
122
123export interface ThreadManagementWaitInput {
124 readonly projectId: ProjectId;
125 readonly threadId: ThreadId;
126 readonly runId?: RunId;
127 readonly timeoutMs: number;
128 readonly pollIntervalMs?: number;
129}
130
131export interface ThreadManagementWaitResult {
132 readonly threadId: ThreadId;
133 readonly run: OrchestrationV2Run | null;
134 readonly timedOut: boolean;
135}
136
137export interface ThreadManagementInterruptInput {
138 readonly projectId: ProjectId;
139 readonly commandId: CommandId;
140 readonly threadId: ThreadId;
141 readonly runId?: RunId;
142 readonly reason?: string;
143}
144
145export type ThreadManagementInterruptResult =
146 | {
147 readonly type: "interrupt_requested";
148 readonly run: OrchestrationV2Run;
149 readonly dispatch: OrchestratorV2DispatchResult;
150 }
151 | { readonly type: "no_active_run" }
152 | {
153 readonly type: "already_terminal";
154 readonly run: OrchestrationV2Run & { readonly status: ThreadManagementTerminalRunStatus };
155 };
156
157export class ThreadManagementThreadNotFoundError extends Schema.TaggedErrorClass<ThreadManagementThreadNotFoundError>()(
158 "ThreadManagementThreadNotFoundError",
159 {
160 projectId: ProjectId,
161 threadId: ThreadId,
162 },
163) {
164 override get message(): string {
165 return `Thread ${this.threadId} was not found in project ${this.projectId}.`;
166 }
167}
168
169export class ThreadManagementRunNotFoundError extends Schema.TaggedErrorClass<ThreadManagementRunNotFoundError>()(
170 "ThreadManagementRunNotFoundError",
171 {
172 threadId: ThreadId,
173 runId: RunId,
174 },
175) {
176 override get message(): string {
177 return `Run ${this.runId} does not belong to thread ${this.threadId}.`;
178 }
179}
180
181export class ThreadManagementThreadArchivedError extends Schema.TaggedErrorClass<ThreadManagementThreadArchivedError>()(
182 "ThreadManagementThreadArchivedError",
183 {
184 threadId: ThreadId,
185 },
186) {
187 override get message(): string {
188 return `Thread ${this.threadId} is archived and cannot receive messages.`;
189 }
190}
191
192export class ThreadManagementNoSteerableRunError extends Schema.TaggedErrorClass<ThreadManagementNoSteerableRunError>()(
193 "ThreadManagementNoSteerableRunError",
194 {
195 threadId: ThreadId,
196 mode: Schema.Literals(["steer", "restart"]),
197 },
198) {
199 override get message(): string {
200 return `Thread ${this.threadId} has no running turn that can be ${this.mode === "steer" ? "steered" : "restarted"}.`;
201 }
202}
203
204export class ThreadManagementThreadNotInterruptibleError extends Schema.TaggedErrorClass<ThreadManagementThreadNotInterruptibleError>()(
205 "ThreadManagementThreadNotInterruptibleError",
206 {
207 threadId: ThreadId,
208 runId: RunId,
209 },
210) {
211 override get message(): string {
212 return `Run ${this.runId} is not currently interruptible.`;
213 }
214}
215
216export class ThreadManagementProjectionLoadError extends Schema.TaggedErrorClass<ThreadManagementProjectionLoadError>()(
217 "ThreadManagementProjectionLoadError",
218 {
219 projectId: ProjectId,
220 threadId: ThreadId,
221 cause: Schema.Defect(),
222 },
223) {
224 override get message(): string {
225 return `Unable to load thread ${this.threadId} in project ${this.projectId}.`;
226 }
227}
228
229export class ThreadManagementProjectThreadsListError extends Schema.TaggedErrorClass<ThreadManagementProjectThreadsListError>()(
230 "ThreadManagementProjectThreadsListError",
231 {
232 projectId: ProjectId,
233 cause: Schema.Defect(),
234 },
235) {
236 override get message(): string {
237 return `Unable to list threads in project ${this.projectId}.`;
238 }
239}
240
241export class ThreadManagementDurableRunProjectionError extends Schema.TaggedErrorClass<ThreadManagementDurableRunProjectionError>()(
242 "ThreadManagementDurableRunProjectionError",
243 {
244 threadId: ThreadId,
245 messageId: MessageId,
246 },
247) {
248 override get message(): string {
249 return `Message ${this.messageId} was accepted on thread ${this.threadId} without a durable run projection.`;
250 }
251}
252
253export const ThreadManagementError = Schema.Union([
254 ThreadManagementThreadNotFoundError,
255 ThreadManagementRunNotFoundError,
256 ThreadManagementThreadArchivedError,
257 ThreadManagementNoSteerableRunError,
258 ThreadManagementThreadNotInterruptibleError,
259 ThreadManagementProjectionLoadError,
260 ThreadManagementProjectThreadsListError,
261 ThreadManagementDurableRunProjectionError,
262]);
263export type ThreadManagementError = typeof ThreadManagementError.Type;
264
265type ThreadManagementFailure = ThreadManagementError | OrchestratorV2Error;
266
267export interface ThreadManagementServiceShape {
268 readonly ensureLegacyTranscript: (
269 threadId: ThreadId,
270 ) => Effect.Effect<void, LegacyV1ThreadImportError>;
271 readonly dispatch: (
272 command: OrchestrationV2Command,
273 ) => Effect.Effect<OrchestratorV2DispatchResult, OrchestratorV2Error>;
274 readonly getThreadProjection: (
275 threadId: ThreadId,
276 ) => Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorV2Error>;
277 readonly getThreadSnapshot: OrchestratorV2["Service"]["getThreadSnapshot"];
278 readonly getThreadSnapshotWindow: OrchestratorV2["Service"]["getThreadSnapshotWindow"];
279 readonly getProjectThread: (input: {
280 readonly projectId: ProjectId;
281 readonly threadId: ThreadId;
282 }) => Effect.Effect<OrchestrationV2ThreadProjection, ThreadManagementError>;
283 readonly getShellSnapshot: (options?: {
284 readonly location?: "active" | "archive";
285 }) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
286 readonly getThreadShell: OrchestratorV2["Service"]["getThreadShell"];
287 readonly listProjectThreads: (input: {
288 readonly projectId: ProjectId;
289 readonly includeSubagents: boolean;
290 }) => Effect.Effect<ReadonlyArray<OrchestrationV2ThreadShell>, ThreadManagementError>;
291 readonly sendToThread: (
292 input: ThreadManagementSendInput,
293 ) => Effect.Effect<ThreadManagementSendResult, ThreadManagementFailure>;
294 readonly waitForThread: (
295 input: ThreadManagementWaitInput,
296 ) => Effect.Effect<ThreadManagementWaitResult, ThreadManagementError>;
297 readonly interruptThread: (
298 input: ThreadManagementInterruptInput,
299 ) => Effect.Effect<ThreadManagementInterruptResult, ThreadManagementFailure>;
300 readonly getThreadEventSequence: OrchestratorV2["Service"]["getThreadEventSequence"];
301 readonly streamStoredEvents: OrchestratorV2["Service"]["streamStoredEvents"];
302 readonly streamStoredEventsFrom: OrchestratorV2["Service"]["streamStoredEventsFrom"];
303 readonly streamDomainEvents: OrchestratorV2["Service"]["streamDomainEvents"];
304}
305
306export class ThreadManagementService extends Context.Service<
307 ThreadManagementService,
308 ThreadManagementServiceShape
309>()("t3/orchestration-v2/ThreadManagementService") {}
310
311export function isActiveRun(run: OrchestrationV2Run): boolean {
312 return (
313 run.status === "preparing" ||
314 run.status === "starting" ||
315 run.status === "running" ||
316 run.status === "waiting"
317 );
318}
319
320export function isTerminalRunStatus(
321 status: OrchestrationV2Run["status"],
322): status is ThreadManagementTerminalRunStatus {
323 return (
324 status === "completed" ||
325 status === "failed" ||
326 status === "cancelled" ||
327 status === "interrupted" ||
328 status === "rolled_back"
329 );
330}
331
332export function latestRun(
333 projection: OrchestrationV2ThreadProjection,
334): OrchestrationV2Run | undefined {
335 return projection.runs.toSorted((left, right) => right.ordinal - left.ordinal)[0];
336}
337
338export function latestActiveRun(
339 projection: OrchestrationV2ThreadProjection,
340): OrchestrationV2Run | undefined {
341 return projection.runs
342 .filter(isActiveRun)
343 .toSorted((left, right) => right.ordinal - left.ordinal)[0];
344}
345
346export function latestSteerableRun(
347 projection: OrchestrationV2ThreadProjection,
348): OrchestrationV2Run | undefined {
349 return projection.runs
350 .filter(
351 (run) =>
352 run.status === "running" &&
353 run.activeAttemptId !== null &&
354 projection.providerTurns.some(
355 (turn) => turn.runAttemptId === run.activeAttemptId && turn.status === "running",
356 ),
357 )
358 .toSorted((left, right) => right.ordinal - left.ordinal)[0];
359}
360
361const make = Effect.gen(function* () {
362 const orchestrator = yield* OrchestratorV2;
363 const legacyImporter = yield* LegacyV1ThreadImporter;
364
365 const ensureLegacyTranscript = Effect.fn(
366 "orchestrationV2.threadManagement.ensureLegacyTranscript",
367 )(function* (threadId: ThreadId) {
368 yield* legacyImporter.ensureTranscript(threadId).pipe(
369 Effect.tapError((cause) =>
370 Effect.logWarning("Unable to hydrate migrated v1 thread transcript", {
371 threadId,
372 cause,
373 }),
374 ),
375 );
376 });
377
378 const ensureProjectionTranscript = (threadId: ThreadId) =>
379 ensureLegacyTranscript(threadId).pipe(
380 Effect.mapError(
381 (cause) =>
382 new OrchestratorProjectionError({
383 threadId,
384 cause,
385 }),
386 ),
387 );
388
389 const ensureCommandTranscripts = Effect.fn(
390 "orchestrationV2.threadManagement.ensureCommandTranscripts",
391 )(function* (command: OrchestrationV2Command) {
392 yield* Effect.forEach(
393 existingThreadIdsForCommand(command),
394 (threadId) => ensureLegacyTranscript(threadId),
395 { discard: true },
396 ).pipe(
397 Effect.mapError(
398 (cause) =>
399 new OrchestratorDispatchError({
400 commandId: command.commandId,
401 commandType: command.type,
402 cause,
403 }),
404 ),
405 );
406 });
407
408 const getThreadProjection: ThreadManagementServiceShape["getThreadProjection"] = (threadId) =>
409 ensureProjectionTranscript(threadId).pipe(
410 Effect.andThen(orchestrator.getThreadProjection(threadId)),
411 );
412
413 const getThreadSnapshot: ThreadManagementServiceShape["getThreadSnapshot"] = (threadId) =>
414 ensureProjectionTranscript(threadId).pipe(
415 Effect.andThen(orchestrator.getThreadSnapshot(threadId)),
416 );
417 const getThreadSnapshotWindow: ThreadManagementServiceShape["getThreadSnapshotWindow"] = (
418 threadId,
419 options,
420 ) =>
421 ensureProjectionTranscript(threadId).pipe(
422 Effect.andThen(orchestrator.getThreadSnapshotWindow(threadId, options)),
423 );
424
425 const dispatch: ThreadManagementServiceShape["dispatch"] = (command) =>
426 ensureCommandTranscripts(command).pipe(Effect.andThen(orchestrator.dispatch(command)));
427
428 const getProjectThread: ThreadManagementServiceShape["getProjectThread"] = (input) =>
429 getThreadProjection(input.threadId).pipe(
430 Effect.mapError(
431 (cause) =>
432 new ThreadManagementProjectionLoadError({
433 projectId: input.projectId,
434 threadId: input.threadId,
435 cause,
436 }),
437 ),
438 Effect.flatMap((projection) =>
439 projection.thread.projectId === input.projectId && projection.thread.deletedAt === null
440 ? Effect.succeed(projection)
441 : Effect.fail(
442 new ThreadManagementThreadNotFoundError({
443 projectId: input.projectId,
444 threadId: input.threadId,
445 }),
446 ),
447 ),
448 );
449
450 const listProjectThreads: ThreadManagementServiceShape["listProjectThreads"] = (input) =>
451 orchestrator.getShellSnapshot().pipe(
452 Effect.mapError(
453 (cause) =>
454 new ThreadManagementProjectThreadsListError({
455 projectId: input.projectId,
456 cause,
457 }),
458 ),
459 Effect.map((snapshot) =>
460 snapshot.threads
461 .filter((thread) => thread.projectId === input.projectId)
462 .filter(
463 (thread) =>
464 input.includeSubagents || thread.lineage.relationshipToParent !== "subagent",
465 )
466 .toSorted(
467 (left, right) =>
468 DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
469 right.id.localeCompare(left.id),
470 ),
471 ),
472 );
473
474 const sendToThread: ThreadManagementServiceShape["sendToThread"] = (input) =>
475 Effect.gen(function* () {
476 const target = yield* getProjectThread(input);
477 if (target.thread.archivedAt !== null) {
478 return yield* new ThreadManagementThreadArchivedError({
479 threadId: input.threadId,
480 });
481 }
482
483 const steerableRun = latestSteerableRun(target);
484 let dispatchMode: Extract<
485 OrchestrationV2Command,
486 { readonly type: "message.dispatch" }
487 >["dispatchMode"];
488 if (input.mode === "steer" || input.mode === "restart") {
489 if (steerableRun === undefined) {
490 return yield* new ThreadManagementNoSteerableRunError({
491 threadId: input.threadId,
492 mode: input.mode,
493 });
494 }
495 dispatchMode = {
496 type: input.mode === "steer" ? "steer_active" : "restart_active",
497 targetRunId: steerableRun.id,
498 };
499 } else if (input.mode === "auto" && steerableRun !== undefined) {
500 dispatchMode = { type: "steer_active", targetRunId: steerableRun.id };
501 } else {
502 dispatchMode = {
503 type: input.mode === "queue" ? "queue_after_active" : "start_immediately",
504 };
505 }
506
507 const dispatch = yield* orchestrator.dispatch({
508 type: "message.dispatch",
509 commandId: input.commandId,
510 threadId: input.threadId,
511 messageId: input.messageId,
512 text: input.text,
513 attachments: input.attachments,
514 ...(input.modelSelection === undefined ? {} : { modelSelection: input.modelSelection }),
515 dispatchMode,
516 createdBy: input.createdBy,
517 creationSource: input.creationSource,
518 });
519 const projection = yield* getProjectThread(input);
520 const message = projection.messages.find((candidate) => candidate.id === input.messageId);
521 const run =
522 message?.runId === null || message?.runId === undefined
523 ? undefined
524 : projection.runs.find((candidate) => candidate.id === message.runId);
525 const turnItem =
526 projection.turnItems.find(
527 (
528 candidate,
529 ): candidate is Extract<OrchestrationV2TurnItem, { readonly type: "user_message" }> =>
530 candidate.type === "user_message" && candidate.messageId === input.messageId,
531 ) ?? null;
532 // A queued message's user turn item is deliberately not emitted at
533 // dispatch time — it materializes when the queued turn actually starts,
534 // so it can map onto the provider turn. Every other dispatch mode still
535 // produces its turn item transactionally with the run.
536 if (
537 message === undefined ||
538 run === undefined ||
539 (turnItem === null && run.status !== "queued")
540 ) {
541 return yield* new ThreadManagementDurableRunProjectionError({
542 threadId: input.threadId,
543 messageId: input.messageId,
544 });
545 }
546 const delivery: ThreadManagementSendResult["delivery"] =
547 turnItem === null || turnItem.inputIntent === "queued_turn"
548 ? "queued"
549 : turnItem.inputIntent === "turn_start"
550 ? "started"
551 : input.mode === "restart"
552 ? "restarted"
553 : "steered";
554 return { dispatch, projection, message, run, turnItem, delivery };
555 });
556
557 const waitForThread: ThreadManagementServiceShape["waitForThread"] = (input) =>
558 Effect.gen(function* () {
559 const target = yield* getProjectThread(input);
560 const selectedRun =
561 input.runId === undefined
562 ? latestRun(target)
563 : target.runs.find((candidate) => candidate.id === input.runId);
564 if (input.runId !== undefined && selectedRun === undefined) {
565 return yield* new ThreadManagementRunNotFoundError({
566 threadId: input.threadId,
567 runId: input.runId,
568 });
569 }
570 if (selectedRun === undefined) {
571 return { threadId: input.threadId, run: null, timedOut: false };
572 }
573 if (isTerminalRunStatus(selectedRun.status)) {
574 return { threadId: input.threadId, run: selectedRun, timedOut: false };
575 }
576
577 const wait = Effect.gen(function* () {
578 while (true) {
579 const current = yield* getProjectThread(input);
580 const run = current.runs.find((candidate) => candidate.id === selectedRun.id);
581 if (run === undefined) {
582 return yield* new ThreadManagementRunNotFoundError({
583 threadId: input.threadId,
584 runId: selectedRun.id,
585 });
586 }
587 if (isTerminalRunStatus(run.status)) return run;
588 yield* Effect.sleep(Duration.millis(Math.max(1, input.pollIntervalMs ?? 250)));
589 }
590 }).pipe(Effect.timeoutOption(Duration.millis(Math.max(1, input.timeoutMs))));
591 const waited = yield* wait;
592 if (Option.isSome(waited)) {
593 return { threadId: input.threadId, run: waited.value, timedOut: false };
594 }
595 const current = yield* getProjectThread(input);
596 const run = current.runs.find((candidate) => candidate.id === selectedRun.id);
597 if (run === undefined) {
598 return yield* new ThreadManagementRunNotFoundError({
599 threadId: input.threadId,
600 runId: selectedRun.id,
601 });
602 }
603 return { threadId: input.threadId, run, timedOut: true };
604 });
605
606 const interruptThread: ThreadManagementServiceShape["interruptThread"] = (input) =>
607 Effect.gen(function* () {
608 const target = yield* getProjectThread(input);
609 const explicitRun =
610 input.runId === undefined
611 ? undefined
612 : target.runs.find((candidate) => candidate.id === input.runId);
613 if (input.runId !== undefined && explicitRun === undefined) {
614 return yield* new ThreadManagementRunNotFoundError({
615 threadId: input.threadId,
616 runId: input.runId,
617 });
618 }
619 if (explicitRun !== undefined && isTerminalRunStatus(explicitRun.status)) {
620 return {
621 type: "already_terminal",
622 run: explicitRun as OrchestrationV2Run & {
623 readonly status: ThreadManagementTerminalRunStatus;
624 },
625 } as const;
626 }
627 const interruptibleRun = latestActiveRun(target);
628 if (interruptibleRun === undefined) {
629 if (input.runId === undefined) {
630 return { type: "no_active_run" } as const;
631 }
632 return yield* new ThreadManagementThreadNotInterruptibleError({
633 threadId: input.threadId,
634 runId: input.runId,
635 });
636 }
637 if (input.runId !== undefined && interruptibleRun.id !== input.runId) {
638 return yield* new ThreadManagementThreadNotInterruptibleError({
639 threadId: input.threadId,
640 runId: input.runId,
641 });
642 }
643 const dispatch = yield* orchestrator.dispatch({
644 type: "run.interrupt",
645 commandId: input.commandId,
646 threadId: input.threadId,
647 runId: interruptibleRun.id,
648 ...(input.reason === undefined ? {} : { reason: input.reason }),
649 });
650 return { type: "interrupt_requested", run: interruptibleRun, dispatch } as const;
651 });
652
653 return ThreadManagementService.of({
654 ensureLegacyTranscript,
655 dispatch,
656 getThreadProjection,
657 getThreadSnapshot,
658 getThreadSnapshotWindow,
659 getProjectThread,
660 getShellSnapshot: orchestrator.getShellSnapshot,
661 getThreadShell: orchestrator.getThreadShell,
662 listProjectThreads,
663 sendToThread,
664 waitForThread,
665 interruptThread,
666 getThreadEventSequence: orchestrator.getThreadEventSequence,
667 streamStoredEvents: orchestrator.streamStoredEvents,
668 streamStoredEventsFrom: orchestrator.streamStoredEventsFrom,
669 streamDomainEvents: orchestrator.streamDomainEvents,
670 });
671});
672
673const legacyV1ThreadImporterNoopLayer = Layer.succeed(
674 LegacyV1ThreadImporter,
675 LegacyV1ThreadImporter.of({
676 pendingThreadCount: Effect.succeed(0),
677 reconcileShells: Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }),
678 ensureTranscript: () => Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }),
679 importPendingTranscripts: Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }),
680 }),
681);
682
683export const layer: Layer.Layer<ThreadManagementService, never, OrchestratorV2> = Layer.effect(
684 ThreadManagementService,
685 make,
686).pipe(Layer.provide(legacyV1ThreadImporterNoopLayer));
687
688export const layerWithLegacyImporter: Layer.Layer<
689 ThreadManagementService,
690 never,
691 LegacyV1ThreadImporter | OrchestratorV2
692> = Layer.effect(ThreadManagementService, make);