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

OrchestrationEventStore.ts

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