Files
t3code/apps/server/src/orchestration/Services/OrchestrationEngine.ts
T
3405a64dd8 bump effect to latest beta (#1866)
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: codex <[email protected]>
2026-04-10 01:51:31 -07:00

81 lines
2.6 KiB
TypeScript

/**
* OrchestrationEngineService - Service interface for orchestration command handling.
*
* Owns command validation/dispatch and in-memory read-model updates backed by
* `OrchestrationEventStore` persistence. It does not own provider process
* management or transport concerns (e.g. websocket request parsing).
*
* Uses Effect `Context.Service` for dependency injection. Command dispatch,
* replay, and unknown-input decoding all return typed domain errors.
*
* @module OrchestrationEngineService
*/
import type {
OrchestrationCommand,
OrchestrationEvent,
OrchestrationReadModel,
} from "@t3tools/contracts";
import { Context } from "effect";
import type { Effect, Stream } from "effect";
import type { OrchestrationDispatchError } from "../Errors.ts";
import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
/**
* OrchestrationEngineShape - Service API for orchestration command and event flow.
*/
export interface OrchestrationEngineShape {
/**
* Read the current in-memory orchestration read model.
*
* @returns Effect containing the latest read model.
*/
readonly getReadModel: () => Effect.Effect<OrchestrationReadModel, never, never>;
/**
* Replay persisted orchestration events from an exclusive sequence cursor.
*
* @param fromSequenceExclusive - Sequence cursor (exclusive).
* @returns Stream containing ordered events.
*/
readonly readEvents: (
fromSequenceExclusive: number,
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
/**
* Dispatch a validated orchestration command.
*
* @param command - Valid orchestration command.
* @returns Effect containing the sequence of the persisted event.
*
* Dispatch is serialized through an internal queue and deduplicated via
* command receipts.
*/
readonly dispatch: (
command: OrchestrationCommand,
) => Effect.Effect<{ sequence: number }, OrchestrationDispatchError, never>;
/**
* Stream persisted domain events in dispatch order.
*
* This is a hot runtime stream (new events only), not a historical replay.
*/
readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
}
/**
* OrchestrationEngineService - Service tag for orchestration engine access.
*
* @example
* ```ts
* const program = Effect.gen(function* () {
* const engine = yield* OrchestrationEngineService
* return yield* engine.getReadModel()
* })
* ```
*/
export class OrchestrationEngineService extends Context.Service<
OrchestrationEngineService,
OrchestrationEngineShape
>()("t3/orchestration/Services/OrchestrationEngine/OrchestrationEngineService") {}