Open History to see the last commit
1import {
2 type ApplicationProjectEvent,
3 type ApplicationStoredEvent,
4 CommandId,
5 OrchestrationV2DomainEvent,
6 OrchestrationV2StoredEvent,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Context from "effect/Context";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Schema from "effect/Schema";
13import * as Stream from "effect/Stream";
14import type * as SqlClient from "effect/unstable/sql/SqlClient";
15
16import { OrchestrationEventStoreLive } from "../persistence/Layers/OrchestrationEventStore.ts";
17import * as OrchestrationEventStore from "../persistence/Services/OrchestrationEventStore.ts";
18
19export class EventStoreAppendEventsError extends Schema.TaggedError<EventStoreAppendEventsError>()(
20 "EventStoreAppendEventsError",
21 {
22 eventCount: Schema.Number,
23 cause: Schema.optional(Schema.Defect()),
24 },
25) {
26 override get message(): string {
27 return `Failed to append ${this.eventCount} agent orchestration event(s).`;
28 }
29}
30
31export class EventStoreReadEventsError extends Schema.TaggedError<EventStoreReadEventsError>()(
32 "EventStoreReadEventsError",
33 {
34 afterSequence: Schema.optional(Schema.Number),
35 threadId: Schema.optional(ThreadId),
36 cause: Schema.optional(Schema.Defect()),
37 },
38) {
39 override get message(): string {
40 return this.threadId === undefined
41 ? "Failed to read agent orchestration events."
42 : `Failed to read agent orchestration events for thread ${this.threadId}.`;
43 }
44}
45
46export const EventStoreV2Error = Schema.Union([
47 EventStoreAppendEventsError,
48 EventStoreReadEventsError,
49]);
50export type EventStoreV2Error = typeof EventStoreV2Error.Type;
51
52export interface EventStoreV2Shape {
53 readonly append: (input: {
54 readonly commandId?: CommandId;
55 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
56 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventStoreV2Error>;
57 readonly appendProjectEvent: (
58 event: OrchestrationEventStore.UnsequencedProjectEvent,
59 ) => Effect.Effect<ApplicationProjectEvent, EventStoreV2Error>;
60 readonly read: (input?: {
61 readonly afterSequence?: number;
62 readonly throughSequence?: number;
63 readonly threadId?: ThreadId;
64 readonly eventType?: OrchestrationV2DomainEvent["type"];
65 readonly limit?: number;
66 }) => Stream.Stream<OrchestrationV2StoredEvent, EventStoreV2Error>;
67 readonly readByCommandId: (input: {
68 readonly commandId: CommandId;
69 }) => Stream.Stream<OrchestrationV2StoredEvent, EventStoreV2Error>;
70 readonly latestSequence: (input?: {
71 readonly threadId?: ThreadId;
72 }) => Effect.Effect<number, EventStoreV2Error>;
73 /** Latest sequence across project and V2 thread events. */
74 readonly latestApplicationSequence: Effect.Effect<number, EventStoreV2Error>;
75 readonly publishCommitted: (events: ReadonlyArray<ApplicationStoredEvent>) => Effect.Effect<void>;
76}
77
78export class EventStoreV2 extends Context.Service<EventStoreV2, EventStoreV2Shape>()(
79 "t3/orchestration-v2/EventStore/EventStoreV2",
80) {}
81
82const baseLayer: Layer.Layer<EventStoreV2, never, OrchestrationEventStore.OrchestrationEventStore> =
83 Layer.effect(
84 EventStoreV2,
85 Effect.gen(function* () {
86 const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
87
88 const read: EventStoreV2Shape["read"] = (input) =>
89 applicationEvents
90 .readAgentEvents({
91 ...(input?.afterSequence === undefined ? {} : { afterSequence: input.afterSequence }),
92 ...(input?.throughSequence === undefined
93 ? {}
94 : { throughSequence: input.throughSequence }),
95 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
96 ...(input?.eventType === undefined ? {} : { eventType: input.eventType }),
97 ...(input?.limit === undefined ? {} : { limit: input.limit }),
98 })
99 .pipe(
100 Stream.mapError(
101 (cause) =>
102 new EventStoreReadEventsError({
103 ...(input?.afterSequence === undefined
104 ? {}
105 : { afterSequence: input.afterSequence }),
106 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
107 cause,
108 }),
109 ),
110 );
111
112 return EventStoreV2.of({
113 append: (input) =>
114 applicationEvents.appendAgentEvents(input).pipe(
115 Effect.mapError(
116 (cause) =>
117 new EventStoreAppendEventsError({
118 eventCount: input.events.length,
119 cause,
120 }),
121 ),
122 ),
123 appendProjectEvent: (event) =>
124 applicationEvents
125 .appendProjectEvent(event)
126 .pipe(
127 Effect.mapError((cause) => new EventStoreAppendEventsError({ eventCount: 1, cause })),
128 ),
129 read,
130 readByCommandId: ({ commandId }) =>
131 applicationEvents
132 .readAgentEvents({ commandId })
133 .pipe(Stream.mapError((cause) => new EventStoreReadEventsError({ cause }))),
134 latestSequence: (input) =>
135 applicationEvents.latestAgentSequence(input?.threadId).pipe(
136 Effect.mapError(
137 (cause) =>
138 new EventStoreReadEventsError({
139 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
140 cause,
141 }),
142 ),
143 ),
144 latestApplicationSequence: applicationEvents.latestApplicationSequence.pipe(
145 Effect.mapError((cause) => new EventStoreReadEventsError({ cause })),
146 ),
147 publishCommitted: applicationEvents.publishCommitted,
148 });
149 }),
150 );
151
152export const layer: Layer.Layer<EventStoreV2, never, SqlClient.SqlClient> = baseLayer.pipe(
153 Layer.provide(OrchestrationEventStoreLive),
154);
155
156export const layerFromOrchestrationEventStore = baseLayer;