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

OrchestrationEngine.ts

70 lines2.4 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 * @returns Stream containing ordered events.
30 */
31 readonly readEvents: (
32 fromSequenceExclusive: number,
33 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
34​
35 /**
36 * Dispatch a validated orchestration command.
37 *
38 * @param command - Valid orchestration command.
39 * @returns Effect containing the sequence of the persisted event.
40 *
41 * Dispatch is serialized through an internal queue and deduplicated via
42 * command receipts.
43 */
44 readonly dispatch: (
45 command: OrchestrationCommand,
46 ) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
47​
48 /**
49 * Stream persisted domain events in dispatch order.
50 *
51 * This is a hot runtime stream (new events only), not a historical replay.
52 */
53 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
55​
56/**
57 * OrchestrationEngineService - Service tag for orchestration engine access.
58 *
59 * @example
60 * ```ts
61 * const program = Effect.gen(function* () {
62 * const engine = yield* OrchestrationEngineService
63 * return yield* engine.dispatch(command)
64 * })
65 * ```
66 */
67export class OrchestrationEngineService extends Context.Service<
68 OrchestrationEngineService,
69 OrchestrationEngineShape
70>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}