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 title_regeneration_request_id,
58 title_regeneration_started_at,
59 latest_user_message_at,
60 pending_approval_count,
61 pending_user_input_count,
62 has_actionable_proposed_plan,
63 deleted_at
64 )
65 VALUES (
66 ${row.threadId},
67 ${row.projectId},
68 ${row.title},
69 ${row.titleState == null ? null : JSON.stringify(row.titleState)},
70 ${JSON.stringify(row.modelSelection)},
71 ${row.runtimeMode},
72 ${row.interactionMode},
73 ${row.branch},
74 ${row.worktreePath},
75 ${row.linkedPullRequest === undefined || row.linkedPullRequest === null ? null : JSON.stringify(row.linkedPullRequest)},
76 ${row.branchPullRequest === undefined || row.branchPullRequest === null ? null : JSON.stringify(row.branchPullRequest)},
77 ${row.latestTurnId},
78 ${row.createdAt},
79 ${row.updatedAt},
80 ${row.archivedAt},
81 ${row.settledOverride},
82 ${row.settledAt},
83 ${row.unsettledAt},
84 ${row.snoozedUntil},
85 ${row.snoozedAt},
86 ${row.pinnedAt},
87 ${row.pinOrderKey ?? null},
88 ${row.activeOrderKey ?? null},
89 ${row.titleRegenerationRequestId ?? null},
90 ${row.titleRegenerationStartedAt ?? null},
91 ${row.latestUserMessageAt},
92 ${row.pendingApprovalCount},
93 ${row.pendingUserInputCount},
94 ${row.hasActionableProposedPlan},
95 ${row.deletedAt}
96 )
97 ON CONFLICT (thread_id)
98 DO UPDATE SET
99 project_id = excluded.project_id,
100 title = excluded.title,
101 title_state_json = excluded.title_state_json,
102 model_selection_json = excluded.model_selection_json,
103 runtime_mode = excluded.runtime_mode,
104 interaction_mode = excluded.interaction_mode,
105 branch = excluded.branch,
106 worktree_path = excluded.worktree_path,
107 linked_pull_request_json = excluded.linked_pull_request_json,
108 branch_pull_request_json = excluded.branch_pull_request_json,
109 latest_turn_id = excluded.latest_turn_id,
110 created_at = excluded.created_at,
111 updated_at = excluded.updated_at,
112 archived_at = excluded.archived_at,
113 settled_override = excluded.settled_override,
114 settled_at = excluded.settled_at,
115 unsettled_at = excluded.unsettled_at,
116 snoozed_until = excluded.snoozed_until,
117 snoozed_at = excluded.snoozed_at,
118 pinned_at = excluded.pinned_at,
119 pin_order_key = excluded.pin_order_key,
120 active_order_key = excluded.active_order_key,
121 title_regeneration_request_id = excluded.title_regeneration_request_id,
122 title_regeneration_started_at = excluded.title_regeneration_started_at,
123 latest_user_message_at = excluded.latest_user_message_at,
124 pending_approval_count = excluded.pending_approval_count,
125 pending_user_input_count = excluded.pending_user_input_count,
126 has_actionable_proposed_plan = excluded.has_actionable_proposed_plan,
127 deleted_at = excluded.deleted_at
128 `,
129 });
130
131 const getProjectionThreadRow = SqlSchema.findOneOption({
132 Request: GetProjectionThreadInput,
133 Result: ProjectionThreadDbRow,
134 execute: ({ threadId }) =>
135 sql`
136 SELECT
137 thread_id AS "threadId",
138 project_id AS "projectId",
139 title,
140 title_state_json AS "titleState",
141 model_selection_json AS "modelSelection",
142 runtime_mode AS "runtimeMode",
143 interaction_mode AS "interactionMode",
144 branch,
145 worktree_path AS "worktreePath",
146 linked_pull_request_json AS "linkedPullRequest",
147 branch_pull_request_json AS "branchPullRequest",
148 latest_turn_id AS "latestTurnId",
149 created_at AS "createdAt",
150 updated_at AS "updatedAt",
151 archived_at AS "archivedAt",
152 settled_override AS "settledOverride",
153 settled_at AS "settledAt",
154 unsettled_at AS "unsettledAt",
155 snoozed_until AS "snoozedUntil",
156 snoozed_at AS "snoozedAt",
157 pinned_at AS "pinnedAt",
158 pin_order_key AS "pinOrderKey",
159 active_order_key AS "activeOrderKey",
160 title_regeneration_request_id AS "titleRegenerationRequestId",
161 title_regeneration_started_at AS "titleRegenerationStartedAt",
162 latest_user_message_at AS "latestUserMessageAt",
163 pending_approval_count AS "pendingApprovalCount",
164 pending_user_input_count AS "pendingUserInputCount",
165 has_actionable_proposed_plan AS "hasActionableProposedPlan",
166 deleted_at AS "deletedAt"
167 FROM projection_threads
168 WHERE thread_id = ${threadId}
169 `,
170 });
171
172 const upsert: ProjectionThreadRepositoryShape["upsert"] = (row) =>
173 upsertProjectionThreadRow(row).pipe(
174 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.upsert:query")),
175 );
176
177 const getById: ProjectionThreadRepositoryShape["getById"] = (input) =>
178 getProjectionThreadRow(input).pipe(
179 Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.getById:query")),
180 );
181
182 return {
183 upsert,
184 getById,
185 } satisfies ProjectionThreadRepositoryShape;
186});
187
188export const ProjectionThreadRepositoryLive = Layer.effect(
189 ProjectionThreadRepository,
190 makeProjectionThreadRepository,
191);