Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionThreadSessions.ts

111 lines3.4 KB

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