Open History to see the last commit
1/**
2 * OrchestrationEngineService - Service interface for orchestration command handling.
3 *
4 * Owns command validation/dispatch and in-memory read-model updates backed by
5 * `OrchestrationEventStore` persistence. It does not own provider process
6 * management or transport concerns (e.g. websocket request parsing).
7 *
8 * Uses Effect `Context.Service` for dependency injection. Command dispatch,
9 * replay, and unknown-input decoding all return typed domain errors.
10 *
11 * @module OrchestrationEngineService
12 */
13import type {
14 OrchestrationClientOrigin,
15 OrchestrationCommand,
16 OrchestrationEvent,
17 ThreadId,
18} from "@t3tools/contracts";
19import * as Context from "effect/Context";
20import type * as Effect from "effect/Effect";
21import type * as Scope from "effect/Scope";
22import type * as Stream from "effect/Stream";
23
24import type { OrchestrationDispatchError } from "../Errors.ts";
25import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
26import type { OrchestrationAggregateReplayStats } from "../../persistence/Services/OrchestrationEventStore.ts";
27
28export interface OrchestrationThreadReplayRange {
29 readonly threadId: ThreadId;
30 readonly fromSequenceExclusive: number;
31 readonly toSequenceInclusive: number;
32}
33
34/**
35 * OrchestrationEngineShape - Service API for orchestration command and event flow.
36 */
37export interface OrchestrationEngineShape {
38 /**
39 * Replay persisted orchestration events from an exclusive sequence cursor.
40 *
41 * @param fromSequenceExclusive - Sequence cursor (exclusive).
42 * @param limit - Maximum number of events to read. Defaults to the event
43 * store's page-bounded default; pass a higher value when the caller must
44 * read a wider global range. Thread subscriptions use readThreadEvents.
45 * @returns Stream containing ordered events.
46 */
47 readonly readEvents: (
48 fromSequenceExclusive: number,
49 limit?: number,
50 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
51
52 /** Read only this thread's events through a captured authoritative head. */
53 readonly readThreadEvents: (
54 input: OrchestrationThreadReplayRange & { readonly limit?: number },
55 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
56
57 /** Measure a bounded thread replay without decoding its event bodies. */
58 readonly getThreadReplayStats: (
59 input: OrchestrationThreadReplayRange & { readonly maxEvents: number },
60 ) => Effect.Effect<OrchestrationAggregateReplayStats, OrchestrationEventStoreError>;
61
62 /**
63 * Dispatch a validated orchestration command.
64 *
65 * @param command - Valid orchestration command.
66 * @param options - Optional client origin (surface/app version) stamped into
67 * the metadata of every event the command produces.
68 * @returns Effect containing the sequence of the persisted event.
69 *
70 * Dispatch is serialized through an internal queue and deduplicated via
71 * command receipts.
72 */
73 readonly dispatch: (
74 command: OrchestrationCommand,
75 options?: { readonly origin?: OrchestrationClientOrigin },
76 ) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
77
78 /**
79 * Stream persisted domain events in dispatch order.
80 *
81 * This is a hot runtime stream (new events only), not a historical replay.
82 */
83 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
84
85 /**
86 * Acquire a domain-event subscription before starting a consumer.
87 * The subscription is ready when this effect returns and closes with the scope.
88 */
89 readonly subscribeDomainEvents: Effect.Effect<
90 Stream.Stream<OrchestrationEvent>,
91 never,
92 Scope.Scope
93 >;
94
95 /**
96 * The latest sequence reflected in the engine's authoritative command read
97 * model (0 if none). Used to gauge how far behind a resuming client is before
98 * choosing between an incremental replay and a fresh projected snapshot.
99 */
100 readonly latestSequence: Effect.Effect<number, never, never>;
101}
102
103/**
104 * OrchestrationEngineService - Service tag for orchestration engine access.
105 *
106 * @example
107 * ```ts
108 * const program = Effect.gen(function* () {
109 * const engine = yield* OrchestrationEngineService
110 * return yield* engine.dispatch(command)
111 * })
112 * ```
113 */
114export class OrchestrationEngineService extends Context.Service<
115 OrchestrationEngineService,
116 OrchestrationEngineShape
117>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}