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

1.9 KB
Raw Blame
/**
 * chatCore executor client-header normalizer (Quality Gate v2 / Fase 9 — chatCore god-file
 * decomposition, #3501).
 *
 * Pure helper extracted from chatCore: normalizes a Headers instance or a plain header object into a
 * lowercased-tolerant Record<string,string>, and backfills the client User-Agent (both casings) when
 * one is supplied and not already present. Returns null when nothing was collected. Side-effect-free;
 * OpenCode additionally retains explicit conversation identity before body translation.
 */
import { preserveOpencodeSessionIdentity } from "../../utils/opencodeSessionIdentity.ts";

// Pre-compiled set for O(1) lease control header checks (hot path)
const LEASE_CONTROL_HEADERS = new Set([
  "x-omniroute-lease-owner",
  "x-omniroute-lease-generation",
  "x-deadline-token",
]);

function isLeaseControlHeader(key: string): boolean {
  return LEASE_CONTROL_HEADERS.has(key.toLowerCase());
}

export function buildExecutorClientHeaders(
  headers: Headers | Record<string, unknown> | null | undefined,
  userAgent?: string | null,
  request?: { provider?: string; body?: unknown }
) {
  const normalized: Record<string, string> = {};

  if (headers instanceof Headers) {
    headers.forEach((value, key) => {
      if (isLeaseControlHeader(key)) return;
      normalized[key] = value;
    });
  } else if (headers && typeof headers === "object") {
    for (const [key, value] of Object.entries(headers)) {
      if (isLeaseControlHeader(key)) continue;
      if (typeof value === "string") {
        normalized[key] = value;
      }
    }
  }

  const normalizedUserAgent = typeof userAgent === "string" ? userAgent.trim() : "";
  if (normalizedUserAgent && !normalized["user-agent"] && !normalized["User-Agent"]) {
    normalized["user-agent"] = normalizedUserAgent;
    normalized["User-Agent"] = normalizedUserAgent;
  }

  preserveOpencodeSessionIdentity(normalized, request);
  return Object.keys(normalized).length > 0 ? normalized : null;
}