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

OrchestrationEventStore.ts

126 lines4.7 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 ApplicationProjectEvent,
15 ApplicationStoredEvent,
16 CommandId,
17 OrchestrationV2DomainEvent,
18 OrchestrationV2StoredEvent,
19 ThreadId,
20} from "@t3tools/contracts";
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/** A project event before the store assigns its sequence. */
28export type UnsequencedProjectEvent = ApplicationProjectEvent extends infer Event
29 ? Event extends ApplicationProjectEvent
30 ? Omit<Event, "sequence">
31 : never
32 : never;
33​
34/**
35 * OrchestrationEventStoreShape - Service API for orchestration event persistence.
36 */
37export interface OrchestrationEventStoreShape {
38 /** Append one project event to the shared application log. */
39 readonly appendProjectEvent: (
40 event: UnsequencedProjectEvent,
41 ) => Effect.Effect<ApplicationProjectEvent, OrchestrationEventStoreError>;
42​
43 /** Append V2 agent events to the same globally ordered application log. */
44 readonly appendAgentEvents: (input: {
45 readonly commandId?: CommandId;
46 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
47 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, OrchestrationEventStoreError>;
48​
49 /**
50 * Read only V2 thread events from the application log.
51 *
52 * Reads in fixed-size sequence pages until the filtered range is exhausted;
53 * `limit` caps the total emitted events across pages.
54 */
55 readonly readAgentEvents: (input?: {
56 readonly afterSequence?: number;
57 readonly throughSequence?: number;
58 readonly threadId?: ThreadId;
59 readonly commandId?: CommandId;
60 readonly eventType?: OrchestrationV2DomainEvent["type"];
61 readonly limit?: number;
62 }) => Stream.Stream<OrchestrationV2StoredEvent, OrchestrationEventStoreError>;
63​
64 /** Measure one thread's bounded replay without loading or decoding its payloads. */
65 readonly getAgentReplayStats: (input: {
66 readonly threadId: ThreadId;
67 readonly afterSequence: number;
68 readonly throughSequence: number;
69 readonly maxEvents: number;
70 }) => Effect.Effect<
71 {
72 readonly eventCount: number;
73 /** UTF-8 bytes in persisted payload JSON before decoding or wire projection. */
74 readonly rawPayloadBytes: number;
75 readonly hasCreateEvent: boolean;
76 },
77 OrchestrationEventStoreError
78 >;
79​
80 /**
81 * Measure the retained application-event range `(afterSequence, throughSequence]`
82 * without loading or decoding its payloads.
83 */
84 readonly getReplayStats: (input: {
85 readonly afterSequence: number;
86 readonly throughSequence: number;
87 }) => Effect.Effect<
88 {
89 readonly eventCount: number;
90 /** UTF-8 bytes in persisted payload JSON before decoding or wire projection. */
91 readonly rawPayloadBytes: number;
92 },
93 OrchestrationEventStoreError
94 >;
95​
96 readonly latestAgentSequence: (
97 threadId?: ThreadId,
98 ) => Effect.Effect<number, OrchestrationEventStoreError>;
99​
100 readonly latestApplicationSequence: Effect.Effect<number, OrchestrationEventStoreError>;
101​
102 /** Read the finite retained application-event range `(afterSequence, throughSequence]`. */
103 readonly readApplicationEvents: (input: {
104 readonly afterSequence: number;
105 readonly throughSequence: number;
106 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
107​
108 /** Publish only after the surrounding event/projection transaction commits. */
109 readonly publishCommitted: (events: ReadonlyArray<ApplicationStoredEvent>) => Effect.Effect<void>;
110​
111 /** Race-free replay-to-live stream for project and V2 thread events. */
112 readonly streamApplicationEvents: (input?: {
113 readonly afterSequence?: number;
114 }) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
115 /** Project transport events before bounding replay and the live tail. */
116 readonly streamProjectedApplicationEvents: <A extends { readonly sequence: number }>(input: {
117 readonly afterSequence?: number;
118 readonly project: (event: ApplicationStoredEvent) => A;
119 }) => Stream.Stream<A, OrchestrationEventStoreError>;
121​
122/** OrchestrationEventStore - Service tag for the shared application event log. */
123export class OrchestrationEventStore extends Context.Service<
124 OrchestrationEventStore,
125 OrchestrationEventStoreShape
126>()("t3/persistence/Services/OrchestrationEventStore") {}