@mochabug/adapt-sdk 0.4.14 → 0.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- import{isSchema as ae,isValidSchema as G,validate as H}from"jtd";var X=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function Y(n){return n.length===0?"/":n.map(r=>/^\d+$/.test(r)?`[${r}]`:`.${r}`).join("")}function F(n,r){let t=n;for(let i=0;i<r.length-1&&(t&&typeof t=="object"&&r[i]in t);i++)t=t[r[i]];return t}function z(n,r){let t=n;for(let i of r)if(t&&typeof t=="object")t=t[i];else return;return t}function W(n,r,t){let{instancePath:i,schemaPath:e}=n,o=Y(i),s=e[e.length-1];if(s==="type"){let c=F(r,e)?.type;return c==="boolean"?`${o}: expected boolean`:c==="string"?`${o}: expected string`:c==="timestamp"?`${o}: expected timestamp string`:X.has(c)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let c=F(r,e)?.enum;return`${o}: expected one of [${c.join(", ")}]`}if(e.length>=2&&e[e.length-2]==="properties"&&s)return`${o==="/"?"":o}.${s}: missing required property`;if(s==="elements")return`${o}: expected array`;if(s==="properties"||s==="optionalProperties"||s==="values")return`${o}: expected object`;if(s==="discriminator")return`${o}: expected string discriminator`;if(s==="mapping"){let a=z(t,i);return`${o}: unknown variant "${a}"`}return e.length===0?`${o}: unexpected property`:`${o}: validation error at /${e.join("/")}`}function M(n,r,t){let i=t??n.definitions,e=i?{...n,definitions:i}:n;return G(e)?H(e,r,{maxDepth:Z,maxErrors:Q}).map(s=>W(s,e,r)):n.ref?[`/: unknown ref "${n.ref}"`]:["/: invalid schema"]}var Z=32,Q=100;var T=32,A={int8:{minimum:-128,maximum:127},uint8:{minimum:0,maximum:255},int16:{minimum:-32768,maximum:32767},uint16:{minimum:0,maximum:65535},int32:{minimum:-2147483648,maximum:2147483647},uint32:{minimum:0,maximum:4294967295}},R={minimum:-34028235e31,maximum:34028235e31},k=`Must be a valid JSON value encoded as a string. For objects: '{"key": "value"}', for arrays: '[1, 2]', for primitives: '"hello"' or '42' or 'true' or 'null'.`,_=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,q="Array of key-value entries representing a map/dictionary. Each entry has a string 'key' (the property name) and a 'value' (the property value).";function g(n,r){return r?[n,"null"]:n}function D(n,r){if(n===r)return!0;if(n==null||r==null||typeof n!=typeof r||typeof n!="object")return!1;if(Array.isArray(n))return Array.isArray(r)&&n.length===r.length&&n.every((o,s)=>D(o,r[s]));if(Array.isArray(r))return!1;let t=n,i=r,e=Object.keys(t);if(e.length!==Object.keys(i).length)return!1;for(let o of e)if(!(o in i)||!D(t[o],i[o]))return!1;return!0}function O(n){return!n||typeof n!="object"?!0:n.type||n.enum||n.elements||n.values||n.discriminator||n.ref||n.properties&&Object.keys(n.properties).length>0||n.optionalProperties&&Object.keys(n.optionalProperties).length>0?!1:(n.properties!==void 0||n.optionalProperties!==void 0)&&n.additionalProperties===!0?!0:!(n.properties!==void 0||n.optionalProperties!==void 0)}function x(n,r){let t=n,i=0;for(;t.ref&&i<T;){let e=r?.[t.ref];if(!e)return!1;t=e,i++}return i>=T?!1:O(t)}function $(n,r){let t=new Set,i=[...n];for(;i.length>0;){let e=i.pop();if(!(!e||typeof e!="object")){if(e.ref){!t.has(e.ref)&&r[e.ref]&&(t.add(e.ref),i.push(r[e.ref]));continue}if(e.elements&&i.push(e.elements),e.values&&i.push(e.values),e.properties)for(let o of Object.values(e.properties))i.push(o);if(e.optionalProperties)for(let o of Object.values(e.optionalProperties))i.push(o);if(e.mapping)for(let o of Object.values(e.mapping))i.push(o)}}return t}function C(n,r,t){let i=n.definitions,e=t(r,n,i,0),o=M(n,e,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:e}}function V(n,r,t){let i=r?`${r}. `:"",e={type:"object",properties:{kind:{type:"string",enum:["value"]},data:n},required:["kind","data"],description:`${i}Inline value.`},o={type:"object",properties:{kind:{type:"string",enum:["reference"]},ref:{type:"string",description:'A file reference. The referenced file MUST contain data that conforms to the same format described in the "value" variant.'}},required:["kind","ref"],description:`${i}Reference to a file instead of inline data.`};return t!==!1&&(e.additionalProperties=!1,o.additionalProperties=!1),{anyOf:[e,o]}}function ee(n,r){let t=n.trim().toLowerCase(),i=r.trim().toLowerCase();return i==="*/*"?!0:i.endsWith("/*")?t.startsWith(i.slice(0,i.indexOf("/")+1)):t===i}function ne(n,r){if(typeof r!="object"||r===null)return{success:!1,errors:[`/: expected object with data and mimeType fields, got ${typeof r}`]};let t=r;if(typeof t.mimeType!="string"||!t.mimeType.includes("/"))return{success:!1,errors:['/mimeType: required concrete MIME type (e.g. "image/png", "application/pdf")']};if(!ee(t.mimeType,n))return{success:!1,errors:[`/mimeType: "${t.mimeType}" does not match accepted type "${n}"`]};let i;if(t.data instanceof Uint8Array)i=t.data;else if(typeof t.data=="string")i=new Uint8Array(Buffer.from(t.data,"base64"));else return{success:!1,errors:[`/data: expected base64 string or Uint8Array, got ${typeof t.data}`]};let e={success:!0,kind:"binary",mimeType:t.mimeType,data:i};return typeof t.filename=="string"&&t.filename&&(e.filename=t.filename),e}function U(n,r,t){return n.mimeType?ne(n.mimeType,r):n.jtdSchema?C(n.jtdSchema,r,t):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function I(n,r,t){let i=n.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return U(i[0],r,t);let e=[];for(let o of i){let s=U(o,r,t);if(s.success)return s;e.push(...s.errors)}return{success:!1,errors:[`No format matched for signal '${n.name??"unnamed"}'. Errors: ${e.join("; ")}`]}}function B(n,r,t){if(typeof r!="object"||r===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof r]};let i=r,e={},o=[];for(let s of n){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let c=I(s,a,t);if(c.success)c.kind==="binary"?e[s.name]={data:c.data,mimeType:c.mimeType,filename:c.filename}:c.kind==="json"&&(e[s.name]=c.data);else for(let f of c.errors)o.push(`/${s.name}${f.startsWith("/")?"":": "}${f}`)}return o.length>0?{success:!1,errors:o}:{success:!0,kind:"signals",signals:e}}function v(n,r){if(!n||typeof n!="object")return n;let t={...n};return t.ref&&r.has(t.ref)&&(t.ref=r.get(t.ref)),t.properties&&(t.properties=Object.fromEntries(Object.entries(t.properties).map(([i,e])=>[i,v(e,r)]))),t.optionalProperties&&(t.optionalProperties=Object.fromEntries(Object.entries(t.optionalProperties).map(([i,e])=>[i,v(e,r)]))),t.elements&&(t.elements=v(t.elements,r)),t.values&&(t.values=v(t.values,r)),t.mapping&&(t.mapping=Object.fromEntries(Object.entries(t.mapping).map(([i,e])=>[i,v(e,r)]))),t.definitions&&delete t.definitions,t}function L(n){let r={},t=new Map;for(let s=0;s<n.length;s++){let a=n[s].formats;if(a)for(let c=0;c<a.length;c++){let f=a[c].jtdSchema?.definitions;if(!f||Object.keys(f).length===0)continue;let u=new Map,p=!1;for(let[l,m]of Object.entries(f)){let h=r[l];if(h&&!D(h,m)){p=!0;break}}if(p)for(let[l,m]of Object.entries(f)){let h=r[l];if(!(h&&D(h,m)))if(h){let d=1;for(;r[`${l}_${d}`];)d++;let S=`${l}_${d}`;u.set(l,S),r[S]=m}else r[l]=m}else for(let[l,m]of Object.entries(f))r[l]||(r[l]=m);u.size>0&&t.set(`${s}:${c}`,u)}}for(let[,s]of t)for(let[,a]of s)r[a]=v(r[a],s);let i=n.map((s,a)=>{let c=s.formats;if(!c)return s;let f=!1,u=c.map((p,l)=>{if(!p.jtdSchema)return p;let m=t.get(`${a}:${l}`),h=p.jtdSchema.definitions&&Object.keys(p.jtdSchema.definitions).length>0;if(!m&&!h)return p;f=!0;let d=m?v(p.jtdSchema,m):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return f?{...s,formats:u}:s}),e=i.flatMap(s=>(s.formats??[]).filter(a=>a.jtdSchema).map(a=>a.jtdSchema)),o=$(e,r);for(let s of Object.keys(r))o.has(s)||delete r[s];return{mergedDefs:r,signalsCopy:i}}function j(n){if(n.type==="null"||Array.isArray(n.type)&&n.type.includes("null"))return n;let r={...n};return r.type&&(r.type=Array.isArray(r.type)?[...r.type,"null"]:[r.type,"null"]),r.enum&&!r.enum.includes(null)&&(r.enum=[...r.enum,null]),r}function E(n){let r=new Set;function t(i){if(!(!i||typeof i!="object")){if(i.$ref&&r.add(i.$ref.replace("#/$defs/","")),i.properties)for(let e of Object.values(i.properties))t(e);if(i.items&&t(i.items),i.anyOf)for(let e of i.anyOf)t(e);if(i.$defs)for(let e of Object.values(i.$defs))t(e)}}return t(n),r}var te=new Set(["title","label","name","description"]);function J(n,r){let t=r.metadata;if(!t)return;let i=[],e=typeof t.title=="string"&&t.title.trim()||typeof t.label=="string"&&t.label.trim()||typeof t.name=="string"&&t.name.trim(),o=typeof t.description=="string"?t.description.trim():"";e&&o?i.push(`${e}. ${o}`):e?i.push(e):o&&i.push(o);let s=[];for(let[a,c]of Object.entries(t))te.has(a)||(typeof c=="string"&&c.trim()?s.push(`[${a}: ${c.trim()}]`):(typeof c=="number"||typeof c=="boolean")&&s.push(`[${a}: ${c}]`));if(s.length>0&&i.push(s.join(" ")),i.length>0){let a=i.join(" ");n.description=n.description?`${n.description} ${a}`:a}}function P(n,r){let t=new Set;function i(e,o){if(!(!e||typeof e!="object")){if(e.ref){if(e.nullable===!0)t.add(e.ref);else if(o){let s=r[e.ref];s&&!s.elements&&!s.values&&t.add(e.ref)}return}if(e.elements&&i(e.elements,!1),e.values&&i(e.values,!1),e.properties)for(let s of Object.values(e.properties))i(s,!1);if(e.optionalProperties)for(let s of Object.values(e.optionalProperties))i(s,!0);if(e.mapping)for(let s of Object.values(e.mapping))i(s,!1)}}for(let e of n)i(e,!1);for(let e of Object.values(r))i(e,!1);return t}function y(n,r,t){if(t>T)throw new Error(`Schema nesting exceeded ${T} levels`);if(!n||typeof n!="object")return{type:"string",description:k};if(O(n)){let e={type:g("string",n.nullable===!0),description:k};return J(e,n),e}let i=n.nullable===!0;if(n.ref){let e=n.ref;if(!r?.[e])throw new Error(`Unresolved ref "${e}"`);return i?{$ref:`#/$defs/${e}_nullable`}:{$ref:`#/$defs/${e}`}}if(n.type){let e={},o=A[n.type];if(o)e.type=g("integer",i),e.minimum=o.minimum,e.maximum=o.maximum;else switch(n.type){case"boolean":e.type=g("boolean",i);break;case"string":e.type=g("string",i);break;case"timestamp":e.type=g("string",i),e.format="date-time";break;case"float32":e.type=g("number",i),e.minimum=R.minimum,e.maximum=R.maximum;break;case"float64":e.type=g("number",i);break;default:throw new Error(`Unknown JTD type "${n.type}"`)}return J(e,n),e}if(n.enum){let e={type:g("string",i),enum:i?[...n.enum,null]:n.enum};return J(e,n),e}if(n.elements){let e={type:g("array",i),items:y(n.elements,r,t+1)};return J(e,n),e}if(n.properties||n.optionalProperties){let e={},o=[];if(n.properties)for(let[a,c]of Object.entries(n.properties))e[a]=y(c,r,t+1),o.push(a);if(n.optionalProperties)for(let[a,c]of Object.entries(n.optionalProperties)){if(c.ref){let f=r?.[c.ref];f&&(f.elements||f.values)?e[a]={$ref:`#/$defs/${c.ref}`}:e[a]={$ref:`#/$defs/${c.ref}_nullable`}}else if(c.elements||c.values)e[a]=y(c,r,t+1);else if(c.discriminator){let f=y(c,r,t+1);e[a]={...f,anyOf:[...f.anyOf??[],{type:"null"}]}}else e[a]=j(y(c,r,t+1));o.push(a)}let s={type:g("object",i),properties:e,additionalProperties:!1,required:o};return J(s,n),s}if(n.values){let e=y(n.values,r,t+1),o={type:g("array",i),description:q,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:e},required:["key","value"],additionalProperties:!1}};return J(o,n),o}if(n.discriminator&&n.mapping){let e=n.discriminator,o=n.mapping,s=[];for(let[c,f]of Object.entries(o)){let u;O(f)?u={type:"object",properties:{[e]:{type:"string",enum:[c]},_data:{type:"string",description:_}},required:[e,"_data"],additionalProperties:!1}:(u=y(f,r,t+1),u.properties[e]={type:"string",enum:[c]},(u.required??=[]).unshift(e),u.additionalProperties=!1),J(u,f),s.push(u)}i&&s.push({type:"null"});let a={anyOf:s};return J(a,n),a}return{type:"string",description:k}}var re=new Set(Object.keys(A));function b(n,r,t,i){if(i>T||n==null)return n;if(r.ref){let e=t?.[r.ref];return e?b(n,e,t,i+1):n}if(O(r)){if(typeof n=="string")try{return JSON.parse(n)}catch{return n}return n}if(r.type&&re.has(r.type))return typeof n=="number"&&!Number.isInteger(n)?Math.round(n):n;if(r.elements&&Array.isArray(n)){let e=r.elements;return n.map(o=>b(o,e,t,i+1))}if((r.properties||r.optionalProperties)&&typeof n=="object"&&n!==null){let e={...n};if(r.properties)for(let[o,s]of Object.entries(r.properties))o in e&&(e[o]=b(e[o],s,t,i+1));if(r.optionalProperties)for(let[o,s]of Object.entries(r.optionalProperties))o in e&&(e[o]===null||Array.isArray(e[o])&&e[o].length===0&&(s.elements||s.values||s.ref)?delete e[o]:e[o]=b(e[o],s,t,i+1));return e}if(r.values&&Array.isArray(n)){let e={};for(let o of n)if(typeof o=="object"&&o!==null){let s=o,a=s.key;e[a]=b(s.value,r.values,t,i+1)}return e}if(r.discriminator&&r.mapping&&typeof n=="object"&&n!==null){let e=n,o=r.discriminator,s=e[o];if(typeof s!="string")return n;let a=r.mapping[s];if(!a)return n;if(O(a)){let f=e._data;if(typeof f=="string")try{let u=JSON.parse(f);if(typeof u=="object"&&u!==null)return{[o]:s,...u}}catch{}return{[o]:s}}let c={[o]:s};if(a.properties)for(let[f,u]of Object.entries(a.properties))f in e&&(c[f]=b(e[f],u,t,i+1));if(a.optionalProperties)for(let[f,u]of Object.entries(a.optionalProperties))!(f in e)||e[f]===null||Array.isArray(e[f])&&e[f].length===0&&(u.elements||u.values||u.ref)||(c[f]=b(e[f],u,t,i+1));return c}return n}function ie(n){return{type:"object",description:`Binary content (accepted MIME type: ${n}).`,properties:{data:{type:"string",description:"The base64-encoded binary content."},filename:{type:"string",description:'Filename for the content (e.g. "report.pdf", "image.png").'},mimeType:{type:"string",description:`The concrete MIME type of the content. Must match: ${n}.`}},required:["data","mimeType","filename"],additionalProperties:!1}}var w=class n{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(r){let t=r.definitions,i=!x(r,t),e;if(i){if(e=y(r,t,0),t&&Object.keys(t).length>0){let o=$([r],t),s=P([r],t),a=[];for(let[f,u]of Object.entries(t)){if(!o.has(f))continue;let p=y(u,t,0);a.push([f,p]),s.has(f)&&a.push([`${f}_nullable`,j(p)])}e.$defs=Object.fromEntries(a);let c=E(e);for(let f of Object.keys(e.$defs))c.has(f)||delete e.$defs[f]}}else e={},J(e,r);return new n(e,i,r,void 0)}static fromSignal(r,t,i){let{schema:e,strict:o}=oe(r,t);if(i){let s=V(e,r.description);return new n(s,o,void 0,r)}return new n(e,o,void 0,r)}static fromSignals(r,t,i,e){let{mergedDefs:o,signalsCopy:s}=L(r),a={},c=[],f=!0;for(let p of s){if(!p.name)continue;let l=n.fromSignal(p,o,e);if(l.strict||(f=!1),p.optional)if(l.schema.$ref){let m=l.schema.$ref.replace("#/$defs/","");a[p.name]={$ref:`#/$defs/${m}_nullable`}}else a[p.name]=j(l.schema);else a[p.name]=l.schema;c.push(p.name)}let u={type:"object",properties:a,required:c,additionalProperties:!1};if(Object.keys(o).length>0){let p=s.flatMap(d=>(d.formats??[]).filter(S=>S.jtdSchema).map(S=>S.jtdSchema)),l=P(p,o);for(let d of s)if(d.optional)for(let S of d.formats??[])S.jtdSchema?.ref&&l.add(S.jtdSchema.ref);let m=[];for(let[d,S]of Object.entries(o)){let N=y(S,o,0);m.push([d,N]),l.has(d)&&m.push([`${d}_nullable`,j(N)])}u.$defs=Object.fromEntries(m);let h=E(u);for(let d of Object.keys(u.$defs))h.has(d)||delete u.$defs[d]}return i&&t?u.description=`${i}. ${t}`:i?u.description=i:t&&(u.description=t),new n(u,f,void 0,void 0,r,e)}static fromJSON(r){let t=typeof r=="string"?JSON.parse(r):r;if(t.descriptors)return n.fromSignals(t.descriptors,t.description,t.label,t.referenceMode);if(t.descriptor)return n.fromSignal(t.descriptor);if(t.jtdSchema)return n.fromJTD(t.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(r,t,i,e,o,s){this.schema=r,this.strict=t,this.jtdSchema=i,this.descriptor=e,this.descriptors=o,this.referenceMode=s??!1}convert=r=>this.descriptors?B(this.descriptors,r,b):this.jtdSchema?C(this.jtdSchema,r,b):this.descriptor?I(this.descriptor,r,b):{success:!1,errors:["No source schema available"]};toJSON(){if(this.descriptors){let r={schema:this.schema,strict:this.strict,descriptors:this.descriptors};return this.schema.description&&(r.description=this.schema.description),this.referenceMode&&(r.referenceMode=!0),r}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function oe(n,r){let t=n.formats;if(!t||t.length===0)throw new Error("Signal descriptor has no formats");if(t.length===1){let[a]=t,{schema:c,strict:f}=K(a,r);if(n.label||n.description){let u=[];n.label&&u.push(n.label),n.description&&u.push(n.description);let p=u.join(". ");c.description=c.description?`${p} ${c.description}`:p}return{schema:c,strict:f}}let i=[],e=!0;for(let a of t){let{schema:c,strict:f}=K(a,r);f||(e=!1),a.jtdSchema&&(c.description=c.description?`Structured JSON data. ${c.description}`:"Structured JSON data."),i.push(c)}let o=n.description??"";return{schema:{anyOf:i,description:o||void 0},strict:e}}function K(n,r){if(n.jtdSchema){let t=r??n.jtdSchema.definitions,i=!x(n.jtdSchema,t),e;if(i){if(e=y(n.jtdSchema,t,0),!r&&t&&Object.keys(t).length>0){let o=$([n.jtdSchema],t),s=P([n.jtdSchema],t),a=[];for(let[f,u]of Object.entries(t)){if(!o.has(f))continue;let p=y(u,t,0);a.push([f,p]),s.has(f)&&a.push([`${f}_nullable`,j(p)])}e.$defs=Object.fromEntries(a);let c=E(e);for(let f of Object.keys(e.$defs))c.has(f)||delete e.$defs[f]}}else e={},J(e,n.jtdSchema);return{schema:e,strict:i}}if(n.mimeType)return{schema:ie(n.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function le(n){return w.fromJTD(n)}function de(n){return w.fromSignal(n)}export{w as OpenAIConversion,de as convertSignalToOpenAISchema,le as convertToOpenAISchema};
1
+ import{isSchema as ae,isValidSchema as G,validate as H}from"jtd";var X=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function Y(n){return n.length===0?"/":n.map(r=>/^\d+$/.test(r)?`[${r}]`:`.${r}`).join("")}function F(n,r){let t=n;for(let i=0;i<r.length-1&&(t&&typeof t=="object"&&r[i]in t);i++)t=t[r[i]];return t}function z(n,r){let t=n;for(let i of r)if(t&&typeof t=="object")t=t[i];else return;return t}function W(n,r,t){let{instancePath:i,schemaPath:e}=n,o=Y(i),s=e[e.length-1];if(s==="type"){let c=F(r,e)?.type;return c==="boolean"?`${o}: expected boolean`:c==="string"?`${o}: expected string`:c==="timestamp"?`${o}: expected timestamp string`:X.has(c)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let c=F(r,e)?.enum;return`${o}: expected one of [${c.join(", ")}]`}if(e.length>=2&&e[e.length-2]==="properties"&&s)return`${o==="/"?"":o}.${s}: missing required property`;if(s==="elements")return`${o}: expected array`;if(s==="properties"||s==="optionalProperties"||s==="values")return`${o}: expected object`;if(s==="discriminator")return`${o}: expected string discriminator`;if(s==="mapping"){let a=z(t,i);return`${o}: unknown variant "${a}"`}return e.length===0?`${o}: unexpected property`:`${o}: validation error at /${e.join("/")}`}function M(n,r,t){let i=t??n.definitions,e=i?{...n,definitions:i}:n;return G(e)?H(e,r,{maxDepth:Z,maxErrors:Q}).map(s=>W(s,e,r)):n.ref?[`/: unknown ref "${n.ref}"`]:["/: invalid schema"]}var Z=32,Q=100;var T=32,R={int8:{minimum:-128,maximum:127},uint8:{minimum:0,maximum:255},int16:{minimum:-32768,maximum:32767},uint16:{minimum:0,maximum:65535},int32:{minimum:-2147483648,maximum:2147483647},uint32:{minimum:0,maximum:4294967295}},A={minimum:-34028235e31,maximum:34028235e31},$=`Must be a valid JSON value encoded as a string. For objects: '{"key": "value"}', for arrays: '[1, 2]', for primitives: '"hello"' or '42' or 'true' or 'null'.`,_=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,q="Array of key-value entries representing a map/dictionary. Each entry has a string 'key' (the property name) and a 'value' (the property value).";function g(n,r){return r?[n,"null"]:n}function D(n,r){if(n===r)return!0;if(n==null||r==null||typeof n!=typeof r||typeof n!="object")return!1;if(Array.isArray(n))return Array.isArray(r)&&n.length===r.length&&n.every((o,s)=>D(o,r[s]));if(Array.isArray(r))return!1;let t=n,i=r,e=Object.keys(t);if(e.length!==Object.keys(i).length)return!1;for(let o of e)if(!(o in i)||!D(t[o],i[o]))return!1;return!0}function O(n){return!n||typeof n!="object"?!0:n.type||n.enum||n.elements||n.values||n.discriminator||n.ref||n.properties&&Object.keys(n.properties).length>0||n.optionalProperties&&Object.keys(n.optionalProperties).length>0?!1:(n.properties!==void 0||n.optionalProperties!==void 0)&&n.additionalProperties===!0?!0:!(n.properties!==void 0||n.optionalProperties!==void 0)}function x(n,r){let t=n,i=0;for(;t.ref&&i<T;){let e=r?.[t.ref];if(!e)return!1;t=e,i++}return i>=T?!1:O(t)}function k(n,r){let t=new Set,i=[...n];for(;i.length>0;){let e=i.pop();if(!(!e||typeof e!="object")){if(e.ref){!t.has(e.ref)&&r[e.ref]&&(t.add(e.ref),i.push(r[e.ref]));continue}if(e.elements&&i.push(e.elements),e.values&&i.push(e.values),e.properties)for(let o of Object.values(e.properties))i.push(o);if(e.optionalProperties)for(let o of Object.values(e.optionalProperties))i.push(o);if(e.mapping)for(let o of Object.values(e.mapping))i.push(o)}}return t}function C(n,r,t){let i=n.definitions,e=t(r,n,i,0),o=M(n,e,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:e}}function V(n,r,t){let i=r?`${r}. `:"",e={type:"object",properties:{kind:{type:"string",enum:["value"]},data:n},required:["kind","data"],description:`${i}Inline value.`},o={type:"object",properties:{kind:{type:"string",enum:["reference"]},ref:{type:"string",description:'A file reference. The referenced file MUST contain data that conforms to the same format described in the "value" variant.'}},required:["kind","ref"],description:`${i}Reference to a file instead of inline data.`};return t!==!1&&(e.additionalProperties=!1,o.additionalProperties=!1),{anyOf:[e,o]}}function ee(n,r){let t=n.trim().toLowerCase(),i=r.trim().toLowerCase();return i==="*/*"?!0:i.endsWith("/*")?t.startsWith(i.slice(0,i.indexOf("/")+1)):t===i}function ne(n,r){if(typeof r!="object"||r===null)return{success:!1,errors:[`/: expected object with data and mimeType fields, got ${typeof r}`]};let t=r;if(typeof t.mimeType!="string"||!t.mimeType.includes("/"))return{success:!1,errors:['/mimeType: required concrete MIME type (e.g. "image/png", "application/pdf")']};if(!ee(t.mimeType,n))return{success:!1,errors:[`/mimeType: "${t.mimeType}" does not match accepted type "${n}"`]};let i;if(t.data instanceof Uint8Array)i=t.data;else if(typeof t.data=="string")i=new Uint8Array(Buffer.from(t.data,"base64"));else return{success:!1,errors:[`/data: expected base64 string or Uint8Array, got ${typeof t.data}`]};let e={success:!0,kind:"binary",mimeType:t.mimeType,data:i};return typeof t.filename=="string"&&t.filename&&(e.filename=t.filename),e}function U(n,r,t){return n.mimeType?ne(n.mimeType,r):n.jtdSchema?C(n.jtdSchema,r,t):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function I(n,r,t){let i=n.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return U(i[0],r,t);let e=[];for(let o of i){let s=U(o,r,t);if(s.success)return s;e.push(...s.errors)}return{success:!1,errors:[`No format matched for signal '${n.name??"unnamed"}'. Errors: ${e.join("; ")}`]}}function B(n,r,t){if(typeof r!="object"||r===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof r]};let i=r,e={},o=[];for(let s of n){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let c=I(s,a,t);if(c.success)c.kind==="binary"?e[s.name]={data:c.data,mimeType:c.mimeType,filename:c.filename}:c.kind==="json"&&(e[s.name]=c.data);else for(let f of c.errors)o.push(`/${s.name}${f.startsWith("/")?"":": "}${f}`)}return o.length>0?{success:!1,errors:o}:{success:!0,kind:"signals",signals:e}}function v(n,r){if(!n||typeof n!="object")return n;let t={...n};return t.ref&&r.has(t.ref)&&(t.ref=r.get(t.ref)),t.properties&&(t.properties=Object.fromEntries(Object.entries(t.properties).map(([i,e])=>[i,v(e,r)]))),t.optionalProperties&&(t.optionalProperties=Object.fromEntries(Object.entries(t.optionalProperties).map(([i,e])=>[i,v(e,r)]))),t.elements&&(t.elements=v(t.elements,r)),t.values&&(t.values=v(t.values,r)),t.mapping&&(t.mapping=Object.fromEntries(Object.entries(t.mapping).map(([i,e])=>[i,v(e,r)]))),t.definitions&&delete t.definitions,t}function L(n){let r={},t=new Map;for(let s=0;s<n.length;s++){let a=n[s].formats;if(a)for(let c=0;c<a.length;c++){let f=a[c].jtdSchema?.definitions;if(!f||Object.keys(f).length===0)continue;let u=new Map,p=!1;for(let[l,m]of Object.entries(f)){let h=r[l];if(h&&!D(h,m)){p=!0;break}}if(p)for(let[l,m]of Object.entries(f)){let h=r[l];if(!(h&&D(h,m)))if(h){let d=1;for(;r[`${l}_${d}`];)d++;let S=`${l}_${d}`;u.set(l,S),r[S]=m}else r[l]=m}else for(let[l,m]of Object.entries(f))r[l]||(r[l]=m);u.size>0&&t.set(`${s}:${c}`,u)}}for(let[,s]of t)for(let[,a]of s)r[a]=v(r[a],s);let i=n.map((s,a)=>{let c=s.formats;if(!c)return s;let f=!1,u=c.map((p,l)=>{if(!p.jtdSchema)return p;let m=t.get(`${a}:${l}`),h=p.jtdSchema.definitions&&Object.keys(p.jtdSchema.definitions).length>0;if(!m&&!h)return p;f=!0;let d=m?v(p.jtdSchema,m):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return f?{...s,formats:u}:s}),e=i.flatMap(s=>(s.formats??[]).filter(a=>a.jtdSchema).map(a=>a.jtdSchema)),o=k(e,r);for(let s of Object.keys(r))o.has(s)||delete r[s];return{mergedDefs:r,signalsCopy:i}}function j(n){if(n.type==="null"||Array.isArray(n.type)&&n.type.includes("null"))return n;let r={...n};return r.type&&(r.type=Array.isArray(r.type)?[...r.type,"null"]:[r.type,"null"]),r.enum&&!r.enum.includes(null)&&(r.enum=[...r.enum,null]),r}function P(n){let r=new Set;function t(i){if(!(!i||typeof i!="object")){if(i.$ref&&r.add(i.$ref.replace("#/$defs/","")),i.properties)for(let e of Object.values(i.properties))t(e);if(i.items&&t(i.items),i.anyOf)for(let e of i.anyOf)t(e);if(i.$defs)for(let e of Object.values(i.$defs))t(e)}}return t(n),r}var te=new Set(["title","label","name","description"]);function b(n,r){let t=r.metadata;if(!t)return;let i=[],e=typeof t.title=="string"&&t.title.trim()||typeof t.label=="string"&&t.label.trim()||typeof t.name=="string"&&t.name.trim(),o=typeof t.description=="string"?t.description.trim():"";e&&o?i.push(`${e}. ${o}`):e?i.push(e):o&&i.push(o);let s=[];for(let[a,c]of Object.entries(t))te.has(a)||(typeof c=="string"&&c.trim()?s.push(`[${a}: ${c.trim()}]`):(typeof c=="number"||typeof c=="boolean")&&s.push(`[${a}: ${c}]`));if(s.length>0&&i.push(s.join(" ")),i.length>0){let a=i.join(" ");n.description=n.description?`${n.description} ${a}`:a}}function E(n,r){let t=new Set;function i(e,o){if(!(!e||typeof e!="object")){if(e.ref){if(e.nullable===!0)t.add(e.ref);else if(o){let s=r[e.ref];s&&!s.elements&&!s.values&&t.add(e.ref)}return}if(e.elements&&i(e.elements,!1),e.values&&i(e.values,!1),e.properties)for(let s of Object.values(e.properties))i(s,!1);if(e.optionalProperties)for(let s of Object.values(e.optionalProperties))i(s,!0);if(e.mapping)for(let s of Object.values(e.mapping))i(s,!1)}}for(let e of n)i(e,!1);for(let e of Object.values(r))i(e,!1);return t}function y(n,r,t){if(t>T)throw new Error(`Schema nesting exceeded ${T} levels`);if(!n||typeof n!="object")return{type:"string",description:$};if(O(n)){let e={type:g("string",n.nullable===!0),description:$};return b(e,n),e}let i=n.nullable===!0;if(n.ref){let e=n.ref;if(!r?.[e])throw new Error(`Unresolved ref "${e}"`);return i?{$ref:`#/$defs/${e}_nullable`}:{$ref:`#/$defs/${e}`}}if(n.type){let e={},o=R[n.type];if(o)e.type=g("integer",i),e.minimum=o.minimum,e.maximum=o.maximum;else switch(n.type){case"boolean":e.type=g("boolean",i);break;case"string":e.type=g("string",i);break;case"timestamp":e.type=g("string",i),e.format="date-time";break;case"float32":e.type=g("number",i),e.minimum=A.minimum,e.maximum=A.maximum;break;case"float64":e.type=g("number",i);break;default:throw new Error(`Unknown JTD type "${n.type}"`)}return b(e,n),e}if(n.enum){let e={type:g("string",i),enum:i?[...n.enum,null]:n.enum};return b(e,n),e}if(n.elements){let e={type:g("array",i),items:y(n.elements,r,t+1)};return b(e,n),e}if(n.properties||n.optionalProperties){let e={},o=[];if(n.properties)for(let[a,c]of Object.entries(n.properties))e[a]=y(c,r,t+1),o.push(a);if(n.optionalProperties)for(let[a,c]of Object.entries(n.optionalProperties)){if(c.ref){let f=r?.[c.ref];f&&(f.elements||f.values)?e[a]={$ref:`#/$defs/${c.ref}`}:e[a]={$ref:`#/$defs/${c.ref}_nullable`}}else if(c.elements||c.values)e[a]=y(c,r,t+1);else if(c.discriminator){let f=y(c,r,t+1);e[a]={...f,anyOf:[...f.anyOf??[],{type:"null"}]}}else e[a]=j(y(c,r,t+1));o.push(a)}let s={type:g("object",i),properties:e,additionalProperties:!1,required:o};return b(s,n),s}if(n.values){let e=y(n.values,r,t+1),o={type:g("array",i),description:q,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:e},required:["key","value"],additionalProperties:!1}};return b(o,n),o}if(n.discriminator&&n.mapping){let e=n.discriminator,o=n.mapping,s=[];for(let[c,f]of Object.entries(o)){let u;O(f)?u={type:"object",properties:{[e]:{type:"string",enum:[c]},_data:{type:"string",description:_}},required:[e,"_data"],additionalProperties:!1}:(u=y(f,r,t+1),u.properties[e]={type:"string",enum:[c]},(u.required??=[]).unshift(e),u.additionalProperties=!1),b(u,f),s.push(u)}i&&s.push({type:"null"});let a={anyOf:s};return b(a,n),a}return{type:"string",description:$}}var re=new Set(Object.keys(R));function J(n,r,t,i){if(i>T||n==null)return n;if(r.ref){let e=t?.[r.ref];return e?J(n,e,t,i+1):n}if(O(r)){if(typeof n=="string")try{return JSON.parse(n)}catch{return n}return n}if(r.type&&re.has(r.type))return typeof n=="number"&&!Number.isInteger(n)?Math.round(n):n;if(r.elements&&Array.isArray(n)){let e=r.elements;return n.map(o=>J(o,e,t,i+1))}if((r.properties||r.optionalProperties)&&typeof n=="object"&&n!==null){let e={...n};if(r.properties)for(let[o,s]of Object.entries(r.properties))o in e&&(e[o]=J(e[o],s,t,i+1));if(r.optionalProperties)for(let[o,s]of Object.entries(r.optionalProperties))o in e&&(e[o]===null||Array.isArray(e[o])&&e[o].length===0&&(s.elements||s.values||s.ref)?delete e[o]:e[o]=J(e[o],s,t,i+1));return e}if(r.values&&Array.isArray(n)){let e={};for(let o of n)if(typeof o=="object"&&o!==null){let s=o,a=s.key;e[a]=J(s.value,r.values,t,i+1)}return e}if(r.discriminator&&r.mapping&&typeof n=="object"&&n!==null){let e=n,o=r.discriminator,s=e[o];if(typeof s!="string")return n;let a=r.mapping[s];if(!a)return n;if(O(a)){let f=e._data;if(typeof f=="string")try{let u=JSON.parse(f);if(typeof u=="object"&&u!==null)return{[o]:s,...u}}catch{}return{[o]:s}}let c={[o]:s};if(a.properties)for(let[f,u]of Object.entries(a.properties))f in e&&(c[f]=J(e[f],u,t,i+1));if(a.optionalProperties)for(let[f,u]of Object.entries(a.optionalProperties))!(f in e)||e[f]===null||Array.isArray(e[f])&&e[f].length===0&&(u.elements||u.values||u.ref)||(c[f]=J(e[f],u,t,i+1));return c}return n}function ie(n){return{type:"object",description:`Binary content (accepted MIME type: ${n}).`,properties:{data:{type:"string",description:"The base64-encoded binary content."},filename:{type:"string",description:'Filename for the content (e.g. "report.pdf", "image.png").'},mimeType:{type:"string",description:`The concrete MIME type of the content. Must match: ${n}.`}},required:["data","mimeType","filename"],additionalProperties:!1}}var w=class n{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(r){let t=r.definitions,i=!x(r,t),e;if(i){if(e=y(r,t,0),t&&Object.keys(t).length>0){let o=k([r],t),s=E([r],t),a=[];for(let[f,u]of Object.entries(t)){if(!o.has(f))continue;let p=y(u,t,0);a.push([f,p]),s.has(f)&&a.push([`${f}_nullable`,j(p)])}e.$defs=Object.fromEntries(a);let c=P(e);for(let f of Object.keys(e.$defs))c.has(f)||delete e.$defs[f]}if(r.discriminator&&r.mapping){let o=e;e={type:"object",properties:{input:o},required:["input"],additionalProperties:!1},o.$defs&&(e.$defs=o.$defs,delete o.$defs)}}else e={},b(e,r);return new n(e,i,r,void 0)}static fromSignal(r,t,i){let{schema:e,strict:o}=oe(r,t);if(i){let s=V(e,r.description);return new n(s,o,void 0,r)}return new n(e,o,void 0,r)}static fromSignals(r,t,i,e){let{mergedDefs:o,signalsCopy:s}=L(r),a={},c=[],f=!0;for(let p of s){if(!p.name)continue;let l=n.fromSignal(p,o,e);if(l.strict||(f=!1),p.optional)if(l.schema.$ref){let m=l.schema.$ref.replace("#/$defs/","");a[p.name]={$ref:`#/$defs/${m}_nullable`}}else a[p.name]=j(l.schema);else a[p.name]=l.schema;c.push(p.name)}let u={type:"object",properties:a,required:c,additionalProperties:!1};if(Object.keys(o).length>0){let p=s.flatMap(d=>(d.formats??[]).filter(S=>S.jtdSchema).map(S=>S.jtdSchema)),l=E(p,o);for(let d of s)if(d.optional)for(let S of d.formats??[])S.jtdSchema?.ref&&l.add(S.jtdSchema.ref);let m=[];for(let[d,S]of Object.entries(o)){let N=y(S,o,0);m.push([d,N]),l.has(d)&&m.push([`${d}_nullable`,j(N)])}u.$defs=Object.fromEntries(m);let h=P(u);for(let d of Object.keys(u.$defs))h.has(d)||delete u.$defs[d]}return i&&t?u.description=`${i}. ${t}`:i?u.description=i:t&&(u.description=t),new n(u,f,void 0,void 0,r,e)}static fromJSON(r){let t=typeof r=="string"?JSON.parse(r):r;if(t.descriptors)return n.fromSignals(t.descriptors,t.description,t.label,t.referenceMode);if(t.descriptor)return n.fromSignal(t.descriptor);if(t.jtdSchema)return n.fromJTD(t.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(r,t,i,e,o,s){this.schema=r,this.strict=t,this.jtdSchema=i,this.descriptor=e,this.descriptors=o,this.referenceMode=s??!1}convert=r=>{if(this.descriptors)return B(this.descriptors,r,J);if(this.jtdSchema){let t=r;return this.jtdSchema.discriminator&&this.jtdSchema.mapping&&typeof t=="object"&&t!==null&&"input"in t&&(t=t.input),C(this.jtdSchema,t,J)}return this.descriptor?I(this.descriptor,r,J):{success:!1,errors:["No source schema available"]}};toJSON(){if(this.descriptors){let r={schema:this.schema,strict:this.strict,descriptors:this.descriptors};return this.schema.description&&(r.description=this.schema.description),this.referenceMode&&(r.referenceMode=!0),r}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function oe(n,r){let t=n.formats;if(!t||t.length===0)throw new Error("Signal descriptor has no formats");if(t.length===1){let[a]=t,{schema:c,strict:f}=K(a,r);if(n.label||n.description){let u=[];n.label&&u.push(n.label),n.description&&u.push(n.description);let p=u.join(". ");c.description=c.description?`${p} ${c.description}`:p}return{schema:c,strict:f}}let i=[],e=!0;for(let a of t){let{schema:c,strict:f}=K(a,r);f||(e=!1),a.jtdSchema&&(c.description=c.description?`Structured JSON data. ${c.description}`:"Structured JSON data."),i.push(c)}let o=n.description??"";return{schema:{anyOf:i,description:o||void 0},strict:e}}function K(n,r){if(n.jtdSchema){let t=r??n.jtdSchema.definitions,i=!x(n.jtdSchema,t),e;if(i){if(e=y(n.jtdSchema,t,0),!r&&t&&Object.keys(t).length>0){let o=k([n.jtdSchema],t),s=E([n.jtdSchema],t),a=[];for(let[f,u]of Object.entries(t)){if(!o.has(f))continue;let p=y(u,t,0);a.push([f,p]),s.has(f)&&a.push([`${f}_nullable`,j(p)])}e.$defs=Object.fromEntries(a);let c=P(e);for(let f of Object.keys(e.$defs))c.has(f)||delete e.$defs[f]}}else e={},b(e,n.jtdSchema);return{schema:e,strict:i}}if(n.mimeType)return{schema:ie(n.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function le(n){return w.fromJTD(n)}function de(n){return w.fromSignal(n)}export{w as OpenAIConversion,de as convertSignalToOpenAISchema,le as convertToOpenAISchema};
2
2
  //# sourceMappingURL=openai.mjs.map