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

OrchestrationEventStore.ts

153 lines5.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 /**
71 * Read only V2 thread events from the application log.
72 *
73 * Reads in fixed-size sequence pages until the filtered range is exhausted;
74 * `limit` caps the total emitted events across pages.
75 */
76 readonly readAgentEvents: (input?: {
77 readonly afterSequence?: number;
78 readonly throughSequence?: number;
79 readonly threadId?: ThreadId;
80 readonly commandId?: CommandId;
81 readonly limit?: number;
82 }) => Stream.Stream<OrchestrationV2StoredEvent, OrchestrationEventStoreError>;
83​
84 /** Measure one thread's bounded replay without loading or decoding its payloads. */
85 readonly getAgentReplayStats: (input: {
86 readonly threadId: ThreadId;
87 readonly afterSequence: number;
88 readonly throughSequence: number;
89 readonly maxEvents: number;
90 }) => Effect.Effect<
91 {
92 readonly eventCount: number;
93 /** UTF-8 bytes in persisted payload JSON before decoding or wire projection. */
94 readonly rawPayloadBytes: number;
95 readonly hasCreateEvent: boolean;
96 },
97 OrchestrationEventStoreError
98 >;
99​
100 readonly latestAgentSequence: (
101 threadId?: ThreadId,
102 ) => Effect.Effect<number, OrchestrationEventStoreError>;
103​
104 readonly latestApplicationSequence: Effect.Effect<number, OrchestrationEventStoreError>;
105​
106 /** Read the finite retained application-event range `(afterSequence, throughSequence]`. */
107 readonly readApplicationEvents: (input: {
108 readonly afterSequence: number;
109 readonly throughSequence: number;
110 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
111​
112 /** Publish only after the surrounding event/projection transaction commits. */
113 readonly publishCommitted: (events: ReadonlyArray<ApplicationStoredEvent>) => Effect.Effect<void>;
114​
115 /** Race-free replay-to-live stream for project and V2 thread events. */
116 readonly streamApplicationEvents: (input?: {
117 readonly afterSequence?: number;
118 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
119 /** Project transport events before bounding replay and the live tail. */
120 readonly streamProjectedApplicationEvents: <A extends { readonly sequence: number }>(input: {
121 readonly afterSequence?: number;
122 readonly project: (event: ApplicationStoredEvent) => A;
123 }) => Stream.Stream<A, OrchestrationEventStoreError>;
124 /**
125 * Check whether an aggregate has an event after a sequence, optionally
126 * restricted to one event type.
127 *
128 * Used during replay to tell whether a later event supersedes the one being
129 * applied, without streaming the rest of the log.
130 */
131 readonly hasEventAfter: (input: {
132 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
133 readonly aggregateId: string;
134 readonly type?: OrchestrationEvent["type"];
135 readonly sequenceExclusive: number;
136 }) => Effect.Effect<boolean, OrchestrationEventStoreError>;
138​
139/**
140 * OrchestrationEventStore - Service tag for orchestration event persistence.
141 *
142 * @example
143 * ```ts
144 * const program = Effect.gen(function* () {
145 * const events = yield* OrchestrationEventStore
146 * return yield* Stream.runCollect(events.readAll())
147 * })
148 * ```
149 */
150export class OrchestrationEventStore extends Context.Service<
151 OrchestrationEventStore,
152 OrchestrationEventStoreShape
153>()("t3/persistence/Services/OrchestrationEventStore") {}