Open History to see the last commit
1import * as SqlClient from "effect/unstable/sql/SqlClient";
2import * as SqlSchema from "effect/unstable/sql/SqlSchema";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import { toPersistenceSqlError } from "../Errors.ts";
7
8import {
9 ProjectionThreadSession,
10 ProjectionThreadSessionRepository,
11 type ProjectionThreadSessionRepositoryShape,
12 DeleteProjectionThreadSessionInput,
13 GetProjectionThreadSessionInput,
14} from "../Services/ProjectionThreadSessions.ts";
15
16const makeProjectionThreadSessionRepository = Effect.gen(function* () {
17 const sql = yield* SqlClient.SqlClient;
18
19 const upsertProjectionThreadSessionRow = SqlSchema.void({
20 Request: ProjectionThreadSession,
21 execute: (row) =>
22 sql`
23 INSERT INTO projection_thread_sessions (
24 thread_id,
25 status,
26 provider_name,
27 provider_instance_id,
28 runtime_mode,
29 active_turn_id,
30 last_error,
31 last_error_kind,
32 last_error_resets_at,
33 updated_at
34 )
35 VALUES (
36 ${row.threadId},
37 ${row.status},
38 ${row.providerName},
39 ${row.providerInstanceId},
40 ${row.runtimeMode},
41 ${row.activeTurnId},
42 ${row.lastError},
43 ${row.lastErrorKind ?? null},
44 ${row.lastErrorResetsAt ?? null},
45 ${row.updatedAt}
46 )
47 ON CONFLICT (thread_id)
48 DO UPDATE SET
49 status = excluded.status,
50 provider_name = excluded.provider_name,
51 provider_instance_id = excluded.provider_instance_id,
52 runtime_mode = excluded.runtime_mode,
53 active_turn_id = excluded.active_turn_id,
54 last_error = excluded.last_error,
55 last_error_kind = excluded.last_error_kind,
56 last_error_resets_at = excluded.last_error_resets_at,
57 updated_at = excluded.updated_at
58 `,
59 });
60
61 const getProjectionThreadSessionRow = SqlSchema.findOneOption({
62 Request: GetProjectionThreadSessionInput,
63 Result: ProjectionThreadSession,
64 execute: ({ threadId }) =>
65 sql`
66 SELECT
67 thread_id AS "threadId",
68 status,
69 provider_name AS "providerName",
70 provider_instance_id AS "providerInstanceId",
71 runtime_mode AS "runtimeMode",
72 active_turn_id AS "activeTurnId",
73 last_error AS "lastError",
74 last_error_kind AS "lastErrorKind",
75 last_error_resets_at AS "lastErrorResetsAt",
76 updated_at AS "updatedAt"
77 FROM projection_thread_sessions
78 WHERE thread_id = ${threadId}
79 `,
80 });
81
82 const deleteProjectionThreadSessionRow = SqlSchema.void({
83 Request: DeleteProjectionThreadSessionInput,
84 execute: ({ threadId }) =>
85 sql`
86 DELETE FROM projection_thread_sessions
87 WHERE thread_id = ${threadId}
88 `,
89 });
90
91 const upsert: ProjectionThreadSessionRepositoryShape["upsert"] = (row) =>
92 upsertProjectionThreadSessionRow(row).pipe(
93 Effect.mapError(toPersistenceSqlError("ProjectionThreadSessionRepository.upsert:query")),
94 );
95
96 const getByThreadId: ProjectionThreadSessionRepositoryShape["getByThreadId"] = (input) =>
97 getProjectionThreadSessionRow(input).pipe(
98 Effect.mapError(
99 toPersistenceSqlError("ProjectionThreadSessionRepository.getByThreadId:query"),
100 ),
101 );
102
103 const deleteByThreadId: ProjectionThreadSessionRepositoryShape["deleteByThreadId"] = (input) =>
104 deleteProjectionThreadSessionRow(input).pipe(
105 Effect.mapError(
106 toPersistenceSqlError("ProjectionThreadSessionRepository.deleteByThreadId:query"),
107 ),
108 );
109
110 return {
111 upsert,
112 getByThreadId,
113 deleteByThreadId,
114 } satisfies ProjectionThreadSessionRepositoryShape;
115});
116
117export const ProjectionThreadSessionRepositoryLive = Layer.effect(
118 ProjectionThreadSessionRepository,
119 makeProjectionThreadSessionRepository,
120);