UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

70 lines (69 loc) • 2.87 kB
/** * Codex (OpenAI Responses) SSE usage tap. * * The Codex proxy engine relays `upstream.body` to the client untouched and * logs before a single byte is read, so no Codex request has ever carried token * counts. This module adds a pass-through tap that scrapes `usage` out of the * stream without holding back or altering any bytes. * * ## Safety contract * * This sits in the hot path of a live proxy, so it is built to be incapable of * breaking a stream: * * - every chunk is enqueued **before** it is inspected; * - all parsing runs inside try/catch, and a throw is swallowed; * - a stream whose shape is unrecognised resolves `usage` to `null`, which is * exactly today's behaviour (a log with no token fields). * * The worst case is therefore "no tokens recorded", never a truncated or * corrupted response. * * ## Wire shape * * **Verified against real traffic.** Captured from a live `codex exec` run * through the proxy on 2026-08-21; the trimmed sample is at * `test/fixtures/codex-response-usage.sse` and is asserted against in the * codex suite. The real shape is * * event: response.completed * data: {"type":"response.completed","response":{"usage":{ * "input_tokens":N,"output_tokens":M, * "input_tokens_details":{"cached_tokens":K,"cache_write_tokens":W}, * "output_tokens_details":{"reasoning_tokens":R}}}} * * Note that `response.created` arrives first carrying `usage: null`, which is * why the scanner keeps the last non-null result rather than the first. * * It also accepts a `usage` object at the top level of any event and the * `prompt_tokens`/`completion_tokens` spellings. A `null` result means "not * observed", never "zero tokens". */ import type { CodexStreamUsage, CodexStreamEvidence } from "../types/index.js"; /** * Pull usage out of one parsed SSE `data:` payload. * * Returns null when the payload carries no recognisable usage object, so the * caller can keep the last non-null result rather than overwriting it with a * later event that happens not to carry usage. */ export declare function extractCodexUsage(payload: unknown): CodexStreamUsage | null; /** * Scan a slice of SSE text for usage, returning the last one found. * * Exported for tests: it is the whole parsing decision, and driving it through * a real Codex stream would need a live ChatGPT subscription. */ export declare function scanCodexSSEForUsage(text: string): CodexStreamUsage | null; /** * A pass-through TransformStream that reports the usage seen on a Codex SSE * stream. * * `usage` resolves when the stream ends: to the last usage observed, or null if * none was. It never rejects. */ export declare function createCodexUsageTap(): { stream: TransformStream<Uint8Array, Uint8Array>; usage: Promise<CodexStreamUsage | null>; evidence: () => CodexStreamEvidence; };