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

ProjectionThreadSessions.ts

112 lines3.5 KB

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