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 } 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 pinned_at,
51 title_regeneration_request_id,
52 title_regeneration_started_at,
53 latest_user_message_at,
54 pending_approval_count,
55 pending_user_input_count,
56 has_actionable_proposed_plan,
57 deleted_at
58 )
59 VALUES (
60 ${row.threadId},
61 ${row.projectId},
62 ${row.title},
63 ${JSON.stringify(row.modelSelection)},
64 ${row.runtimeMode},
65 ${row.interactionMode},
66 ${row.branch},
67 ${row.worktreePath},
68 ${row.latestTurnId},
69 ${row.createdAt},
70 ${row.updatedAt},
71 ${row.archivedAt},
72 ${row.settledOverride},
73 ${row.settledAt},
74 ${row.snoozedUntil},
75 ${row.snoozedAt},
76 ${row.pinnedAt},
77 ${row.titleRegenerationRequestId ?? null},
78 ${row.titleRegenerationStartedAt ?? null},
79 ${row.latestUserMessageAt},
80 ${row.pendingApprovalCount},
81 ${row.pendingUserInputCount},
82 ${row.hasActionableProposedPlan},
83 ${row.deletedAt}
84 )
85 ON CONFLICT (thread_id)
86 DO UPDATE SET
87 project_id = excluded.project_id,
88 title = excluded.title,
89 model_selection_json = excluded.model_selection_json,
90 runtime_mode = excluded.runtime_mode,
91 interaction_mode = excluded.interaction_mode,
92 branch = excluded.branch,
93 worktree_path = excluded.worktree_path,
94 latest_turn_id = excluded.latest_turn_id,
95 created_at = excluded.created_at,
96 updated_at = excluded.updated_at,
97 archived_at = excluded.archived_at,
98 settled_override = excluded.settled_override,
99 settled_at = excluded.settled_at,
100 snoozed_until = excluded.snoozed_until,
101 snoozed_at = excluded.snoozed_at,
102 pinned_at = excluded.pinned_at,
103 title_regeneration_request_id = excluded.title_regeneration_request_id,
104 title_regeneration_started_at = excluded.title_regeneration_started_at,
105 latest_user_message_at = excluded.latest_user_message_at,
106 pending_approval_count = excluded.pending_approval_count,
107 pending_user_input_count = excluded.pending_user_input_count,
108 has_actionable_proposed_plan = excluded.has_actionable_proposed_plan,
109 deleted_at = excluded.deleted_at
110 `,
111 });
112
113 const getProjectionThreadRow = SqlSchema.findOneOption({
114 Request: GetProjectionThreadInput,
115 Result: ProjectionThreadDbRow,
116 execute: ({ threadId }) =>
117 sql`
118 SELECT
119 thread_id AS "threadId",
120 project_id AS "projectId",
121 title,
122 model_selection_json AS "modelSelection",
123 runtime_mode AS "runtimeMode",
124 interaction_mode AS "interactionMode",
125 branch,
126 worktree_path AS "worktreePath",
127 latest_turn_id AS "latestTurnId",
128 created_at AS "createdAt",
129 updated_at AS "updatedAt",
130 archived_at AS "archivedAt",
131 settled_override AS "settledOverride",
132 settled_at AS "settledAt",
133 snoozed_until AS "snoozedUntil",
134 snoozed_at AS "snoozedAt",
135 pinned_at AS "pinnedAt",
136 title_regeneration_request_id AS "titleRegenerationRequestId",
137 title_regeneration_started_at AS "titleRegenerationStartedAt",
138 latest_user_message_at AS "latestUserMessageAt",
139 pending_approval_count AS "pendingApprovalCount",
140 pending_user_input_count AS "pendingUserInputCount",
141 has_actionable_proposed_plan AS "hasActionableProposedPlan",
142 deleted_at AS "deletedAt"
143 FROM projection_threads
144 WHERE thread_id = ${threadId}
145 `,
146 });
147
148 const listProjectionThreadRows = SqlSchema.findAll({
149 Request: ListProjectionThreadsByProjectInput,
150 Result: ProjectionThreadDbRow,
151 execute: ({ projectId }) =>
152 sql`
153 SELECT
154 thread_id AS "threadId",
155 project_id AS "projectId",
156 title,
157 model_selection_json AS "modelSelection",
158 runtime_mode AS "runtimeMode",
159 interaction_mode AS "interactionMode",
160 branch,
161 worktree_path AS "worktreePath",
162 latest_turn_id AS "latestTurnId",
163 created_at AS "createdAt",
164 updated_at AS "updatedAt",
165 archived_at AS "archivedAt",
166 settled_override AS "settledOverride",
167 settled_at AS "settledAt",
168 snoozed_until AS "snoozedUntil",
169 snoozed_at AS "snoozedAt",
170 pinned_at AS "pinnedAt",
171 title_regeneration_request_id AS "titleRegenerationRequestId",
172 title_regeneration_started_at AS "titleRegenerationStartedAt",
173 latest_user_message_at AS "latestUserMessageAt",
174 pending_approval_count AS "pendingApprovalCount",
175 pending_user_input_count AS "pendingUserInputCount",
176 has_actionable_proposed_plan AS "hasActionableProposedPlan",
177 deleted_at AS "deletedAt"
178 FROM projection_threads
179 WHERE project_id = ${projectId}
180 ORDER BY created_at ASC, thread_id ASC
181 `,
182 });
183
184 const deleteProjectionThreadRow = SqlSchema.void({
185 Request: DeleteProjectionThreadInput,
186 execute: ({ threadId }) =>
187 sql`
188 DELETE FROM projection_threads
189 WHERE thread_id = ${threadId}
190 `,
191 });
192
193 const upsert: ProjectionThreadRepositoryShape["upsert"] = (row) =>
194 upsertProjectionThreadRow(row).pipe(
195 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.upsert:query")),
196 );
197
198 const getById: ProjectionThreadRepositoryShape["getById"] = (input) =>
199 getProjectionThreadRow(input).pipe(
200 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.getById:query")),
201 );
202
203 const listByProjectId: ProjectionThreadRepositoryShape["listByProjectId"] = (input) =>
204 listProjectionThreadRows(input).pipe(
205 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.listByProjectId:query")),
206 );
207
208 const deleteById: ProjectionThreadRepositoryShape["deleteById"] = (input) =>
209 deleteProjectionThreadRow(input).pipe(
210 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.deleteById:query")),
211 );
212
213 return {
214 upsert,
215 getById,
216 listByProjectId,
217 deleteById,
218 } satisfies ProjectionThreadRepositoryShape;
219});
220
221export const ProjectionThreadRepositoryLive = Layer.effect(
222 ProjectionThreadRepository,
223 makeProjectionThreadRepository,
224);