openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
480 lines (479 loc) • 16.2 kB
JavaScript
import { l as isRecord, u as isStringArray } from "./record-coerce-CVPuDqcc.mjs";
import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs";
import { n as decodeLocalSchemaRefFragment, o as normalizeJsonSchemaForTypeBox, t as decodeJsonPointerSegment } from "./json-schema-DKEhiksH.mjs";
import { Check } from "typebox/schema";
//#region src/shared/json-schema-keywords.ts
const schemaCombinatorKeywords = /* @__PURE__ */ new Set([
"allOf",
"anyOf",
"oneOf"
]);
const jsonSchemaTypes = /* @__PURE__ */ new Set([
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]);
const schemaKeywordGroups = [
[/* @__PURE__ */ new Set([
"$anchor",
"$comment",
"$dynamicAnchor",
"$dynamicRef",
"$id",
"$schema",
"$ref",
"contentEncoding",
"contentMediaType",
"description",
"format",
"pattern",
"title"
]), "string"],
[/* @__PURE__ */ new Set([
"exclusiveMaximum",
"exclusiveMinimum",
"maximum",
"minimum",
"multipleOf"
]), "number"],
[/* @__PURE__ */ new Set([
"maxContains",
"maxItems",
"maxLength",
"maxProperties",
"minContains",
"minItems",
"minLength",
"minProperties"
]), "non-negative integer"],
[/* @__PURE__ */ new Set([
"deprecated",
"readOnly",
"uniqueItems",
"writeOnly"
]), "boolean"]
];
function validateTypeKeyword(e, t) {
if (typeof e === "string") return jsonSchemaTypes.has(e) ? void 0 : `${t}.type: unsupported JSON Schema type`;
if (Array.isArray(e) && e.length > 0) {
for (const n of e) if (typeof n !== "string" || !jsonSchemaTypes.has(n)) return `${t}.type: unsupported JSON Schema type`;
return new Set(e).size === e.length ? void 0 : `${t}.type: expected unique JSON Schema types`;
}
return `${t}.type: expected string or non-empty string array`;
}
function hasDuplicateJsonValues(e) {
const t = /* @__PURE__ */ new Set();
for (const n of e) {
const e = JSON.stringify(n);
if (t.has(e)) return true;
t.add(e);
}
return false;
}
function validateSchemaKeywordShapes(e, t) {
for (const [n, r] of schemaKeywordGroups) for (const i of n) {
const n = e[i];
if (n !== void 0 && (r === "non-negative integer" ? !Number.isInteger(n) || typeof n === "number" && n < 0 : typeof n !== r)) return `${t}.${i}: expected ${r}`;
}
if (e.multipleOf !== void 0 && typeof e.multipleOf === "number" && e.multipleOf <= 0) return `${t}.multipleOf: expected positive number`;
if (e.required !== void 0) {
if (!isStringArray(e.required)) return `${t}.required: expected string array`;
if (new Set(e.required).size !== e.required.length) return `${t}.required: expected unique string array`;
}
if (e.enum !== void 0) {
if (!Array.isArray(e.enum)) return `${t}.enum: expected array`;
if (e.enum.length === 0 || hasDuplicateJsonValues(e.enum)) return `${t}.enum: expected non-empty array with unique values`;
}
for (const n of schemaCombinatorKeywords) {
const r = e[n];
if (Array.isArray(r) && r.length === 0) return `${t}.${n}: expected non-empty schema array`;
}
if (e.dependentRequired !== void 0) {
if (!isRecord(e.dependentRequired)) return `${t}.dependentRequired: expected string array map`;
for (const [n, r] of Object.entries(e.dependentRequired)) if (!isStringArray(r)) return `${t}.dependentRequired.${n}: expected string array`;
}
if (e.dependencies !== void 0) {
if (!isRecord(e.dependencies)) return `${t}.dependencies: expected schema or string array map`;
for (const [n, r] of Object.entries(e.dependencies)) if (!isStringArray(r) && typeof r !== "boolean" && !isRecord(r)) return `${t}.dependencies.${n}: expected schema or string array`;
}
}
//#endregion
//#region src/shared/json-schema-defaults.ts
const schemaResourceIds = /* @__PURE__ */ new WeakMap();
let nextSchemaResourceId = 1;
const schemaMapKeywords = /* @__PURE__ */ new Set([
"$defs",
"definitions",
"dependentSchemas",
"patternProperties",
"properties"
]);
const schemaValueKeywords = /* @__PURE__ */ new Set([
"additionalItems",
"additionalProperties",
"contains",
"else",
"if",
"items",
"not",
"propertyNames",
"then",
"unevaluatedItems",
"unevaluatedProperties"
]);
const schemaArrayKeywords = /* @__PURE__ */ new Set([
"allOf",
"anyOf",
"oneOf",
"prefixItems"
]);
const JSON_POINTER_ARRAY_INDEX_SEGMENT = /^(0|[1-9]\d*)$/;
function schemaTypeIncludes(e, t) {
return e.type === t || Array.isArray(e.type) && e.type.includes(t);
}
function schemaResourceRefKey(e, t, n) {
if (!isRecord(e)) return `boolean:${String(e)}:${n ?? ""}:${t}`;
let r = schemaResourceIds.get(e);
if (r === void 0) {
r = nextSchemaResourceId++;
schemaResourceIds.set(e, r);
}
return `schema:${r}:${n ?? ""}:${t}`;
}
function parseJsonPointerArrayIndex(e) {
if (!JSON_POINTER_ARRAY_INDEX_SEGMENT.test(e)) return;
const t = Number(e);
return Number.isSafeInteger(t) ? t : void 0;
}
function resolveLocalAnchor(e, t, n = true) {
if (!isRecord(e)) return;
if (typeof e.$id === "string" && e.$id.startsWith("#")) {
if (decodeLocalSchemaRefFragment(e.$id) === t) return e;
} else if (!n && typeof e.$id === "string") return;
if (e.$anchor === t || e.$dynamicAnchor === t) return e;
return visitSchemaChildren(e, (e) => resolveLocalAnchor(e, t, false));
}
function resolveLocalRef(e, t, n) {
if (isRecord(e) && typeof e.$id === "string" && e.$id !== "") {
if (t === e.$id) return {
found: true,
schema: e,
resourceRoot: e,
resourceBaseId: n
};
if (t.startsWith(`${e.$id}#`)) return resolveLocalRef(e, t.slice(e.$id.length), n);
}
const r = decodeLocalSchemaRefFragment(t);
if (r === void 0) return { found: false };
if (r === "") return {
found: true,
schema: e,
resourceRoot: e,
resourceBaseId: n
};
if (r.startsWith("/")) {
let t = e;
let i = e;
let a = n;
for (const e of r.slice(1).split("/").map(decodeJsonPointerSegment)) {
if (Array.isArray(t)) {
const n = parseJsonPointerArrayIndex(e);
if (n === void 0) return { found: false };
t = t[n];
} else if (isRecord(t)) t = t[e];
else return { found: false };
if (isRecord(t) && typeof t.$id === "string" && !t.$id.startsWith("#")) {
i = t;
a = resolveSchemaId(t.$id, a);
}
}
return typeof t === "boolean" || isRecord(t) ? {
found: true,
schema: t,
resourceRoot: i,
resourceBaseId: a
} : { found: false };
}
const i = resolveLocalAnchor(e, r);
return i === void 0 ? { found: false } : {
found: true,
schema: i,
resourceRoot: e,
resourceBaseId: n
};
}
function splitResourceRef(e) {
const t = e.indexOf("#");
return t === -1 ? {
resource: e,
fragment: ""
} : {
resource: e.slice(0, t),
fragment: e.slice(t)
};
}
function stripFragment(e) {
return splitResourceRef(e).resource;
}
function resolveSchemaId(e, t) {
if (!t) return stripFragment(e);
try {
return stripFragment(new URL(e, t).href);
} catch {
return stripFragment(e);
}
}
function resolveSchemaResourceRef(e, t, n) {
const r = splitResourceRef(t);
const i = r.resource === "" ? r.resource : resolveSchemaId(r.resource, n);
const a = /* @__PURE__ */ new Set();
const visit = (e, t) => {
if (!isRecord(e) || a.has(e)) return { found: false };
a.add(e);
let n = t;
if (typeof e.$id === "string" && e.$id !== "") {
const a = resolveSchemaId(e.$id, t);
n = a;
if (i === a || r.resource === stripFragment(e.$id)) return r.fragment ? resolveLocalRef(e, r.fragment, n) : {
found: true,
schema: e,
resourceRoot: e,
resourceBaseId: n
};
}
return visitSchemaChildren(e, (e) => {
const t = visit(e, n);
return t.found ? t : void 0;
}) ?? { found: false };
};
return visit(e, void 0);
}
function resolveSchemaRef(e, t, n, r) {
const i = resolveLocalRef(t, n, r);
return i.found ? i : resolveSchemaResourceRef(e, n, r);
}
function resolveContextRef(e, t, n) {
if (!e) return { found: false };
const r = e.resources.get(e.resolveId(t, n));
if (r !== void 0) return {
found: true,
schema: r,
resourceRoot: r,
resourceBaseId: n
};
const { resource: i, fragment: a } = splitResourceRef(t);
const o = stripFragment(e.resolveId(i, n));
const s = e.resources.get(o);
if (s === void 0) return { found: false };
return a ? resolveLocalRef(s, a, o) : {
found: true,
schema: s,
resourceRoot: s,
resourceBaseId: o
};
}
function visitSchemaChildren(e, t, n) {
for (const r of schemaMapKeywords) {
const i = e[r];
if (!isRecord(i)) {
if (i !== void 0 && n) return n(r, "expected schema map");
continue;
}
for (const [e, n] of Object.entries(i)) {
const i = t(n, r, e);
if (i !== void 0) return i;
}
}
if (isRecord(e.dependencies)) {
for (const [n, r] of Object.entries(e.dependencies)) if (!isStringArray(r)) {
const e = t(r, "dependencies", n);
if (e !== void 0) return e;
}
}
for (const r of schemaValueKeywords) {
const i = e[r];
if (typeof i === "boolean" || isRecord(i)) {
const e = t(i, r);
if (e !== void 0) return e;
continue;
}
if (r === "items" && Array.isArray(i)) for (const [e, n] of i.entries()) {
const i = t(n, r, e);
if (i !== void 0) return i;
}
else if (i !== void 0 && n) return n(r, Array.isArray(i) ? "expected schema" : "schema must be an object or boolean");
}
for (const r of schemaArrayKeywords) {
const i = e[r];
if (!Array.isArray(i)) {
if (i !== void 0 && n) return n(r, "expected schema array");
continue;
}
for (const [e, n] of i.entries()) {
const i = t(n, r, e);
if (i !== void 0) return i;
}
}
}
function findJsonSchemaNodeError(e, t, n, r, i, a) {
if (typeof e === "boolean") return;
if (!isRecord(e)) return `${t}: schema must be an object or boolean`;
if (Object.hasOwn(e, "type")) {
const n = validateTypeKeyword(e.type, t);
if (n) return n;
}
if (e.nullable !== void 0) {
if (typeof e.nullable !== "boolean") return `${t}.nullable: expected boolean`;
if (!Object.hasOwn(e, "type")) return `${t}.nullable: expected type`;
}
const o = validateSchemaKeywordShapes(e, t);
if (o) return o;
const s = typeof e.$id === "string" && !e.$id.startsWith("#") ? e : r;
const c = typeof e.$id === "string" ? a ? stripFragment(a.resolveId(e.$id, i)) : resolveSchemaId(e.$id, i) : i;
const findChildError = (e, t) => findJsonSchemaNodeError(e, t, n, s, c, a);
for (const r of ["$ref", "$dynamicRef"]) if (typeof e[r] === "string" && !resolveSchemaRef(n, s, e[r], c).found && !resolveContextRef(a, e[r], c).found) return `${t}.${r}: unresolved ref`;
return visitSchemaChildren(e, (e, n, r) => findChildError(e, `${t}.${n}${r === void 0 ? "" : `.${r}`}`), (e, n) => `${t}.${e}: ${n}`);
}
/** Return the first structural JSON Schema error that would make validation/defaulting unsafe. */
function findJsonSchemaShapeError(e, t) {
return findJsonSchemaNodeError(e, "<schema>", e, e, void 0, t);
}
function inlineLocalRefsForMatch(e, t, n, r, i = /* @__PURE__ */ new Set()) {
if (Array.isArray(e)) return e.map((e) => inlineLocalRefsForMatch(e, t, n, r, i));
if (!isRecord(e)) return e;
const a = typeof e.$id === "string" && !e.$id.startsWith("#") ? e : n;
const o = typeof e.$id === "string" ? resolveSchemaId(e.$id, r) : r;
const inlineChild = (e) => inlineLocalRefsForMatch(e, t, a, o, i);
if (typeof e.$ref === "string") {
const n = schemaResourceRefKey(a, e.$ref, o);
const r = i.has(n) ? { found: false } : resolveSchemaRef(t, a, e.$ref, o);
if (r.found) {
const { $ref: a, ...o } = e;
i.add(n);
const s = inlineLocalRefsForMatch(r.schema, t, r.resourceRoot, r.resourceBaseId, i);
i.delete(n);
if (Object.keys(o).length === 0) return s;
return { allOf: [s, inlineChild(o)] };
}
}
return Object.fromEntries(Object.entries(e).map(([e, t]) => {
if ((schemaMapKeywords.has(e) || e === "dependencies") && isRecord(t)) return [e, Object.fromEntries(Object.entries(t).map(([t, n]) => [t, e === "dependencies" && isStringArray(n) ? n : inlineChild(n)]))];
if (schemaValueKeywords.has(e) || schemaArrayKeywords.has(e)) return [e, inlineChild(t)];
return [e, t];
}));
}
function schemaMatches(e, t, n, r, i) {
try {
const a = inlineLocalRefsForMatch(e, n, r, i);
let o = a;
if (isRecord(a) && isRecord(r)) o = {
...a,
...typeof r.$id === "string" && a.$id === void 0 ? { $id: r.$id } : {},
...isRecord(r.$defs) ? { $defs: r.$defs } : {},
...isRecord(r.definitions) ? { definitions: r.definitions } : {}
};
return Check(normalizeJsonSchemaForTypeBox(o), t);
} catch {
return false;
}
}
function countSchemaNodes(e, t = /* @__PURE__ */ new Set()) {
if (!isRecord(e) || t.has(e)) return 1;
t.add(e);
let n = 1;
visitSchemaChildren(e, (e) => {
n += countSchemaNodes(e, t);
});
return n;
}
function applySchemaDefaults(e, t, n = e, r = /* @__PURE__ */ new Set(), i = n, a) {
let o = t;
if (!isRecord(e)) return o;
if (o === void 0 && Object.hasOwn(e, "default")) o = structuredClone(e.default);
const s = typeof e.$id === "string" && !e.$id.startsWith("#") ? e : i;
const c = typeof e.$id === "string" ? resolveSchemaId(e.$id, a) : a;
const applyChild = (e, t) => applySchemaDefaults(e, t, n, r, s, c);
const l = typeof e.$ref === "string" ? schemaResourceRefKey(s, e.$ref, c) : void 0;
if (typeof e.$ref === "string" && l !== void 0 && !r.has(l)) {
const t = resolveSchemaRef(n, s, e.$ref, c);
if (t.found) {
r.add(l);
o = applySchemaDefaults(t.schema, o, n, r, t.resourceRoot, t.resourceBaseId);
r.delete(l);
}
}
const u = Array.isArray(e.allOf) ? e.allOf : [];
for (const e of u) o = applyChild(e, o);
const d = isRecord(e.properties) || isRecord(e.patternProperties) || isRecord(e.additionalProperties) || isRecord(e.dependencies) || isRecord(e.dependentSchemas) || typeof e.if === "boolean" || isRecord(e.if);
if ((schemaTypeIncludes(e, "object") || d) && isRecord(o)) {
let t = o;
const r = countSchemaNodes(e);
const settlePropertiesAndDependencies = () => {
for (let n = 0; n < r; n++) {
const n = JSON.stringify(t);
const r = isRecord(e.properties) ? e.properties : {};
for (const [e, n] of Object.entries(r)) {
if (isBlockedObjectKey(e)) continue;
const r = t[e];
const i = applyChild(n, r);
if (i !== void 0 && i !== r) t[e] = i;
}
const i = /* @__PURE__ */ new Set();
if (isRecord(e.patternProperties)) for (const [n, r] of Object.entries(e.patternProperties)) {
let e;
try {
e = new RegExp(n);
} catch {
continue;
}
for (const n of Object.keys(t)) {
if (isBlockedObjectKey(n) || !e.test(n)) continue;
i.add(n);
t[n] = applyChild(r, t[n]);
}
}
if (isRecord(e.additionalProperties)) {
for (const n of Object.keys(t)) if (!isBlockedObjectKey(n) && !Object.hasOwn(r, n) && !i.has(n)) t[n] = applyChild(e.additionalProperties, t[n]);
}
for (const n of ["dependencies", "dependentSchemas"]) {
if (!isRecord(e[n])) continue;
for (const [r, i] of Object.entries(e[n])) if (Object.hasOwn(t, r) && !(n === "dependencies" && isStringArray(i))) t = applyChild(i, t);
}
if (JSON.stringify(t) === n) break;
}
};
settlePropertiesAndDependencies();
if (typeof e.if === "boolean" || isRecord(e.if)) {
const r = schemaMatches(e.if, t, n, s, c) ? e.then : e.else;
if (typeof r === "boolean" || isRecord(r)) t = applyChild(r, t);
}
settlePropertiesAndDependencies();
return t;
}
if ((schemaTypeIncludes(e, "array") || e.items !== void 0 || e.prefixItems !== void 0) && Array.isArray(o)) {
const t = Array.isArray(e.prefixItems) ? e.prefixItems : Array.isArray(e.items) ? e.items : null;
if (t) {
const n = o.slice();
for (const [e, r] of t.entries()) {
const t = applyChild(r, n[e]);
if (t !== void 0) n[e] = t;
}
const r = isRecord(e.items) ? e.items : isRecord(e.additionalItems) ? e.additionalItems : null;
if (r) for (let e = t.length; e < n.length; e++) n[e] = applyChild(r, n[e]);
return n;
}
if (!isRecord(e.items)) return o;
return o.map((t) => applyChild(e.items, t));
}
return o;
}
/** Apply schema defaults to a config value while preserving caller-owned value shape. */
function applyJsonSchemaDefaults(e, t) {
return applySchemaDefaults(e, t);
}
//#endregion
export { findJsonSchemaShapeError as n, splitResourceRef as r, applyJsonSchemaDefaults as t };