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 GetProjectionThreadInput,
11 ProjectionThread,
12 ProjectionThreadRepository,
13 type ProjectionThreadRepositoryShape,
14} from "../Services/ProjectionThreads.ts";
15import { ModelSelection, ThreadLinkedPullRequest, ThreadTitleState } from "@t3tools/contracts";
16
17const ProjectionThreadDbRow = ProjectionThread.mapFields(
18 Struct.assign({
19 modelSelection: Schema.fromJsonString(ModelSelection),
20 titleState: Schema.NullOr(Schema.fromJsonString(ThreadTitleState)),
21 linkedPullRequest: Schema.NullOr(Schema.fromJsonString(ThreadLinkedPullRequest)),
22 branchPullRequest: Schema.NullOr(Schema.fromJsonString(ThreadLinkedPullRequest)),
23 }),
24);
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 title_state_json,
38 model_selection_json,
39 runtime_mode,
40 interaction_mode,
41 branch,
42 worktree_path,
43 linked_pull_request_json,
44 branch_pull_request_json,
45 latest_turn_id,
46 created_at,
47 updated_at,
48 archived_at,
49 settled_override,
50 settled_at,
51 unsettled_at,
52 snoozed_until,
53 snoozed_at,
54 pinned_at,
55 pin_order_key,
56 active_order_key,
57 auto_settle_disabled_at,
58 title_regeneration_request_id,
59 title_regeneration_started_at,
60 latest_user_message_at,
61 pending_approval_count,
62 pending_user_input_count,
63 has_actionable_proposed_plan,
64 deleted_at
65 )
66 VALUES (
67 ${row.threadId},
68 ${row.projectId},
69 ${row.title},
70 ${row.titleState == null ? null : JSON.stringify(row.titleState)},
71 ${JSON.stringify(row.modelSelection)},
72 ${row.runtimeMode},
73 ${row.interactionMode},
74 ${row.branch},
75 ${row.worktreePath},
76 ${row.linkedPullRequest === undefined || row.linkedPullRequest === null ? null : JSON.stringify(row.linkedPullRequest)},
77 ${row.branchPullRequest === undefined || row.branchPullRequest === null ? null : JSON.stringify(row.branchPullRequest)},
78 ${row.latestTurnId},
79 ${row.createdAt},
80 ${row.updatedAt},
81 ${row.archivedAt},
82 ${row.settledOverride},
83 ${row.settledAt},
84 ${row.unsettledAt},
85 ${row.snoozedUntil},
86 ${row.snoozedAt},
87 ${row.pinnedAt},
88 ${row.pinOrderKey ?? null},
89 ${row.activeOrderKey ?? null},
90 ${row.autoSettleDisabledAt ?? null},
91 ${row.titleRegenerationRequestId ?? null},
92 ${row.titleRegenerationStartedAt ?? null},
93 ${row.latestUserMessageAt},
94 ${row.pendingApprovalCount},
95 ${row.pendingUserInputCount},
96 ${row.hasActionableProposedPlan},
97 ${row.deletedAt}
98 )
99 ON CONFLICT (thread_id)
100 DO UPDATE SET
101 project_id = excluded.project_id,
102 title = excluded.title,
103 title_state_json = excluded.title_state_json,
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 pinned_at = excluded.pinned_at,
121 pin_order_key = excluded.pin_order_key,
122 active_order_key = excluded.active_order_key,
123 auto_settle_disabled_at = excluded.auto_settle_disabled_at,
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 title_state_json AS "titleState",
144 model_selection_json AS "modelSelection",
145 runtime_mode AS "runtimeMode",
146 interaction_mode AS "interactionMode",
147 branch,
148 worktree_path AS "worktreePath",
149 linked_pull_request_json AS "linkedPullRequest",
150 branch_pull_request_json AS "branchPullRequest",
151 latest_turn_id AS "latestTurnId",
152 created_at AS "createdAt",
153 updated_at AS "updatedAt",
154 archived_at AS "archivedAt",
155 settled_override AS "settledOverride",
156 settled_at AS "settledAt",
157 unsettled_at AS "unsettledAt",
158 snoozed_until AS "snoozedUntil",
159 snoozed_at AS "snoozedAt",
160 pinned_at AS "pinnedAt",
161 pin_order_key AS "pinOrderKey",
162 active_order_key AS "activeOrderKey",
163 auto_settle_disabled_at AS "autoSettleDisabledAt",
164 title_regeneration_request_id AS "titleRegenerationRequestId",
165 title_regeneration_started_at AS "titleRegenerationStartedAt",
166 latest_user_message_at AS "latestUserMessageAt",
167 pending_approval_count AS "pendingApprovalCount",
168 pending_user_input_count AS "pendingUserInputCount",
169 has_actionable_proposed_plan AS "hasActionableProposedPlan",
170 deleted_at AS "deletedAt"
171 FROM projection_threads
172 WHERE thread_id = ${threadId}
173 `,
174 });
175
176 const upsert: ProjectionThreadRepositoryShape["upsert"] = (row) =>
177 upsertProjectionThreadRow(row).pipe(
178 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.upsert:query")),
179 );
180
181 const getById: ProjectionThreadRepositoryShape["getById"] = (input) =>
182 getProjectionThreadRow(input).pipe(
183 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.getById:query")),
184 );
185
186 return {
187 upsert,
188 getById,
189 } satisfies ProjectionThreadRepositoryShape;
190});
191
192export const ProjectionThreadRepositoryLive = Layer.effect(
193 ProjectionThreadRepository,
194 makeProjectionThreadRepository,
195);