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