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