The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as SqlClient from "effect/sql/SqlClient"; |
| 3 | |
| 4 | /** |
| 5 | * Shell reads count items per (thread, run) here instead of walking every turn item. Triggers |
| 6 | * keep it exact for every writer, including projection rebuilds and dev-database pruning. |
| 7 | */ |
| 8 | export default Effect.gen(function* () { |
| 9 | const sql = yield* SqlClient.SqlClient; |
| 10 | |
| 11 | // run_key is '' for items without a run; NULLs never conflict in a primary key. |
| 12 | yield* sql` |
| 13 | CREATE TABLE orchestration_v2_projection_turn_item_counts ( |
| 14 | thread_id TEXT NOT NULL, |
| 15 | run_key TEXT NOT NULL, |
| 16 | item_count INTEGER NOT NULL, |
| 17 | PRIMARY KEY (thread_id, run_key) |
| 18 | ) WITHOUT ROWID |
| 19 | `; |
| 20 | yield* sql` |
| 21 | INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count) |
| 22 | SELECT thread_id, COALESCE(run_id, ''), COUNT(*) |
| 23 | FROM orchestration_v2_projection_turn_items |
| 24 | GROUP BY thread_id, run_id |
| 25 | `; |
| 26 | // An upsert that hits an existing item fires only the update trigger. |
| 27 | yield* sql` |
| 28 | CREATE TRIGGER orchestration_v2_turn_item_counts_insert |
| 29 | AFTER INSERT ON orchestration_v2_projection_turn_items |
| 30 | BEGIN |
| 31 | INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count) |
| 32 | VALUES (new.thread_id, COALESCE(new.run_id, ''), 1) |
| 33 | ON CONFLICT (thread_id, run_key) DO UPDATE SET item_count = item_count + 1; |
| 34 | END |
| 35 | `; |
| 36 | yield* sql` |
| 37 | CREATE TRIGGER orchestration_v2_turn_item_counts_update |
| 38 | AFTER UPDATE OF thread_id, run_id ON orchestration_v2_projection_turn_items |
| 39 | WHEN old.thread_id IS NOT new.thread_id OR old.run_id IS NOT new.run_id |
| 40 | BEGIN |
| 41 | UPDATE orchestration_v2_projection_turn_item_counts SET item_count = item_count - 1 |
| 42 | WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, ''); |
| 43 | DELETE FROM orchestration_v2_projection_turn_item_counts |
| 44 | WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '') AND item_count <= 0; |
| 45 | INSERT INTO orchestration_v2_projection_turn_item_counts (thread_id, run_key, item_count) |
| 46 | VALUES (new.thread_id, COALESCE(new.run_id, ''), 1) |
| 47 | ON CONFLICT (thread_id, run_key) DO UPDATE SET item_count = item_count + 1; |
| 48 | END |
| 49 | `; |
| 50 | yield* sql` |
| 51 | CREATE TRIGGER orchestration_v2_turn_item_counts_delete |
| 52 | AFTER DELETE ON orchestration_v2_projection_turn_items |
| 53 | BEGIN |
| 54 | UPDATE orchestration_v2_projection_turn_item_counts SET item_count = item_count - 1 |
| 55 | WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, ''); |
| 56 | DELETE FROM orchestration_v2_projection_turn_item_counts |
| 57 | WHERE thread_id = old.thread_id AND run_key = COALESCE(old.run_id, '') AND item_count <= 0; |
| 58 | END |
| 59 | `; |
| 60 | }); |