Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

986 B
Raw Blame
/**
 * Request-body resolution helpers for the chat handler.
 *
 * Kept dependency-free (no DB / executor imports) so it can be unit-tested in
 * isolation without pulling the full chat pipeline.
 */

/**
 * Resolve the chat request body, preferring a body already parsed upstream (#4380).
 *
 * The /v1/chat/completions route parses the body once for the prompt-injection guard,
 * then hands it here via `preParsedBody` so a large coding-agent payload (270-550 KB) is
 * not JSON-parsed a second time on the hot path — the double parse materialized the body
 * twice in heap and fed the OOM crash-loop under concurrent load. Callers that don't
 * pre-parse (most routes) pass `null`/`undefined` and the body is parsed from the request
 * as before.
 */
export async function resolveChatRequestBody(
  request: { json: () => Promise<unknown> },
  preParsedBody: unknown = null
): Promise<unknown> {
  if (preParsedBody != null) return preParsedBody;
  return await request.json();
}