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} from "@t3tools/contracts";
18import * as Context from "effect/Context";
19import type * as Effect from "effect/Effect";
20import type * as Scope from "effect/Scope";
21import type * as Stream from "effect/Stream";
22
23import type { OrchestrationDispatchError } from "../Errors.ts";
24import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
25
26/**
27 * OrchestrationEngineShape - Service API for orchestration command and event flow.
28 */
29export interface OrchestrationEngineShape {
30 /**
31 * Replay persisted orchestration events from an exclusive sequence cursor.
32 *
33 * @param fromSequenceExclusive - Sequence cursor (exclusive).
34 * @param limit - Maximum number of events to read. Defaults to the event
35 * store's page-bounded default; pass a higher value when the caller must
36 * read every event after the cursor (e.g. per-thread catch-up that filters
37 * a small subset out of a potentially larger global range).
38 * @returns Stream containing ordered events.
39 */
40 readonly readEvents: (
41 fromSequenceExclusive: number,
42 limit?: number,
43 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
44
45 /**
46 * Dispatch a validated orchestration command.
47 *
48 * @param command - Valid orchestration command.
49 * @param options - Optional client origin (surface/app version) stamped into
50 * the metadata of every event the command produces.
51 * @returns Effect containing the sequence of the persisted event.
52 *
53 * Dispatch is serialized through an internal queue and deduplicated via
54 * command receipts.
55 */
56 readonly dispatch: (
57 command: OrchestrationCommand,
58 options?: { readonly origin?: OrchestrationClientOrigin },
59 ) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
60
61 /**
62 * Stream persisted domain events in dispatch order.
63 *
64 * This is a hot runtime stream (new events only), not a historical replay.
65 */
66 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
67
68 /**
69 * Acquire a domain-event subscription before starting a consumer.
70 * The subscription is ready when this effect returns and closes with the scope.
71 */
72 readonly subscribeDomainEvents: Effect.Effect<
73 Stream.Stream<OrchestrationEvent>,
74 never,
75 Scope.Scope
76 >;
77
78 /**
79 * The latest sequence reflected in the engine's authoritative command read
80 * model (0 if none). Used to gauge how far behind a resuming client is before
81 * choosing between an incremental replay and a fresh projected snapshot.
82 */
83 readonly latestSequence: Effect.Effect<number, never, never>;
84}
85
86/**
87 * OrchestrationEngineService - Service tag for orchestration engine access.
88 *
89 * @example
90 * ```ts
91 * const program = Effect.gen(function* () {
92 * const engine = yield* OrchestrationEngineService
93 * return yield* engine.dispatch(command)
94 * })
95 * ```
96 */
97export class OrchestrationEngineService extends Context.Service<
98 OrchestrationEngineService,
99 OrchestrationEngineShape
100>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}