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

main
ts 329 lines 14.1 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as SqlClient from "effect/sql/SqlClient";
3
4 import ApplicationEventSequenceIndexes from "./OrchestrationV2/ApplicationEventSequenceIndexes.ts";
5 import ApplicationEventSource from "./OrchestrationV2/ApplicationEventSource.ts";
6 import OrchestrationV2EffectCancellation from "./OrchestrationV2/EffectCancellation.ts";
7 import OrchestrationV2Foundation from "./OrchestrationV2/Foundation.ts";
8 import LegacyV1ImportState from "./OrchestrationV2/LegacyV1ImportState.ts";
9 import OrchestrationV2ProviderSessionBindings from "./OrchestrationV2/ProviderSessionBindings.ts";
10 import OrchestrationV2RecoveryIndexes from "./OrchestrationV2/RecoveryIndexes.ts";
11 import ScheduledTasks from "./OrchestrationV2/ScheduledTasks.ts";
12 import OrchestrationV2ShellIndexes from "./OrchestrationV2/ShellIndexes.ts";
13 import OrchestrationV2Subagents from "./OrchestrationV2/Subagents.ts";
14 import OrchestrationV2ThreadLaunchWorkflows from "./OrchestrationV2/ThreadLaunchWorkflows.ts";
15
16 const OrchestrationV2Base = Effect.gen(function* () {
17 const sql = yield* SqlClient.SqlClient;
18
19 yield* sql`
20 CREATE TABLE orchestration_v2_events (
21 sequence INTEGER PRIMARY KEY AUTOINCREMENT,
22 event_id TEXT NOT NULL UNIQUE,
23 command_id TEXT,
24 thread_id TEXT NOT NULL,
25 run_id TEXT,
26 node_id TEXT,
27 provider TEXT,
28 raw_event_id TEXT,
29 event_type TEXT NOT NULL,
30 occurred_at TEXT NOT NULL,
31 payload_json TEXT NOT NULL
32 )
33 `;
34 yield* sql`CREATE INDEX orchestration_v2_events_command_idx ON orchestration_v2_events(command_id, sequence)`;
35 yield* sql`CREATE INDEX orchestration_v2_events_thread_sequence_idx ON orchestration_v2_events(thread_id, sequence)`;
36 yield* sql`CREATE INDEX orchestration_v2_events_thread_type_sequence_idx ON orchestration_v2_events(thread_id, event_type, sequence)`;
37 yield* sql`CREATE INDEX orchestration_v2_events_run_sequence_idx ON orchestration_v2_events(run_id, sequence)`;
38 yield* sql`CREATE INDEX orchestration_v2_events_node_sequence_idx ON orchestration_v2_events(node_id, sequence)`;
39 yield* sql`CREATE INDEX orchestration_v2_events_raw_event_idx ON orchestration_v2_events(raw_event_id)`;
40
41 yield* sql`
42 CREATE TABLE orchestration_v2_command_receipts (
43 command_id TEXT PRIMARY KEY,
44 thread_id TEXT NOT NULL,
45 command_type TEXT NOT NULL,
46 accepted_at TEXT NOT NULL,
47 result_sequence INTEGER NOT NULL,
48 status TEXT NOT NULL,
49 error TEXT
50 )
51 `;
52 yield* sql`CREATE INDEX orchestration_v2_command_receipts_thread_sequence_idx ON orchestration_v2_command_receipts(thread_id, result_sequence)`;
53
54 yield* sql`
55 CREATE TABLE orchestration_v2_projection_threads (
56 thread_id TEXT PRIMARY KEY,
57 project_id TEXT NOT NULL,
58 title TEXT NOT NULL,
59 default_provider TEXT NOT NULL,
60 runtime_mode TEXT NOT NULL,
61 interaction_mode TEXT NOT NULL,
62 active_provider_thread_id TEXT,
63 created_at TEXT NOT NULL,
64 updated_at TEXT NOT NULL,
65 archived_at TEXT,
66 deleted_at TEXT,
67 payload_json TEXT NOT NULL
68 )
69 `;
70 yield* sql`CREATE INDEX orchestration_v2_projection_threads_project_updated_idx ON orchestration_v2_projection_threads(project_id, updated_at)`;
71
72 yield* sql`
73 CREATE TABLE orchestration_v2_projection_runs (
74 run_id TEXT PRIMARY KEY,
75 thread_id TEXT NOT NULL,
76 ordinal INTEGER NOT NULL,
77 provider TEXT NOT NULL,
78 provider_thread_id TEXT,
79 status TEXT NOT NULL,
80 requested_at TEXT NOT NULL,
81 completed_at TEXT,
82 payload_json TEXT NOT NULL
83 )
84 `;
85 yield* sql`CREATE UNIQUE INDEX orchestration_v2_projection_runs_thread_ordinal_idx ON orchestration_v2_projection_runs(thread_id, ordinal)`;
86 yield* sql`CREATE INDEX orchestration_v2_projection_runs_provider_thread_idx ON orchestration_v2_projection_runs(provider_thread_id)`;
87 yield* sql`CREATE INDEX orchestration_v2_projection_runs_thread_status_idx ON orchestration_v2_projection_runs(thread_id, status)`;
88
89 yield* sql`
90 CREATE TABLE orchestration_v2_projection_run_attempts (
91 attempt_id TEXT PRIMARY KEY,
92 thread_id TEXT NOT NULL,
93 run_id TEXT NOT NULL,
94 attempt_ordinal INTEGER NOT NULL,
95 root_node_id TEXT NOT NULL,
96 provider TEXT NOT NULL,
97 provider_thread_id TEXT NOT NULL,
98 provider_turn_id TEXT,
99 status TEXT NOT NULL,
100 payload_json TEXT NOT NULL
101 )
102 `;
103 yield* sql`CREATE UNIQUE INDEX orchestration_v2_projection_run_attempts_run_ordinal_idx ON orchestration_v2_projection_run_attempts(run_id, attempt_ordinal)`;
104 yield* sql`CREATE INDEX orchestration_v2_projection_run_attempts_thread_idx ON orchestration_v2_projection_run_attempts(thread_id, run_id)`;
105
106 yield* sql`
107 CREATE TABLE orchestration_v2_projection_nodes (
108 node_id TEXT PRIMARY KEY,
109 thread_id TEXT NOT NULL,
110 run_id TEXT,
111 parent_node_id TEXT,
112 root_node_id TEXT NOT NULL,
113 kind TEXT NOT NULL,
114 status TEXT NOT NULL,
115 provider_thread_id TEXT,
116 provider_turn_id TEXT,
117 runtime_request_id TEXT,
118 checkpoint_scope_id TEXT,
119 started_at TEXT,
120 completed_at TEXT,
121 payload_json TEXT NOT NULL
122 )
123 `;
124 yield* sql`CREATE INDEX orchestration_v2_projection_nodes_thread_run_idx ON orchestration_v2_projection_nodes(thread_id, run_id)`;
125 yield* sql`CREATE INDEX orchestration_v2_projection_nodes_parent_idx ON orchestration_v2_projection_nodes(parent_node_id)`;
126 yield* sql`CREATE INDEX orchestration_v2_projection_nodes_provider_turn_idx ON orchestration_v2_projection_nodes(provider_turn_id)`;
127
128 yield* sql`
129 CREATE TABLE orchestration_v2_projection_provider_sessions (
130 provider_session_id TEXT PRIMARY KEY,
131 thread_id TEXT,
132 provider TEXT NOT NULL,
133 status TEXT NOT NULL,
134 model TEXT,
135 updated_at TEXT NOT NULL,
136 payload_json TEXT NOT NULL
137 )
138 `;
139 yield* sql`CREATE INDEX orchestration_v2_projection_provider_sessions_thread_idx ON orchestration_v2_projection_provider_sessions(thread_id)`;
140 yield* sql`CREATE INDEX orchestration_v2_projection_provider_sessions_provider_status_idx ON orchestration_v2_projection_provider_sessions(provider, status)`;
141
142 yield* sql`
143 CREATE TABLE orchestration_v2_projection_provider_threads (
144 provider_thread_id TEXT PRIMARY KEY,
145 thread_id TEXT,
146 owner_node_id TEXT,
147 provider TEXT NOT NULL,
148 provider_session_id TEXT,
149 status TEXT NOT NULL,
150 first_run_ordinal INTEGER,
151 last_run_ordinal INTEGER,
152 updated_at TEXT NOT NULL,
153 payload_json TEXT NOT NULL
154 )
155 `;
156 yield* sql`CREATE INDEX orchestration_v2_projection_provider_threads_thread_idx ON orchestration_v2_projection_provider_threads(thread_id)`;
157 yield* sql`CREATE INDEX orchestration_v2_projection_provider_threads_session_idx ON orchestration_v2_projection_provider_threads(provider_session_id)`;
158 yield* sql`CREATE INDEX orchestration_v2_projection_provider_threads_owner_idx ON orchestration_v2_projection_provider_threads(owner_node_id)`;
159
160 yield* sql`
161 CREATE TABLE orchestration_v2_projection_provider_turns (
162 provider_turn_id TEXT PRIMARY KEY,
163 thread_id TEXT NOT NULL,
164 provider_thread_id TEXT NOT NULL,
165 node_id TEXT NOT NULL,
166 run_attempt_id TEXT,
167 ordinal INTEGER NOT NULL,
168 status TEXT NOT NULL,
169 started_at TEXT,
170 completed_at TEXT,
171 payload_json TEXT NOT NULL
172 )
173 `;
174 yield* sql`CREATE INDEX orchestration_v2_projection_provider_turns_thread_idx ON orchestration_v2_projection_provider_turns(thread_id)`;
175 yield* sql`CREATE UNIQUE INDEX orchestration_v2_projection_provider_turns_thread_ordinal_idx ON orchestration_v2_projection_provider_turns(provider_thread_id, ordinal)`;
176
177 yield* sql`
178 CREATE TABLE orchestration_v2_projection_runtime_requests (
179 runtime_request_id TEXT PRIMARY KEY,
180 thread_id TEXT NOT NULL,
181 node_id TEXT NOT NULL,
182 provider_turn_id TEXT,
183 kind TEXT NOT NULL,
184 status TEXT NOT NULL,
185 created_at TEXT NOT NULL,
186 resolved_at TEXT,
187 payload_json TEXT NOT NULL
188 )
189 `;
190 yield* sql`CREATE INDEX orchestration_v2_projection_runtime_requests_thread_status_idx ON orchestration_v2_projection_runtime_requests(thread_id, status)`;
191 yield* sql`CREATE INDEX orchestration_v2_projection_runtime_requests_provider_turn_idx ON orchestration_v2_projection_runtime_requests(provider_turn_id)`;
192
193 yield* sql`
194 CREATE TABLE orchestration_v2_projection_messages (
195 message_id TEXT PRIMARY KEY,
196 thread_id TEXT NOT NULL,
197 run_id TEXT,
198 node_id TEXT,
199 role TEXT NOT NULL,
200 streaming INTEGER NOT NULL,
201 created_at TEXT NOT NULL,
202 updated_at TEXT NOT NULL,
203 payload_json TEXT NOT NULL
204 )
205 `;
206 yield* sql`CREATE INDEX orchestration_v2_projection_messages_thread_created_idx ON orchestration_v2_projection_messages(thread_id, created_at, message_id)`;
207 yield* sql`CREATE INDEX orchestration_v2_projection_messages_run_idx ON orchestration_v2_projection_messages(run_id)`;
208 yield* sql`CREATE INDEX orchestration_v2_projection_messages_node_idx ON orchestration_v2_projection_messages(node_id)`;
209
210 yield* sql`
211 CREATE TABLE orchestration_v2_projection_plans (
212 plan_id TEXT PRIMARY KEY,
213 thread_id TEXT NOT NULL,
214 run_id TEXT,
215 node_id TEXT NOT NULL,
216 kind TEXT NOT NULL,
217 status TEXT NOT NULL,
218 payload_json TEXT NOT NULL
219 )
220 `;
221 yield* sql`CREATE INDEX orchestration_v2_projection_plans_thread_idx ON orchestration_v2_projection_plans(thread_id)`;
222 yield* sql`CREATE INDEX orchestration_v2_projection_plans_run_idx ON orchestration_v2_projection_plans(run_id)`;
223
224 yield* sql`
225 CREATE TABLE orchestration_v2_projection_turn_items (
226 turn_item_id TEXT PRIMARY KEY,
227 thread_id TEXT NOT NULL,
228 run_id TEXT,
229 node_id TEXT,
230 provider_thread_id TEXT,
231 provider_turn_id TEXT,
232 parent_item_id TEXT,
233 ordinal INTEGER NOT NULL,
234 type TEXT NOT NULL,
235 status TEXT NOT NULL,
236 updated_at TEXT NOT NULL,
237 payload_json TEXT NOT NULL
238 )
239 `;
240 yield* sql`CREATE INDEX orchestration_v2_projection_turn_items_thread_ordinal_idx ON orchestration_v2_projection_turn_items(thread_id, ordinal, turn_item_id)`;
241 yield* sql`CREATE INDEX orchestration_v2_projection_turn_items_run_ordinal_idx ON orchestration_v2_projection_turn_items(run_id, ordinal)`;
242 yield* sql`CREATE INDEX orchestration_v2_projection_turn_items_node_ordinal_idx ON orchestration_v2_projection_turn_items(node_id, ordinal)`;
243 yield* sql`CREATE INDEX orchestration_v2_projection_turn_items_provider_turn_idx ON orchestration_v2_projection_turn_items(provider_turn_id)`;
244
245 yield* sql`
246 CREATE TABLE orchestration_v2_projection_checkpoint_scopes (
247 scope_id TEXT PRIMARY KEY,
248 thread_id TEXT NOT NULL,
249 run_id TEXT,
250 node_id TEXT NOT NULL,
251 parent_scope_id TEXT,
252 provider_thread_id TEXT,
253 kind TEXT NOT NULL,
254 ordinal_within_parent INTEGER NOT NULL,
255 advances_app_run_count INTEGER NOT NULL,
256 created_at TEXT NOT NULL,
257 payload_json TEXT NOT NULL
258 )
259 `;
260 yield* sql`CREATE INDEX orchestration_v2_projection_checkpoint_scopes_thread_idx ON orchestration_v2_projection_checkpoint_scopes(thread_id)`;
261 yield* sql`CREATE INDEX orchestration_v2_projection_checkpoint_scopes_parent_idx ON orchestration_v2_projection_checkpoint_scopes(parent_scope_id)`;
262
263 yield* sql`
264 CREATE TABLE orchestration_v2_projection_checkpoints (
265 checkpoint_id TEXT PRIMARY KEY,
266 thread_id TEXT NOT NULL,
267 scope_id TEXT NOT NULL,
268 run_id TEXT,
269 node_id TEXT NOT NULL,
270 parent_checkpoint_id TEXT,
271 ordinal_within_scope INTEGER NOT NULL,
272 app_run_ordinal INTEGER,
273 status TEXT NOT NULL,
274 captured_at TEXT NOT NULL,
275 payload_json TEXT NOT NULL
276 )
277 `;
278 yield* sql`CREATE UNIQUE INDEX orchestration_v2_projection_checkpoints_scope_ordinal_idx ON orchestration_v2_projection_checkpoints(scope_id, ordinal_within_scope)`;
279 yield* sql`CREATE INDEX orchestration_v2_projection_checkpoints_thread_idx ON orchestration_v2_projection_checkpoints(thread_id)`;
280 yield* sql`CREATE INDEX orchestration_v2_projection_checkpoints_parent_idx ON orchestration_v2_projection_checkpoints(parent_checkpoint_id)`;
281
282 yield* sql`
283 CREATE TABLE orchestration_v2_projection_context_handoffs (
284 context_handoff_id TEXT PRIMARY KEY,
285 thread_id TEXT NOT NULL,
286 target_run_id TEXT NOT NULL,
287 to_provider_thread_id TEXT NOT NULL,
288 strategy TEXT NOT NULL,
289 status TEXT NOT NULL,
290 updated_at TEXT NOT NULL,
291 payload_json TEXT NOT NULL
292 )
293 `;
294 yield* sql`CREATE INDEX orchestration_v2_projection_context_handoffs_thread_idx ON orchestration_v2_projection_context_handoffs(thread_id)`;
295 yield* sql`CREATE INDEX orchestration_v2_projection_context_handoffs_target_run_idx ON orchestration_v2_projection_context_handoffs(target_run_id)`;
296
297 yield* sql`
298 CREATE TABLE orchestration_v2_projection_context_transfers (
299 context_transfer_id TEXT PRIMARY KEY,
300 source_thread_id TEXT NOT NULL,
301 target_thread_id TEXT NOT NULL,
302 target_run_id TEXT,
303 type TEXT NOT NULL,
304 status TEXT NOT NULL,
305 source_provider TEXT,
306 target_provider TEXT,
307 updated_at TEXT NOT NULL,
308 payload_json TEXT NOT NULL
309 )
310 `;
311 yield* sql`CREATE INDEX orchestration_v2_projection_context_transfers_source_thread_idx ON orchestration_v2_projection_context_transfers(source_thread_id)`;
312 yield* sql`CREATE INDEX orchestration_v2_projection_context_transfers_target_thread_idx ON orchestration_v2_projection_context_transfers(target_thread_id, status)`;
313 yield* sql`CREATE INDEX orchestration_v2_projection_context_transfers_target_run_idx ON orchestration_v2_projection_context_transfers(target_run_id)`;
314 });
315
316 export default Effect.gen(function* () {
317 yield* OrchestrationV2Base;
318 yield* OrchestrationV2Subagents;
319 yield* OrchestrationV2Foundation;
320 yield* OrchestrationV2ProviderSessionBindings;
321 yield* OrchestrationV2ThreadLaunchWorkflows;
322 yield* ApplicationEventSource;
323 yield* OrchestrationV2EffectCancellation;
324 yield* ScheduledTasks;
325 yield* LegacyV1ImportState;
326 yield* ApplicationEventSequenceIndexes;
327 yield* OrchestrationV2RecoveryIndexes;
328 yield* OrchestrationV2ShellIndexes;
329 });