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

2.6 KB
Raw Blame
import { getOpenAICompatibleType } from "../services/provider.ts";

/**
 * Force OpenAI-compatible upstreams onto the native `/responses` endpoint.
 *
 * A Responses-API-shaped request (`input` / `previous_response_id` /
 * `max_output_tokens` / `reasoning`) that carries MCP (`namespace`) or
 * `tool_search*` tools loses the Codex deferred tool-discovery mechanism when
 * OmniRoute downgrades it to `/chat/completions` — so the MCP namespaces never
 * surface to the model and `apply_patch` is mis-handled (#5483). Detecting that
 * shape lets the executor pass it through natively instead of downgrading.
 */

type CredentialsLike = { providerSpecificData?: Record<string, unknown> | null };

function isRecord(value: unknown): value is Record<string, unknown> {
  return !!value && typeof value === "object" && !Array.isArray(value);
}

export function shouldForceResponsesUpstream(
  provider: string,
  body: unknown,
  credentials: CredentialsLike | null
): boolean {
  if (!provider.startsWith("openai-compatible-")) return false;
  if (!isRecord(body)) return false;

  const providerSpecificData = credentials?.providerSpecificData ?? null;
  if (providerSpecificData?._omnirouteForceResponsesUpstream === true) return true;
  if (getOpenAICompatibleType(provider, providerSpecificData) === "responses") return false;
  // apiType="chat" means the operator explicitly chose the chat/completions
  // wire. Don't second-guess that choice by forcing /responses just because the
  // body carries namespace tools — the standard namespace→flatten path
  // (openai-responses.ts) handles those correctly for chat backends.
  if (
    providerSpecificData &&
    typeof providerSpecificData.apiType === "string" &&
    providerSpecificData.apiType === "chat"
  ) {
    return false;
  }

  const hasResponsesShape =
    body.input !== undefined ||
    body.previous_response_id !== undefined ||
    body.max_output_tokens !== undefined ||
    body.reasoning !== undefined;
  if (!hasResponsesShape) return false;

  const tools = Array.isArray(body.tools) ? body.tools : [];
  return tools.some((toolValue) => {
    if (!isRecord(toolValue)) return false;
    const toolType = typeof toolValue.type === "string" ? toolValue.type : "";
    return toolType === "namespace" || /^tool_search/.test(toolType);
  });
}

export function withForcedResponsesUpstream<T extends CredentialsLike>(
  provider: string,
  body: unknown,
  credentials: T
): T {
  if (!shouldForceResponsesUpstream(provider, body, credentials)) return credentials;
  return {
    ...credentials,
    providerSpecificData: {
      ...credentials.providerSpecificData,
      _omnirouteForceResponsesUpstream: true,
    },
  } as T;
}