@mochabug/adapt-sdk 0.4.18 → 0.4.20
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.
- package/dist/api/index.d.ts +2 -2
- package/dist/api/index.d.ts.map +1 -1
- package/dist/cjs/anthropic.cjs +1 -1
- package/dist/cjs/anthropic.cjs.map +2 -2
- package/dist/cjs/api.cjs.map +1 -1
- package/dist/cjs/gemini.cjs +1 -1
- package/dist/cjs/gemini.cjs.map +2 -2
- package/dist/cjs/openai.cjs +1 -1
- package/dist/cjs/openai.cjs.map +2 -2
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/esm/anthropic.mjs +1 -1
- package/dist/esm/anthropic.mjs.map +2 -2
- package/dist/esm/api.mjs.map +1 -1
- package/dist/esm/gemini.mjs +1 -1
- package/dist/esm/gemini.mjs.map +2 -2
- package/dist/esm/openai.mjs +1 -1
- package/dist/esm/openai.mjs.map +2 -2
- package/dist/esm/router.mjs.map +1 -1
- package/dist/schema-utils.d.ts +9 -5
- package/dist/schema-utils.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/cjs/openai.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var x=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(n,r)=>{for(var t in r)x(n,t,{get:r[t],enumerable:!0})},Z=(n,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of Y(r))!z.call(n,e)&&e!==t&&x(n,e,{get:()=>r[e],enumerable:!(i=X(r,e))||i.enumerable});return n};var Q=n=>Z(x({},"__esModule",{value:!0}),n);var me={};W(me,{OpenAIConversion:()=>k,convertSignalToOpenAISchema:()=>de,convertToOpenAISchema:()=>le});module.exports=Q(me);var j=require("jtd");var ee=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function ne(n){return n.length===0?"root":`'${n.map((t,i)=>/^\d+$/.test(t)?`[${t}]`:i===0?t:`.${t}`).join("")}'`}function D(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 te(n,r){let t=n;for(let i of r)if(t&&typeof t=="object")t=t[i];else return;return t}function re(n,r,t){let{instancePath:i,schemaPath:e}=n,s=ne(i),o=e[e.length-1],a=s==="root"?"root value":`parameter ${s}`;if(o==="type"){let c=D(r,e)?.type;return c==="boolean"?`${a}: expected boolean`:c==="string"?`${a}: expected string`:c==="timestamp"?`${a}: expected timestamp string`:ee.has(c)?`${a}: expected integer`:`${a}: expected number`}if(o==="enum"){let c=D(r,e)?.enum;return`${a}: expected one of [${c.join(", ")}]`}if(e.length>=2&&e[e.length-2]==="properties"&&o)return s==="root"?`missing required parameter '${o}'`:`missing required parameter ${s}.${o}`;if(o==="elements")return`${a}: expected array`;if(o==="properties"||o==="optionalProperties"||o==="values")return`${a}: expected object`;if(o==="discriminator"){let c=D(r,e)?.discriminator;return c?`${a}: parameter '${c}' must be a string`:`${a}: expected string discriminator`}if(o==="mapping"){let f=te(t,i),u=D(r,e)?.mapping,p=u?Object.keys(u):[];return p.length>0?`${a}: unknown variant "${f}". Valid values: [${p.join(", ")}]`:`${a}: unknown variant "${f}"`}return e.length===0?`${a}: unexpected property`:`${a}: validation error at /${e.join("/")}`}function _(n,r,t){let i=t??n.definitions,e=i?{...n,definitions:i}:n;return(0,j.isValidSchema)(e)?(0,j.validate)(e,r,{maxDepth:ie,maxErrors:oe}).map(o=>re(o,e,r)):n.ref?[`unknown ref "${n.ref}" in schema`]:["invalid schema"]}var ie=32,oe=100;var T=32,C={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}},I={minimum:-34028235e31,maximum:34028235e31},R=`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'.`,V=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,B="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 w(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((s,o)=>w(s,r[o]));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 s of e)if(!(s in i)||!w(t[s],i[s]))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 P(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 A(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 s of Object.values(e.properties))i.push(s);if(e.optionalProperties)for(let s of Object.values(e.optionalProperties))i.push(s);if(e.mapping)for(let s of Object.values(e.mapping))i.push(s)}}return t}function E(n,r,t){let i=n.definitions,e=t(r,n,i,0),s=_(n,e,i);return s.length>0?{success:!1,errors:s}:{success:!0,kind:"json",data:e}}function L(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.`},s={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,s.additionalProperties=!1),{anyOf:[e,s]}}function se(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 ae(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:[`parameter 'mimeType': required concrete MIME type (e.g. "image/png", "application/pdf")`]};if(!se(t.mimeType,n))return{success:!1,errors:[`parameter '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:[`parameter '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 q(n,r,t){return n.mimeType?ae(n.mimeType,r):n.jtdSchema?E(n.jtdSchema,r,t):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function N(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 q(i[0],r,t);let e=[];for(let s of i){let o=q(s,r,t);if(o.success)return o;e.push(...o.errors)}return{success:!1,errors:[`No format matched for signal '${n.name??"unnamed"}'. Errors: ${e.join("; ")}`]}}function K(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={},s=[];for(let o of n){if(!o.name)continue;let a=i[o.name];if(a==null)continue;let f=N(o,a,t);if(f.success)f.kind==="binary"?e[o.name]={data:f.data,mimeType:f.mimeType,filename:f.filename}:f.kind==="json"&&(e[o.name]=f.data);else for(let c of f.errors)s.push(`parameter '${o.name}': ${c}`)}return s.length>0?{success:!1,errors:s}:{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 G(n){let r={},t=new Map;for(let o=0;o<n.length;o++){let a=n[o].formats;if(a)for(let f=0;f<a.length;f++){let c=a[f].jtdSchema?.definitions;if(!c||Object.keys(c).length===0)continue;let u=new Map,p=!1;for(let[l,m]of Object.entries(c)){let h=r[l];if(h&&!w(h,m)){p=!0;break}}if(p)for(let[l,m]of Object.entries(c)){let h=r[l];if(!(h&&w(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(c))r[l]||(r[l]=m);u.size>0&&t.set(`${o}:${f}`,u)}}for(let[,o]of t)for(let[,a]of o)r[a]=v(r[a],o);let i=n.map((o,a)=>{let f=o.formats;if(!f)return o;let c=!1,u=f.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;c=!0;let d=m?v(p.jtdSchema,m):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return c?{...o,formats:u}:o}),e=i.flatMap(o=>(o.formats??[]).filter(a=>a.jtdSchema).map(a=>a.jtdSchema)),s=A(e,r);for(let o of Object.keys(r))s.has(o)||delete r[o];return{mergedDefs:r,signalsCopy:i}}function $(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 F(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 ce=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(),s=typeof t.description=="string"?t.description.trim():"";e&&s?i.push(`${e}. ${s}`):e?i.push(e):s&&i.push(s);let o=[];for(let[a,f]of Object.entries(t))ce.has(a)||(typeof f=="string"&&f.trim()?o.push(`[${a}: ${f.trim()}]`):(typeof f=="number"||typeof f=="boolean")&&o.push(`[${a}: ${f}]`));if(o.length>0&&i.push(o.join(" ")),i.length>0){let a=i.join(" ");n.description=n.description?`${n.description} ${a}`:a}}function M(n,r){let t=new Set;function i(e,s){if(!(!e||typeof e!="object")){if(e.ref){if(e.nullable===!0)t.add(e.ref);else if(s){let o=r[e.ref];o&&!o.elements&&!o.values&&t.add(e.ref)}return}if(e.elements&&i(e.elements,!1),e.values&&i(e.values,!1),e.properties)for(let o of Object.values(e.properties))i(o,!1);if(e.optionalProperties)for(let o of Object.values(e.optionalProperties))i(o,!0);if(e.mapping)for(let o of Object.values(e.mapping))i(o,!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:R};if(O(n)){let e={type:g("string",n.nullable===!0),description:R};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={},s=C[n.type];if(s)e.type=g("integer",i),e.minimum=s.minimum,e.maximum=s.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=I.minimum,e.maximum=I.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={},s=[];if(n.properties)for(let[a,f]of Object.entries(n.properties))e[a]=y(f,r,t+1),s.push(a);if(n.optionalProperties)for(let[a,f]of Object.entries(n.optionalProperties)){if(f.ref){let c=r?.[f.ref];c&&(c.elements||c.values)?e[a]={$ref:`#/$defs/${f.ref}`}:e[a]={$ref:`#/$defs/${f.ref}_nullable`}}else if(f.elements||f.values)e[a]=y(f,r,t+1);else if(f.discriminator){let c=y(f,r,t+1);e[a]={...c,anyOf:[...c.anyOf??[],{type:"null"}]}}else e[a]=$(y(f,r,t+1));s.push(a)}let o={type:g("object",i),properties:e,additionalProperties:!1,required:s};return b(o,n),o}if(n.values){let e=y(n.values,r,t+1),s={type:g("array",i),description:B,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:e},required:["key","value"],additionalProperties:!1}};return b(s,n),s}if(n.discriminator&&n.mapping){let e=n.discriminator,s=n.mapping,o=[];for(let[f,c]of Object.entries(s)){let u;O(c)?u={type:"object",properties:{[e]:{type:"string",enum:[f]},_data:{type:"string",description:V}},required:[e,"_data"],additionalProperties:!1}:(u=y(c,r,t+1),u.properties[e]={type:"string",enum:[f]},(u.required??=[]).unshift(e),u.additionalProperties=!1),b(u,c),o.push(u)}i&&o.push({type:"null"});let a={anyOf:o};return b(a,n),a}return{type:"string",description:R}}var fe=new Set(Object.keys(C));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&&fe.has(r.type))return typeof n=="number"&&!Number.isInteger(n)?Math.round(n):n;if(r.elements){let e=r.elements;return(Array.isArray(n)?n:[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[s,o]of Object.entries(r.properties))s in e&&(e[s]=J(e[s],o,t,i+1));if(r.optionalProperties)for(let[s,o]of Object.entries(r.optionalProperties))s in e&&(e[s]===null||Array.isArray(e[s])&&e[s].length===0&&(o.elements||o.values||o.ref)?delete e[s]:e[s]=J(e[s],o,t,i+1));return e}if(r.values&&Array.isArray(n)){let e={};for(let s of n)if(typeof s=="object"&&s!==null){let o=s,a=o.key;e[a]=J(o.value,r.values,t,i+1)}return e}if(r.discriminator&&r.mapping&&typeof n=="object"&&n!==null){let e=n,s=r.discriminator,o=e[s];if(typeof o!="string")return n;let a=r.mapping[o];if(!a)return n;if(O(a)){let c=e._data;if(typeof c=="string")try{let u=JSON.parse(c);if(typeof u=="object"&&u!==null)return{[s]:o,...u}}catch{}return{[s]:o}}let f={[s]:o};if(a.properties)for(let[c,u]of Object.entries(a.properties))c in e&&(f[c]=J(e[c],u,t,i+1));if(a.optionalProperties)for(let[c,u]of Object.entries(a.optionalProperties))!(c in e)||e[c]===null||Array.isArray(e[c])&&e[c].length===0&&(u.elements||u.values||u.ref)||(f[c]=J(e[c],u,t,i+1));return f}return n}function ue(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 k=class n{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(r){let t=r.definitions,i=!P(r,t),e;if(i){if(e=y(r,t,0),t&&Object.keys(t).length>0){let s=A([r],t),o=M([r],t),a=[];for(let[c,u]of Object.entries(t)){if(!s.has(c))continue;let p=y(u,t,0);a.push([c,p]),o.has(c)&&a.push([`${c}_nullable`,$(p)])}e.$defs=Object.fromEntries(a);let f=F(e);for(let c of Object.keys(e.$defs))f.has(c)||delete e.$defs[c]}if(r.discriminator&&r.mapping){let s=e;e={type:"object",properties:{input:s},required:["input"],additionalProperties:!1},s.$defs&&(e.$defs=s.$defs,delete s.$defs)}}else e={},b(e,r);return new n(e,i,r,void 0)}static fromSignal(r,t,i){let{schema:e,strict:s}=pe(r,t);if(i){let o=L(e,r.description);return new n(o,s,void 0,r)}return new n(e,s,void 0,r)}static fromSignals(r,t,i,e){let{mergedDefs:s,signalsCopy:o}=G(r),a={},f=[],c=!0;for(let p of o){if(!p.name)continue;let l=n.fromSignal(p,s,e);if(l.strict||(c=!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]=$(l.schema);else a[p.name]=l.schema;f.push(p.name)}let u={type:"object",properties:a,required:f,additionalProperties:!1};if(Object.keys(s).length>0){let p=o.flatMap(d=>(d.formats??[]).filter(S=>S.jtdSchema).map(S=>S.jtdSchema)),l=M(p,s);for(let d of o)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(s)){let U=y(S,s,0);m.push([d,U]),l.has(d)&&m.push([`${d}_nullable`,$(U)])}u.$defs=Object.fromEntries(m);let h=F(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,c,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,s,o){this.schema=r,this.strict=t,this.jtdSchema=i,this.descriptor=e,this.descriptors=s,this.referenceMode=o??!1}convert=r=>{if(this.descriptors)return K(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),E(this.jtdSchema,t,J)}return this.descriptor?N(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 pe(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:f,strict:c}=H(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(". ");f.description=f.description?`${p} ${f.description}`:p}return{schema:f,strict:c}}let i=[],e=!0;for(let a of t){let{schema:f,strict:c}=H(a,r);c||(e=!1),a.jtdSchema&&(f.description=f.description?`Structured JSON data. ${f.description}`:"Structured JSON data."),i.push(f)}let s=n.description??"";return{schema:{anyOf:i,description:s||void 0},strict:e}}function H(n,r){if(n.jtdSchema){let t=r??n.jtdSchema.definitions,i=!P(n.jtdSchema,t),e;if(i){if(e=y(n.jtdSchema,t,0),!r&&t&&Object.keys(t).length>0){let s=A([n.jtdSchema],t),o=M([n.jtdSchema],t),a=[];for(let[c,u]of Object.entries(t)){if(!s.has(c))continue;let p=y(u,t,0);a.push([c,p]),o.has(c)&&a.push([`${c}_nullable`,$(p)])}e.$defs=Object.fromEntries(a);let f=F(e);for(let c of Object.keys(e.$defs))f.has(c)||delete e.$defs[c]}}else e={},b(e,n.jtdSchema);return{schema:e,strict:i}}if(n.mimeType)return{schema:ue(n.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function le(n){return k.fromJTD(n)}function de(n){return k.fromSignal(n)}0&&(module.exports={OpenAIConversion,convertSignalToOpenAISchema,convertToOpenAISchema});
|
|
1
|
+
"use strict";var x=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(n,r)=>{for(var t in r)x(n,t,{get:r[t],enumerable:!0})},Z=(n,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of Y(r))!z.call(n,e)&&e!==t&&x(n,e,{get:()=>r[e],enumerable:!(i=X(r,e))||i.enumerable});return n};var Q=n=>Z(x({},"__esModule",{value:!0}),n);var me={};W(me,{OpenAIConversion:()=>k,convertSignalToOpenAISchema:()=>de,convertToOpenAISchema:()=>le});module.exports=Q(me);var j=require("jtd");var ee=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function ne(n){return n.length===0?"root":`'${n.map((t,i)=>/^\d+$/.test(t)?`[${t}]`:i===0?t:`.${t}`).join("")}'`}function D(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 te(n,r){let t=n;for(let i of r)if(t&&typeof t=="object")t=t[i];else return;return t}function re(n,r,t){let{instancePath:i,schemaPath:e}=n,s=ne(i),o=e[e.length-1],a=s==="root"?"root value":`parameter ${s}`;if(o==="type"){let c=D(r,e)?.type;return c==="boolean"?`${a}: expected boolean`:c==="string"?`${a}: expected string`:c==="timestamp"?`${a}: expected timestamp string`:ee.has(c)?`${a}: expected integer`:`${a}: expected number`}if(o==="enum"){let c=D(r,e)?.enum;return`${a}: expected one of [${c.join(", ")}]`}if(e.length>=2&&e[e.length-2]==="properties"&&o)return s==="root"?`missing required parameter '${o}'`:`missing required parameter ${s}.${o}`;if(o==="elements")return`${a}: expected array`;if(o==="properties"||o==="optionalProperties"||o==="values")return`${a}: expected object`;if(o==="discriminator"){let c=D(r,e)?.discriminator;return c?`${a}: parameter '${c}' must be a string`:`${a}: expected string discriminator`}if(o==="mapping"){let f=te(t,i),u=D(r,e)?.mapping,p=u?Object.keys(u):[];return p.length>0?`${a}: unknown variant "${f}". Valid values: [${p.join(", ")}]`:`${a}: unknown variant "${f}"`}return e.length===0?`${a}: unexpected property`:`${a}: validation error at /${e.join("/")}`}function U(n,r,t){let i=t??n.definitions,e=i?{...n,definitions:i}:n;return(0,j.isValidSchema)(e)?(0,j.validate)(e,r,{maxDepth:ie,maxErrors:oe}).map(o=>re(o,e,r)):n.ref?[`unknown ref "${n.ref}" in schema`]:["invalid schema"]}var ie=32,oe=100;var T=32,C={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}},I={minimum:-34028235e31,maximum:34028235e31},R=`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'.`,V=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,B="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 w(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((s,o)=>w(s,r[o]));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 s of e)if(!(s in i)||!w(t[s],i[s]))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 P(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 A(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 s of Object.values(e.properties))i.push(s);if(e.optionalProperties)for(let s of Object.values(e.optionalProperties))i.push(s);if(e.mapping)for(let s of Object.values(e.mapping))i.push(s)}}return t}function E(n,r,t){let i=n.definitions,e=t(r,n,i,0),s=U(n,e,i);return s.length>0?{success:!1,errors:s}:{success:!0,kind:"json",data:e}}function L(n,r,t){let i=r?`${r}. `:"",e={type:"object",properties:{_kind:{type:"string",enum:["_value"]},_value:n},required:["_kind","_value"],description:`${i}Inline value.`},s={type:"object",properties:{_kind:{type:"string",enum:["_ref"]},_ref:{type:"string",description:"A file reference. The referenced file MUST contain data that conforms to the same format described in the inline value variant."}},required:["_kind","_ref"],description:`${i}Reference to a file instead of inline data.`};return t!==!1&&(e.additionalProperties=!1,s.additionalProperties=!1),{anyOf:[e,s]}}function se(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 ae(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:[`parameter 'mimeType': required concrete MIME type (e.g. "image/png", "application/pdf")`]};if(!se(t.mimeType,n))return{success:!1,errors:[`parameter '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:[`parameter '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 q(n,r,t){return n.mimeType?ae(n.mimeType,r):n.jtdSchema?E(n.jtdSchema,r,t):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function N(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 q(i[0],r,t);let e=[];for(let s of i){let o=q(s,r,t);if(o.success)return o;e.push(...o.errors)}return{success:!1,errors:[`No format matched for signal '${n.name??"unnamed"}'. Errors: ${e.join("; ")}`]}}function K(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={},s=[];for(let o of n){if(!o.name)continue;let a=i[o.name];if(a==null)continue;let f=N(o,a,t);if(f.success)f.kind==="binary"?e[o.name]={data:f.data,mimeType:f.mimeType,filename:f.filename}:f.kind==="json"&&(e[o.name]=f.data);else for(let c of f.errors)s.push(`parameter '${o.name}': ${c}`)}return s.length>0?{success:!1,errors:s}:{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 G(n){let r={},t=new Map;for(let o=0;o<n.length;o++){let a=n[o].formats;if(a)for(let f=0;f<a.length;f++){let c=a[f].jtdSchema?.definitions;if(!c||Object.keys(c).length===0)continue;let u=new Map,p=!1;for(let[l,m]of Object.entries(c)){let h=r[l];if(h&&!w(h,m)){p=!0;break}}if(p)for(let[l,m]of Object.entries(c)){let h=r[l];if(!(h&&w(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(c))r[l]||(r[l]=m);u.size>0&&t.set(`${o}:${f}`,u)}}for(let[,o]of t)for(let[,a]of o)r[a]=v(r[a],o);let i=n.map((o,a)=>{let f=o.formats;if(!f)return o;let c=!1,u=f.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;c=!0;let d=m?v(p.jtdSchema,m):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return c?{...o,formats:u}:o}),e=i.flatMap(o=>(o.formats??[]).filter(a=>a.jtdSchema).map(a=>a.jtdSchema)),s=A(e,r);for(let o of Object.keys(r))s.has(o)||delete r[o];return{mergedDefs:r,signalsCopy:i}}function $(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 F(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 ce=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(),s=typeof t.description=="string"?t.description.trim():"";e&&s?i.push(`${e}. ${s}`):e?i.push(e):s&&i.push(s);let o=[];for(let[a,f]of Object.entries(t))ce.has(a)||(typeof f=="string"&&f.trim()?o.push(`[${a}: ${f.trim()}]`):(typeof f=="number"||typeof f=="boolean")&&o.push(`[${a}: ${f}]`));if(o.length>0&&i.push(o.join(" ")),i.length>0){let a=i.join(" ");n.description=n.description?`${n.description} ${a}`:a}}function _(n,r){let t=new Set;function i(e,s){if(!(!e||typeof e!="object")){if(e.ref){if(e.nullable===!0)t.add(e.ref);else if(s){let o=r[e.ref];o&&!o.elements&&!o.values&&t.add(e.ref)}return}if(e.elements&&i(e.elements,!1),e.values&&i(e.values,!1),e.properties)for(let o of Object.values(e.properties))i(o,!1);if(e.optionalProperties)for(let o of Object.values(e.optionalProperties))i(o,!0);if(e.mapping)for(let o of Object.values(e.mapping))i(o,!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:R};if(O(n)){let e={type:g("string",n.nullable===!0),description:R};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={},s=C[n.type];if(s)e.type=g("integer",i),e.minimum=s.minimum,e.maximum=s.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=I.minimum,e.maximum=I.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={},s=[];if(n.properties)for(let[a,f]of Object.entries(n.properties))e[a]=y(f,r,t+1),s.push(a);if(n.optionalProperties)for(let[a,f]of Object.entries(n.optionalProperties)){if(f.ref){let c=r?.[f.ref];c&&(c.elements||c.values)?e[a]={$ref:`#/$defs/${f.ref}`}:e[a]={$ref:`#/$defs/${f.ref}_nullable`}}else if(f.elements||f.values)e[a]=y(f,r,t+1);else if(f.discriminator){let c=y(f,r,t+1);e[a]={...c,anyOf:[...c.anyOf??[],{type:"null"}]}}else e[a]=$(y(f,r,t+1));s.push(a)}let o={type:g("object",i),properties:e,additionalProperties:!1,required:s};return b(o,n),o}if(n.values){let e=y(n.values,r,t+1),s={type:g("array",i),description:B,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:e},required:["key","value"],additionalProperties:!1}};return b(s,n),s}if(n.discriminator&&n.mapping){let e=n.discriminator,s=n.mapping,o=[];for(let[f,c]of Object.entries(s)){let u;O(c)?u={type:"object",properties:{[e]:{type:"string",enum:[f]},_data:{type:"string",description:V}},required:[e,"_data"],additionalProperties:!1}:(u=y(c,r,t+1),u.properties[e]={type:"string",enum:[f]},(u.required??=[]).unshift(e),u.additionalProperties=!1),b(u,c),o.push(u)}i&&o.push({type:"null"});let a={anyOf:o};return b(a,n),a}return{type:"string",description:R}}var fe=new Set(Object.keys(C));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&&fe.has(r.type))return typeof n=="number"&&!Number.isInteger(n)?Math.round(n):n;if(r.elements){let e=r.elements;return(Array.isArray(n)?n:[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[s,o]of Object.entries(r.properties))s in e&&(e[s]=J(e[s],o,t,i+1));if(r.optionalProperties)for(let[s,o]of Object.entries(r.optionalProperties))s in e&&(e[s]===null||Array.isArray(e[s])&&e[s].length===0&&(o.elements||o.values||o.ref)?delete e[s]:e[s]=J(e[s],o,t,i+1));return e}if(r.values&&Array.isArray(n)){let e={};for(let s of n)if(typeof s=="object"&&s!==null){let o=s,a=o.key;e[a]=J(o.value,r.values,t,i+1)}return e}if(r.discriminator&&r.mapping&&typeof n=="object"&&n!==null){let e=n,s=r.discriminator,o=e[s];if(typeof o!="string")return n;let a=r.mapping[o];if(!a)return n;if(O(a)){let c=e._data;if(typeof c=="string")try{let u=JSON.parse(c);if(typeof u=="object"&&u!==null)return{[s]:o,...u}}catch{}return{[s]:o}}let f={[s]:o};if(a.properties)for(let[c,u]of Object.entries(a.properties))c in e&&(f[c]=J(e[c],u,t,i+1));if(a.optionalProperties)for(let[c,u]of Object.entries(a.optionalProperties))!(c in e)||e[c]===null||Array.isArray(e[c])&&e[c].length===0&&(u.elements||u.values||u.ref)||(f[c]=J(e[c],u,t,i+1));return f}return n}function ue(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 k=class n{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(r){let t=r.definitions,i=!P(r,t),e;if(i){if(e=y(r,t,0),t&&Object.keys(t).length>0){let s=A([r],t),o=_([r],t),a=[];for(let[c,u]of Object.entries(t)){if(!s.has(c))continue;let p=y(u,t,0);a.push([c,p]),o.has(c)&&a.push([`${c}_nullable`,$(p)])}e.$defs=Object.fromEntries(a);let f=F(e);for(let c of Object.keys(e.$defs))f.has(c)||delete e.$defs[c]}if(r.discriminator&&r.mapping){let s=e;e={type:"object",properties:{input:s},required:["input"],additionalProperties:!1},s.$defs&&(e.$defs=s.$defs,delete s.$defs)}}else e={},b(e,r);return new n(e,i,r,void 0)}static fromSignal(r,t,i){let{schema:e,strict:s}=pe(r,t);if(i){let o=L(e,r.description);return new n(o,s,void 0,r)}return new n(e,s,void 0,r)}static fromSignals(r,t,i,e){let{mergedDefs:s,signalsCopy:o}=G(r),a={},f=[],c=!0;for(let p of o){if(!p.name)continue;let l=n.fromSignal(p,s,e);if(l.strict||(c=!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]=$(l.schema);else a[p.name]=l.schema;f.push(p.name)}let u={type:"object",properties:a,required:f,additionalProperties:!1};if(Object.keys(s).length>0){let p=o.flatMap(d=>(d.formats??[]).filter(S=>S.jtdSchema).map(S=>S.jtdSchema)),l=_(p,s);for(let d of o)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(s)){let M=y(S,s,0);m.push([d,M]),l.has(d)&&m.push([`${d}_nullable`,$(M)])}u.$defs=Object.fromEntries(m);let h=F(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,c,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,s,o){this.schema=r,this.strict=t,this.jtdSchema=i,this.descriptor=e,this.descriptors=s,this.referenceMode=o??!1}convert=r=>{if(this.descriptors)return K(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),E(this.jtdSchema,t,J)}return this.descriptor?N(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 pe(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:f,strict:c}=H(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(". ");f.description=f.description?`${p} ${f.description}`:p}return{schema:f,strict:c}}let i=[],e=!0;for(let a of t){let{schema:f,strict:c}=H(a,r);c||(e=!1),a.jtdSchema&&(f.description=f.description?`Structured JSON data. ${f.description}`:"Structured JSON data."),i.push(f)}let s=n.description??"";return{schema:{anyOf:i,description:s||void 0},strict:e}}function H(n,r){if(n.jtdSchema){let t=r??n.jtdSchema.definitions,i=!P(n.jtdSchema,t),e;if(i){if(e=y(n.jtdSchema,t,0),!r&&t&&Object.keys(t).length>0){let s=A([n.jtdSchema],t),o=_([n.jtdSchema],t),a=[];for(let[c,u]of Object.entries(t)){if(!s.has(c))continue;let p=y(u,t,0);a.push([c,p]),o.has(c)&&a.push([`${c}_nullable`,$(p)])}e.$defs=Object.fromEntries(a);let f=F(e);for(let c of Object.keys(e.$defs))f.has(c)||delete e.$defs[c]}}else e={},b(e,n.jtdSchema);return{schema:e,strict:i}}if(n.mimeType)return{schema:ue(n.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function le(n){return k.fromJTD(n)}function de(n){return k.fromSignal(n)}0&&(module.exports={OpenAIConversion,convertSignalToOpenAISchema,convertToOpenAISchema});
|
|
2
2
|
//# sourceMappingURL=openai.cjs.map
|