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.1 KB
Raw Blame
type JsonRecord = Record<string, unknown>;

export function isQwenThinkingActive(body: JsonRecord): boolean {
  const thinking = body.thinking;

  if (thinking === true || body.enable_thinking === true) {
    return true;
  }

  return (
    typeof thinking === "object" &&
    thinking !== null &&
    !Array.isArray(thinking) &&
    (thinking as JsonRecord).type === "enabled"
  );
}

export function isQwenThinkingToolChoiceIncompatible(toolChoice: unknown): boolean {
  return toolChoice === "required" || (typeof toolChoice === "object" && toolChoice !== null);
}

export function sanitizeQwenThinkingToolChoice(
  body: JsonRecord,
  providerLabel = "Qwen"
): JsonRecord {
  if (!isQwenThinkingActive(body)) {
    return body;
  }

  const toolChoice = body.tool_choice;
  if (!isQwenThinkingToolChoiceIncompatible(toolChoice)) {
    return body;
  }

  const toolChoiceLabel = typeof toolChoice === "string" ? toolChoice : "object";
  console.warn(
    `[${providerLabel}] Neutralizing incompatible tool_choice ${toolChoiceLabel} to "auto" (thinking mode active)`
  );

  return {
    ...body,
    tool_choice: "auto",
  };
}