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

OrchestrationEngine.ts

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