Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OrchestrationEngine.ts

82 lines3.0 KB

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