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 provider_session_id,
27 provider_thread_id,
28 approval_policy,
29 sandbox_mode,
30 active_turn_id,
31 last_error,
32 updated_at
33 )
34 VALUES (
35 ${row.threadId},
36 ${row.status},
37 ${row.providerName},
38 ${row.providerSessionId},
39 ${row.providerThreadId},
40 ${row.approvalPolicy},
41 ${row.sandboxMode},
42 ${row.activeTurnId},
43 ${row.lastError},
44 ${row.updatedAt}
45 )
46 ON CONFLICT (thread_id)
47 DO UPDATE SET
48 status = excluded.status,
49 provider_name = excluded.provider_name,
50 provider_session_id = excluded.provider_session_id,
51 provider_thread_id = excluded.provider_thread_id,
52 approval_policy = excluded.approval_policy,
53 sandbox_mode = excluded.sandbox_mode,
54 active_turn_id = excluded.active_turn_id,
55 last_error = excluded.last_error,
56 updated_at = excluded.updated_at
57 `,
58 });
59
60 const getProjectionThreadSessionRow = SqlSchema.findOneOption({
61 Request: GetProjectionThreadSessionInput,
62 Result: ProjectionThreadSession,
63 execute: ({ threadId }) =>
64 sql`
65 SELECT
66 thread_id AS "threadId",
67 status,
68 provider_name AS "providerName",
69 provider_session_id AS "providerSessionId",
70 provider_thread_id AS "providerThreadId",
71 approval_policy AS "approvalPolicy",
72 sandbox_mode AS "sandboxMode",
73 active_turn_id AS "activeTurnId",
74 last_error AS "lastError",
75 updated_at AS "updatedAt"
76 FROM projection_thread_sessions
77 WHERE thread_id = ${threadId}
78 `,
79 });
80
81 const deleteProjectionThreadSessionRow = SqlSchema.void({
82 Request: DeleteProjectionThreadSessionInput,
83 execute: ({ threadId }) =>
84 sql`
85 DELETE FROM projection_thread_sessions
86 WHERE thread_id = ${threadId}
87 `,
88 });
89
90 const upsert: ProjectionThreadSessionRepositoryShape["upsert"] = (row) =>
91 upsertProjectionThreadSessionRow(row).pipe(
92 Effect.mapError(toPersistenceSqlError("ProjectionThreadSessionRepository.upsert:query")),
93 );
94
95 const getByThreadId: ProjectionThreadSessionRepositoryShape["getByThreadId"] = (input) =>
96 getProjectionThreadSessionRow(input).pipe(
97 Effect.mapError(
98 toPersistenceSqlError("ProjectionThreadSessionRepository.getByThreadId:query"),
99 ),
100 );
101
102 const deleteByThreadId: ProjectionThreadSessionRepositoryShape["deleteByThreadId"] = (input) =>
103 deleteProjectionThreadSessionRow(input).pipe(
104 Effect.mapError(
105 toPersistenceSqlError("ProjectionThreadSessionRepository.deleteByThreadId:query"),
106 ),
107 );
108
109 return {
110 upsert,
111 getByThreadId,
112 deleteByThreadId,
113 } satisfies ProjectionThreadSessionRepositoryShape;
114});
115
116export const ProjectionThreadSessionRepositoryLive = Layer.effect(
117 ProjectionThreadSessionRepository,
118 makeProjectionThreadSessionRepository,
119);