The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeFS from "node:fs"; |
| 2 | import * as NodeReadline from "node:readline"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | import * as NodeURL from "node:url"; |
| 5 | |
| 6 | const here = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)); |
| 7 | const fixture = JSON.parse( |
| 8 | NodeFS.readFileSync(NodePath.join(here, "codexMultiAgentWire.json"), "utf8"), |
| 9 | ); |
| 10 | const script = JSON.parse(NodeFS.readFileSync(process.env.T3_CODEX_COLLAB_SCRIPT, "utf8")); |
| 11 | |
| 12 | const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); |
| 13 | let turnStartCount = 0; |
| 14 | let activeTurn; |
| 15 | const openServerRequests = new Map(); |
| 16 | const rl = NodeReadline.createInterface({ input: process.stdin }); |
| 17 | rl.on("line", (line) => { |
| 18 | let message; |
| 19 | try { |
| 20 | message = JSON.parse(line); |
| 21 | } catch { |
| 22 | return; |
| 23 | } |
| 24 | const { id, method } = message; |
| 25 | if (openServerRequests.has(id)) { |
| 26 | // Match real Codex's resolved item ID so the runtime closes the corresponding approval card. |
| 27 | const request = openServerRequests.get(id); |
| 28 | openServerRequests.delete(id); |
| 29 | NodeFS.appendFileSync( |
| 30 | `${process.env.T3_CODEX_COLLAB_SCRIPT}.approvalResponses`, |
| 31 | `${JSON.stringify({ id, label: request.label, result: message.result ?? null })}\n`, |
| 32 | ); |
| 33 | write({ |
| 34 | jsonrpc: "2.0", |
| 35 | method: "serverRequest/resolved", |
| 36 | params: { |
| 37 | threadId: script.rootThreadId, |
| 38 | requestId: request.itemId ?? request.label, |
| 39 | }, |
| 40 | }); |
| 41 | return; |
| 42 | } |
| 43 | if (method === undefined && script.serverRequests?.some((request) => request.id === id)) { |
| 44 | NodeFS.appendFileSync( |
| 45 | `${process.env.T3_CODEX_COLLAB_SCRIPT}.responses`, |
| 46 | `${JSON.stringify({ id, result: message.result, error: message.error })}\n`, |
| 47 | ); |
| 48 | if (script.completeTurnOnServerResponse && activeTurn) { |
| 49 | write({ |
| 50 | jsonrpc: "2.0", |
| 51 | method: "turn/completed", |
| 52 | params: { |
| 53 | threadId: script.rootThreadId, |
| 54 | turn: { ...activeTurn, status: "completed" }, |
| 55 | }, |
| 56 | }); |
| 57 | } |
| 58 | return; |
| 59 | } |
| 60 | if (method === "initialize") { |
| 61 | write({ |
| 62 | id, |
| 63 | result: { |
| 64 | userAgent: "t3-collab-mock/0.0.0", |
| 65 | codexHome: "/tmp", |
| 66 | platformFamily: "unix", |
| 67 | platformOs: "linux", |
| 68 | }, |
| 69 | }); |
| 70 | return; |
| 71 | } |
| 72 | if (method === "account/read") { |
| 73 | write({ |
| 74 | id, |
| 75 | result: { account: script.account ?? { type: "apiKey" }, requiresOpenaiAuth: false }, |
| 76 | }); |
| 77 | return; |
| 78 | } |
| 79 | if (method === "account/rateLimits/read" && script.failRateLimitsRead) { |
| 80 | write({ id, error: { code: -32000, message: "usage unavailable" } }); |
| 81 | return; |
| 82 | } |
| 83 | if (method === "account/rateLimitResetCredit/consume" && script.resetCreditOutcome) { |
| 84 | write({ id, result: { outcome: script.resetCreditOutcome } }); |
| 85 | return; |
| 86 | } |
| 87 | if (method === "skills/list" || method === "model/list") { |
| 88 | write({ id, result: { data: [] } }); |
| 89 | return; |
| 90 | } |
| 91 | if (method === "thread/start") { |
| 92 | write({ id, result: fixture.responses.threadStart }); |
| 93 | return; |
| 94 | } |
| 95 | if (method === "thread/inject_items" && script.recordRequests) { |
| 96 | NodeFS.appendFileSync( |
| 97 | `${process.env.T3_CODEX_COLLAB_SCRIPT}.requests`, |
| 98 | `${JSON.stringify({ method, params: message.params })}\n`, |
| 99 | ); |
| 100 | write({ id, result: {} }); |
| 101 | return; |
| 102 | } |
| 103 | if (method === "thread/resume") { |
| 104 | if (script.recordRequests) { |
| 105 | NodeFS.appendFileSync( |
| 106 | `${process.env.T3_CODEX_COLLAB_SCRIPT}.requests`, |
| 107 | `${JSON.stringify({ method, params: message.params })}\n`, |
| 108 | ); |
| 109 | } |
| 110 | const threadId = message.params?.threadId; |
| 111 | const childSnapshot = script.childResumeSnapshots?.[threadId]; |
| 112 | if (script.resumeRequestMarker) { |
| 113 | write({ |
| 114 | jsonrpc: "2.0", |
| 115 | method: "serverRequest/resolved", |
| 116 | params: { |
| 117 | threadId: script.rootThreadId, |
| 118 | requestId: script.resumeRequestMarker, |
| 119 | }, |
| 120 | }); |
| 121 | } |
| 122 | if (childSnapshot?.hang) { |
| 123 | return; |
| 124 | } |
| 125 | if (childSnapshot?.error) { |
| 126 | write({ id, error: { code: -32000, message: childSnapshot.error } }); |
| 127 | return; |
| 128 | } |
| 129 | if (childSnapshot) { |
| 130 | write({ |
| 131 | id, |
| 132 | result: { |
| 133 | ...fixture.responses.threadStart, |
| 134 | model: childSnapshot.model, |
| 135 | reasoningEffort: childSnapshot.reasoningEffort, |
| 136 | thread: { |
| 137 | ...fixture.responses.threadStart.thread, |
| 138 | id: threadId, |
| 139 | sessionId: threadId, |
| 140 | }, |
| 141 | }, |
| 142 | }); |
| 143 | for (const notification of childSnapshot.notifications ?? []) { |
| 144 | write({ jsonrpc: "2.0", method: notification.method, params: notification.params }); |
| 145 | } |
| 146 | return; |
| 147 | } |
| 148 | write({ id, result: fixture.responses.threadStart }); |
| 149 | return; |
| 150 | } |
| 151 | if (method === "turn/start") { |
| 152 | const turnId = script.turnIds?.[turnStartCount]; |
| 153 | const turn = turnId |
| 154 | ? { ...fixture.responses.turnStart.turn, id: turnId } |
| 155 | : fixture.responses.turnStart.turn; |
| 156 | activeTurn = turn; |
| 157 | turnStartCount += 1; |
| 158 | write({ id, result: { ...fixture.responses.turnStart, turn } }); |
| 159 | const rootThreadId = script.rootThreadId; |
| 160 | if (script.onlyFirstTurnStarts !== true || turnStartCount === 1) { |
| 161 | write({ |
| 162 | jsonrpc: "2.0", |
| 163 | method: "turn/started", |
| 164 | params: { threadId: rootThreadId, turn }, |
| 165 | }); |
| 166 | } |
| 167 | for (const notification of script.notifications) { |
| 168 | write({ jsonrpc: "2.0", method: notification.method, params: notification.params }); |
| 169 | } |
| 170 | for (const [index, request] of (script.serverRequests ?? []).entries()) { |
| 171 | if (request.id !== undefined) { |
| 172 | write({ jsonrpc: "2.0", id: request.id, method: request.method, params: request.params }); |
| 173 | continue; |
| 174 | } |
| 175 | const requestId = 9000 + index; |
| 176 | const label = request.label ?? `approval-${requestId}`; |
| 177 | openServerRequests.set(requestId, { label, itemId: request.params?.itemId }); |
| 178 | const params = JSON.parse( |
| 179 | JSON.stringify(request.params) |
| 180 | .replaceAll("${threadId}", String(rootThreadId)) |
| 181 | .replaceAll("${turnId}", String(turn.id)), |
| 182 | ); |
| 183 | write({ jsonrpc: "2.0", id: requestId, method: request.method, params }); |
| 184 | } |
| 185 | if (script.holdTurnOpen !== true) { |
| 186 | write({ |
| 187 | jsonrpc: "2.0", |
| 188 | method: "turn/completed", |
| 189 | params: { |
| 190 | threadId: rootThreadId, |
| 191 | turn: { ...turn, status: "completed" }, |
| 192 | }, |
| 193 | }); |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | if (method === "turn/interrupt") { |
| 198 | const target = message.params?.threadId; |
| 199 | NodeFS.appendFileSync( |
| 200 | `${process.env.T3_CODEX_COLLAB_SCRIPT}.interrupts`, |
| 201 | `${JSON.stringify({ threadId: target, turnId: message.params?.turnId })}\n`, |
| 202 | ); |
| 203 | if ( |
| 204 | script.expectedActiveTurnId && |
| 205 | message.params?.threadId === script.rootThreadId && |
| 206 | message.params?.turnId !== script.expectedActiveTurnId |
| 207 | ) { |
| 208 | write({ |
| 209 | id, |
| 210 | error: { |
| 211 | code: -32000, |
| 212 | message: `expected active turn id ${message.params?.turnId} but found ${script.expectedActiveTurnId}`, |
| 213 | }, |
| 214 | }); |
| 215 | return; |
| 216 | } |
| 217 | if (script.failInterruptFor && script.failInterruptFor === target) { |
| 218 | write({ id, error: { code: -32000, message: "thread already closed" } }); |
| 219 | return; |
| 220 | } |
| 221 | if (script.hangInterruptFor && script.hangInterruptFor === target) { |
| 222 | return; |
| 223 | } |
| 224 | write({ id, result: {} }); |
| 225 | return; |
| 226 | } |
| 227 | if (id !== undefined) { |
| 228 | write({ id, result: {} }); |
| 229 | } |
| 230 | }); |