import { getAccessToken } from "./dist/auth/token-refresh.js"; import { getAccountId } from "./dist/auth/token-refresh.js"; const accessToken = await getAccessToken(); const accountId = await getAccountId(); if (!accessToken || !accountId) { console.error("No token or account ID"); process.exit(1); } const instructions = await import("./dist/prompts/index.js").then((m) => m.getCodexInstructions("gpt-5.1"), ); console.log("Fetching from Codex API..."); const response = await fetch( "https://chatgpt.com/backend-api/codex/responses", { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, "openai-account-id": accountId, "openai-beta": "responses=2", "openai-originator": "codex_cli_rs", "Content-Type": "application/json", }, body: JSON.stringify({ model: "gpt-5.1", input: [ { type: "message", role: "user", content: [ { type: "input_text", text: "Say hello in one word", }, ], }, ], stream: true, store: false, reasoning: { effort: "medium", summary: "auto" }, instructions: instructions, }), }, ); console.log("Status:", response.status); if (response.status !== 200) { const errorText = await response.text(); console.error("Error:", errorText); process.exit(1); } const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ""; let chunks = 0; console.log("\n=== Raw SSE Chunks ===\n"); while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); const lines = buffer.split("\n"); buffer = lines.pop() || ""; for (const line of lines) { if (line.startsWith("data: ")) { chunks++; const data = line.substring(6); if (data.trim() !== "[DONE]") { try { const parsed = JSON.parse(data); console.log(`\n--- Chunk ${chunks} ---`); console.log(`Type: ${parsed.type}`); console.log(`Has delta: ${!!parsed.delta}`); if (parsed.delta) { console.log(`Delta:`, JSON.stringify(parsed.delta, null, 2)); } console.log(`Full chunk keys:`, Object.keys(parsed)); if (parsed.item) { console.log( `Item type: ${parsed.item.type}, role: ${parsed.item.role}`, ); } if (chunks >= 20) { console.log("\n=== Stopping after 20 chunks ==="); process.exit(0); } } catch (e) { console.log(`\nChunk ${chunks} (raw):`, data.substring(0, 200)); } } } } } console.log("\n=== End of stream ===");