UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

102 lines (101 loc) • 2.84 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { i as logWarn } from "./logger-CIwfuhOU.mjs"; import { t as mergeLiteralSchemas } from "./json-schema-literals-DBR4wfx8.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/gateway/mcp-http.schema.ts const MCP_LOOPBACK_LOG_PREFIX = "mcp-loopback"; function readMcpLoopbackToolName(e) { return normalizeOptionalString(e.name); } function readLoopbackToolParameters(e) { let t; try { t = e.parameters; } catch { return; } if (!isRecord(t)) return {}; try { return { ...t }; } catch { return; } } function flattenUnionSchema(e, t) { const n = e.anyOf ?? e.oneOf; if (!Array.isArray(n) || n.length === 0) return e; const r = Object.create(null); const i = []; for (const e of n) { if (e === true) { i.push(/* @__PURE__ */ new Set()); continue; } if (!isRecord(e)) continue; const n = isRecord(e.properties) ? e.properties : void 0; if (n) for (const [e, i] of Object.entries(n)) { if (!isPropertySchema(i)) { warnSchemaOnce(`${MCP_LOOPBACK_LOG_PREFIX}: malformed schema definition for "${t}.${e}", ignoring that variant`); continue; } if (!Object.hasOwn(r, e)) { r[e] = i; continue; } const n = r[e]; if (n === true || i === true) { r[e] = true; continue; } if (n === false) { r[e] = i; continue; } if (i === false) continue; if (isDeepStrictEqual(n, i, { skipPrototype: true })) continue; const a = mergeLiteralSchemas(n, i); if (a) { r[e] = a; continue; } warnSchemaOnce(`${MCP_LOOPBACK_LOG_PREFIX}: conflicting schema definitions for "${t}.${e}", keeping the first variant`); } i.push(new Set(Array.isArray(e.required) ? e.required : [])); } const a = i.length > 0 ? [...i[0] ?? []].filter((e) => Object.hasOwn(r, e) && i.every((t) => t.has(e))) : []; const { anyOf: o, oneOf: s, ...c } = e; return { ...c, type: "object", properties: r, required: a }; } function isPropertySchema(e) { return typeof e === "boolean" || isRecord(e); } const emittedSchemaWarnings = /* @__PURE__ */ new Set(); function warnSchemaOnce(e) { if (emittedSchemaWarnings.has(e)) return; emittedSchemaWarnings.add(e); logWarn(e); } function buildMcpToolSchema(e) { return e.flatMap((e) => { const t = readMcpLoopbackToolName(e); if (!t) return []; let n = readLoopbackToolParameters(e); if (!n) return []; if (n.anyOf || n.oneOf) n = flattenUnionSchema(n, t); if (n.type !== "object") n.type = "object"; if (!n.properties) n.properties = {}; return { name: t, description: typeof e.description === "string" ? e.description : void 0, inputSchema: n }; }); } //#endregion export { readMcpLoopbackToolName as n, buildMcpToolSchema as t };