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

ProjectionThreads.ts

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