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