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

ProjectionThreads.ts

219 lines7.7 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";
5import * as Schema from "effect/Schema";
6import * as Struct from "effect/Struct";
7​
8import { toPersistenceSqlError } from "../Errors.ts";
9import {
10 DeleteProjectionThreadInput,
11 GetProjectionThreadInput,
12 ListProjectionThreadsByProjectInput,
13 ProjectionThread,
14 ProjectionThreadRepository,
15 type ProjectionThreadRepositoryShape,
16} from "../Services/ProjectionThreads.ts";
17import { ModelSelection } from "@t3tools/contracts";
18​
19const ProjectionThreadDbRow = ProjectionThread.mapFields(
20 Struct.assign({
21 modelSelection: Schema.fromJsonString(ModelSelection),
22 }),
23);
24type ProjectionThreadDbRow = typeof ProjectionThreadDbRow.Type;
25​
26const makeProjectionThreadRepository = Effect.gen(function* () {
27 const sql = yield* SqlClient.SqlClient;
28​
29 const upsertProjectionThreadRow = SqlSchema.void({
30 Request: ProjectionThread,
31 execute: (row) =>
32 sql`
33 INSERT INTO projection_threads (
34 thread_id,
35 project_id,
36 title,
37 model_selection_json,
38 runtime_mode,
39 interaction_mode,
40 branch,
41 worktree_path,
42 latest_turn_id,
43 created_at,
44 updated_at,
45 archived_at,
46 settled_override,
47 settled_at,
48 snoozed_until,
49 snoozed_at,
50 title_regeneration_request_id,
51 title_regeneration_started_at,
52 latest_user_message_at,
53 pending_approval_count,
54 pending_user_input_count,
55 has_actionable_proposed_plan,
56 deleted_at
57 )
58 VALUES (
59 ${row.threadId},
60 ${row.projectId},
61 ${row.title},
62 ${JSON.stringify(row.modelSelection)},
63 ${row.runtimeMode},
64 ${row.interactionMode},
65 ${row.branch},
66 ${row.worktreePath},
67 ${row.latestTurnId},
68 ${row.createdAt},
69 ${row.updatedAt},
70 ${row.archivedAt},
71 ${row.settledOverride},
72 ${row.settledAt},
73 ${row.snoozedUntil},
74 ${row.snoozedAt},
75 ${row.titleRegenerationRequestId ?? null},
76 ${row.titleRegenerationStartedAt ?? null},
77 ${row.latestUserMessageAt},
78 ${row.pendingApprovalCount},
79 ${row.pendingUserInputCount},
80 ${row.hasActionableProposedPlan},
81 ${row.deletedAt}
82 )
83 ON CONFLICT (thread_id)
84 DO UPDATE SET
85 project_id = excluded.project_id,
86 title = excluded.title,
87 model_selection_json = excluded.model_selection_json,
88 runtime_mode = excluded.runtime_mode,
89 interaction_mode = excluded.interaction_mode,
90 branch = excluded.branch,
91 worktree_path = excluded.worktree_path,
92 latest_turn_id = excluded.latest_turn_id,
93 created_at = excluded.created_at,
94 updated_at = excluded.updated_at,
95 archived_at = excluded.archived_at,
96 settled_override = excluded.settled_override,
97 settled_at = excluded.settled_at,
98 snoozed_until = excluded.snoozed_until,
99 snoozed_at = excluded.snoozed_at,
100 title_regeneration_request_id = excluded.title_regeneration_request_id,
101 title_regeneration_started_at = excluded.title_regeneration_started_at,
102 latest_user_message_at = excluded.latest_user_message_at,
103 pending_approval_count = excluded.pending_approval_count,
104 pending_user_input_count = excluded.pending_user_input_count,
105 has_actionable_proposed_plan = excluded.has_actionable_proposed_plan,
106 deleted_at = excluded.deleted_at
107 `,
108 });
109​
110 const getProjectionThreadRow = SqlSchema.findOneOption({
111 Request: GetProjectionThreadInput,
112 Result: ProjectionThreadDbRow,
113 execute: ({ threadId }) =>
114 sql`
115 SELECT
116 thread_id AS "threadId",
117 project_id AS "projectId",
118 title,
119 model_selection_json AS "modelSelection",
120 runtime_mode AS "runtimeMode",
121 interaction_mode AS "interactionMode",
122 branch,
123 worktree_path AS "worktreePath",
124 latest_turn_id AS "latestTurnId",
125 created_at AS "createdAt",
126 updated_at AS "updatedAt",
127 archived_at AS "archivedAt",
128 settled_override AS "settledOverride",
129 settled_at AS "settledAt",
130 snoozed_until AS "snoozedUntil",
131 snoozed_at AS "snoozedAt",
132 title_regeneration_request_id AS "titleRegenerationRequestId",
133 title_regeneration_started_at AS "titleRegenerationStartedAt",
134 latest_user_message_at AS "latestUserMessageAt",
135 pending_approval_count AS "pendingApprovalCount",
136 pending_user_input_count AS "pendingUserInputCount",
137 has_actionable_proposed_plan AS "hasActionableProposedPlan",
138 deleted_at AS "deletedAt"
139 FROM projection_threads
140 WHERE thread_id = ${threadId}
141 `,
142 });
143​
144 const listProjectionThreadRows = SqlSchema.findAll({
145 Request: ListProjectionThreadsByProjectInput,
146 Result: ProjectionThreadDbRow,
147 execute: ({ projectId }) =>
148 sql`
149 SELECT
150 thread_id AS "threadId",
151 project_id AS "projectId",
152 title,
153 model_selection_json AS "modelSelection",
154 runtime_mode AS "runtimeMode",
155 interaction_mode AS "interactionMode",
156 branch,
157 worktree_path AS "worktreePath",
158 latest_turn_id AS "latestTurnId",
159 created_at AS "createdAt",
160 updated_at AS "updatedAt",
161 archived_at AS "archivedAt",
162 settled_override AS "settledOverride",
163 settled_at AS "settledAt",
164 snoozed_until AS "snoozedUntil",
165 snoozed_at AS "snoozedAt",
166 title_regeneration_request_id AS "titleRegenerationRequestId",
167 title_regeneration_started_at AS "titleRegenerationStartedAt",
168 latest_user_message_at AS "latestUserMessageAt",
169 pending_approval_count AS "pendingApprovalCount",
170 pending_user_input_count AS "pendingUserInputCount",
171 has_actionable_proposed_plan AS "hasActionableProposedPlan",
172 deleted_at AS "deletedAt"
173 FROM projection_threads
174 WHERE project_id = ${projectId}
175 ORDER BY created_at ASC, thread_id ASC
176 `,
177 });
178​
179 const deleteProjectionThreadRow = SqlSchema.void({
180 Request: DeleteProjectionThreadInput,
181 execute: ({ threadId }) =>
182 sql`
183 DELETE FROM projection_threads
184 WHERE thread_id = ${threadId}
185 `,
186 });
187​
188 const upsert: ProjectionThreadRepositoryShape["upsert"] = (row) =>
189 upsertProjectionThreadRow(row).pipe(
190 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.upsert:query")),
191 );
192​
193 const getById: ProjectionThreadRepositoryShape["getById"] = (input) =>
194 getProjectionThreadRow(input).pipe(
195 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.getById:query")),
196 );
197​
198 const listByProjectId: ProjectionThreadRepositoryShape["listByProjectId"] = (input) =>
199 listProjectionThreadRows(input).pipe(
200 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.listByProjectId:query")),
201 );
202​
203 const deleteById: ProjectionThreadRepositoryShape["deleteById"] = (input) =>
204 deleteProjectionThreadRow(input).pipe(
205 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.deleteById:query")),
206 );
207​
208 return {
209 upsert,
210 getById,
211 listByProjectId,
212 deleteById,
213 } satisfies ProjectionThreadRepositoryShape;
214});
215​
216export const ProjectionThreadRepositoryLive = Layer.effect(
217 ProjectionThreadRepository,
218 makeProjectionThreadRepository,
219);