The best way to control Claude Code, Codex and more from any computer or phone

main
ts 279 lines 7.86 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Schema from "effect/Schema";
3 import * as SqlClient from "effect/sql/SqlClient";
4
5 interface LegacyV2EventRow {
6 readonly event_id: string;
7 readonly command_id: string | null;
8 readonly thread_id: string;
9 readonly run_id: string | null;
10 readonly node_id: string | null;
11 readonly driver: string | null;
12 readonly provider_instance_id: string | null;
13 readonly raw_event_id: string | null;
14 readonly event_type: string;
15 readonly occurred_at: string;
16 readonly payload_json: string;
17 }
18
19 interface ProjectProjectionRow {
20 readonly project_id: string;
21 readonly title: string;
22 readonly workspace_root: string;
23 readonly default_model_selection_json: string | null;
24 readonly scripts_json: string;
25 readonly created_at: string;
26 readonly updated_at: string;
27 readonly deleted_at: string | null;
28 }
29
30 const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown));
31 const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
32
33 export default Effect.gen(function* () {
34 const sql = yield* SqlClient.SqlClient;
35
36 yield* sql`
37 ALTER TABLE orchestration_events
38 ADD COLUMN application_event_version INTEGER NOT NULL DEFAULT 1
39 `;
40 yield* sql`
41 CREATE INDEX idx_orchestration_events_application_sequence
42 ON orchestration_events(application_event_version, sequence)
43 `;
44 yield* sql`
45 ALTER TABLE orchestration_command_receipts
46 ADD COLUMN command_type TEXT NOT NULL DEFAULT 'legacy'
47 `;
48
49 const legacyV2Events = yield* sql<LegacyV2EventRow>`
50 SELECT
51 event_id,
52 command_id,
53 thread_id,
54 run_id,
55 node_id,
56 driver,
57 provider_instance_id,
58 raw_event_id,
59 event_type,
60 occurred_at,
61 payload_json
62 FROM orchestration_v2_events
63 ORDER BY sequence ASC
64 `;
65
66 yield* Effect.forEach(
67 legacyV2Events,
68 (event) =>
69 Effect.gen(function* () {
70 const metadata = yield* encodeJson({
71 applicationEventVersion: 2,
72 ...(event.run_id === null ? {} : { runId: event.run_id }),
73 ...(event.node_id === null ? {} : { nodeId: event.node_id }),
74 ...(event.driver === null ? {} : { driver: event.driver }),
75 ...(event.provider_instance_id === null
76 ? {}
77 : { providerInstanceId: event.provider_instance_id }),
78 ...(event.raw_event_id === null ? {} : { rawEventId: event.raw_event_id }),
79 });
80 yield* sql`
81 INSERT INTO orchestration_events (
82 event_id,
83 aggregate_kind,
84 stream_id,
85 stream_version,
86 event_type,
87 occurred_at,
88 command_id,
89 causation_event_id,
90 correlation_id,
91 actor_kind,
92 payload_json,
93 metadata_json,
94 application_event_version
95 )
96 SELECT
97 ${event.event_id},
98 'thread',
99 ${event.thread_id},
100 COALESCE(
101 (
102 SELECT MAX(stream_version) + 1
103 FROM orchestration_events
104 WHERE aggregate_kind = 'thread' AND stream_id = ${event.thread_id}
105 ),
106 0
107 ),
108 ${event.event_type},
109 ${event.occurred_at},
110 ${event.command_id},
111 NULL,
112 ${event.command_id},
113 ${event.raw_event_id === null ? "server" : "provider"},
114 ${event.payload_json},
115 ${metadata},
116 2
117 WHERE NOT EXISTS (
118 SELECT 1 FROM orchestration_events WHERE event_id = ${event.event_id}
119 )
120 `;
121 }),
122 { concurrency: 1, discard: true },
123 );
124
125 // Re-baseline current rows so shared-log rebuilds preserve pre-migration project state.
126 const projectRows = yield* sql<ProjectProjectionRow>`
127 SELECT
128 project_id,
129 title,
130 workspace_root,
131 default_model_selection_json,
132 scripts_json,
133 created_at,
134 updated_at,
135 deleted_at
136 FROM projection_projects
137 ORDER BY created_at ASC, project_id ASC
138 `;
139 yield* Effect.forEach(
140 projectRows,
141 (project) =>
142 Effect.gen(function* () {
143 const createdEventId = `migration:39:project:${project.project_id}:baseline`;
144 const createdPayload = {
145 projectId: project.project_id,
146 title: project.title,
147 workspaceRoot: project.workspace_root,
148 defaultModelSelection:
149 project.default_model_selection_json === null
150 ? null
151 : yield* decodeJson(project.default_model_selection_json),
152 scripts: yield* decodeJson(project.scripts_json),
153 createdAt: project.created_at,
154 updatedAt: project.updated_at,
155 };
156 const createdPayloadJson = yield* encodeJson(createdPayload);
157 const migrationMetadataJson = yield* encodeJson({
158 applicationEventVersion: 2,
159 migration: 39,
160 });
161 yield* sql`
162 INSERT INTO orchestration_events (
163 event_id,
164 aggregate_kind,
165 stream_id,
166 stream_version,
167 event_type,
168 occurred_at,
169 command_id,
170 causation_event_id,
171 correlation_id,
172 actor_kind,
173 payload_json,
174 metadata_json,
175 application_event_version
176 )
177 VALUES (
178 ${createdEventId},
179 'project',
180 ${project.project_id},
181 COALESCE(
182 (
183 SELECT MAX(stream_version) + 1
184 FROM orchestration_events
185 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
186 ),
187 0
188 ),
189 'project.created',
190 ${project.updated_at},
191 NULL,
192 NULL,
193 NULL,
194 'server',
195 ${createdPayloadJson},
196 ${migrationMetadataJson},
197 2
198 )
199 `;
200
201 if (project.deleted_at !== null) {
202 const deletedPayloadJson = yield* encodeJson({
203 projectId: project.project_id,
204 deletedAt: project.deleted_at,
205 });
206 yield* sql`
207 INSERT INTO orchestration_events (
208 event_id,
209 aggregate_kind,
210 stream_id,
211 stream_version,
212 event_type,
213 occurred_at,
214 command_id,
215 causation_event_id,
216 correlation_id,
217 actor_kind,
218 payload_json,
219 metadata_json,
220 application_event_version
221 )
222 VALUES (
223 ${`migration:39:project:${project.project_id}:deleted`},
224 'project',
225 ${project.project_id},
226 (
227 SELECT MAX(stream_version) + 1
228 FROM orchestration_events
229 WHERE aggregate_kind = 'project' AND stream_id = ${project.project_id}
230 ),
231 'project.deleted',
232 ${project.deleted_at},
233 NULL,
234 ${createdEventId},
235 NULL,
236 'server',
237 ${deletedPayloadJson},
238 ${migrationMetadataJson},
239 2
240 )
241 `;
242 }
243 }),
244 { concurrency: 1, discard: true },
245 );
246
247 yield* sql`
248 INSERT INTO orchestration_command_receipts (
249 command_id,
250 aggregate_kind,
251 aggregate_id,
252 accepted_at,
253 result_sequence,
254 status,
255 error,
256 command_type
257 )
258 SELECT
259 command_id,
260 'thread',
261 thread_id,
262 accepted_at,
263 COALESCE(
264 (
265 SELECT MAX(events.sequence)
266 FROM orchestration_events events
267 WHERE events.application_event_version = 2
268 AND events.command_id = receipts.command_id
269 ),
270 0
271 ),
272 status,
273 error,
274 command_type
275 FROM orchestration_v2_command_receipts receipts
276 WHERE TRUE
277 ON CONFLICT(command_id) DO NOTHING
278 `;
279 });