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.4 KB
Raw Blame
// open-sse/handlers/chatCore/toolCallingRequiredCheck.ts
// Extracted from handleChatCore (chatCore god-file decomposition).
//
// Combo requests are protected from ever reaching a tool-incapable target in
// the first place by filterTargetsByRequestCompatibility (comboStructure.ts,
// backed by getResolvedModelCapabilities' toolCalling resolution). But a
// direct/pinned request (isCombo: false) has no other target to fail over
// to — silently stripping `tools` and flattening history there produces a
// 200 response that can never actually call anything (the model narrates a
// fake action instead, live incident: AI Horde/Behemoth-X-123B). A clear,
// explicit error is better than a response that looks successful but
// silently drops the client's intended action.

export interface ToolCallingRequiredCheckResult {
  blocked: boolean;
  message?: string;
}

export function checkToolCallingRequiredButUnsupported(
  body: Record<string, unknown>,
  unsupported: readonly string[],
  isCombo: boolean,
  model: string
): ToolCallingRequiredCheckResult {
  if (isCombo) return { blocked: false };
  if (!unsupported.includes("tools")) return { blocked: false };
  if (!Array.isArray(body.tools) || body.tools.length === 0) return { blocked: false };

  return {
    blocked: true,
    message: `Model "${model}" does not support tool calling. Remove "tools" from the request or choose a different model.`,
  };
}