The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Run in the isolated dev client's browser with preview_evaluate. |
| 3 | * Seed only IndexedDB; authoritative server state remains unchanged. |
| 4 | * Await completion, then reload before exercising Stop. |
| 5 | */ |
| 6 | globalThis.seedStaleThreadCommand = async function ({ environmentId, threadId, itemId }) { |
| 7 | const response = await fetch(`/api/orchestration/threads/${threadId}`, { |
| 8 | headers: { "x-t3-orchestration-protocol": "2" }, |
| 9 | }); |
| 10 | if (!response.ok) throw new Error(`Thread fetch failed: ${response.status}`); |
| 11 | const full = await response.json(); |
| 12 | const authoritative = full.projection.turnItems.find((item) => item.id === itemId); |
| 13 | const visible = full.projection.visibleTurnItems.find((row) => row.item.id === itemId); |
| 14 | if ( |
| 15 | authoritative?.type !== "command_execution" || |
| 16 | authoritative.status !== "completed" || |
| 17 | !visible |
| 18 | ) { |
| 19 | throw new Error("Choose an authoritative completed command with a visible row"); |
| 20 | } |
| 21 | return new Promise((resolve, reject) => { |
| 22 | const request = indexedDB.open("t3code:connection-runtime", 4); |
| 23 | request.addEventListener("error", () => reject(request.error)); |
| 24 | request.addEventListener("success", () => { |
| 25 | const db = request.result; |
| 26 | const transaction = db.transaction("thread", "readwrite"); |
| 27 | const store = transaction.objectStore("thread"); |
| 28 | const key = `${environmentId}:${threadId}`; |
| 29 | const read = store.get(key); |
| 30 | let assertion; |
| 31 | transaction.addEventListener("error", () => reject(transaction.error)); |
| 32 | transaction.addEventListener("abort", () => |
| 33 | reject(transaction.error ?? new Error("Seed aborted")), |
| 34 | ); |
| 35 | transaction.addEventListener("complete", () => { |
| 36 | db.close(); |
| 37 | resolve(assertion); |
| 38 | }); |
| 39 | read.addEventListener("success", () => { |
| 40 | if (!read.result) { |
| 41 | transaction.abort(); |
| 42 | return; |
| 43 | } |
| 44 | const saved = JSON.parse(read.result); |
| 45 | const snapshot = saved.snapshot; |
| 46 | const projection = snapshot.projection; |
| 47 | const stale = { ...authoritative, status: "running", completedAt: null }; |
| 48 | assertion = { |
| 49 | authoritativeStatus: authoritative.status, |
| 50 | omittedBeforeSeed: !projection.turnItems.some((item) => item.id === itemId), |
| 51 | originalCount: projection.turnItems.length, |
| 52 | snapshotSequence: snapshot.snapshotSequence, |
| 53 | historyCursor: snapshot.historyCursor, |
| 54 | hasMoreHistory: snapshot.hasMoreHistory, |
| 55 | }; |
| 56 | projection.turnItems = [ |
| 57 | stale, |
| 58 | ...projection.turnItems.filter((item) => item.id !== itemId), |
| 59 | ]; |
| 60 | projection.visibleTurnItems = [ |
| 61 | { ...visible, item: stale }, |
| 62 | ...projection.visibleTurnItems.filter((row) => row.item.id !== itemId), |
| 63 | ]; |
| 64 | store.put(JSON.stringify(saved), key); |
| 65 | }); |
| 66 | }); |
| 67 | }); |
| 68 | }; |