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