UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

52 lines (51 loc) • 1.75 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/shared/json-schema-literals.ts const SCHEMA_ANNOTATION_KEYS = /* @__PURE__ */ new Set([ "$comment", "default", "deprecated", "description", "example", "examples", "readOnly", "title", "writeOnly" ]); /** Keeps only the keys that document a property, dropping every constraint. */ function schemaAnnotationsOnly(e) { if (!isRecord(e)) return {}; return Object.fromEntries(Object.entries(e).filter(([e]) => SCHEMA_ANNOTATION_KEYS.has(e))); } /** Const and enum constrain the same value, so retain their intersection. */ function readLiteralSchemaValues(e) { const t = Array.isArray(e.enum) ? e.enum : void 0; if (Object.hasOwn(e, "const")) { if (!t) return [e.const]; return t.some((t) => isDeepStrictEqual(t, e.const)) ? [e.const] : []; } return t; } function readLiteralValidationConstraints(e) { return Object.fromEntries(Object.entries(e).filter(([e]) => e !== "const" && e !== "enum" && !SCHEMA_ANNOTATION_KEYS.has(e))); } /** Pools property literals only when every non-annotation constraint agrees. */ function mergeLiteralSchemas(e, t) { const n = readLiteralSchemaValues(e); const r = readLiteralSchemaValues(t); if (n === void 0 || r === void 0) return; const i = readLiteralValidationConstraints(e); const a = readLiteralValidationConstraints(t); if (!isDeepStrictEqual(i, a)) return; const o = [...n, ...r]; const s = o.filter((e, t) => o.findIndex((t) => isDeepStrictEqual(t, e)) === t); if (s.length === 0) return; const c = { ...e, enum: s }; delete c.const; return c; } //#endregion export { readLiteralSchemaValues as n, schemaAnnotationsOnly as r, mergeLiteralSchemas as t };