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.3 KB
Raw Blame
// Pure, stateless helper: build a Map<toolName, parametersSchema> from a request body's
// `tools[]` (Chat Completions `{type:"function",function:{name,parameters}}` shape or
// Responses API `{type:"function",name,parameters}` shape). Used to thread each tool's
// JSON Schema into response-side normalization (#6951 — stripEmptyOptionalToolArgs) so
// it can be schema-aware instead of allowlist-only. No stream state, no host import.

type JsonRecord = Record<string, unknown>;

function asRecord(value: unknown): JsonRecord | null {
  return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null;
}

export function extractToolSchemaMap(body: unknown): Map<string, JsonRecord> | null {
  const record = asRecord(body);
  const tools = record?.tools;
  if (!Array.isArray(tools)) return null;

  const map = new Map<string, JsonRecord>();
  for (const tool of tools) {
    const item = asRecord(tool);
    if (!item) continue;
    const fn = asRecord(item.function);
    const name = (
      typeof fn?.name === "string" ? fn.name : typeof item.name === "string" ? item.name : ""
    ).trim();
    if (!name) continue;
    const schema = asRecord(fn?.parameters ?? item.parameters ?? item.input_schema);
    if (schema) map.set(name, schema);
  }
  return map.size > 0 ? map : null;
}