@copilotkit/aimock 1.27.3 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"recorder.js","names":["crypto","http"],"sources":["../src/recorder.ts"],"sourcesContent":["import * as http from \"node:http\";\nimport * as https from \"node:https\";\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as crypto from \"node:crypto\";\nimport type {\n ChatCompletionRequest,\n Fixture,\n FixtureMatch,\n FixtureResponse,\n RecordConfig,\n RecordedTimings,\n RecordProviderKey,\n ToolCall,\n} from \"./types.js\";\nimport { getLastMessageByRole, getTextContent } from \"./router.js\";\nimport { normalizeModelName } from \"./model-utils.js\";\nimport type { Logger } from \"./logger.js\";\nimport { collapseStreamingResponse } from \"./stream-collapse.js\";\nimport { writeErrorResponse } from \"./sse-writer.js\";\nimport { resolveUpstreamUrl } from \"./url.js\";\nimport { getTestId, slugifyTestId } from \"./helpers.js\";\nimport { DEFAULT_TEST_ID } from \"./constants.js\";\n\n/** Headers to strip when proxying — hop-by-hop (RFC 2616 §13.5.1) + client-set. */\nconst STRIP_HEADERS = new Set([\n // Hop-by-hop (RFC 2616 §13.5.1)\n \"connection\",\n \"keep-alive\",\n \"transfer-encoding\",\n \"te\",\n \"trailer\",\n \"upgrade\",\n \"proxy-authorization\",\n \"proxy-authenticate\",\n // Set by HTTP client from the target URL / body\n \"host\",\n \"content-length\",\n // Not relevant for LLM APIs; avoid leaking or mismatched encoding\n \"cookie\",\n \"accept-encoding\",\n]);\n\n/**\n * Captured upstream response, exposed to the `beforeWriteResponse` hook so\n * callers can decide whether to relay it or mutate it (e.g. chaos injection).\n */\nexport interface ProxyCapturedResponse {\n status: number;\n contentType: string;\n body: Buffer;\n}\n\nexport interface ProxyOptions {\n /**\n * Called after the upstream response has been captured and recorded, but\n * before the relay to the client. Contract when the hook returns `true`:\n * 1. It wrote its own response body on `res`.\n * 2. It journaled the outcome (proxyAndRecord will NOT journal it).\n * 3. proxyAndRecord skips its default relay and returns `\"handled_by_hook\"`.\n *\n * Returning `false` (or omitting the hook) lets proxyAndRecord relay the\n * upstream response normally and leaves journaling to the caller via the\n * `\"relayed\"` outcome. Rejected promises propagate and leave the response\n * unwritten.\n *\n * NOT invoked when the upstream response was streamed progressively to the\n * client (SSE, NDJSON, or binary event streams) — the bytes are already on\n * the wire and can't be mutated.\n * Callers that need to observe the bypass should pass `onHookBypassed`.\n */\n beforeWriteResponse?: (response: ProxyCapturedResponse) => boolean | Promise<boolean>;\n /**\n * Called when `beforeWriteResponse` was provided but could not be invoked\n * because the upstream response was streamed to the client progressively.\n * The hook was rolled + wired but the bytes left before it could fire.\n * Intended for observability (log/metric/journal annotation) — proxyAndRecord\n * still returns `\"relayed\"`.\n */\n onHookBypassed?: (reason: \"sse_streamed\" | \"ndjson_streamed\" | \"binary_streamed\") => void;\n}\n\n/**\n * Outcome of a proxyAndRecord call, returned so the caller can decide whether\n * to journal, fall through, or stop — without sharing a mutable flag with the\n * `beforeWriteResponse` hook.\n *\n * - `\"not_configured\"` — no upstream URL for this provider; caller should fall\n * through to its next branch (typically strict/404).\n * - `\"relayed\"` — the default code path wrote a response (upstream success or\n * synthesized 502 error). Caller should journal the outcome.\n * - `\"handled_by_hook\"` — the hook wrote + journaled its own response. Caller\n * should not double-journal.\n */\nexport type ProxyOutcome = \"not_configured\" | \"relayed\" | \"handled_by_hook\";\n\n/**\n * Result of `persistFixture`:\n * - `\"skipped\"` — proxy-only mode; the caller has nothing else to do.\n * - `\"written\"` — fixture saved to `filepath` and (unless the match was empty)\n * registered into the in-memory cache so the next identical request matches.\n * - `\"failed\"` — filesystem write failed. Caller decides how to surface it\n * (e.g. setting `X-AIMock-Record-Error` on a relay response).\n */\nexport type PersistFixtureResult =\n | { kind: \"skipped\" }\n | { kind: \"written\"; filepath: string }\n | { kind: \"failed\"; error: string };\n\n/**\n * Write a built fixture to disk (snapshot vs. timestamp file layout) and, when\n * the match is non-empty, register it in the in-memory cache so subsequent\n * identical requests match. Extracted from `proxyAndRecord` so the fal\n * queue-walk recorder (which makes multiple upstream calls before knowing the\n * final body) can share the same persistence behavior without re-implementing\n * snapshot-mode merging and warnings.\n */\nexport function persistFixture(opts: {\n record: RecordConfig;\n providerKey: RecordProviderKey;\n testId: string;\n fixture: Fixture;\n fixtures: Fixture[];\n warnings?: string[];\n logger: Logger;\n}): PersistFixtureResult {\n const { record, providerKey, testId, fixture, fixtures, warnings = [], logger } = opts;\n\n // Match criteria with no userMessage / inputText / endpoint will not match\n // any future request — warn, then save to disk for inspection but skip the\n // in-memory registration so a defective fixture doesn't shadow real ones.\n // turnIndex/hasToolResult are pure multi-turn disambiguators on their own.\n const m = fixture.match;\n const isEmptyMatch =\n m.userMessage === undefined && m.inputText === undefined && m.endpoint === undefined;\n if (isEmptyMatch) {\n logger.warn(\"Recorded fixture has empty match criteria — skipping in-memory registration\");\n }\n\n if (record.proxyOnly) {\n logger.info(`Proxied ${providerKey} request (proxy-only mode)`);\n return { kind: \"skipped\" };\n }\n\n const fixturePath = record.fixturePath ?? \"./fixtures/recorded\";\n let isSnapshotMode = testId !== DEFAULT_TEST_ID;\n let filepath: string;\n let mergeExisting = false;\n\n if (isSnapshotMode) {\n const slug = slugifyTestId(testId);\n if (!slug) {\n // Slug resolved to empty (e.g. testId was all punctuation) — fall back\n // to timestamp-based recording so we still capture the fixture.\n isSnapshotMode = false;\n const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n filepath = path.join(\n fixturePath,\n `${providerKey}-${timestamp}-${crypto.randomUUID().slice(0, 8)}.json`,\n );\n } else {\n filepath = path.join(fixturePath, slug, `${providerKey}.json`);\n mergeExisting = true;\n }\n } else {\n const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n const timestampFile = `${providerKey}-${timestamp}-${crypto.randomUUID().slice(0, 8)}.json`;\n filepath = fixture.match.context\n ? path.join(fixturePath, fixture.match.context, timestampFile)\n : path.join(fixturePath, timestampFile);\n }\n\n const fileWarnings = [\n ...(isEmptyMatch ? [\"Empty match criteria — this fixture will not match any request\"] : []),\n ...warnings,\n ];\n\n try {\n fs.mkdirSync(path.dirname(filepath), { recursive: true });\n\n // Auth headers are forwarded to upstream but excluded from saved fixtures.\n // The persisted fixture is always the real upstream response, even when\n // chaos later mutates the relay; replay must see what upstream said.\n let fileContent: { fixtures: unknown[]; _warning?: string };\n if (mergeExisting && fs.existsSync(filepath)) {\n try {\n const existing = JSON.parse(fs.readFileSync(filepath, \"utf-8\"));\n fileContent = { fixtures: [...(existing.fixtures ?? []), fixture] };\n } catch (mergeErr) {\n const msg = mergeErr instanceof Error ? mergeErr.message : \"unknown\";\n logger.warn(`Could not read existing fixture file ${filepath} (${msg}) — overwriting`);\n fileContent = { fixtures: [fixture] };\n }\n } else {\n fileContent = { fixtures: [fixture] };\n }\n if (fileWarnings.length > 0) {\n fileContent._warning = fileWarnings.join(\"; \");\n }\n // Atomic write: write to temp file then rename to avoid read-modify-write\n // races. Keep synchronous — for streamed responses the HTTP reply is\n // already on the wire, so async writes would race with callers checking\n // the filesystem before the fixture has landed.\n const tmpPath = filepath + \".tmp.\" + process.pid;\n fs.writeFileSync(tmpPath, JSON.stringify(fileContent, null, 2), \"utf-8\");\n fs.renameSync(tmpPath, filepath);\n\n if (!isEmptyMatch) {\n fixtures.push(fixture);\n }\n logger.warn(`Response recorded → ${filepath}`);\n return { kind: \"written\", filepath };\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown filesystem error\";\n logger.error(`Failed to save fixture to disk: ${msg}`);\n return { kind: \"failed\", error: msg };\n }\n}\n\n/**\n * Proxy an unmatched request to the real upstream provider, record the\n * response as a fixture on disk and in memory, then relay the response\n * back to the original client.\n */\nexport async function proxyAndRecord(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n request: ChatCompletionRequest,\n providerKey: RecordProviderKey,\n pathname: string,\n fixtures: Fixture[],\n defaults: {\n record?: RecordConfig;\n logger: Logger;\n requestTransform?: (req: ChatCompletionRequest) => ChatCompletionRequest;\n },\n rawBody?: string,\n options?: ProxyOptions,\n): Promise<ProxyOutcome> {\n const record = defaults.record;\n if (!record) return \"not_configured\";\n\n const providers = record.providers;\n // Gemini Interactions uses the same upstream API as Gemini (identical base URL\n // and auth), so we remap the provider key to reuse the configured Gemini URL.\n const lookupKey = providerKey === \"gemini-interactions\" ? \"gemini\" : providerKey;\n const upstreamUrl = providers[lookupKey];\n\n if (!upstreamUrl) {\n defaults.logger.warn(`No upstream URL configured for provider \"${providerKey}\" — cannot proxy`);\n return \"not_configured\";\n }\n\n let target: URL;\n try {\n target = resolveUpstreamUrl(upstreamUrl, pathname);\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n defaults.logger.error(\n `Invalid upstream URL for provider \"${providerKey}\": ${upstreamUrl} (${msg})`,\n );\n writeErrorResponse(\n res,\n 502,\n JSON.stringify({\n error: { message: `Invalid upstream URL: ${upstreamUrl}`, type: \"proxy_error\" },\n }),\n );\n return \"relayed\";\n }\n\n defaults.logger.warn(`NO FIXTURE MATCH — proxying to ${upstreamUrl}${pathname}`);\n\n // Forward all request headers except hop-by-hop and client-set ones.\n const forwardHeaders: Record<string, string> = {};\n for (const [name, val] of Object.entries(req.headers)) {\n if (val !== undefined && !STRIP_HEADERS.has(name)) {\n forwardHeaders[name] = Array.isArray(val) ? val.join(\", \") : val;\n }\n }\n\n const requestBody = rawBody ?? JSON.stringify(request);\n\n // Make upstream request\n let upstreamStatus: number;\n let upstreamHeaders: http.IncomingHttpHeaders;\n let upstreamBody: string;\n let rawBuffer: Buffer;\n\n // Track whether we streamed SSE progressively to the client; if so,\n // skip the final res.writeHead/res.end relay at the bottom of this fn.\n let streamedToClient = false;\n let clientDisconnected = false;\n let frameTimestamps: number[] = [];\n let streamStartTime = 0;\n try {\n const result = await makeUpstreamRequest(\n target,\n forwardHeaders,\n requestBody,\n res,\n req.method,\n defaults.logger,\n { upstreamTimeoutMs: record.upstreamTimeoutMs, bodyTimeoutMs: record.bodyTimeoutMs },\n );\n upstreamStatus = result.status;\n upstreamHeaders = result.headers;\n upstreamBody = result.body;\n rawBuffer = result.rawBuffer;\n streamedToClient = result.streamedToClient;\n clientDisconnected = result.clientDisconnected;\n frameTimestamps = result.frameTimestamps;\n streamStartTime = result.streamStartTime;\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown proxy error\";\n defaults.logger.error(`Proxy request failed: ${msg}`);\n if (!res.headersSent) {\n res.writeHead(502, { \"Content-Type\": \"application/json\" });\n res.end(\n JSON.stringify({\n error: { message: `Proxy to upstream failed: ${msg}`, type: \"proxy_error\" },\n }),\n );\n } else {\n // SSE headers already sent — gracefully close the connection\n res.end();\n }\n return \"relayed\";\n }\n\n // Detect streaming response and collapse if necessary.\n // NOTE: collapse buffers the entire upstream body in memory. Fine for\n // current chat-completions traffic (responses are small), but revisit if\n // this path ever proxies long-lived or large streams — both the buffer\n // here and the hook below receive the full payload.\n const contentType = upstreamHeaders[\"content-type\"];\n const ctString = Array.isArray(contentType) ? contentType.join(\", \") : (contentType ?? \"\");\n const isBinaryStream = ctString.toLowerCase().includes(\"application/vnd.amazon.eventstream\");\n const collapsed = collapseStreamingResponse(\n ctString,\n providerKey,\n isBinaryStream ? rawBuffer : upstreamBody,\n defaults.logger,\n );\n\n let fixtureResponse: FixtureResponse;\n\n // TTS response — binary audio, not JSON\n const isAudioResponse = ctString.toLowerCase().startsWith(\"audio/\");\n if (isAudioResponse && rawBuffer.length > 0) {\n // Derive format from Content-Type (audio/mpeg→mp3, audio/opus→opus, etc.)\n const audioFormat = ctString\n .toLowerCase()\n .replace(\"audio/\", \"\")\n .replace(\"mpeg\", \"mp3\")\n .split(\";\")[0]\n .trim();\n fixtureResponse = {\n audio: rawBuffer.toString(\"base64\"),\n ...(audioFormat && audioFormat !== \"mp3\" ? { format: audioFormat } : {}),\n };\n } else if (collapsed) {\n // Streaming response — use collapsed result\n defaults.logger.warn(`Streaming response detected (${ctString}) — collapsing to fixture`);\n if (collapsed.truncated) {\n defaults.logger.warn(\"Bedrock EventStream: CRC mismatch — response may be truncated\");\n }\n if (collapsed.droppedChunks && collapsed.droppedChunks > 0) {\n defaults.logger.warn(`${collapsed.droppedChunks} chunk(s) dropped during stream collapse`);\n }\n // Audio from streamed inlineData (e.g. Gemini SSE with audio parts)\n if (collapsed.audioB64) {\n fixtureResponse = {\n audio: {\n b64Json: collapsed.audioB64,\n contentType: collapsed.audioMimeType ?? \"audio/mpeg\",\n },\n };\n } else if (\n collapsed.content === \"\" &&\n (!collapsed.toolCalls || collapsed.toolCalls.length === 0)\n ) {\n defaults.logger.warn(\"Stream collapse produced empty content — fixture may be incomplete\");\n const reasoningSpread = collapsed.reasoning ? { reasoning: collapsed.reasoning } : {};\n fixtureResponse = { content: collapsed.content ?? \"\", ...reasoningSpread };\n } else {\n const reasoningSpread = collapsed.reasoning ? { reasoning: collapsed.reasoning } : {};\n if (collapsed.toolCalls && collapsed.toolCalls.length > 0) {\n const sanitizedToolCalls = collapsed.toolCalls.map((tc) => ({\n ...tc,\n name: tc.name ?? \"\",\n arguments: tc.arguments ?? \"{}\",\n }));\n if (collapsed.content) {\n // Both content and toolCalls present — save as ContentWithToolCallsResponse\n fixtureResponse = {\n content: collapsed.content,\n toolCalls: sanitizedToolCalls,\n ...reasoningSpread,\n };\n } else {\n fixtureResponse = { toolCalls: sanitizedToolCalls, ...reasoningSpread };\n }\n } else {\n fixtureResponse = { content: collapsed.content ?? \"\", ...reasoningSpread };\n }\n }\n } else {\n // Non-streaming — try to parse as JSON\n let parsedResponse: unknown = null;\n try {\n parsedResponse = JSON.parse(upstreamBody);\n } catch (parseErr) {\n const msg = parseErr instanceof Error ? parseErr.message : \"unknown\";\n defaults.logger.warn(\n `Upstream response is not valid JSON (${msg}) — saving as error fixture`,\n );\n }\n // fal.ai returns arbitrary, model-specific JSON shapes (images, video URLs,\n // audio file objects, etc.). Round-trip the payload verbatim instead of\n // letting buildFixtureResponse mis-classify it as ImageResponse / VideoResponse.\n if (request._endpointType === \"fal\" && parsedResponse !== null) {\n const obj = parsedResponse as Record<string, unknown>;\n const isErrorShape =\n typeof obj.error === \"object\" &&\n obj.error !== null &&\n typeof (obj.error as Record<string, unknown>).message === \"string\";\n if (isErrorShape) {\n const err = obj.error as Record<string, unknown>;\n fixtureResponse = {\n error: {\n message: String(err.message ?? \"Unknown error\"),\n type: String(err.type ?? \"api_error\"),\n code: err.code ? String(err.code) : undefined,\n },\n status: upstreamStatus,\n };\n } else {\n fixtureResponse = { json: parsedResponse, status: upstreamStatus };\n }\n } else {\n let encodingFormat: string | undefined;\n try {\n encodingFormat = rawBody ? JSON.parse(rawBody).encoding_format : undefined;\n } catch (err) {\n defaults.logger.debug(\n `Could not parse encoding_format from raw body: ${err instanceof Error ? err.message : \"unknown error\"}`,\n );\n }\n fixtureResponse = buildFixtureResponse(parsedResponse, upstreamStatus, encodingFormat);\n }\n }\n\n // If the client disconnected mid-stream, the collected data is likely\n // truncated. Saving a partial fixture is worse than saving none — skip\n // fixture persistence entirely.\n if (clientDisconnected) {\n defaults.logger.warn(\n \"Client disconnected mid-stream — skipping fixture save to avoid truncated data\",\n );\n return \"relayed\";\n }\n\n // Build RecordedTimings from frame timestamps captured during streaming.\n // Requires at least 2 timestamps (first frame + at least one more) to\n // produce meaningful timing data.\n let recordedTimings: RecordedTimings | undefined;\n if (frameTimestamps.length > 1) {\n const ts = frameTimestamps;\n recordedTimings = {\n ttftMs: ts[0] - streamStartTime,\n interChunkDelaysMs: ts.slice(1).map((t, i) => t - ts[i]),\n totalDurationMs: ts[ts.length - 1] - streamStartTime,\n };\n }\n\n const matchRequest = defaults.requestTransform ? defaults.requestTransform(request) : request;\n const metadata = buildFixtureMetadata(request);\n const fixture: Fixture = {\n match: buildFixtureMatch(matchRequest, defaults.record),\n response: fixtureResponse,\n ...(recordedTimings && { recordedTimings }),\n ...(metadata && { metadata }),\n };\n\n const persistWarnings: string[] = [];\n if (collapsed?.truncated) {\n persistWarnings.push(\"Stream response was truncated — fixture may be incomplete\");\n }\n const persistResult = persistFixture({\n record,\n providerKey,\n testId: getTestId(req),\n fixture,\n fixtures,\n warnings: persistWarnings,\n logger: defaults.logger,\n });\n if (persistResult.kind === \"failed\") {\n if (!res.headersSent) {\n res.setHeader(\"X-AIMock-Record-Error\", persistResult.error);\n } else {\n defaults.logger.warn(`Cannot set X-AIMock-Record-Error header — headers already sent`);\n }\n defaults.logger.warn(`Response relayed but NOT saved to disk — see error above`);\n }\n\n // Relay upstream response to client (skip when the response was already\n // streamed progressively by makeUpstreamRequest — headers and body are\n // already on the wire).\n if (streamedToClient) {\n // The hook can't run because the body is already on the wire. Surface\n // the bypass so the caller (typically the chaos layer) can record it —\n // otherwise a configured chaos action silently no-ops on streamed traffic.\n if (options?.beforeWriteResponse && options.onHookBypassed) {\n const bypassReason: \"sse_streamed\" | \"ndjson_streamed\" | \"binary_streamed\" = isBinaryStream\n ? \"binary_streamed\"\n : ctString.toLowerCase().includes(\"application/x-ndjson\")\n ? \"ndjson_streamed\"\n : \"sse_streamed\";\n try {\n options.onHookBypassed(bypassReason);\n } catch (err) {\n defaults.logger.warn(\n `onHookBypassed callback threw: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n } else {\n // Give the caller a chance to mutate or replace the response before relay.\n // Used by the chaos layer to turn a successful proxy into a malformed body.\n // `body` is the raw upstream bytes so binary payloads survive round-tripping.\n if (options?.beforeWriteResponse) {\n let handled: boolean | undefined;\n try {\n handled = await options.beforeWriteResponse({\n status: upstreamStatus,\n contentType: ctString,\n body: rawBuffer,\n });\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n throw new Error(`beforeWriteResponse hook failed for ${providerKey}: ${msg}`);\n }\n if (handled) return \"handled_by_hook\";\n }\n\n // Normalize status codes for the client: aimock acts as a gateway, so\n // upstream provider details (429 rate-limits, 503 outages, etc.) should\n // not leak. Successes → 200, errors → 502 (Bad Gateway).\n const clientStatus = upstreamStatus >= 200 && upstreamStatus < 300 ? 200 : 502;\n const isAudioRelay = ctString.toLowerCase().startsWith(\"audio/\");\n // When an upstream error (non-2xx) is relayed for an audio endpoint, the\n // body is typically a JSON error object — override the content-type so\n // clients don't try to decode JSON as audio.\n const relayHeaders: Record<string, string> = {};\n const clientCt =\n (clientStatus >= 200 && clientStatus < 300) || !isAudioRelay\n ? ctString || \"application/json\"\n : \"application/json\";\n if (clientCt) {\n relayHeaders[\"Content-Type\"] = clientCt;\n }\n res.writeHead(clientStatus, relayHeaders);\n res.end(isBinaryStream || isAudioRelay ? rawBuffer : upstreamBody);\n }\n\n return \"relayed\";\n}\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\nfunction clampTimeout(value: number | undefined, fallback: number): number {\n if (value == null || !Number.isFinite(value) || value <= 0) return fallback;\n return value;\n}\n\nfunction makeUpstreamRequest(\n target: URL,\n headers: Record<string, string>,\n body: string,\n clientRes?: http.ServerResponse,\n method: string = \"POST\",\n logger?: Logger,\n timeouts?: Pick<RecordConfig, \"upstreamTimeoutMs\" | \"bodyTimeoutMs\">,\n): Promise<{\n status: number;\n headers: http.IncomingHttpHeaders;\n body: string;\n rawBuffer: Buffer;\n streamedToClient: boolean;\n clientDisconnected: boolean;\n frameTimestamps: number[];\n streamStartTime: number;\n}> {\n return new Promise((resolve, reject) => {\n const transport = target.protocol === \"https:\" ? https : http;\n const UPSTREAM_TIMEOUT_MS = clampTimeout(timeouts?.upstreamTimeoutMs, 30_000);\n const BODY_TIMEOUT_MS = clampTimeout(timeouts?.bodyTimeoutMs, 30_000);\n const req = transport.request(\n target,\n {\n method,\n timeout: UPSTREAM_TIMEOUT_MS,\n headers: {\n ...headers,\n \"Content-Length\": Buffer.byteLength(body).toString(),\n },\n },\n (res) => {\n res.setTimeout(BODY_TIMEOUT_MS, () => {\n req.destroy(new Error(`Upstream response timed out after ${BODY_TIMEOUT_MS / 1000}s`));\n });\n // Detect streaming content types so we can tee upstream chunks to the\n // client as they arrive rather than buffering the entire stream and\n // replaying it in a single res.end() at the bottom of proxyAndRecord.\n // Buffering collapses every frame into one client-visible write,\n // which defeats progressive rendering in downstream consumers and\n // can trip HTTP idle timeouts on slow calls.\n const ct = res.headers[\"content-type\"];\n const ctStr = Array.isArray(ct) ? ct.join(\", \") : (ct ?? \"\");\n const ctLower = ctStr.toLowerCase();\n const isSSE = ctLower.includes(\"text/event-stream\");\n const isNDJSON = ctLower.includes(\"application/x-ndjson\");\n const isBinaryEventStream = ctLower.includes(\"application/vnd.amazon.eventstream\");\n const isProgressiveStream = isSSE || isNDJSON || isBinaryEventStream;\n // SSE/NDJSON frame timing capture — timestamps each complete frame\n // so proxyAndRecord can build RecordedTimings for the fixture.\n const frameTimestamps: number[] = [];\n const streamStartTime = Date.now();\n let frameBuffer = \"\";\n let binaryFrameBuffer = Buffer.alloc(0);\n\n let streamedToClient = false;\n let clientDisconnected = false;\n if (isProgressiveStream && clientRes && !clientRes.headersSent) {\n const relayHeaders: Record<string, string> = {\n \"Cache-Control\": \"no-cache, no-transform\",\n Connection: \"keep-alive\",\n \"X-Accel-Buffering\": \"no\",\n };\n if (ctStr) relayHeaders[\"Content-Type\"] = ctStr;\n // Normalize status codes for the client: aimock acts as a gateway,\n // so upstream provider details should not leak.\n // Successes → 200, errors → 502 (Bad Gateway).\n const rawStatus = res.statusCode ?? 200;\n const clientStatus = rawStatus >= 200 && rawStatus < 300 ? 200 : 502;\n clientRes.writeHead(clientStatus, relayHeaders);\n // Flush headers immediately so the client starts parsing frames\n // before the first data chunk arrives.\n if (typeof clientRes.flushHeaders === \"function\") clientRes.flushHeaders();\n streamedToClient = true;\n // Stop relaying if the client disconnects mid-stream.\n // Check writableFinished to distinguish normal completion (where\n // \"close\" also fires) from premature client disconnects.\n clientRes.on(\"close\", () => {\n if (!clientRes.writableFinished) {\n clientDisconnected = true;\n req.destroy();\n }\n });\n }\n const chunks: Buffer[] = [];\n res.on(\"data\", (chunk: Buffer) => {\n chunks.push(chunk);\n\n // Capture per-frame timestamps for SSE/NDJSON streams.\n // TCP data events don't align with SSE frames — buffer and\n // split on the protocol delimiter to timestamp each complete frame.\n if (isSSE || isNDJSON) {\n frameBuffer += chunk.toString();\n const delimiter = isNDJSON ? \"\\n\" : \"\\n\\n\";\n const parts = frameBuffer.split(delimiter);\n // All complete frames (everything except the last part which\n // may be incomplete).\n for (let fi = 0; fi < parts.length - 1; fi++) {\n if (parts[fi].trim().length > 0) {\n frameTimestamps.push(Date.now());\n }\n }\n // Last part stays in buffer (may be incomplete)\n frameBuffer = parts[parts.length - 1];\n }\n\n // Binary EventStream frame boundary detection — parse the 4-byte\n // total-length prefix to detect complete frames without decoding\n // frame contents (CRC validation happens in stream-collapse).\n if (isBinaryEventStream) {\n binaryFrameBuffer = Buffer.concat([binaryFrameBuffer, chunk]);\n while (binaryFrameBuffer.length >= 4) {\n const totalLen = binaryFrameBuffer.readUInt32BE(0);\n if (totalLen < 12 || binaryFrameBuffer.length < totalLen) break;\n frameTimestamps.push(Date.now());\n binaryFrameBuffer = binaryFrameBuffer.subarray(totalLen);\n }\n }\n\n if (\n streamedToClient &&\n clientRes &&\n !clientDisconnected &&\n !clientRes.destroyed &&\n !clientRes.writableEnded\n ) {\n try {\n clientRes.write(chunk);\n } catch (writeErr) {\n logger?.debug(\n `Failed to relay chunk to client: ${writeErr instanceof Error ? writeErr.message : \"unknown\"}`,\n );\n clientDisconnected = true;\n }\n }\n });\n res.on(\"error\", reject);\n res.on(\"end\", () => {\n if (res.socket) res.setTimeout(0);\n // Flush remaining text frame buffer — captures the last frame if\n // the stream ended without a trailing delimiter. Binary EventStream\n // frames are length-prefixed so partial frames at end-of-stream are\n // genuinely incomplete and should not be timestamped.\n if ((isSSE || isNDJSON) && frameBuffer.trim().length > 0) {\n frameTimestamps.push(Date.now());\n }\n const rawBuffer = Buffer.concat(chunks);\n if (\n streamedToClient &&\n clientRes &&\n !clientDisconnected &&\n !clientRes.destroyed &&\n !clientRes.writableEnded\n ) {\n try {\n clientRes.end();\n } catch (endErr) {\n logger?.debug(\n `Failed to end client response: ${endErr instanceof Error ? endErr.message : \"unknown\"}`,\n );\n }\n }\n resolve({\n status: res.statusCode ?? 500,\n headers: res.headers,\n body: rawBuffer.toString(),\n rawBuffer,\n streamedToClient,\n clientDisconnected,\n frameTimestamps,\n streamStartTime,\n });\n });\n },\n );\n req.on(\"timeout\", () => {\n req.destroy(\n new Error(\n `Upstream request timed out after ${UPSTREAM_TIMEOUT_MS / 1000}s: ${target.href}`,\n ),\n );\n });\n req.on(\"error\", reject);\n req.write(body);\n req.end();\n });\n}\n\n/**\n * Detect the response format from the parsed upstream JSON and convert\n * it into an aimock FixtureResponse.\n */\nfunction buildFixtureResponse(\n parsed: unknown,\n status: number,\n encodingFormat?: string,\n): FixtureResponse {\n if (parsed === null || parsed === undefined) {\n // Raw / unparseable response — save as error\n return {\n error: { message: \"Upstream returned non-JSON response\", type: \"proxy_error\" },\n status,\n };\n }\n\n const obj = parsed as Record<string, unknown>;\n\n // Error response — only match the actual { error: { message: \"...\" } } shape\n // used by OpenAI/Anthropic/etc., not arbitrary truthy `.error` fields.\n if (\n typeof obj.error === \"object\" &&\n obj.error !== null &&\n typeof (obj.error as Record<string, unknown>).message === \"string\"\n ) {\n const err = obj.error as Record<string, unknown>;\n return {\n error: {\n message: String(err.message ?? \"Unknown error\"),\n type: String(err.type ?? \"api_error\"),\n code: err.code ? String(err.code) : undefined,\n },\n status,\n };\n }\n\n // OpenAI embeddings: { data: [{ embedding: [...] }] }\n if (Array.isArray(obj.data) && obj.data.length > 0) {\n const first = obj.data[0] as Record<string, unknown>;\n if (Array.isArray(first.embedding)) {\n return { embedding: first.embedding as number[] };\n }\n if (typeof first.embedding === \"string\" && encodingFormat === \"base64\") {\n const buf = Buffer.from(first.embedding, \"base64\");\n if (buf.byteLength % 4 !== 0) {\n // Malformed embedding — return a zero-dimension embedding fixture\n return { embedding: [] };\n }\n // Uint8Array constructor copies Buffer data to a fresh ArrayBuffer at offset 0,\n // guaranteeing the alignment Float32Array requires.\n const copied = new Uint8Array(buf);\n const floats = new Float32Array(copied.buffer, 0, buf.byteLength / 4);\n return { embedding: Array.from(floats) };\n }\n // OpenAI image generation: { created, data: [{ url, b64_json, revised_prompt }] }\n if (first.url || first.b64_json) {\n const images = (obj.data as Array<Record<string, unknown>>).map((item) => ({\n ...(item.url ? { url: String(item.url) } : {}),\n ...(item.b64_json ? { b64Json: String(item.b64_json) } : {}),\n ...(item.revised_prompt ? { revisedPrompt: String(item.revised_prompt) } : {}),\n }));\n if (images.length === 1) {\n return { image: images[0] };\n }\n return { images };\n }\n }\n\n // Gemini Imagen: { predictions: [...] }\n if (Array.isArray(obj.predictions)) {\n const images = (obj.predictions as Array<Record<string, unknown>>).map((p) => ({\n ...(p.bytesBase64Encoded ? { b64Json: String(p.bytesBase64Encoded) } : {}),\n ...(p.mimeType ? { mimeType: String(p.mimeType) } : {}),\n }));\n if (images.length === 1) {\n return { image: images[0] };\n }\n return { images };\n }\n\n // OpenAI transcription: { text: \"...\", ... }\n if (\n typeof obj.text === \"string\" &&\n (obj.task === \"transcribe\" || obj.language !== undefined || obj.duration !== undefined)\n ) {\n return {\n transcription: {\n text: obj.text as string,\n ...(obj.language ? { language: String(obj.language) } : {}),\n ...(obj.duration !== undefined ? { duration: Number(obj.duration) } : {}),\n ...(Array.isArray(obj.words) ? { words: obj.words } : {}),\n ...(Array.isArray(obj.segments) ? { segments: obj.segments } : {}),\n },\n };\n }\n\n // Gemini Interactions: { id, status, outputs: [{ type: \"text\", text }, { type: \"function_call\", name, arguments }] }\n if (\n Array.isArray(obj.outputs) &&\n obj.outputs.length > 0 &&\n !(\"choices\" in obj) &&\n !(\"content\" in obj) &&\n !(\"candidates\" in obj)\n ) {\n const outputs = obj.outputs as Array<Record<string, unknown>>;\n const fnCallOutputs = outputs.filter((o) => o.type === \"function_call\");\n const textOutputs = outputs.filter((o) => o.type === \"text\" && typeof o.text === \"string\");\n const hasToolCalls = fnCallOutputs.length > 0;\n const joinedText = textOutputs.map((o) => String(o.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = fnCallOutputs.map((o) => ({\n name: String(o.name),\n arguments: typeof o.arguments === \"string\" ? o.arguments : JSON.stringify(o.arguments),\n ...(o.id ? { id: String(o.id) } : {}),\n }));\n if (hasContent) {\n return { content: joinedText, toolCalls };\n }\n return { toolCalls };\n }\n if (hasContent) {\n return { content: joinedText };\n }\n // Recognized Gemini Interactions shape but empty content\n return { content: \"\" };\n }\n\n // OpenAI video generation: { id, status, ... }\n // Guard against false positives: many API responses have `id` + `status` fields\n // (e.g. chat completions, Anthropic messages). Reject if the response has fields\n // that indicate a known non-video format.\n if (\n typeof obj.id === \"string\" &&\n typeof obj.status === \"string\" &&\n (obj.status === \"completed\" || obj.status === \"in_progress\" || obj.status === \"failed\") &&\n !(\"choices\" in obj) &&\n !(\"content\" in obj) &&\n !(\"candidates\" in obj) &&\n !(\"message\" in obj) &&\n !(\"data\" in obj) &&\n !(\"object\" in obj) &&\n !(\"outputs\" in obj) &&\n !(\"model\" in obj) &&\n !(\"response\" in obj) &&\n !(\"done\" in obj) &&\n !(\"usage\" in obj) &&\n !(\"error\" in obj)\n ) {\n if (obj.status === \"completed\" && obj.url) {\n return {\n video: {\n id: String(obj.id),\n status: \"completed\" as const,\n url: String(obj.url),\n },\n };\n }\n return {\n video: {\n id: String(obj.id),\n status: obj.status === \"failed\" ? (\"failed\" as const) : (\"processing\" as const),\n },\n };\n }\n\n // Direct embedding: { embedding: [...] }\n if (Array.isArray(obj.embedding)) {\n return { embedding: obj.embedding as number[] };\n }\n\n // OpenAI chat completion: { choices: [{ message: { content, tool_calls } }] }\n if (Array.isArray(obj.choices) && obj.choices.length > 0) {\n const choice = obj.choices[0] as Record<string, unknown>;\n const message = choice.message as Record<string, unknown> | undefined;\n if (message) {\n const hasToolCalls = Array.isArray(message.tool_calls) && message.tool_calls.length > 0;\n const hasContent = typeof message.content === \"string\" && message.content.length > 0;\n\n const openaiReasoning =\n typeof message.reasoning_content === \"string\" && message.reasoning_content.length > 0\n ? message.reasoning_content\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = (message.tool_calls as Array<Record<string, unknown>>).map(\n (tc) => {\n const fn = tc.function as Record<string, unknown>;\n return {\n name: String(fn.name),\n arguments: String(fn.arguments),\n ...(tc.id ? { id: String(tc.id) } : {}),\n };\n },\n );\n if (hasContent) {\n return {\n content: message.content as string,\n toolCalls,\n ...(openaiReasoning ? { reasoning: openaiReasoning } : {}),\n };\n }\n return { toolCalls, ...(openaiReasoning ? { reasoning: openaiReasoning } : {}) };\n }\n // Text content only\n if (hasContent) {\n return {\n content: message.content as string,\n ...(openaiReasoning ? { reasoning: openaiReasoning } : {}),\n };\n }\n // Recognized OpenAI shape but empty content (e.g. content filtering, zero max_tokens)\n return { content: \"\", ...(openaiReasoning ? { reasoning: openaiReasoning } : {}) };\n }\n }\n\n // Anthropic: { content: [{ type: \"text\", text: \"...\" }] } or tool_use\n if (Array.isArray(obj.content) && obj.content.length > 0) {\n const blocks = obj.content as Array<Record<string, unknown>>;\n const toolUseBlocks = blocks.filter((b) => b.type === \"tool_use\");\n const textBlocks = blocks.filter((b) => b.type === \"text\" && typeof b.text === \"string\");\n const thinkingBlocks = blocks.filter((b) => b.type === \"thinking\");\n const hasToolCalls = toolUseBlocks.length > 0;\n const joinedText = textBlocks.map((b) => String(b.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const anthropicReasoning =\n thinkingBlocks.length > 0\n ? thinkingBlocks.map((b) => String(b.thinking ?? \"\")).join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = toolUseBlocks.map((b) => ({\n name: String(b.name),\n arguments: typeof b.input === \"string\" ? b.input : JSON.stringify(b.input),\n ...(b.id ? { id: String(b.id) } : {}),\n }));\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}),\n };\n }\n return { toolCalls, ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}),\n };\n }\n // Thinking-only response (no text, no tool calls)\n if (anthropicReasoning) {\n return { content: \"\", reasoning: anthropicReasoning };\n }\n }\n\n // Gemini: { candidates: [{ content: { parts: [{ text: \"...\" }] } }] }\n if (Array.isArray(obj.candidates) && obj.candidates.length > 0) {\n const candidate = obj.candidates[0] as Record<string, unknown>;\n const content = candidate.content as Record<string, unknown> | undefined;\n if (content && Array.isArray(content.parts)) {\n const parts = content.parts as Array<Record<string, unknown>>;\n\n // Audio inlineData parts take priority over text\n const audioParts = parts.filter(\n (p: Record<string, unknown>) =>\n p.inlineData &&\n typeof (p.inlineData as Record<string, unknown>).mimeType === \"string\" &&\n ((p.inlineData as Record<string, unknown>).mimeType as string).startsWith(\"audio/\"),\n );\n if (audioParts.length > 0) {\n const inlineData = audioParts[0].inlineData as Record<string, unknown>;\n return {\n audio: {\n b64Json: String(inlineData.data ?? \"\"),\n contentType: String(inlineData.mimeType),\n },\n };\n }\n\n const fnCallParts = parts.filter((p) => p.functionCall);\n const textParts = parts.filter((p) => typeof p.text === \"string\" && !p.thought);\n const thoughtParts = parts.filter((p) => p.thought === true && typeof p.text === \"string\");\n const hasToolCalls = fnCallParts.length > 0;\n const joinedText = textParts.map((p) => String(p.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const geminiReasoning =\n thoughtParts.length > 0\n ? thoughtParts.map((p) => String(p.text ?? \"\")).join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = fnCallParts.map((p) => {\n const fc = p.functionCall as Record<string, unknown>;\n return {\n name: String(fc.name),\n arguments: typeof fc.args === \"string\" ? fc.args : JSON.stringify(fc.args),\n };\n });\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(geminiReasoning ? { reasoning: geminiReasoning } : {}),\n };\n }\n return { toolCalls, ...(geminiReasoning ? { reasoning: geminiReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(geminiReasoning ? { reasoning: geminiReasoning } : {}),\n };\n }\n // Recognized Gemini shape but empty content\n return { content: \"\", ...(geminiReasoning ? { reasoning: geminiReasoning } : {}) };\n }\n }\n\n // Bedrock Converse: { output: { message: { role, content: [{ text }, { toolUse }] } } }\n if (obj.output && typeof obj.output === \"object\") {\n const output = obj.output as Record<string, unknown>;\n const msg = output.message as Record<string, unknown> | undefined;\n if (msg && Array.isArray(msg.content)) {\n const blocks = msg.content as Array<Record<string, unknown>>;\n const toolUseBlocks = blocks.filter((b) => b.toolUse);\n const textBlocks = blocks.filter((b) => typeof b.text === \"string\");\n const reasoningBlocks = blocks.filter((b) => b.reasoningContent);\n const hasToolCalls = toolUseBlocks.length > 0;\n const joinedText = textBlocks.map((b) => String(b.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const bedrockReasoning =\n reasoningBlocks.length > 0\n ? reasoningBlocks\n .map((b) => {\n const rc = b.reasoningContent as Record<string, unknown>;\n const rt = rc?.reasoningText as Record<string, unknown> | undefined;\n return String(rt?.text ?? \"\");\n })\n .join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = toolUseBlocks.map((b) => {\n const tu = b.toolUse as Record<string, unknown>;\n return {\n name: String(tu.name ?? \"\"),\n arguments: typeof tu.input === \"string\" ? tu.input : JSON.stringify(tu.input),\n ...(tu.toolUseId ? { id: String(tu.toolUseId) } : {}),\n };\n });\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}),\n };\n }\n return { toolCalls, ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}),\n };\n }\n // Recognized Bedrock Converse shape but empty content\n return { content: \"\", ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}) };\n }\n }\n\n // Cohere v2 chat: { finish_reason: \"...\", message: { content: [{ type: \"text\", text: \"...\" }] } }\n // Must come before Ollama since both have `message`, but Cohere has `finish_reason` at top level\n // (not nested in `choices`) and `message.content` as an array of typed objects.\n if (\n typeof obj.finish_reason === \"string\" &&\n obj.message &&\n typeof obj.message === \"object\" &&\n Array.isArray((obj.message as Record<string, unknown>).content)\n ) {\n const msg = obj.message as Record<string, unknown>;\n const contentBlocks = msg.content as Array<Record<string, unknown>>;\n const textBlock = contentBlocks.find((b) => b.type === \"text\" && typeof b.text === \"string\");\n const hasContent = textBlock && typeof textBlock.text === \"string\" && textBlock.text.length > 0;\n const toolCallBlocks = contentBlocks.filter((b) => b.type === \"tool_call\");\n\n // Also check message-level tool_calls (Cohere v2 puts tool calls here, not in content blocks)\n const msgToolCalls = Array.isArray(msg.tool_calls)\n ? (msg.tool_calls as Array<Record<string, unknown>>)\n : [];\n\n if (toolCallBlocks.length > 0) {\n const toolCalls: ToolCall[] = toolCallBlocks.map((b) => ({\n name: String(b.name ?? (b.function as Record<string, unknown>)?.name ?? \"\"),\n arguments:\n typeof b.parameters === \"string\"\n ? b.parameters\n : typeof b.parameters === \"object\"\n ? JSON.stringify(b.parameters)\n : typeof (b.function as Record<string, unknown>)?.arguments === \"string\"\n ? String((b.function as Record<string, unknown>).arguments)\n : JSON.stringify((b.function as Record<string, unknown>)?.arguments),\n ...(b.id ? { id: String(b.id) } : {}),\n }));\n if (hasContent) {\n return { content: textBlock.text as string, toolCalls };\n }\n return { toolCalls };\n }\n if (msgToolCalls.length > 0) {\n const toolCalls: ToolCall[] = msgToolCalls.map((tc) => {\n const fn = tc.function as Record<string, unknown> | undefined;\n return {\n name: String(tc.name ?? fn?.name ?? \"\"),\n arguments:\n typeof tc.parameters === \"string\"\n ? tc.parameters\n : typeof tc.parameters === \"object\"\n ? JSON.stringify(tc.parameters)\n : typeof fn?.arguments === \"string\"\n ? String(fn.arguments)\n : JSON.stringify(fn?.arguments),\n ...(tc.id ? { id: String(tc.id) } : {}),\n };\n });\n if (hasContent) {\n return { content: textBlock.text as string, toolCalls };\n }\n return { toolCalls };\n }\n if (hasContent) {\n return { content: textBlock.text as string };\n }\n }\n\n // Ollama: { message: { content: \"...\", tool_calls: [...] } }\n if (obj.message && typeof obj.message === \"object\") {\n const msg = obj.message as Record<string, unknown>;\n const hasOllamaToolCalls = Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0;\n const hasOllamaContent = typeof msg.content === \"string\" && msg.content.length > 0;\n\n if (hasOllamaToolCalls) {\n const toolCalls: ToolCall[] = (msg.tool_calls as Array<Record<string, unknown>>)\n .filter((tc) => tc.function != null)\n .map((tc) => {\n const fn = tc.function as Record<string, unknown>;\n return {\n name: String(fn.name ?? \"\"),\n arguments:\n typeof fn.arguments === \"string\" ? fn.arguments : JSON.stringify(fn.arguments),\n };\n });\n if (hasOllamaContent) {\n return { content: msg.content as string, toolCalls };\n }\n return { toolCalls };\n }\n if (hasOllamaContent) {\n return { content: msg.content as string };\n }\n // Ollama message with content array (like Cohere)\n if (Array.isArray(msg.content) && msg.content.length > 0) {\n const first = msg.content[0] as Record<string, unknown>;\n if (typeof first.text === \"string\") {\n return { content: first.text };\n }\n }\n }\n\n // Ollama /api/generate: { response: \"...\", done: true/false }\n if (typeof obj.response === \"string\" && \"done\" in obj) {\n return { content: obj.response };\n }\n\n // Fallback: unknown format — save as error\n return {\n error: {\n message: \"Could not detect response format from upstream\",\n type: \"proxy_error\",\n },\n status,\n };\n}\n\n/**\n * Derive fixture match criteria from the original request.\n */\ntype EndpointType = NonNullable<FixtureMatch[\"endpoint\"]>;\n\nexport function buildFixtureMatch(\n request: ChatCompletionRequest,\n recordConfig?: RecordConfig,\n): {\n userMessage?: string;\n inputText?: string;\n model?: string;\n endpoint?: EndpointType;\n turnIndex?: number;\n hasToolResult?: boolean;\n context?: string;\n} {\n const match: {\n userMessage?: string;\n inputText?: string;\n model?: string;\n endpoint?: EndpointType;\n turnIndex?: number;\n hasToolResult?: boolean;\n context?: string;\n } = {};\n\n // Include endpoint type for multimedia fixtures\n if (request._endpointType && request._endpointType !== \"chat\") {\n match.endpoint = request._endpointType as EndpointType;\n }\n\n // Embedding request\n if (request.embeddingInput) {\n match.inputText = request.embeddingInput;\n return match;\n }\n\n // Chat/multimedia request — match on the last user message\n const lastUser = getLastMessageByRole(request.messages ?? [], \"user\");\n if (lastUser) {\n const text = getTextContent(lastUser.content);\n if (text) {\n match.userMessage = text;\n }\n }\n\n // Record normalized model for all requests so fixtures disambiguate\n // calls that share the same userMessage but target different models.\n if (request.model) {\n match.model =\n normalizeModelName(request.model, recordConfig?.recordFullModelVersion) ?? request.model;\n }\n\n // Multi-turn disambiguation: writing only `userMessage` lets the recorder's\n // in-memory cache shadow follow-up turns that share it (initial tool call\n // vs. text reply after the tool result). turnIndex + hasToolResult give\n // each call a distinct, matcher-aware key. Skip for non-chat (no messages).\n const messages = request.messages ?? [];\n if (\n messages.length > 0 &&\n (request._endpointType === \"chat\" || request._endpointType === undefined)\n ) {\n match.turnIndex = messages.filter((m) => m.role === \"assistant\").length;\n match.hasToolResult = messages.some((m) => m.role === \"tool\");\n }\n\n if (request._context) {\n match.context = request._context;\n }\n\n return match;\n}\n\n/**\n * Build optional metadata for drift detection. Contains 8-char SHA-256\n * hashes of the system prompt and tool definitions present in the request.\n * Returns undefined when neither is present.\n */\nfunction buildFixtureMetadata(\n request: ChatCompletionRequest,\n): { systemHash?: string; toolsHash?: string } | undefined {\n const meta: { systemHash?: string; toolsHash?: string } = {};\n\n const messages = request.messages ?? [];\n const systemTexts = messages\n .filter((m) => m.role === \"system\")\n .map((m) => (typeof m.content === \"string\" ? m.content : JSON.stringify(m.content)))\n .join(\"\\n\");\n if (systemTexts) {\n meta.systemHash = crypto.createHash(\"sha256\").update(systemTexts).digest(\"hex\").slice(0, 8);\n }\n\n if (request.tools && request.tools.length > 0) {\n meta.toolsHash = crypto\n .createHash(\"sha256\")\n .update(JSON.stringify(request.tools))\n .digest(\"hex\")\n .slice(0, 8);\n }\n\n return Object.keys(meta).length > 0 ? meta : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyBA,MAAM,gBAAgB,IAAI,IAAI;CAE5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CAEA;CACA;CACD,CAAC;;;;;;;;;AA4EF,SAAgB,eAAe,MAQN;CACvB,MAAM,EAAE,QAAQ,aAAa,QAAQ,SAAS,UAAU,WAAW,EAAE,EAAE,WAAW;CAMlF,MAAM,IAAI,QAAQ;CAClB,MAAM,eACJ,EAAE,gBAAgB,UAAa,EAAE,cAAc,UAAa,EAAE,aAAa;AAC7E,KAAI,aACF,QAAO,KAAK,8EAA8E;AAG5F,KAAI,OAAO,WAAW;AACpB,SAAO,KAAK,WAAW,YAAY,4BAA4B;AAC/D,SAAO,EAAE,MAAM,WAAW;;CAG5B,MAAM,cAAc,OAAO,eAAe;CAC1C,IAAI,iBAAiB,WAAW;CAChC,IAAI;CACJ,IAAI,gBAAgB;AAEpB,KAAI,gBAAgB;EAClB,MAAM,OAAO,cAAc,OAAO;AAClC,MAAI,CAAC,MAAM;AAGT,oBAAiB;GACjB,MAAM,6BAAY,IAAI,MAAM,EAAC,aAAa,CAAC,QAAQ,SAAS,IAAI;AAChE,cAAW,KAAK,KACd,aACA,GAAG,YAAY,GAAG,UAAU,GAAGA,SAAO,YAAY,CAAC,MAAM,GAAG,EAAE,CAAC,OAChE;SACI;AACL,cAAW,KAAK,KAAK,aAAa,MAAM,GAAG,YAAY,OAAO;AAC9D,mBAAgB;;QAEb;EAEL,MAAM,gBAAgB,GAAG,YAAY,oBADnB,IAAI,MAAM,EAAC,aAAa,CAAC,QAAQ,SAAS,IAAI,CACd,GAAGA,SAAO,YAAY,CAAC,MAAM,GAAG,EAAE,CAAC;AACrF,aAAW,QAAQ,MAAM,UACrB,KAAK,KAAK,aAAa,QAAQ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,kBAAkB,SAC/D;AACA,QAAM,YAAY,SAAS,QAAQ,MAAM,EAAE,SAAS,YAAY,CAAC;AACjE,QAAM,gBAAgB,SAAS,MAAM,MAAM,EAAE,SAAS,OAAO;;AAG/D,KAAI,QAAQ,SACV,OAAM,UAAU,QAAQ;AAG1B,QAAO;;;;;;;AAQT,SAAS,qBACP,SACyD;CACzD,MAAM,OAAoD,EAAE;CAG5D,MAAM,eADW,QAAQ,YAAY,EAAE,EAEpC,QAAQ,MAAM,EAAE,SAAS,SAAS,CAClC,KAAK,MAAO,OAAO,EAAE,YAAY,WAAW,EAAE,UAAU,KAAK,UAAU,EAAE,QAAQ,CAAE,CACnF,KAAK,KAAK;AACb,KAAI,YACF,MAAK,aAAaD,SAAO,WAAW,SAAS,CAAC,OAAO,YAAY,CAAC,OAAO,MAAM,CAAC,MAAM,GAAG,EAAE;AAG7F,KAAI,QAAQ,SAAS,QAAQ,MAAM,SAAS,EAC1C,MAAK,YAAYA,SACd,WAAW,SAAS,CACpB,OAAO,KAAK,UAAU,QAAQ,MAAM,CAAC,CACrC,OAAO,MAAM,CACb,MAAM,GAAG,EAAE;AAGhB,QAAO,OAAO,KAAK,KAAK,CAAC,SAAS,IAAI,OAAO"}
1
+ {"version":3,"file":"recorder.js","names":["crypto","http"],"sources":["../src/recorder.ts"],"sourcesContent":["import * as http from \"node:http\";\nimport * as https from \"node:https\";\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as crypto from \"node:crypto\";\nimport { StringDecoder } from \"node:string_decoder\";\nimport type {\n ChatCompletionRequest,\n Fixture,\n FixtureMatch,\n FixtureResponse,\n RecordConfig,\n RecordedTimings,\n RecordProviderKey,\n ToolCall,\n} from \"./types.js\";\nimport { getLastMessageByRole, getTextContent } from \"./router.js\";\nimport { normalizeModelName } from \"./model-utils.js\";\nimport type { Logger } from \"./logger.js\";\nimport { collapseStreamingResponse } from \"./stream-collapse.js\";\nimport { writeErrorResponse } from \"./sse-writer.js\";\nimport { resolveUpstreamUrl } from \"./url.js\";\nimport { getTestId, slugifyTestId } from \"./helpers.js\";\nimport { DEFAULT_TEST_ID } from \"./constants.js\";\n\n/** Headers to strip when proxying — hop-by-hop (RFC 2616 §13.5.1) + client-set. */\nconst STRIP_HEADERS = new Set([\n // Hop-by-hop (RFC 2616 §13.5.1)\n \"connection\",\n \"keep-alive\",\n \"transfer-encoding\",\n \"te\",\n \"trailer\",\n \"upgrade\",\n \"proxy-authorization\",\n \"proxy-authenticate\",\n // Set by HTTP client from the target URL / body\n \"host\",\n \"content-length\",\n // Not relevant for LLM APIs; avoid leaking or mismatched encoding\n \"cookie\",\n \"accept-encoding\",\n]);\n\n/**\n * Captured upstream response, exposed to the `beforeWriteResponse` hook so\n * callers can decide whether to relay it or mutate it (e.g. chaos injection).\n */\nexport interface ProxyCapturedResponse {\n status: number;\n contentType: string;\n body: Buffer;\n}\n\nexport interface ProxyOptions {\n /**\n * Called after the upstream response has been captured and recorded, but\n * before the relay to the client. Contract when the hook returns `true`:\n * 1. It wrote its own response body on `res`.\n * 2. It journaled the outcome (proxyAndRecord will NOT journal it).\n * 3. proxyAndRecord skips its default relay and returns `\"handled_by_hook\"`.\n *\n * Returning `false` (or omitting the hook) lets proxyAndRecord relay the\n * upstream response normally and leaves journaling to the caller via the\n * `\"relayed\"` outcome. Rejected promises propagate and leave the response\n * unwritten.\n *\n * NOT invoked when the upstream response was streamed progressively to the\n * client (SSE, NDJSON, or binary event streams) — the bytes are already on\n * the wire and can't be mutated.\n * Callers that need to observe the bypass should pass `onHookBypassed`.\n */\n beforeWriteResponse?: (response: ProxyCapturedResponse) => boolean | Promise<boolean>;\n /**\n * Called when `beforeWriteResponse` was provided but could not be invoked\n * because the upstream response was streamed to the client progressively.\n * The hook was rolled + wired but the bytes left before it could fire.\n * Intended for observability (log/metric/journal annotation) — proxyAndRecord\n * still returns `\"relayed\"`.\n */\n onHookBypassed?: (reason: \"sse_streamed\" | \"ndjson_streamed\" | \"binary_streamed\") => void;\n}\n\n/**\n * Outcome of a proxyAndRecord call, returned so the caller can decide whether\n * to journal, fall through, or stop — without sharing a mutable flag with the\n * `beforeWriteResponse` hook.\n *\n * - `\"not_configured\"` — no upstream URL for this provider; caller should fall\n * through to its next branch (typically strict/404).\n * - `\"relayed\"` — the default code path wrote a response (upstream success or\n * synthesized 502 error). Caller should journal the outcome.\n * - `\"handled_by_hook\"` — the hook wrote + journaled its own response. Caller\n * should not double-journal.\n */\nexport type ProxyOutcome = \"not_configured\" | \"relayed\" | \"handled_by_hook\";\n\n/**\n * Result of `persistFixture`:\n * - `\"skipped\"` — proxy-only mode; the caller has nothing else to do.\n * - `\"written\"` — fixture saved to `filepath` and (unless the match was empty)\n * registered into the in-memory cache so the next identical request matches.\n * - `\"failed\"` — filesystem write failed. Caller decides how to surface it\n * (e.g. setting `X-AIMock-Record-Error` on a relay response).\n */\nexport type PersistFixtureResult =\n | { kind: \"skipped\" }\n | { kind: \"written\"; filepath: string }\n | { kind: \"failed\"; error: string };\n\n/**\n * Write a built fixture to disk (snapshot vs. timestamp file layout) and, when\n * the match is non-empty, register it in the in-memory cache so subsequent\n * identical requests match. Extracted from `proxyAndRecord` so the fal\n * queue-walk recorder (which makes multiple upstream calls before knowing the\n * final body) can share the same persistence behavior without re-implementing\n * snapshot-mode merging and warnings.\n */\nexport function persistFixture(opts: {\n record: RecordConfig;\n providerKey: RecordProviderKey;\n testId: string;\n fixture: Fixture;\n fixtures: Fixture[];\n warnings?: string[];\n logger: Logger;\n}): PersistFixtureResult {\n const { record, providerKey, testId, fixture, fixtures, warnings = [], logger } = opts;\n\n // Match criteria with no userMessage / inputText / endpoint will not match\n // any future request — warn, then save to disk for inspection but skip the\n // in-memory registration so a defective fixture doesn't shadow real ones.\n // turnIndex/hasToolResult are pure multi-turn disambiguators on their own.\n const m = fixture.match;\n const isEmptyMatch =\n m.userMessage === undefined && m.inputText === undefined && m.endpoint === undefined;\n if (isEmptyMatch) {\n logger.warn(\"Recorded fixture has empty match criteria — skipping in-memory registration\");\n }\n\n if (record.proxyOnly) {\n logger.info(`Proxied ${providerKey} request (proxy-only mode)`);\n return { kind: \"skipped\" };\n }\n\n const fixturePath = record.fixturePath ?? \"./fixtures/recorded\";\n let isSnapshotMode = testId !== DEFAULT_TEST_ID;\n let filepath: string;\n let mergeExisting = false;\n\n if (isSnapshotMode) {\n const slug = slugifyTestId(testId);\n if (!slug) {\n // Slug resolved to empty (e.g. testId was all punctuation) — fall back\n // to timestamp-based recording so we still capture the fixture.\n isSnapshotMode = false;\n const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n filepath = path.join(\n fixturePath,\n `${providerKey}-${timestamp}-${crypto.randomUUID().slice(0, 8)}.json`,\n );\n } else {\n filepath = path.join(fixturePath, slug, `${providerKey}.json`);\n mergeExisting = true;\n }\n } else {\n const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n const timestampFile = `${providerKey}-${timestamp}-${crypto.randomUUID().slice(0, 8)}.json`;\n filepath = fixture.match.context\n ? path.join(fixturePath, fixture.match.context, timestampFile)\n : path.join(fixturePath, timestampFile);\n }\n\n const fileWarnings = [\n ...(isEmptyMatch ? [\"Empty match criteria — this fixture will not match any request\"] : []),\n ...warnings,\n ];\n\n try {\n fs.mkdirSync(path.dirname(filepath), { recursive: true });\n\n // Auth headers are forwarded to upstream but excluded from saved fixtures.\n // The persisted fixture is always the real upstream response, even when\n // chaos later mutates the relay; replay must see what upstream said.\n let fileContent: { fixtures: unknown[]; _warning?: string };\n if (mergeExisting && fs.existsSync(filepath)) {\n try {\n const existing = JSON.parse(fs.readFileSync(filepath, \"utf-8\"));\n fileContent = { fixtures: [...(existing.fixtures ?? []), fixture] };\n } catch (mergeErr) {\n const msg = mergeErr instanceof Error ? mergeErr.message : \"unknown\";\n logger.warn(`Could not read existing fixture file ${filepath} (${msg}) — overwriting`);\n fileContent = { fixtures: [fixture] };\n }\n } else {\n fileContent = { fixtures: [fixture] };\n }\n if (fileWarnings.length > 0) {\n fileContent._warning = fileWarnings.join(\"; \");\n }\n // Atomic write: write to temp file then rename to avoid read-modify-write\n // races. Keep synchronous — for streamed responses the HTTP reply is\n // already on the wire, so async writes would race with callers checking\n // the filesystem before the fixture has landed.\n const tmpPath = filepath + \".tmp.\" + process.pid;\n fs.writeFileSync(tmpPath, JSON.stringify(fileContent, null, 2), \"utf-8\");\n fs.renameSync(tmpPath, filepath);\n\n if (!isEmptyMatch) {\n fixtures.push(fixture);\n }\n logger.warn(`Response recorded → ${filepath}`);\n return { kind: \"written\", filepath };\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown filesystem error\";\n logger.error(`Failed to save fixture to disk: ${msg}`);\n return { kind: \"failed\", error: msg };\n }\n}\n\n/**\n * Proxy an unmatched request to the real upstream provider, record the\n * response as a fixture on disk and in memory, then relay the response\n * back to the original client.\n */\nexport async function proxyAndRecord(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n request: ChatCompletionRequest,\n providerKey: RecordProviderKey,\n pathname: string,\n fixtures: Fixture[],\n defaults: {\n record?: RecordConfig;\n logger: Logger;\n requestTransform?: (req: ChatCompletionRequest) => ChatCompletionRequest;\n },\n rawBody?: string,\n options?: ProxyOptions,\n): Promise<ProxyOutcome> {\n const record = defaults.record;\n if (!record) return \"not_configured\";\n\n const providers = record.providers;\n // Gemini Interactions uses the same upstream API as Gemini (identical base URL\n // and auth), so we remap the provider key to reuse the configured Gemini URL.\n const lookupKey = providerKey === \"gemini-interactions\" ? \"gemini\" : providerKey;\n const upstreamUrl = providers[lookupKey];\n\n if (!upstreamUrl) {\n defaults.logger.warn(`No upstream URL configured for provider \"${providerKey}\" — cannot proxy`);\n return \"not_configured\";\n }\n\n let target: URL;\n try {\n target = resolveUpstreamUrl(upstreamUrl, pathname);\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n defaults.logger.error(\n `Invalid upstream URL for provider \"${providerKey}\": ${upstreamUrl} (${msg})`,\n );\n writeErrorResponse(\n res,\n 502,\n JSON.stringify({\n error: { message: `Invalid upstream URL: ${upstreamUrl}`, type: \"proxy_error\" },\n }),\n );\n return \"relayed\";\n }\n\n defaults.logger.warn(`NO FIXTURE MATCH — proxying to ${upstreamUrl}${pathname}`);\n\n // Forward all request headers except hop-by-hop and client-set ones.\n const forwardHeaders: Record<string, string> = {};\n for (const [name, val] of Object.entries(req.headers)) {\n if (val !== undefined && !STRIP_HEADERS.has(name)) {\n forwardHeaders[name] = Array.isArray(val) ? val.join(\", \") : val;\n }\n }\n\n const requestBody = rawBody ?? JSON.stringify(request);\n\n // Make upstream request\n let upstreamStatus: number;\n let upstreamHeaders: http.IncomingHttpHeaders;\n let upstreamBody: string;\n let rawBuffer: Buffer;\n\n // Track whether we streamed SSE progressively to the client; if so,\n // skip the final res.writeHead/res.end relay at the bottom of this fn.\n let streamedToClient = false;\n let clientDisconnected = false;\n let frameTimestamps: number[] = [];\n let streamStartTime = 0;\n try {\n const result = await makeUpstreamRequest(\n target,\n forwardHeaders,\n requestBody,\n res,\n req.method,\n defaults.logger,\n { upstreamTimeoutMs: record.upstreamTimeoutMs, bodyTimeoutMs: record.bodyTimeoutMs },\n );\n upstreamStatus = result.status;\n upstreamHeaders = result.headers;\n upstreamBody = result.body;\n rawBuffer = result.rawBuffer;\n streamedToClient = result.streamedToClient;\n clientDisconnected = result.clientDisconnected;\n frameTimestamps = result.frameTimestamps;\n streamStartTime = result.streamStartTime;\n } catch (err) {\n const msg = err instanceof Error ? err.message : \"Unknown proxy error\";\n defaults.logger.error(`Proxy request failed: ${msg}`);\n if (!res.headersSent) {\n res.writeHead(502, { \"Content-Type\": \"application/json\" });\n res.end(\n JSON.stringify({\n error: { message: `Proxy to upstream failed: ${msg}`, type: \"proxy_error\" },\n }),\n );\n } else {\n // SSE headers already sent — gracefully close the connection\n res.end();\n }\n return \"relayed\";\n }\n\n // Detect streaming response and collapse if necessary.\n // NOTE: collapse buffers the entire upstream body in memory. Fine for\n // current chat-completions traffic (responses are small), but revisit if\n // this path ever proxies long-lived or large streams — both the buffer\n // here and the hook below receive the full payload.\n const contentType = upstreamHeaders[\"content-type\"];\n const ctString = Array.isArray(contentType) ? contentType.join(\", \") : (contentType ?? \"\");\n const isBinaryStream = ctString.toLowerCase().includes(\"application/vnd.amazon.eventstream\");\n const collapsed = collapseStreamingResponse(\n ctString,\n providerKey,\n isBinaryStream ? rawBuffer : upstreamBody,\n defaults.logger,\n );\n\n let fixtureResponse: FixtureResponse;\n\n // TTS response — binary audio, not JSON\n const isAudioResponse = ctString.toLowerCase().startsWith(\"audio/\");\n if (isAudioResponse && rawBuffer.length > 0) {\n // Derive format from Content-Type (audio/mpeg→mp3, audio/opus→opus, etc.)\n const audioFormat = ctString\n .toLowerCase()\n .replace(\"audio/\", \"\")\n .replace(\"mpeg\", \"mp3\")\n .split(\";\")[0]\n .trim();\n fixtureResponse = {\n audio: rawBuffer.toString(\"base64\"),\n ...(audioFormat && audioFormat !== \"mp3\" ? { format: audioFormat } : {}),\n };\n } else if (collapsed) {\n // Streaming response — use collapsed result\n defaults.logger.warn(`Streaming response detected (${ctString}) — collapsing to fixture`);\n if (collapsed.truncated) {\n defaults.logger.warn(\"Bedrock EventStream: CRC mismatch — response may be truncated\");\n }\n if (collapsed.droppedChunks && collapsed.droppedChunks > 0) {\n defaults.logger.warn(\n `${collapsed.droppedChunks} chunk(s) dropped during stream collapse${collapsed.firstDroppedSample ? ` — first: ${collapsed.firstDroppedSample}` : \"\"}`,\n );\n }\n if (collapsed.harmonyUnparsed) {\n defaults.logger.warn(\n `Harmony tokens present but unparseable — content preserved verbatim${collapsed.harmonyNote ? ` (${collapsed.harmonyNote})` : \"\"}`,\n );\n }\n // Audio from streamed inlineData (e.g. Gemini SSE with audio parts).\n // A single Gemini turn can interleave audio with a functionCall and/or\n // text/thought parts; preserve those companion modalities so the tool call\n // / content / reasoning are not silently dropped when audio is present.\n if (collapsed.audioB64) {\n const audioToolCallsSpread =\n collapsed.toolCalls && collapsed.toolCalls.length > 0\n ? {\n toolCalls: collapsed.toolCalls.map((tc) => ({\n ...tc,\n name: tc.name ?? \"\",\n arguments: tc.arguments ?? \"{}\",\n })),\n }\n : {};\n const audioContentSpread = collapsed.content ? { content: collapsed.content } : {};\n const audioReasoningSpread = collapsed.reasoning ? { reasoning: collapsed.reasoning } : {};\n fixtureResponse = {\n audio: {\n b64Json: collapsed.audioB64,\n contentType: collapsed.audioMimeType ?? \"audio/mpeg\",\n },\n ...audioToolCallsSpread,\n ...audioContentSpread,\n ...audioReasoningSpread,\n };\n } else if (\n collapsed.content === \"\" &&\n (!collapsed.toolCalls || collapsed.toolCalls.length === 0)\n ) {\n defaults.logger.warn(\"Stream collapse produced empty content — fixture may be incomplete\");\n const reasoningSpread = collapsed.reasoning ? { reasoning: collapsed.reasoning } : {};\n const webSearchesSpread = collapsed.webSearches?.length\n ? { webSearches: collapsed.webSearches }\n : {};\n fixtureResponse = {\n content: collapsed.content ?? \"\",\n ...reasoningSpread,\n ...webSearchesSpread,\n };\n } else {\n const reasoningSpread = collapsed.reasoning ? { reasoning: collapsed.reasoning } : {};\n const webSearchesSpread = collapsed.webSearches?.length\n ? { webSearches: collapsed.webSearches }\n : {};\n if (collapsed.toolCalls && collapsed.toolCalls.length > 0) {\n const sanitizedToolCalls = collapsed.toolCalls.map((tc) => ({\n ...tc,\n name: tc.name ?? \"\",\n arguments: tc.arguments ?? \"{}\",\n }));\n if (collapsed.content) {\n // Both content and toolCalls present — save as ContentWithToolCallsResponse\n fixtureResponse = {\n content: collapsed.content,\n toolCalls: sanitizedToolCalls,\n ...reasoningSpread,\n ...webSearchesSpread,\n };\n } else {\n fixtureResponse = {\n toolCalls: sanitizedToolCalls,\n ...reasoningSpread,\n ...webSearchesSpread,\n };\n }\n } else {\n fixtureResponse = {\n content: collapsed.content ?? \"\",\n ...reasoningSpread,\n ...webSearchesSpread,\n };\n }\n }\n } else {\n // Non-streaming — try to parse as JSON\n let parsedResponse: unknown = null;\n try {\n parsedResponse = JSON.parse(upstreamBody);\n } catch (parseErr) {\n const msg = parseErr instanceof Error ? parseErr.message : \"unknown\";\n defaults.logger.warn(\n `Upstream response is not valid JSON (${msg}) — saving as error fixture`,\n );\n }\n // fal.ai returns arbitrary, model-specific JSON shapes (images, video URLs,\n // audio file objects, etc.). Round-trip the payload verbatim instead of\n // letting buildFixtureResponse mis-classify it as ImageResponse / VideoResponse.\n if (request._endpointType === \"fal\" && parsedResponse !== null) {\n const obj = parsedResponse as Record<string, unknown>;\n const isErrorShape =\n typeof obj.error === \"object\" &&\n obj.error !== null &&\n typeof (obj.error as Record<string, unknown>).message === \"string\";\n if (isErrorShape) {\n const err = obj.error as Record<string, unknown>;\n fixtureResponse = {\n error: {\n message: String(err.message ?? \"Unknown error\"),\n type: String(err.type ?? \"api_error\"),\n code: err.code ? String(err.code) : undefined,\n },\n status: upstreamStatus,\n };\n } else {\n fixtureResponse = { json: parsedResponse, status: upstreamStatus };\n }\n } else {\n let encodingFormat: string | undefined;\n try {\n encodingFormat = rawBody ? JSON.parse(rawBody).encoding_format : undefined;\n } catch (err) {\n defaults.logger.debug(\n `Could not parse encoding_format from raw body: ${err instanceof Error ? err.message : \"unknown error\"}`,\n );\n }\n fixtureResponse = buildFixtureResponse(parsedResponse, upstreamStatus, encodingFormat);\n }\n }\n\n // If the client disconnected mid-stream, the collected data is likely\n // truncated. Saving a partial fixture is worse than saving none — skip\n // fixture persistence entirely.\n if (clientDisconnected) {\n defaults.logger.warn(\n \"Client disconnected mid-stream — skipping fixture save to avoid truncated data\",\n );\n return \"relayed\";\n }\n\n // Build RecordedTimings from frame timestamps captured during streaming.\n // Requires at least 2 timestamps (first frame + at least one more) to\n // produce meaningful timing data.\n let recordedTimings: RecordedTimings | undefined;\n if (frameTimestamps.length > 1) {\n const ts = frameTimestamps;\n recordedTimings = {\n ttftMs: ts[0] - streamStartTime,\n interChunkDelaysMs: ts.slice(1).map((t, i) => t - ts[i]),\n totalDurationMs: ts[ts.length - 1] - streamStartTime,\n };\n }\n\n const matchRequest = defaults.requestTransform ? defaults.requestTransform(request) : request;\n const metadata = buildFixtureMetadata(request);\n const fixture: Fixture = {\n match: buildFixtureMatch(matchRequest, defaults.record),\n response: fixtureResponse,\n ...(recordedTimings && { recordedTimings }),\n ...(metadata && { metadata }),\n };\n\n const persistWarnings: string[] = [];\n if (collapsed?.truncated) {\n persistWarnings.push(\"Stream response was truncated — fixture may be incomplete\");\n }\n const persistResult = persistFixture({\n record,\n providerKey,\n testId: getTestId(req),\n fixture,\n fixtures,\n warnings: persistWarnings,\n logger: defaults.logger,\n });\n if (persistResult.kind === \"failed\") {\n if (!res.headersSent) {\n res.setHeader(\"X-AIMock-Record-Error\", persistResult.error);\n } else {\n defaults.logger.warn(`Cannot set X-AIMock-Record-Error header — headers already sent`);\n }\n defaults.logger.warn(`Response relayed but NOT saved to disk — see error above`);\n }\n\n // Relay upstream response to client (skip when the response was already\n // streamed progressively by makeUpstreamRequest — headers and body are\n // already on the wire).\n if (streamedToClient) {\n // The hook can't run because the body is already on the wire. Surface\n // the bypass so the caller (typically the chaos layer) can record it —\n // otherwise a configured chaos action silently no-ops on streamed traffic.\n if (options?.beforeWriteResponse && options.onHookBypassed) {\n const bypassReason: \"sse_streamed\" | \"ndjson_streamed\" | \"binary_streamed\" = isBinaryStream\n ? \"binary_streamed\"\n : ctString.toLowerCase().includes(\"application/x-ndjson\")\n ? \"ndjson_streamed\"\n : \"sse_streamed\";\n try {\n options.onHookBypassed(bypassReason);\n } catch (err) {\n defaults.logger.warn(\n `onHookBypassed callback threw: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n }\n } else {\n // Give the caller a chance to mutate or replace the response before relay.\n // Used by the chaos layer to turn a successful proxy into a malformed body.\n // `body` is the raw upstream bytes so binary payloads survive round-tripping.\n if (options?.beforeWriteResponse) {\n let handled: boolean | undefined;\n try {\n handled = await options.beforeWriteResponse({\n status: upstreamStatus,\n contentType: ctString,\n body: rawBuffer,\n });\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n throw new Error(`beforeWriteResponse hook failed for ${providerKey}: ${msg}`);\n }\n if (handled) return \"handled_by_hook\";\n }\n\n // Normalize status codes for the client: aimock acts as a gateway, so\n // upstream provider details (429 rate-limits, 503 outages, etc.) should\n // not leak. Successes → 200, errors → 502 (Bad Gateway).\n const clientStatus = upstreamStatus >= 200 && upstreamStatus < 300 ? 200 : 502;\n const isAudioRelay = ctString.toLowerCase().startsWith(\"audio/\");\n // When an upstream error (non-2xx) is relayed for an audio endpoint, the\n // body is typically a JSON error object — override the content-type so\n // clients don't try to decode JSON as audio.\n const relayHeaders: Record<string, string> = {};\n const clientCt =\n (clientStatus >= 200 && clientStatus < 300) || !isAudioRelay\n ? ctString || \"application/json\"\n : \"application/json\";\n if (clientCt) {\n relayHeaders[\"Content-Type\"] = clientCt;\n }\n res.writeHead(clientStatus, relayHeaders);\n res.end(isBinaryStream || isAudioRelay ? rawBuffer : upstreamBody);\n }\n\n return \"relayed\";\n}\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Decodes a sequence of byte chunks to UTF-8 text for SSE/NDJSON frame\n * splitting on the streamed-capture path. Wraps Node's StringDecoder so a\n * multibyte UTF-8 character (CJK, emoji, ...) whose bytes are split across a\n * TCP chunk boundary buffers across chunks instead of decoding to U+FFFD\n * replacement characters — decoding each chunk independently with\n * Buffer#toString() would corrupt the recorded frame text.\n */\nexport class StreamingFrameDecoder {\n private decoder = new StringDecoder(\"utf8\");\n /** Decode a chunk, holding back any trailing partial multibyte sequence. */\n write(chunk: Buffer): string {\n return this.decoder.write(chunk);\n }\n /** Flush any buffered bytes once the stream has ended. */\n end(): string {\n return this.decoder.end();\n }\n}\n\nfunction clampTimeout(value: number | undefined, fallback: number): number {\n if (value == null || !Number.isFinite(value) || value <= 0) return fallback;\n return value;\n}\n\nfunction makeUpstreamRequest(\n target: URL,\n headers: Record<string, string>,\n body: string,\n clientRes?: http.ServerResponse,\n method: string = \"POST\",\n logger?: Logger,\n timeouts?: Pick<RecordConfig, \"upstreamTimeoutMs\" | \"bodyTimeoutMs\">,\n): Promise<{\n status: number;\n headers: http.IncomingHttpHeaders;\n body: string;\n rawBuffer: Buffer;\n streamedToClient: boolean;\n clientDisconnected: boolean;\n frameTimestamps: number[];\n streamStartTime: number;\n}> {\n return new Promise((resolve, reject) => {\n const transport = target.protocol === \"https:\" ? https : http;\n const UPSTREAM_TIMEOUT_MS = clampTimeout(timeouts?.upstreamTimeoutMs, 30_000);\n const BODY_TIMEOUT_MS = clampTimeout(timeouts?.bodyTimeoutMs, 30_000);\n const req = transport.request(\n target,\n {\n method,\n timeout: UPSTREAM_TIMEOUT_MS,\n headers: {\n ...headers,\n \"Content-Length\": Buffer.byteLength(body).toString(),\n },\n },\n (res) => {\n res.setTimeout(BODY_TIMEOUT_MS, () => {\n req.destroy(new Error(`Upstream response timed out after ${BODY_TIMEOUT_MS / 1000}s`));\n });\n // Detect streaming content types so we can tee upstream chunks to the\n // client as they arrive rather than buffering the entire stream and\n // replaying it in a single res.end() at the bottom of proxyAndRecord.\n // Buffering collapses every frame into one client-visible write,\n // which defeats progressive rendering in downstream consumers and\n // can trip HTTP idle timeouts on slow calls.\n const ct = res.headers[\"content-type\"];\n const ctStr = Array.isArray(ct) ? ct.join(\", \") : (ct ?? \"\");\n const ctLower = ctStr.toLowerCase();\n const isSSE = ctLower.includes(\"text/event-stream\");\n const isNDJSON = ctLower.includes(\"application/x-ndjson\");\n const isBinaryEventStream = ctLower.includes(\"application/vnd.amazon.eventstream\");\n const isProgressiveStream = isSSE || isNDJSON || isBinaryEventStream;\n // SSE/NDJSON frame timing capture — timestamps each complete frame\n // so proxyAndRecord can build RecordedTimings for the fixture.\n const frameTimestamps: number[] = [];\n const streamStartTime = Date.now();\n let frameBuffer = \"\";\n // Decode chunks through a streaming-aware decoder so a multibyte UTF-8\n // character split across a TCP chunk boundary buffers across chunks\n // instead of decoding to U+FFFD replacement characters.\n const frameDecoder = new StreamingFrameDecoder();\n let binaryFrameBuffer = Buffer.alloc(0);\n\n let streamedToClient = false;\n let clientDisconnected = false;\n if (isProgressiveStream && clientRes && !clientRes.headersSent) {\n const relayHeaders: Record<string, string> = {\n \"Cache-Control\": \"no-cache, no-transform\",\n Connection: \"keep-alive\",\n \"X-Accel-Buffering\": \"no\",\n };\n if (ctStr) relayHeaders[\"Content-Type\"] = ctStr;\n // Normalize status codes for the client: aimock acts as a gateway,\n // so upstream provider details should not leak.\n // Successes → 200, errors → 502 (Bad Gateway).\n const rawStatus = res.statusCode ?? 200;\n const clientStatus = rawStatus >= 200 && rawStatus < 300 ? 200 : 502;\n clientRes.writeHead(clientStatus, relayHeaders);\n // Flush headers immediately so the client starts parsing frames\n // before the first data chunk arrives.\n if (typeof clientRes.flushHeaders === \"function\") clientRes.flushHeaders();\n streamedToClient = true;\n // Stop relaying if the client disconnects mid-stream.\n // Check writableFinished to distinguish normal completion (where\n // \"close\" also fires) from premature client disconnects.\n clientRes.on(\"close\", () => {\n if (!clientRes.writableFinished) {\n clientDisconnected = true;\n req.destroy();\n }\n });\n }\n const chunks: Buffer[] = [];\n res.on(\"data\", (chunk: Buffer) => {\n chunks.push(chunk);\n\n // Capture per-frame timestamps for SSE/NDJSON streams.\n // TCP data events don't align with SSE frames — buffer and\n // split on the protocol delimiter to timestamp each complete frame.\n if (isSSE || isNDJSON) {\n frameBuffer += frameDecoder.write(chunk);\n // Split on the protocol delimiter, tolerating CRLF line endings.\n // The SSE spec permits CRLF, and some upstreams/proxies emit\n // \\r\\n\\r\\n (SSE) or \\r\\n (NDJSON) frame boundaries. An LF-only\n // split would see the whole CRLF stream as a single frame and\n // lose per-frame timing. The last split element (a partial frame\n // tail) stays buffered, exactly as with a string delimiter.\n const delimiter = isNDJSON ? /\\r?\\n/ : /\\r?\\n\\r?\\n/;\n const parts = frameBuffer.split(delimiter);\n // All complete frames (everything except the last part which\n // may be incomplete).\n for (let fi = 0; fi < parts.length - 1; fi++) {\n if (parts[fi].trim().length > 0) {\n frameTimestamps.push(Date.now());\n }\n }\n // Last part stays in buffer (may be incomplete)\n frameBuffer = parts[parts.length - 1];\n }\n\n // Binary EventStream frame boundary detection — parse the 4-byte\n // total-length prefix to detect complete frames without decoding\n // frame contents (CRC validation happens in stream-collapse).\n if (isBinaryEventStream) {\n binaryFrameBuffer = Buffer.concat([binaryFrameBuffer, chunk]);\n while (binaryFrameBuffer.length >= 4) {\n const totalLen = binaryFrameBuffer.readUInt32BE(0);\n if (totalLen < 12 || binaryFrameBuffer.length < totalLen) break;\n frameTimestamps.push(Date.now());\n binaryFrameBuffer = binaryFrameBuffer.subarray(totalLen);\n }\n }\n\n if (\n streamedToClient &&\n clientRes &&\n !clientDisconnected &&\n !clientRes.destroyed &&\n !clientRes.writableEnded\n ) {\n try {\n clientRes.write(chunk);\n } catch (writeErr) {\n logger?.debug(\n `Failed to relay chunk to client: ${writeErr instanceof Error ? writeErr.message : \"unknown\"}`,\n );\n clientDisconnected = true;\n }\n }\n });\n res.on(\"error\", reject);\n res.on(\"end\", () => {\n if (res.socket) res.setTimeout(0);\n // Flush remaining text frame buffer — captures the last frame if\n // the stream ended without a trailing delimiter. Binary EventStream\n // frames are length-prefixed so partial frames at end-of-stream are\n // genuinely incomplete and should not be timestamped.\n if (isSSE || isNDJSON) {\n // Drain any bytes the decoder buffered for an incomplete multibyte\n // sequence so the final frame text is complete before we test it.\n frameBuffer += frameDecoder.end();\n if (frameBuffer.trim().length > 0) {\n frameTimestamps.push(Date.now());\n }\n }\n const rawBuffer = Buffer.concat(chunks);\n if (\n streamedToClient &&\n clientRes &&\n !clientDisconnected &&\n !clientRes.destroyed &&\n !clientRes.writableEnded\n ) {\n try {\n clientRes.end();\n } catch (endErr) {\n logger?.debug(\n `Failed to end client response: ${endErr instanceof Error ? endErr.message : \"unknown\"}`,\n );\n }\n }\n resolve({\n status: res.statusCode ?? 500,\n headers: res.headers,\n body: rawBuffer.toString(),\n rawBuffer,\n streamedToClient,\n clientDisconnected,\n frameTimestamps,\n streamStartTime,\n });\n });\n },\n );\n req.on(\"timeout\", () => {\n req.destroy(\n new Error(\n `Upstream request timed out after ${UPSTREAM_TIMEOUT_MS / 1000}s: ${target.href}`,\n ),\n );\n });\n req.on(\"error\", reject);\n req.write(body);\n req.end();\n });\n}\n\n/**\n * Detect the response format from the parsed upstream JSON and convert\n * it into an aimock FixtureResponse.\n */\nfunction buildFixtureResponse(\n parsed: unknown,\n status: number,\n encodingFormat?: string,\n): FixtureResponse {\n if (parsed === null || parsed === undefined) {\n // Raw / unparseable response — save as error\n return {\n error: { message: \"Upstream returned non-JSON response\", type: \"proxy_error\" },\n status,\n };\n }\n\n const obj = parsed as Record<string, unknown>;\n\n // Error response — only match the actual { error: { message: \"...\" } } shape\n // used by OpenAI/Anthropic/etc., not arbitrary truthy `.error` fields.\n if (\n typeof obj.error === \"object\" &&\n obj.error !== null &&\n typeof (obj.error as Record<string, unknown>).message === \"string\"\n ) {\n const err = obj.error as Record<string, unknown>;\n return {\n error: {\n message: String(err.message ?? \"Unknown error\"),\n type: String(err.type ?? \"api_error\"),\n code: err.code ? String(err.code) : undefined,\n },\n status,\n };\n }\n\n // OpenAI embeddings: { data: [{ embedding: [...] }] }\n if (Array.isArray(obj.data) && obj.data.length > 0) {\n const first = obj.data[0] as Record<string, unknown>;\n if (Array.isArray(first.embedding)) {\n return { embedding: first.embedding as number[] };\n }\n if (typeof first.embedding === \"string\" && encodingFormat === \"base64\") {\n const buf = Buffer.from(first.embedding, \"base64\");\n if (buf.byteLength % 4 !== 0) {\n // Malformed embedding — return a zero-dimension embedding fixture\n return { embedding: [] };\n }\n // Uint8Array constructor copies Buffer data to a fresh ArrayBuffer at offset 0,\n // guaranteeing the alignment Float32Array requires.\n const copied = new Uint8Array(buf);\n const floats = new Float32Array(copied.buffer, 0, buf.byteLength / 4);\n return { embedding: Array.from(floats) };\n }\n // OpenAI image generation: { created, data: [{ url, b64_json, revised_prompt }] }\n if (first.url || first.b64_json) {\n const images = (obj.data as Array<Record<string, unknown>>).map((item) => ({\n ...(item.url ? { url: String(item.url) } : {}),\n ...(item.b64_json ? { b64Json: String(item.b64_json) } : {}),\n ...(item.revised_prompt ? { revisedPrompt: String(item.revised_prompt) } : {}),\n }));\n if (images.length === 1) {\n return { image: images[0] };\n }\n return { images };\n }\n }\n\n // Gemini Imagen: { predictions: [...] }\n if (Array.isArray(obj.predictions)) {\n const images = (obj.predictions as Array<Record<string, unknown>>).map((p) => ({\n ...(p.bytesBase64Encoded ? { b64Json: String(p.bytesBase64Encoded) } : {}),\n ...(p.mimeType ? { mimeType: String(p.mimeType) } : {}),\n }));\n if (images.length === 1) {\n return { image: images[0] };\n }\n return { images };\n }\n\n // OpenAI transcription: { text: \"...\", ... }\n if (\n typeof obj.text === \"string\" &&\n (obj.task === \"transcribe\" || obj.language !== undefined || obj.duration !== undefined)\n ) {\n return {\n transcription: {\n text: obj.text as string,\n ...(obj.language ? { language: String(obj.language) } : {}),\n ...(obj.duration !== undefined ? { duration: Number(obj.duration) } : {}),\n ...(Array.isArray(obj.words) ? { words: obj.words } : {}),\n ...(Array.isArray(obj.segments) ? { segments: obj.segments } : {}),\n },\n };\n }\n\n // Gemini Interactions: { id, status, outputs: [{ type: \"text\", text }, { type: \"function_call\", name, arguments }] }\n if (\n Array.isArray(obj.outputs) &&\n obj.outputs.length > 0 &&\n !(\"choices\" in obj) &&\n !(\"content\" in obj) &&\n !(\"candidates\" in obj)\n ) {\n const outputs = obj.outputs as Array<Record<string, unknown>>;\n const fnCallOutputs = outputs.filter((o) => o.type === \"function_call\");\n const textOutputs = outputs.filter((o) => o.type === \"text\" && typeof o.text === \"string\");\n const hasToolCalls = fnCallOutputs.length > 0;\n const joinedText = textOutputs.map((o) => String(o.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = fnCallOutputs.map((o) => ({\n name: String(o.name),\n arguments: typeof o.arguments === \"string\" ? o.arguments : JSON.stringify(o.arguments),\n ...(o.id ? { id: String(o.id) } : {}),\n }));\n if (hasContent) {\n return { content: joinedText, toolCalls };\n }\n return { toolCalls };\n }\n if (hasContent) {\n return { content: joinedText };\n }\n // Recognized Gemini Interactions shape but empty content\n return { content: \"\" };\n }\n\n // OpenAI video generation: { id, status, ... }\n // Guard against false positives: many API responses have `id` + `status` fields\n // (e.g. chat completions, Anthropic messages). Reject if the response has fields\n // that indicate a known non-video format.\n if (\n typeof obj.id === \"string\" &&\n typeof obj.status === \"string\" &&\n (obj.status === \"completed\" || obj.status === \"in_progress\" || obj.status === \"failed\") &&\n !(\"choices\" in obj) &&\n !(\"content\" in obj) &&\n !(\"candidates\" in obj) &&\n !(\"message\" in obj) &&\n !(\"data\" in obj) &&\n !(\"object\" in obj) &&\n !(\"outputs\" in obj) &&\n !(\"model\" in obj) &&\n !(\"response\" in obj) &&\n !(\"done\" in obj) &&\n !(\"usage\" in obj) &&\n !(\"error\" in obj)\n ) {\n if (obj.status === \"completed\" && obj.url) {\n return {\n video: {\n id: String(obj.id),\n status: \"completed\" as const,\n url: String(obj.url),\n },\n };\n }\n return {\n video: {\n id: String(obj.id),\n status: obj.status === \"failed\" ? (\"failed\" as const) : (\"processing\" as const),\n },\n };\n }\n\n // Direct embedding: { embedding: [...] }\n if (Array.isArray(obj.embedding)) {\n return { embedding: obj.embedding as number[] };\n }\n\n // OpenAI chat completion: { choices: [{ message: { content, tool_calls } }] }\n if (Array.isArray(obj.choices) && obj.choices.length > 0) {\n const choice = obj.choices[0] as Record<string, unknown>;\n const message = choice.message as Record<string, unknown> | undefined;\n if (message) {\n const hasToolCalls = Array.isArray(message.tool_calls) && message.tool_calls.length > 0;\n const hasContent = typeof message.content === \"string\" && message.content.length > 0;\n\n const openaiReasoning =\n typeof message.reasoning_content === \"string\" && message.reasoning_content.length > 0\n ? message.reasoning_content\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = (message.tool_calls as Array<Record<string, unknown>>).map(\n (tc) => {\n const fn = tc.function as Record<string, unknown>;\n return {\n name: String(fn.name),\n arguments: String(fn.arguments),\n ...(tc.id ? { id: String(tc.id) } : {}),\n };\n },\n );\n if (hasContent) {\n return {\n content: message.content as string,\n toolCalls,\n ...(openaiReasoning ? { reasoning: openaiReasoning } : {}),\n };\n }\n return { toolCalls, ...(openaiReasoning ? { reasoning: openaiReasoning } : {}) };\n }\n // Text content only\n if (hasContent) {\n return {\n content: message.content as string,\n ...(openaiReasoning ? { reasoning: openaiReasoning } : {}),\n };\n }\n // Recognized OpenAI shape but empty content (e.g. content filtering, zero max_tokens)\n return { content: \"\", ...(openaiReasoning ? { reasoning: openaiReasoning } : {}) };\n }\n }\n\n // Anthropic: { content: [{ type: \"text\", text: \"...\" }] } or tool_use\n if (Array.isArray(obj.content) && obj.content.length > 0) {\n const blocks = obj.content as Array<Record<string, unknown>>;\n const toolUseBlocks = blocks.filter((b) => b.type === \"tool_use\");\n const textBlocks = blocks.filter((b) => b.type === \"text\" && typeof b.text === \"string\");\n const thinkingBlocks = blocks.filter((b) => b.type === \"thinking\");\n const hasToolCalls = toolUseBlocks.length > 0;\n const joinedText = textBlocks.map((b) => String(b.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const anthropicReasoning =\n thinkingBlocks.length > 0\n ? thinkingBlocks.map((b) => String(b.thinking ?? \"\")).join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = toolUseBlocks.map((b) => ({\n name: String(b.name),\n arguments: typeof b.input === \"string\" ? b.input : JSON.stringify(b.input),\n ...(b.id ? { id: String(b.id) } : {}),\n }));\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}),\n };\n }\n return { toolCalls, ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(anthropicReasoning ? { reasoning: anthropicReasoning } : {}),\n };\n }\n // Thinking-only response (no text, no tool calls)\n if (anthropicReasoning) {\n return { content: \"\", reasoning: anthropicReasoning };\n }\n }\n\n // Gemini: { candidates: [{ content: { parts: [{ text: \"...\" }] } }] }\n if (Array.isArray(obj.candidates) && obj.candidates.length > 0) {\n const candidate = obj.candidates[0] as Record<string, unknown>;\n const content = candidate.content as Record<string, unknown> | undefined;\n if (content && Array.isArray(content.parts)) {\n const parts = content.parts as Array<Record<string, unknown>>;\n\n // Audio inlineData parts take priority over text\n const audioParts = parts.filter(\n (p: Record<string, unknown>) =>\n p.inlineData &&\n typeof (p.inlineData as Record<string, unknown>).mimeType === \"string\" &&\n ((p.inlineData as Record<string, unknown>).mimeType as string).startsWith(\"audio/\"),\n );\n if (audioParts.length > 0) {\n const inlineData = audioParts[0].inlineData as Record<string, unknown>;\n return {\n audio: {\n b64Json: String(inlineData.data ?? \"\"),\n contentType: String(inlineData.mimeType),\n },\n };\n }\n\n const fnCallParts = parts.filter((p) => p.functionCall);\n const textParts = parts.filter((p) => typeof p.text === \"string\" && !p.thought);\n const thoughtParts = parts.filter((p) => p.thought === true && typeof p.text === \"string\");\n const hasToolCalls = fnCallParts.length > 0;\n const joinedText = textParts.map((p) => String(p.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const geminiReasoning =\n thoughtParts.length > 0\n ? thoughtParts.map((p) => String(p.text ?? \"\")).join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = fnCallParts.map((p) => {\n const fc = p.functionCall as Record<string, unknown>;\n return {\n name: String(fc.name),\n arguments: typeof fc.args === \"string\" ? fc.args : JSON.stringify(fc.args),\n };\n });\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(geminiReasoning ? { reasoning: geminiReasoning } : {}),\n };\n }\n return { toolCalls, ...(geminiReasoning ? { reasoning: geminiReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(geminiReasoning ? { reasoning: geminiReasoning } : {}),\n };\n }\n // Recognized Gemini shape but empty content\n return { content: \"\", ...(geminiReasoning ? { reasoning: geminiReasoning } : {}) };\n }\n }\n\n // Bedrock Converse: { output: { message: { role, content: [{ text }, { toolUse }] } } }\n if (obj.output && typeof obj.output === \"object\") {\n const output = obj.output as Record<string, unknown>;\n const msg = output.message as Record<string, unknown> | undefined;\n if (msg && Array.isArray(msg.content)) {\n const blocks = msg.content as Array<Record<string, unknown>>;\n const toolUseBlocks = blocks.filter((b) => b.toolUse);\n const textBlocks = blocks.filter((b) => typeof b.text === \"string\");\n const reasoningBlocks = blocks.filter((b) => b.reasoningContent);\n const hasToolCalls = toolUseBlocks.length > 0;\n const joinedText = textBlocks.map((b) => String(b.text ?? \"\")).join(\"\");\n const hasContent = joinedText.length > 0;\n const bedrockReasoning =\n reasoningBlocks.length > 0\n ? reasoningBlocks\n .map((b) => {\n const rc = b.reasoningContent as Record<string, unknown>;\n const rt = rc?.reasoningText as Record<string, unknown> | undefined;\n return String(rt?.text ?? \"\");\n })\n .join(\"\")\n : undefined;\n\n if (hasToolCalls) {\n const toolCalls: ToolCall[] = toolUseBlocks.map((b) => {\n const tu = b.toolUse as Record<string, unknown>;\n return {\n name: String(tu.name ?? \"\"),\n arguments: typeof tu.input === \"string\" ? tu.input : JSON.stringify(tu.input),\n ...(tu.toolUseId ? { id: String(tu.toolUseId) } : {}),\n };\n });\n if (hasContent) {\n return {\n content: joinedText,\n toolCalls,\n ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}),\n };\n }\n return { toolCalls, ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}) };\n }\n if (hasContent) {\n return {\n content: joinedText,\n ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}),\n };\n }\n // Recognized Bedrock Converse shape but empty content\n return { content: \"\", ...(bedrockReasoning ? { reasoning: bedrockReasoning } : {}) };\n }\n }\n\n // Cohere v2 chat: { finish_reason: \"...\", message: { content: [{ type: \"text\", text: \"...\" }] } }\n // Must come before Ollama since both have `message`, but Cohere has `finish_reason` at top level\n // (not nested in `choices`) and `message.content` as an array of typed objects.\n if (\n typeof obj.finish_reason === \"string\" &&\n obj.message &&\n typeof obj.message === \"object\" &&\n Array.isArray((obj.message as Record<string, unknown>).content)\n ) {\n const msg = obj.message as Record<string, unknown>;\n const contentBlocks = msg.content as Array<Record<string, unknown>>;\n const textBlock = contentBlocks.find((b) => b.type === \"text\" && typeof b.text === \"string\");\n const hasContent = textBlock && typeof textBlock.text === \"string\" && textBlock.text.length > 0;\n const toolCallBlocks = contentBlocks.filter((b) => b.type === \"tool_call\");\n\n // Also check message-level tool_calls (Cohere v2 puts tool calls here, not in content blocks)\n const msgToolCalls = Array.isArray(msg.tool_calls)\n ? (msg.tool_calls as Array<Record<string, unknown>>)\n : [];\n\n if (toolCallBlocks.length > 0) {\n const toolCalls: ToolCall[] = toolCallBlocks.map((b) => ({\n name: String(b.name ?? (b.function as Record<string, unknown>)?.name ?? \"\"),\n arguments:\n typeof b.parameters === \"string\"\n ? b.parameters\n : typeof b.parameters === \"object\"\n ? JSON.stringify(b.parameters)\n : typeof (b.function as Record<string, unknown>)?.arguments === \"string\"\n ? String((b.function as Record<string, unknown>).arguments)\n : JSON.stringify((b.function as Record<string, unknown>)?.arguments),\n ...(b.id ? { id: String(b.id) } : {}),\n }));\n if (hasContent) {\n return { content: textBlock.text as string, toolCalls };\n }\n return { toolCalls };\n }\n if (msgToolCalls.length > 0) {\n const toolCalls: ToolCall[] = msgToolCalls.map((tc) => {\n const fn = tc.function as Record<string, unknown> | undefined;\n return {\n name: String(tc.name ?? fn?.name ?? \"\"),\n arguments:\n typeof tc.parameters === \"string\"\n ? tc.parameters\n : typeof tc.parameters === \"object\"\n ? JSON.stringify(tc.parameters)\n : typeof fn?.arguments === \"string\"\n ? String(fn.arguments)\n : JSON.stringify(fn?.arguments),\n ...(tc.id ? { id: String(tc.id) } : {}),\n };\n });\n if (hasContent) {\n return { content: textBlock.text as string, toolCalls };\n }\n return { toolCalls };\n }\n if (hasContent) {\n return { content: textBlock.text as string };\n }\n }\n\n // Ollama: { message: { content: \"...\", tool_calls: [...] } }\n if (obj.message && typeof obj.message === \"object\") {\n const msg = obj.message as Record<string, unknown>;\n const hasOllamaToolCalls = Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0;\n const hasOllamaContent = typeof msg.content === \"string\" && msg.content.length > 0;\n\n if (hasOllamaToolCalls) {\n const toolCalls: ToolCall[] = (msg.tool_calls as Array<Record<string, unknown>>)\n .filter((tc) => tc.function != null)\n .map((tc) => {\n const fn = tc.function as Record<string, unknown>;\n return {\n name: String(fn.name ?? \"\"),\n arguments:\n typeof fn.arguments === \"string\" ? fn.arguments : JSON.stringify(fn.arguments),\n };\n });\n if (hasOllamaContent) {\n return { content: msg.content as string, toolCalls };\n }\n return { toolCalls };\n }\n if (hasOllamaContent) {\n return { content: msg.content as string };\n }\n // Ollama message with content array (like Cohere)\n if (Array.isArray(msg.content) && msg.content.length > 0) {\n const first = msg.content[0] as Record<string, unknown>;\n if (typeof first.text === \"string\") {\n return { content: first.text };\n }\n }\n }\n\n // Ollama /api/generate: { response: \"...\", done: true/false }\n if (typeof obj.response === \"string\" && \"done\" in obj) {\n return { content: obj.response };\n }\n\n // Fallback: unknown format — save as error\n return {\n error: {\n message: \"Could not detect response format from upstream\",\n type: \"proxy_error\",\n },\n status,\n };\n}\n\n/**\n * Derive fixture match criteria from the original request.\n */\ntype EndpointType = NonNullable<FixtureMatch[\"endpoint\"]>;\n\nexport function buildFixtureMatch(\n request: ChatCompletionRequest,\n recordConfig?: RecordConfig,\n): {\n userMessage?: string;\n inputText?: string;\n model?: string;\n endpoint?: EndpointType;\n turnIndex?: number;\n hasToolResult?: boolean;\n context?: string;\n} {\n const match: {\n userMessage?: string;\n inputText?: string;\n model?: string;\n endpoint?: EndpointType;\n turnIndex?: number;\n hasToolResult?: boolean;\n context?: string;\n } = {};\n\n // Include endpoint type for multimedia fixtures\n if (request._endpointType && request._endpointType !== \"chat\") {\n match.endpoint = request._endpointType as EndpointType;\n }\n\n // Embedding request\n if (request.embeddingInput) {\n match.inputText = request.embeddingInput;\n return match;\n }\n\n // Chat/multimedia request — match on the last user message\n const lastUser = getLastMessageByRole(request.messages ?? [], \"user\");\n if (lastUser) {\n const text = getTextContent(lastUser.content);\n if (text) {\n match.userMessage = text;\n }\n }\n\n // Record normalized model for all requests so fixtures disambiguate\n // calls that share the same userMessage but target different models.\n if (request.model) {\n match.model =\n normalizeModelName(request.model, recordConfig?.recordFullModelVersion) ?? request.model;\n }\n\n // Multi-turn disambiguation: writing only `userMessage` lets the recorder's\n // in-memory cache shadow follow-up turns that share it (initial tool call\n // vs. text reply after the tool result). turnIndex + hasToolResult give\n // each call a distinct, matcher-aware key. Skip for non-chat (no messages).\n const messages = request.messages ?? [];\n if (\n messages.length > 0 &&\n (request._endpointType === \"chat\" || request._endpointType === undefined)\n ) {\n match.turnIndex = messages.filter((m) => m.role === \"assistant\").length;\n match.hasToolResult = messages.some((m) => m.role === \"tool\");\n }\n\n if (request._context) {\n match.context = request._context;\n }\n\n return match;\n}\n\n/**\n * Build optional metadata for drift detection. Contains 8-char SHA-256\n * hashes of the system prompt and tool definitions present in the request.\n * Returns undefined when neither is present.\n */\nfunction buildFixtureMetadata(\n request: ChatCompletionRequest,\n): { systemHash?: string; toolsHash?: string } | undefined {\n const meta: { systemHash?: string; toolsHash?: string } = {};\n\n const messages = request.messages ?? [];\n const systemTexts = messages\n .filter((m) => m.role === \"system\")\n .map((m) => (typeof m.content === \"string\" ? m.content : JSON.stringify(m.content)))\n .join(\"\\n\");\n if (systemTexts) {\n meta.systemHash = crypto.createHash(\"sha256\").update(systemTexts).digest(\"hex\").slice(0, 8);\n }\n\n if (request.tools && request.tools.length > 0) {\n meta.toolsHash = crypto\n .createHash(\"sha256\")\n .update(JSON.stringify(request.tools))\n .digest(\"hex\")\n .slice(0, 8);\n }\n\n return Object.keys(meta).length > 0 ? meta : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA0BA,MAAM,gBAAgB,IAAI,IAAI;CAE5B;CACA;CACA;C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