|
| 1 | +/** |
| 2 | + * Real upstream "warm" request for issue #182. |
| 3 | + * |
| 4 | + * Sends a single minimal billable request to `POST /codex/responses` — the |
| 5 | + * same endpoint and request shape the live request path uses for its quota |
| 6 | + * probe — so the account's rolling usage window actually starts. A read-only |
| 7 | + * `GET /wham/usage` does NOT open the window (it only reports server-side |
| 8 | + * windows that already exist), so warming must send a genuine inference |
| 9 | + * request. The body is deliberately tiny (reasoning effort "none", verbosity |
| 10 | + * "low", no stored conversation) to keep the quota cost negligible, and mirrors |
| 11 | + * the proven quota-probe request shape used by the live request path. |
| 12 | + * |
| 13 | + * This module owns the network side-effect; the pure iteration/summary logic |
| 14 | + * lives in `warm.ts` and is injected this function via `codex-warm.ts`. |
| 15 | + */ |
| 16 | + |
| 17 | +import { CODEX_BASE_URL } from "../constants.js"; |
| 18 | +import { createCodexHeaders } from "../request/fetch-helpers.js"; |
| 19 | +import { getCodexInstructions } from "../prompts/codex.js"; |
| 20 | +import type { RequestBody } from "../types.js"; |
| 21 | +import { createLogger } from "../logger.js"; |
| 22 | + |
| 23 | +const log = createLogger("warm-request"); |
| 24 | + |
| 25 | +/** Model used for the warm ping. Mirrors the live quota-probe default. */ |
| 26 | +const WARM_MODEL = "gpt-5.4"; |
| 27 | + |
| 28 | +/** Hard ceiling on a warm request so a hung upstream cannot wedge the batch. */ |
| 29 | +const WARM_TIMEOUT_MS = 15_000; |
| 30 | + |
| 31 | +export interface WarmRequestParams { |
| 32 | + accountId: string; |
| 33 | + accessToken: string; |
| 34 | + organizationId: string | undefined; |
| 35 | + /** Override the timeout (tests). */ |
| 36 | + timeoutMs?: number; |
| 37 | + /** Injectable fetch for tests; defaults to global fetch. */ |
| 38 | + fetchImpl?: typeof fetch; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Build the minimal warm-ping request body. Exported for tests so the exact |
| 43 | + * shape (stream/store/reasoning) stays pinned. |
| 44 | + */ |
| 45 | +export async function buildWarmRequestBody(model = WARM_MODEL): Promise<RequestBody> { |
| 46 | + const instructions = await getCodexInstructions(model); |
| 47 | + return { |
| 48 | + model, |
| 49 | + stream: true, |
| 50 | + store: false, |
| 51 | + include: ["reasoning.encrypted_content"], |
| 52 | + instructions, |
| 53 | + input: [ |
| 54 | + { |
| 55 | + type: "message", |
| 56 | + role: "user", |
| 57 | + content: [{ type: "input_text", text: "warm ping" }], |
| 58 | + }, |
| 59 | + ], |
| 60 | + reasoning: { effort: "none", summary: "auto" }, |
| 61 | + text: { verbosity: "low" }, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Send one warm request to open the account's usage window. |
| 67 | + * |
| 68 | + * Resolves `true` when the upstream accepted the request enough to start the |
| 69 | + * window. A 2xx clearly opens it; a 429 (rate-limited) means the window is |
| 70 | + * already open/active, which still satisfies the user intent ("the window is |
| 71 | + * ticking"). Any other non-2xx, or a network/timeout error, throws so the |
| 72 | + * caller records the account as failed. |
| 73 | + */ |
| 74 | +export async function warmAccountWindow(params: WarmRequestParams): Promise<boolean> { |
| 75 | + const doFetch = params.fetchImpl ?? fetch; |
| 76 | + const body = await buildWarmRequestBody(); |
| 77 | + const headers = createCodexHeaders(undefined, params.accountId, params.accessToken, { |
| 78 | + model: WARM_MODEL, |
| 79 | + organizationId: params.organizationId, |
| 80 | + }); |
| 81 | + |
| 82 | + const controller = new AbortController(); |
| 83 | + const timeout = setTimeout( |
| 84 | + () => controller.abort(), |
| 85 | + params.timeoutMs ?? WARM_TIMEOUT_MS, |
| 86 | + ); |
| 87 | + try { |
| 88 | + const response = await doFetch(`${CODEX_BASE_URL}/codex/responses`, { |
| 89 | + method: "POST", |
| 90 | + headers, |
| 91 | + body: JSON.stringify(body), |
| 92 | + signal: controller.signal, |
| 93 | + }); |
| 94 | + |
| 95 | + // Drain/cancel the SSE stream immediately — we only needed to start the |
| 96 | + // window, not consume the response. |
| 97 | + try { |
| 98 | + await response.body?.cancel(); |
| 99 | + } catch { |
| 100 | + // Ignore cancellation failures. |
| 101 | + } |
| 102 | + |
| 103 | + if (response.ok) return true; |
| 104 | + |
| 105 | + // 429 = window already open and currently rate-limited. The user's goal |
| 106 | + // (window is ticking) is satisfied, so treat it as a successful warm. |
| 107 | + if (response.status === 429) { |
| 108 | + log.debug("Warm ping hit 429 — window already active", { |
| 109 | + accountId: params.accountId, |
| 110 | + }); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + throw new Error(`Warm request failed: HTTP ${response.status}`); |
| 115 | + } finally { |
| 116 | + clearTimeout(timeout); |
| 117 | + } |
| 118 | +} |
0 commit comments