Open History to see the last commit
1import { ProviderSessionId } from "@t3tools/contracts";
2import * as SqlClient from "effect/unstable/sql/SqlClient";
3import * as SqlSchema from "effect/unstable/sql/SqlSchema";
4import { Effect, Layer, Option, Schema, Struct } from "effect";
5
6import {
7 toPersistenceDecodeError,
8 toPersistenceSqlError,
9 type ProviderSessionRuntimeRepositoryError,
10} from "../Errors.ts";
11import {
12 ProviderSessionRuntime,
13 ProviderSessionRuntimeRepository,
14 type ProviderSessionRuntimeRepositoryShape,
15} from "../Services/ProviderSessionRuntime.ts";
16
17const ProviderSessionRuntimeDbRowSchema = ProviderSessionRuntime.mapFields(
18 Struct.assign({
19 resumeCursor: Schema.NullOr(Schema.fromJsonString(Schema.Unknown)),
20 runtimePayload: Schema.NullOr(Schema.fromJsonString(Schema.Unknown)),
21 }),
22);
23
24const decodeRuntime = Schema.decodeUnknownEffect(ProviderSessionRuntime);
25
26const GetRuntimeRequestSchema = Schema.Struct({
27 providerSessionId: ProviderSessionId,
28});
29
30const DeleteRuntimeRequestSchema = GetRuntimeRequestSchema;
31
32function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
33 return (cause: unknown): ProviderSessionRuntimeRepositoryError =>
34 Schema.isSchemaError(cause)
35 ? toPersistenceDecodeError(decodeOperation)(cause)
36 : toPersistenceSqlError(sqlOperation)(cause);
37}
38
39const makeProviderSessionRuntimeRepository = Effect.gen(function* () {
40 const sql = yield* SqlClient.SqlClient;
41
42 const upsertRuntimeRow = SqlSchema.void({
43 Request: ProviderSessionRuntimeDbRowSchema,
44 execute: (runtime) =>
45 sql`
46 INSERT INTO provider_session_runtime (
47 provider_session_id,
48 thread_id,
49 provider_name,
50 adapter_key,
51 provider_thread_id,
52 status,
53 last_seen_at,
54 resume_cursor_json,
55 runtime_payload_json
56 )
57 VALUES (
58 ${runtime.providerSessionId},
59 ${runtime.threadId},
60 ${runtime.providerName},
61 ${runtime.adapterKey},
62 ${runtime.providerThreadId},
63 ${runtime.status},
64 ${runtime.lastSeenAt},
65 ${runtime.resumeCursor},
66 ${runtime.runtimePayload}
67 )
68 ON CONFLICT (provider_session_id)
69 DO UPDATE SET
70 thread_id = excluded.thread_id,
71 provider_name = excluded.provider_name,
72 adapter_key = excluded.adapter_key,
73 provider_thread_id = excluded.provider_thread_id,
74 status = excluded.status,
75 last_seen_at = excluded.last_seen_at,
76 resume_cursor_json = excluded.resume_cursor_json,
77 runtime_payload_json = excluded.runtime_payload_json
78 `,
79 });
80
81 const getRuntimeRowBySessionId = SqlSchema.findOneOption({
82 Request: GetRuntimeRequestSchema,
83 Result: ProviderSessionRuntimeDbRowSchema,
84 execute: ({ providerSessionId }) =>
85 sql`
86 SELECT
87 provider_session_id AS "providerSessionId",
88 thread_id AS "threadId",
89 provider_name AS "providerName",
90 adapter_key AS "adapterKey",
91 provider_thread_id AS "providerThreadId",
92 status,
93 last_seen_at AS "lastSeenAt",
94 resume_cursor_json AS "resumeCursor",
95 runtime_payload_json AS "runtimePayload"
96 FROM provider_session_runtime
97 WHERE provider_session_id = ${providerSessionId}
98 `,
99 });
100
101 const listRuntimeRows = SqlSchema.findAll({
102 Request: Schema.Void,
103 Result: ProviderSessionRuntimeDbRowSchema,
104 execute: () =>
105 sql`
106 SELECT
107 provider_session_id AS "providerSessionId",
108 thread_id AS "threadId",
109 provider_name AS "providerName",
110 adapter_key AS "adapterKey",
111 provider_thread_id AS "providerThreadId",
112 status,
113 last_seen_at AS "lastSeenAt",
114 resume_cursor_json AS "resumeCursor",
115 runtime_payload_json AS "runtimePayload"
116 FROM provider_session_runtime
117 ORDER BY last_seen_at ASC, provider_session_id ASC
118 `,
119 });
120
121 const deleteRuntimeBySessionId = SqlSchema.void({
122 Request: DeleteRuntimeRequestSchema,
123 execute: ({ providerSessionId }) =>
124 sql`
125 DELETE FROM provider_session_runtime
126 WHERE provider_session_id = ${providerSessionId}
127 `,
128 });
129
130 const upsert: ProviderSessionRuntimeRepositoryShape["upsert"] = (runtime) =>
131 upsertRuntimeRow(runtime).pipe(
132 Effect.mapError(
133 toPersistenceSqlOrDecodeError(
134 "ProviderSessionRuntimeRepository.upsert:query",
135 "ProviderSessionRuntimeRepository.upsert:encodeRequest",
136 ),
137 ),
138 );
139
140 const getBySessionId: ProviderSessionRuntimeRepositoryShape["getBySessionId"] = (input) =>
141 getRuntimeRowBySessionId(input).pipe(
142 Effect.mapError(
143 toPersistenceSqlOrDecodeError(
144 "ProviderSessionRuntimeRepository.getBySessionId:query",
145 "ProviderSessionRuntimeRepository.getBySessionId:decodeRow",
146 ),
147 ),
148 Effect.flatMap((runtimeRowOption) =>
149 Option.match(runtimeRowOption, {
150 onNone: () => Effect.succeed(Option.none()),
151 onSome: (row) =>
152 decodeRuntime(row).pipe(
153 Effect.mapError(
154 toPersistenceDecodeError(
155 "ProviderSessionRuntimeRepository.getBySessionId:rowToRuntime",
156 ),
157 ),
158 Effect.map((runtime) => Option.some(runtime)),
159 ),
160 }),
161 ),
162 );
163
164 const list: ProviderSessionRuntimeRepositoryShape["list"] = () =>
165 listRuntimeRows(undefined).pipe(
166 Effect.mapError(
167 toPersistenceSqlOrDecodeError(
168 "ProviderSessionRuntimeRepository.list:query",
169 "ProviderSessionRuntimeRepository.list:decodeRows",
170 ),
171 ),
172 Effect.flatMap((rows) =>
173 Effect.forEach(
174 rows,
175 (row) =>
176 decodeRuntime(row).pipe(
177 Effect.mapError(
178 toPersistenceDecodeError("ProviderSessionRuntimeRepository.list:rowToRuntime"),
179 ),
180 ),
181 { concurrency: "unbounded" },
182 ),
183 ),
184 );
185
186 const deleteBySessionId: ProviderSessionRuntimeRepositoryShape["deleteBySessionId"] = (input) =>
187 deleteRuntimeBySessionId(input).pipe(
188 Effect.mapError(
189 toPersistenceSqlError("ProviderSessionRuntimeRepository.deleteBySessionId:query"),
190 ),
191 );
192
193 return {
194 upsert,
195 getBySessionId,
196 list,
197 deleteBySessionId,
198 } satisfies ProviderSessionRuntimeRepositoryShape;
199});
200
201export const ProviderSessionRuntimeRepositoryLive = Layer.effect(
202 ProviderSessionRuntimeRepository,
203 makeProviderSessionRuntimeRepository,
204);