UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.75 kB
import{l as isRecord,u as isStringArray}from"./record-coerce-BTbqdJ3-.mjs";import{Guard}from"typebox/guard";import{Check}from"typebox/schema"; //!packages/normalization-core/src/json-schema.ts function decodeLocalSchemaRefFragment(e){if(!e.startsWith(`#`))return;try{return decodeURIComponent(e.slice(1))}catch{return}}function decodeJsonPointerSegment(e){return e.replaceAll(`~1`,`/`).replaceAll(`~0`,`~`)}function parseLocalSchemaRefPointer(e){const t=decodeLocalSchemaRefFragment(e);return t?.startsWith(`/`)?t.slice(1).split(`/`).map(decodeJsonPointerSegment):void 0}function normalizeTypeBoxValidationErrors(e){const t=[];let n=0;for(const r of e){if(r.keyword===`boolean`){t.push(r);n+=1;continue}const e=r.params?.additionalProperties;if(r.keyword===`additionalProperties`&&typeof r.schemaPath===`string`&&typeof r.instancePath===`string`&&Array.isArray(e)&&e.length>0&&e.length<=n){if(t.slice(-e.length).every((t,n)=>{const i=e[n];return typeof i===`string`&&t.schemaPath===`${r.schemaPath}/additionalProperties`&&t.instancePath===`${r.instancePath}/${i.replace(/~/g,`~0`).replace(/\//g,`~1`)}`}))t.length-=e.length}t.push(r);n=0}return t}const schemaMapKeywords=/* @__PURE__ */ new Set([`$defs`,`definitions`,`dependentSchemas`,`dependencies`,`patternProperties`,`properties`]);const schemaValueKeywords=/* @__PURE__ */ new Set([`additionalItems`,`additionalProperties`,`allOf`,`anyOf`,`contains`,`else`,`if`,`items`,`not`,`oneOf`,`prefixItems`,`propertyNames`,`then`,`unevaluatedItems`,`unevaluatedProperties`]);const schemaResourceKeywords=/* @__PURE__ */ new Set([`$anchor`,`$defs`,`$dynamicAnchor`,`$id`,`$recursiveAnchor`,`$schema`,`$vocabulary`,`definitions`]);function mapJsonSchemaNode(e,t,n=[]){if(Array.isArray(e))return e.map(e=>mapJsonSchemaNode(e,t,n));if(!isRecord(e))return e;const r=[...n,e];return Object.fromEntries(Object.entries(t(e,n)).map(([e,n])=>{if(schemaMapKeywords.has(e)){const i=e===`dependencies`;return[e,isRecord(n)?Object.fromEntries(Object.entries(n).map(([e,n])=>[e,i&&isStringArray(n)?n:mapJsonSchemaNode(n,t,r)])):n]}if(schemaValueKeywords.has(e))return[e,mapJsonSchemaNode(n,t,r)];return[e,n]}))}function mapJsonSchema(e,t){return mapJsonSchemaNode(e,t)}function compilesUnicodePattern(e){try{new RegExp(e,`u`);return true}catch{return false}}function repairJsonSchemaPatternForUnicodeRegExp(e){if(compilesUnicodePattern(e))return e;const t=e.replace(/\\([^\\])/g,(e,t)=>{if(t===`:`||t===`/`)return t;return e});return compilesUnicodePattern(t)?t:e}function normalizePatternProperties(e){const t=/* @__PURE__ */ new Map;for(const[n,r]of Object.entries(e)){const e=repairJsonSchemaPatternForUnicodeRegExp(n);const i=t.get(e);t.set(e,i===void 0?r:{allOf:[i,r]})}return Object.fromEntries(t)}function expandJsonSchemaTypeArray(e){const{nullable:t,type:n,...r}=e;const i=Array.isArray(n)?[...n]:typeof n===`string`?[n]:null;if(!i)return e;if(t===true&&!i.includes(`null`))i.push(`null`);if(i.length===1&&!Array.isArray(n))return e;const a=Object.entries(r);const o=a.filter(([e])=>schemaResourceKeywords.has(e));const s=Object.fromEntries(a.filter(([e])=>!schemaResourceKeywords.has(e)));return{...Object.fromEntries(o),anyOf:i.map(e=>Object.assign({},s,{type:e}))}}function normalizeAdditionalPropertiesSchema(e){if(!isRecord(e.additionalProperties)||isRecord(e.properties)||isRecord(e.patternProperties))return e;const{additionalProperties:t,...n}=e;return{...n,patternProperties:{".*":t},additionalProperties:false}}function isJsonValue(e,t=/* @__PURE__ */ new WeakSet,n=/* @__PURE__ */ new WeakSet){if(e===null||typeof e===`string`||typeof e===`boolean`)return true;if(typeof e===`number`)return Number.isFinite(e);if(typeof e!==`object`)return false;let r;try{if(Array.isArray(e)){const t=Reflect.ownKeys(e);if(t.length!==e.length+1||t.some(t=>{if(t===`length`)return false;if(typeof t!==`string`)return true;const n=Number(t);return!Number.isSafeInteger(n)||n<0||n>=e.length||String(n)!==t}))return false;r=e}else{const t=Object.getPrototypeOf(e);if(t!==Object.prototype&&t!==null)return false;if(Reflect.ownKeys(e).some(t=>typeof t!==`string`||!Object.prototype.propertyIsEnumerable.call(e,t)))return false;r=Object.values(e)}}catch{return false}if(n.has(e))return true;if(t.has(e))return false;t.add(e);const i=r.every(e=>isJsonValue(e,t,n));t.delete(e);if(i)n.add(e);return i}function normalizeJsonSchemaForTypeBox(e,t={}){return mapJsonSchema(e,e=>{const n=normalizeAdditionalPropertiesSchema(expandJsonSchemaTypeArray(e.nullable===true&&e.enumIncludesNull===true&&Array.isArray(e.enum)&&!e.enum.some(e=>e===null)?{...e,enum:[...e.enum,null]}:e));return Object.fromEntries(Object.entries(n).filter(([e])=>e!==`format`||t.format!==`annotation`).map(([e,t])=>{if(e===`$dynamicRef`&&n.$ref===void 0)return[`$ref`,t];if(e===`pattern`&&typeof t===`string`)return[e,repairJsonSchemaPatternForUnicodeRegExp(t)];if(e===`patternProperties`&&isRecord(t))return[e,normalizePatternProperties(t)];return[e,t]}))})}function prepareJsonSchemaFormats(e,t){return mapJsonSchema(e,e=>{const{format:n,...r}=e;const i=typeof n===`string`?t.get(n):void 0;if(!i)return r;const a=Array.isArray(r[`~refine`])?r[`~refine`]:[];return{...r,"~refine":[...a,i]}})}function jsonSchemaValuesEqual(e,t){if(!isJsonValue(e)||!isJsonValue(t))return false;try{return Guard.IsDeepEqual(e,t)}catch{return false}}function isJsonSchemaValueValid(e,t){if(!isJsonValue(t))return false;try{return Check(normalizeJsonSchemaForTypeBox(e),t)}catch{return false}}export{mapJsonSchema as a,parseLocalSchemaRefPointer as c,jsonSchemaValuesEqual as i,prepareJsonSchemaFormats as l,decodeLocalSchemaRefFragment as n,normalizeJsonSchemaForTypeBox as o,isJsonSchemaValueValid as r,normalizeTypeBoxValidationErrors as s,decodeJsonPointerSegment as t};