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

OrchestrationEngine.ts

80 lines2.6 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 `ServiceMap.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 OrchestrationCommand,
15 OrchestrationEvent,
16 OrchestrationReadModel,
17} from "@t3tools/contracts";
18import { ServiceMap } from "effect";
19import type { Effect, Stream } from "effect";
20​
21import type { OrchestrationDispatchError } from "../Errors.ts";
22import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
23​
24/**
25 * OrchestrationEngineShape - Service API for orchestration command and event flow.
26 */
27export interface OrchestrationEngineShape {
28 /**
29 * Read the current in-memory orchestration read model.
30 *
31 * @returns Effect containing the latest read model.
32 */
33 readonly getReadModel: () => Effect.Effect<OrchestrationReadModel, never, never>;
34​
35 /**
36 * Replay persisted orchestration events from an exclusive sequence cursor.
37 *
38 * @param fromSequenceExclusive - Sequence cursor (exclusive).
39 * @returns Stream containing ordered events.
40 */
41 readonly readEvents: (
42 fromSequenceExclusive: number,
43 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
44​
45 /**
46 * Dispatch a validated orchestration command.
47 *
48 * @param command - Valid orchestration command.
49 * @returns Effect containing the sequence of the persisted event.
50 *
51 * Dispatch is serialized through an internal queue and deduplicated via
52 * command receipts.
53 */
54 readonly dispatch: (
55 command: OrchestrationCommand,
56 ) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
57​
58 /**
59 * Stream persisted domain events in dispatch order.
60 *
61 * This is a hot runtime stream (new events only), not a historical replay.
62 */
63 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
65​
66/**
67 * OrchestrationEngineService - Service tag for orchestration engine access.
68 *
69 * @example
70 * ```ts
71 * const program = Effect.gen(function* () {
72 * const engine = yield* OrchestrationEngineService
73 * return yield* engine.getReadModel()
74 * })
75 * ```
76 */
77export class OrchestrationEngineService extends ServiceMap.Service<
78 OrchestrationEngineService,
79 OrchestrationEngineShape
80>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}