|
| 1 | +// Shared stdio transport and response builders for fake Codex App Server E2E processes. |
| 2 | +import fs from "node:fs"; |
| 3 | +import readline from "node:readline"; |
| 4 | + |
| 5 | +function writeMessage(message) { |
| 6 | + process.stdout.write(`${JSON.stringify(message)}\n`); |
| 7 | +} |
| 8 | + |
| 9 | +export function createFakeInitializeResponse({ name, userAgent, version }) { |
| 10 | + return { |
| 11 | + protocolVersion: "2", |
| 12 | + serverInfo: { name, version }, |
| 13 | + userAgent, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +export function createFakeThreadStartResponse({ params, sessionId, threadId, version }) { |
| 18 | + const now = Date.now(); |
| 19 | + const cwd = params?.cwd ?? process.cwd(); |
| 20 | + return { |
| 21 | + thread: { |
| 22 | + id: threadId, |
| 23 | + sessionId, |
| 24 | + forkedFromId: null, |
| 25 | + preview: "", |
| 26 | + ephemeral: false, |
| 27 | + modelProvider: "openai", |
| 28 | + createdAt: now, |
| 29 | + updatedAt: now, |
| 30 | + status: { type: "idle" }, |
| 31 | + path: null, |
| 32 | + cwd, |
| 33 | + cliVersion: version, |
| 34 | + source: "unknown", |
| 35 | + agentNickname: null, |
| 36 | + agentRole: null, |
| 37 | + gitInfo: null, |
| 38 | + name: null, |
| 39 | + turns: [], |
| 40 | + }, |
| 41 | + model: params?.model ?? "gpt-5.6-luna", |
| 42 | + modelProvider: "openai", |
| 43 | + serviceTier: null, |
| 44 | + cwd, |
| 45 | + runtimeWorkspaceRoots: [], |
| 46 | + instructionSources: [], |
| 47 | + approvalPolicy: params?.approvalPolicy ?? "never", |
| 48 | + approvalsReviewer: params?.approvalsReviewer ?? "user", |
| 49 | + sandbox: { type: "dangerFullAccess" }, |
| 50 | + activePermissionProfile: null, |
| 51 | + reasoningEffort: null, |
| 52 | + multiAgentMode: "explicitRequestOnly", |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +export function runFakeCodexAppServer({ handlers, logMode = "requests", requestLog }) { |
| 57 | + function appendLog(message) { |
| 58 | + try { |
| 59 | + fs.appendFileSync(requestLog, `${JSON.stringify(message)}\n`); |
| 60 | + return true; |
| 61 | + } catch (error) { |
| 62 | + const detail = error instanceof Error ? error.message : String(error); |
| 63 | + process.stderr.write(`fake Codex app-server request log write failed: ${detail}\n`); |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function emit(message) { |
| 69 | + if (logMode === "messages") { |
| 70 | + appendLog(message); |
| 71 | + } |
| 72 | + writeMessage(message); |
| 73 | + } |
| 74 | + |
| 75 | + const input = readline.createInterface({ input: process.stdin }); |
| 76 | + input.on("line", (line) => { |
| 77 | + if (!line.trim()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + const request = JSON.parse(line); |
| 81 | + if (!appendLog(request)) { |
| 82 | + if (request?.id !== undefined) { |
| 83 | + writeMessage({ |
| 84 | + error: { message: "fake Codex app-server request log write failed" }, |
| 85 | + id: request.id, |
| 86 | + }); |
| 87 | + } |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const { id, method, params } = request; |
| 92 | + if (id === undefined) { |
| 93 | + return; |
| 94 | + } |
| 95 | + const sendResult = (result) => emit({ id, result }); |
| 96 | + const notify = (notificationMethod, notificationParams) => |
| 97 | + emit({ method: notificationMethod, params: notificationParams }); |
| 98 | + const handler = |
| 99 | + typeof method === "string" && Object.hasOwn(handlers, method) ? handlers[method] : undefined; |
| 100 | + if (handler) { |
| 101 | + handler({ id, notify, params, sendResult }); |
| 102 | + return; |
| 103 | + } |
| 104 | + sendResult({}); |
| 105 | + }); |
| 106 | +} |
0 commit comments