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

OrchestrationEventStore.ts

126 lines4.6 KB

1/**
2 * Historical name for the shared application event store.
3 *
4 * Owns durable append/replay access for project events and V2 agent-thread
5 * events under one global sequence. It does not reduce events into read models
6 * or apply command validation rules.
7 *
8 * Uses Effect `Context.Service` for dependency injection and exposes typed
9 * persistence/decode errors for event append and replay operations.
10 *
11 * @module OrchestrationEventStore
12 */
13import type {
14 ApplicationStoredEvent,
15 CommandId,
16 OrchestrationV2DomainEvent,
17 OrchestrationV2StoredEvent,
18 ThreadId,
19} from "@t3tools/contracts";
20import type { OrchestrationEvent } from "@t3tools/contracts/legacy-orchestration";
21import * as Context from "effect/Context";
22import type * as Effect from "effect/Effect";
23import type * as Stream from "effect/Stream";
24​
25import type { OrchestrationEventStoreError } from "../Errors.ts";
26​
27/**
28 * OrchestrationEventStoreShape - Service API for orchestration event persistence.
29 */
30export interface OrchestrationEventStoreShape {
31 /**
32 * Persist a new orchestration event.
33 *
34 * @param event - Event payload without sequence (assigned by storage).
35 * @returns Effect containing the stored event with assigned sequence.
36 *
37 * Actor kind is inferred from command/metadata before persistence.
38 */
39 readonly append: (
40 event: Omit<OrchestrationEvent, "sequence">,
41 ) => Effect.Effect<OrchestrationEvent, OrchestrationEventStoreError>;
42​
43 /**
44 * Replay events after the provided sequence.
45 *
46 * @param sequenceExclusive - Sequence cursor (exclusive).
47 * @param limit - Maximum number of events to emit.
48 * @returns Stream containing ordered events.
49 *
50 * Reads in fixed-size pages and normalizes non-integer/negative limits.
51 */
52 readonly readFromSequence: (
53 sequenceExclusive: number,
54 limit?: number,
55 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
56​
57 /**
58 * Read all events from the beginning of the stream.
59 *
60 * @returns Stream containing all stored events.
61 */
62 readonly readAll: () => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
63​
64 /** Append V2 agent events to the same globally ordered application log. */
65 readonly appendAgentEvents: (input: {
66 readonly commandId?: CommandId;
67 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
68 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, OrchestrationEventStoreError>;
69​
70 /** Read only V2 thread events from the application log. */
71 readonly readAgentEvents: (input?: {
72 readonly afterSequence?: number;
73 readonly throughSequence?: number;
74 readonly threadId?: ThreadId;
75 readonly commandId?: CommandId;
76 readonly limit?: number;
77 }) => Stream.Stream<OrchestrationV2StoredEvent, OrchestrationEventStoreError>;
78​
79 readonly latestAgentSequence: (
80 threadId?: ThreadId,
81 ) => Effect.Effect<number, OrchestrationEventStoreError>;
82​
83 readonly latestApplicationSequence: Effect.Effect<number, OrchestrationEventStoreError>;
84​
85 /** Read the finite retained application-event range `(afterSequence, throughSequence]`. */
86 readonly readApplicationEvents: (input: {
87 readonly afterSequence: number;
88 readonly throughSequence: number;
89 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
90​
91 /** Publish only after the surrounding event/projection transaction commits. */
92 readonly publishCommitted: (events: ReadonlyArray<ApplicationStoredEvent>) => Effect.Effect<void>;
93​
94 /** Race-free replay-to-live stream for project and V2 thread events. */
95 readonly streamApplicationEvents: (input?: {
96 readonly afterSequence?: number;
97 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
98 /**
99 * Check whether an aggregate has an event of the given type after a sequence.
100 *
101 * Used during replay to tell whether a later event supersedes the one being
102 * applied, without streaming the rest of the log.
103 */
104 readonly hasEventAfter: (input: {
105 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
106 readonly aggregateId: string;
107 readonly type: OrchestrationEvent["type"];
108 readonly sequenceExclusive: number;
109 }) => Effect.Effect<boolean, OrchestrationEventStoreError>;
111​
112/**
113 * OrchestrationEventStore - Service tag for orchestration event persistence.
114 *
115 * @example
116 * ```ts
117 * const program = Effect.gen(function* () {
118 * const events = yield* OrchestrationEventStore
119 * return yield* Stream.runCollect(events.readAll())
120 * })
121 * ```
122 */
123export class OrchestrationEventStore extends Context.Service<
124 OrchestrationEventStore,
125 OrchestrationEventStoreShape
126>()("t3/persistence/Services/OrchestrationEventStore") {}