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