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 { OrchestrationCommand, OrchestrationEvent } from "@t3tools/contracts";
14import * as Context from "effect/Context";
15import type * as Effect from "effect/Effect";
16import type * as Stream from "effect/Stream";
17
18import type { OrchestrationDispatchError } from "../Errors.ts";
19import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
20
21/**
22 * OrchestrationEngineShape - Service API for orchestration command and event flow.
23 */
24export interface OrchestrationEngineShape {
25 /**
26 * Replay persisted orchestration events from an exclusive sequence cursor.
27 *
28 * @param fromSequenceExclusive - Sequence cursor (exclusive).
29 * @param limit - Maximum number of events to read. Defaults to the event
30 * store's page-bounded default. Callers must keep this bounded; use a
31 * projection snapshot instead of replaying an arbitrarily stale cursor.
32 * @returns Stream containing ordered events.
33 */
34 readonly readEvents: (
35 fromSequenceExclusive: number,
36 limit?: number,
37 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
38
39 /**
40 * Dispatch a validated orchestration command.
41 *
42 * @param command - Valid orchestration command.
43 * @returns Effect containing the sequence of the persisted event.
44 *
45 * Dispatch is serialized through an internal queue and deduplicated via
46 * command receipts.
47 */
48 readonly dispatch: (
49 command: OrchestrationCommand,
50 ) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
51
52 /**
53 * Stream persisted domain events in dispatch order.
54 *
55 * This is a hot runtime stream (new events only), not a historical replay.
56 */
57 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
58
59 /**
60 * The latest sequence reflected in the engine's authoritative command read
61 * model (0 if none). Used to gauge how far behind a resuming client is before
62 * choosing between an incremental replay and a fresh projected snapshot.
63 */
64 readonly latestSequence: Effect.Effect<number, never, never>;
65}
66
67/**
68 * OrchestrationEngineService - Service tag for orchestration engine access.
69 *
70 * @example
71 * ```ts
72 * const program = Effect.gen(function* () {
73 * const engine = yield* OrchestrationEngineService
74 * return yield* engine.dispatch(command)
75 * })
76 * ```
77 */
78export class OrchestrationEngineService extends Context.Service<
79 OrchestrationEngineService,
80 OrchestrationEngineShape
81>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}