@mochabug/adapt-sdk 0.4.6 → 0.4.7
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/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.d.ts.map +1 -1
- package/dist/cjs/anthropic.cjs +1 -1
- package/dist/cjs/anthropic.cjs.map +3 -3
- package/dist/cjs/gemini.cjs +1 -1
- package/dist/cjs/gemini.cjs.map +3 -3
- package/dist/cjs/openai.cjs +1 -1
- package/dist/cjs/openai.cjs.map +2 -2
- package/dist/esm/anthropic.mjs +1 -1
- package/dist/esm/anthropic.mjs.map +3 -3
- package/dist/esm/gemini.mjs +1 -1
- package/dist/esm/gemini.mjs.map +3 -3
- package/dist/esm/openai.mjs +1 -1
- package/dist/esm/openai.mjs.map +2 -2
- package/dist/gemini/index.d.ts +2 -2
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/schema-utils.d.ts +6 -0
- 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 j=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var r in t)j(e,r,{get:t[r],enumerable:!0})},L=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of B(t))!V.call(e,n)&&n!==r&&j(e,n,{get:()=>t[n],enumerable:!(i=q(t,n))||i.enumerable});return e};var G=e=>L(j({},"__esModule",{value:!0}),e);var se={};K(se,{OpenAIConversion:()=>T,convertSignalToOpenAISchema:()=>oe,convertToOpenAISchema:()=>ie});module.exports=G(se);var O=require("jtd");var Y=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function H(e){return e.length===0?"/":e.map(t=>/^\d+$/.test(t)?`[${t}]`:`.${t}`).join("")}function E(e,t){let r=e;for(let i=0;i<t.length-1&&(r&&typeof r=="object"&&t[i]in r);i++)r=r[t[i]];return r}function X(e,t){let r=e;for(let i of t)if(r&&typeof r=="object")r=r[i];else return;return r}function z(e,t,r){let{instancePath:i,schemaPath:n}=e,o=H(i),s=n[n.length-1];if(s==="type"){let u=E(t,n)?.type;return u==="boolean"?`${o}: expected boolean`:u==="string"?`${o}: expected string`:u==="timestamp"?`${o}: expected timestamp string`:Y.has(u)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let u=E(t,n)?.enum;return`${o}: expected one of [${u.join(", ")}]`}if(n.length>=2&&n[n.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=X(r,i);return`${o}: unknown variant "${a}"`}return n.length===0?`${o}: unexpected property`:`${o}: validation error at /${n.join("/")}`}function I(e,t,r){let i=r??e.definitions,n=i?{...e,definitions:i}:e;return(0,O.isValidSchema)(n)?(0,O.validate)(n,t,{maxDepth:Z,maxErrors:W}).map(s=>z(s,n,t)):e.ref?[`/: unknown ref "${e.ref}"`]:["/: invalid schema"]}var Z=32,W=100;var h=32,w={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},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'.`,N=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,F="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 m(e,t){return t?[e,"null"]:e}function D(e,t){if(e===t)return!0;if(e==null||t==null||typeof e!=typeof t||typeof e!="object")return!1;if(Array.isArray(e))return Array.isArray(t)&&e.length===t.length&&e.every((o,s)=>D(o,t[s]));if(Array.isArray(t))return!1;let r=e,i=t,n=Object.keys(r);if(n.length!==Object.keys(i).length)return!1;for(let o of n)if(!(o in i)||!D(r[o],i[o]))return!1;return!0}function b(e){return!e||typeof e!="object"?!0:e.type||e.enum||e.elements||e.values||e.discriminator||e.ref||e.properties&&Object.keys(e.properties).length>0||e.optionalProperties&&Object.keys(e.optionalProperties).length>0?!1:(e.properties!==void 0||e.optionalProperties!==void 0)&&e.additionalProperties===!0?!0:!(e.properties!==void 0||e.optionalProperties!==void 0)}function R(e,t){let r=e,i=0;for(;r.ref&&i<h;){let n=t?.[r.ref];if(!n)return!1;r=n,i++}return i>=h?!1:b(r)}function $(e,t,r){let i=e.definitions,n=r(t,e,i,0),o=I(e,n,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:n}}function Q(e,t){if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with 'data' field for MIME type ${e}, got ${typeof t}`]};let r=t;if(typeof r.data!="string")return{success:!1,errors:[`/data: expected base64 string for MIME type ${e}, got ${typeof r.data}`]};let i={success:!0,kind:"binary",mimeType:e,data:new Uint8Array(Buffer.from(r.data,"base64"))};return typeof r.filename=="string"&&r.filename&&(i.filename=r.filename),i}function P(e,t,r){return e.mimeType?Q(e.mimeType,t):e.jtdSchema?$(e.jtdSchema,t,r):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function x(e,t,r){let i=e.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return P(i[0],t,r);if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with format properties, got ${typeof t}`]};let n=t;for(let s=0;s<i.length;s++){let a=`format_${s}`;if(n[a]!==void 0&&n[a]!==null)return P(i[s],n[a],r)}if(e.optional)return{success:!0,kind:"json",data:null};let o=i.map((s,a)=>`format_${a}`);return{success:!1,errors:[`Signal '${e.name??"unnamed"}' is required but no format was provided. Set exactly one of: ${o.join(", ")}.`]}}function U(e,t,r){if(typeof t!="object"||t===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof t]};let i=t,n={},o=[];for(let s of e){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let u=x(s,a,r);if(u.success)u.kind==="binary"?n[s.name]={data:u.data,mimeType:u.mimeType,filename:u.filename}:u.kind==="json"&&(n[s.name]=u.data);else for(let p of u.errors)o.push(`/${s.name}${p.startsWith("/")?"":": "}${p}`)}return o.length>0?{success:!1,errors:o}:{success:!0,kind:"signals",signals:n}}function J(e,t){if(!e||typeof e!="object")return e;let r={...e};return r.ref&&t.has(r.ref)&&(r.ref=t.get(r.ref)),r.properties&&(r.properties=Object.fromEntries(Object.entries(r.properties).map(([i,n])=>[i,J(n,t)]))),r.optionalProperties&&(r.optionalProperties=Object.fromEntries(Object.entries(r.optionalProperties).map(([i,n])=>[i,J(n,t)]))),r.elements&&(r.elements=J(r.elements,t)),r.values&&(r.values=J(r.values,t)),r.mapping&&(r.mapping=Object.fromEntries(Object.entries(r.mapping).map(([i,n])=>[i,J(n,t)]))),r.definitions&&delete r.definitions,r}function M(e){let t={},r=new Map;for(let n=0;n<e.length;n++){let o=e[n].formats;if(o)for(let s=0;s<o.length;s++){let a=o[s].jtdSchema?.definitions;if(!a||Object.keys(a).length===0)continue;let u=new Map,p=!1;for(let[c,f]of Object.entries(a)){let l=t[c];if(l&&!D(l,f)){p=!0;break}}if(p)for(let[c,f]of Object.entries(a)){let l=t[c];if(!(l&&D(l,f)))if(l){let d=1;for(;t[`${c}_${d}`];)d++;let v=`${c}_${d}`;u.set(c,v),t[v]=f}else t[c]=f}else for(let[c,f]of Object.entries(a))t[c]||(t[c]=f);u.size>0&&r.set(`${n}:${s}`,u)}}for(let[,n]of r)for(let[,o]of n)t[o]=J(t[o],n);let i=e.map((n,o)=>{let s=n.formats;if(!s)return n;let a=!1,u=s.map((p,c)=>{if(!p.jtdSchema)return p;let f=r.get(`${o}:${c}`),l=p.jtdSchema.definitions&&Object.keys(p.jtdSchema.definitions).length>0;if(!f&&!l)return p;a=!0;let d=f?J(p.jtdSchema,f):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return a?{...n,formats:u}:n});return{mergedDefs:t,signalsCopy:i}}function C(e){if(Array.isArray(e.type)&&e.type.includes("null")||e.type==="null")return e;if(e.$ref)return{anyOf:[e,{type:"null"}]};if(e.anyOf)return e.anyOf.some(r=>r.type==="null")?e:{...e,anyOf:[...e.anyOf,{type:"null"}]};let t={...e};return t.type&&(t.type=Array.isArray(t.type)?[...t.type,"null"]:[t.type,"null"]),t.enum&&!t.enum.includes(null)&&(t.enum=[...t.enum,null]),t}var ee=new Set(["title","label","name","description"]);function g(e,t){let r=t.metadata;if(!r)return;let i=[],n=typeof r.title=="string"&&r.title.trim()||typeof r.label=="string"&&r.label.trim()||typeof r.name=="string"&&r.name.trim(),o=typeof r.description=="string"?r.description.trim():"";n&&o?i.push(`${n}. ${o}`):n?i.push(n):o&&i.push(o);let s=[];for(let[a,u]of Object.entries(r))ee.has(a)||(typeof u=="string"&&u.trim()?s.push(`[${a}: ${u.trim()}]`):(typeof u=="number"||typeof u=="boolean")&&s.push(`[${a}: ${u}]`));if(s.length>0&&i.push(s.join(" ")),i.length>0){let a=i.join(" ");e.description=e.description?`${e.description} ${a}`:a}}function S(e,t,r){if(r>h)throw new Error(`Schema nesting exceeded ${h} levels`);if(!e||typeof e!="object")return{type:"string",description:k};if(b(e)){let n={type:m("string",e.nullable===!0),description:k};return g(n,e),n}let i=e.nullable===!0;if(e.ref){let n=e.ref;if(!t?.[n])throw new Error(`Unresolved ref "${n}"`);if(i){let o={anyOf:[{$ref:`#/$defs/${n}`},{type:"null"}]};return g(o,e),o}return{$ref:`#/$defs/${n}`}}if(e.type){let n={},o=w[e.type];if(o)n.type=m("integer",i),n.minimum=o.minimum,n.maximum=o.maximum;else switch(e.type){case"boolean":n.type=m("boolean",i);break;case"string":n.type=m("string",i);break;case"timestamp":n.type=m("string",i),n.format="date-time";break;case"float32":n.type=m("number",i),n.minimum=A.minimum,n.maximum=A.maximum;break;case"float64":n.type=m("number",i);break;default:throw new Error(`Unknown JTD type "${e.type}"`)}return g(n,e),n}if(e.enum){let n={type:m("string",i),enum:i?[...e.enum,null]:e.enum};return g(n,e),n}if(e.elements){let n={type:m("array",i),items:S(e.elements,t,r+1)};return g(n,e),n}if(e.properties||e.optionalProperties){let n={},o=[];if(e.properties)for(let[a,u]of Object.entries(e.properties))n[a]=S(u,t,r+1),o.push(a);if(e.optionalProperties)for(let[a,u]of Object.entries(e.optionalProperties)){let p=S(u,t,r+1),c=C(p);c.description=c.description?`(Optional, null if not provided) ${c.description}`:"Optional. Set to null if not provided.",n[a]=c,o.push(a)}let s={type:m("object",i),properties:n,additionalProperties:!1,required:o};return g(s,e),s}if(e.values){let n=S(e.values,t,r+1),o={type:m("array",i),description:F,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:n},required:["key","value"],additionalProperties:!1}};return g(o,e),o}if(e.discriminator&&e.mapping){let n=e.discriminator,o=e.mapping,s=[];for(let[u,p]of Object.entries(o)){let c;b(p)?c={type:"object",properties:{[n]:{type:"string",enum:[u]},_data:{type:"string",description:N}},required:[n,"_data"],additionalProperties:!1}:(c=S(p,t,r+1),c.properties[n]={type:"string",enum:[u]},(c.required??=[]).unshift(n),c.additionalProperties=!1),g(c,p),s.push(c)}i&&s.push({type:"null"});let a={anyOf:s};return g(a,e),a}return{type:"string",description:k}}var te=new Set(Object.keys(w));function y(e,t,r,i){if(i>h||e==null)return e;if(t.ref){let n=r?.[t.ref];return n?y(e,n,r,i+1):e}if(b(t)){if(typeof e=="string")try{return JSON.parse(e)}catch{return e}return e}if(t.type&&te.has(t.type))return typeof e=="number"&&!Number.isInteger(e)?Math.round(e):e;if(t.elements&&Array.isArray(e)){let n=t.elements;return e.map(o=>y(o,n,r,i+1))}if((t.properties||t.optionalProperties)&&typeof e=="object"&&e!==null){let n={...e};if(t.properties)for(let[o,s]of Object.entries(t.properties))o in n&&(n[o]=y(n[o],s,r,i+1));if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))o in n&&(n[o]===null?delete n[o]:n[o]=y(n[o],s,r,i+1));return n}if(t.values&&Array.isArray(e)){let n={};for(let o of e)if(typeof o=="object"&&o!==null){let s=o,a=s.key;n[a]=y(s.value,t.values,r,i+1)}return n}if(t.discriminator&&t.mapping&&typeof e=="object"&&e!==null){let n=e,o=t.discriminator,s=n[o];if(typeof s!="string")return e;let a=t.mapping[s];if(!a)return e;if(b(a)){let p=n._data;if(typeof p=="string")try{let c=JSON.parse(p);if(typeof c=="object"&&c!==null)return{[o]:s,...c}}catch{}return{[o]:s}}let u={[o]:s};if(a.properties)for(let[p,c]of Object.entries(a.properties))p in n&&(u[p]=y(n[p],c,r,i+1));if(a.optionalProperties)for(let[p,c]of Object.entries(a.optionalProperties))p in n&&n[p]!==null&&(u[p]=y(n[p],c,r,i+1));return u}return e}function ne(e){return{type:"object",description:`Binary content (MIME type: ${e}).`,properties:{data:{type:"string",description:`The base64-encoded binary content (MIME type: ${e}).`},filename:{type:["string","null"],description:'Optional filename for the binary content (e.g. "report.pdf"). Set to null if not applicable.'}},required:["data","filename"],additionalProperties:!1}}var T=class e{schema;strict;jtdSchema;descriptor;descriptors;static fromJTD(t){let r=t.definitions,i=!R(t,r),n;return i?(n=S(t,r,0),r&&Object.keys(r).length>0&&(n.$defs=Object.fromEntries(Object.entries(r).map(([o,s])=>[o,S(s,r,0)])))):(n={},g(n,t)),new e(n,i,t,void 0)}static fromSignal(t,r){let{schema:i,strict:n}=re(t,r);return new e(i,n,void 0,t)}static fromSignals(t,r,i){let{mergedDefs:n,signalsCopy:o}=M(t),s={},a=[],u=!0;for(let c of o){if(!c.name)continue;let f=e.fromSignal(c,n);f.strict||(u=!1),c.optional?s[c.name]=C(f.schema):s[c.name]=f.schema,a.push(c.name)}let p={type:"object",properties:s,required:a,additionalProperties:!1};return Object.keys(n).length>0&&(p.$defs=Object.fromEntries(Object.entries(n).map(([c,f])=>[c,S(f,n,0)]))),i&&r?p.description=`${i}. ${r}`:i?p.description=i:r&&(p.description=r),new e(p,u,void 0,void 0,t)}static fromJSON(t){let r=typeof t=="string"?JSON.parse(t):t;if(r.descriptors)return e.fromSignals(r.descriptors,r.description,r.label);if(r.descriptor)return e.fromSignal(r.descriptor);if(r.jtdSchema)return e.fromJTD(r.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(t,r,i,n,o){this.schema=t,this.strict=r,this.jtdSchema=i,this.descriptor=n,this.descriptors=o}convert=t=>this.descriptors?U(this.descriptors,t,y):this.jtdSchema?$(this.jtdSchema,t,y):this.descriptor?x(this.descriptor,t,y):{success:!1,errors:["No source schema available"]};toJSON(){if(this.descriptors){let t={schema:this.schema,strict:this.strict,descriptors:this.descriptors};return this.schema.description&&(t.description=this.schema.description),t}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function re(e,t){let r=e.formats;if(!r||r.length===0)throw new Error("Signal descriptor has no formats");if(r.length===1){let[c]=r,{schema:f,strict:l}=_(c,t);if(e.label||e.description){let d=[];e.label&&d.push(e.label),e.description&&d.push(e.description);let v=d.join(". ");f.description=f.description?`${v} ${f.description}`:v}return{schema:f,strict:l}}let i={},n=[];for(let[c,f]of r.entries()){let{schema:l}=_(f,t),d=`format_${c}`;f.jtdSchema&&(l.description=l.description?`Structured JSON data. ${l.description}`:"Structured JSON data."),i[d]=C(l),n.push(d)}let o=Object.keys(i),s=e.description??"",u=e.optional===!0?"Provide at most ONE of the following format properties, set all others to null.":`You MUST provide exactly ONE of the following format properties: ${o.join(", ")}. Set all others to null.`;return{schema:{type:"object",description:s?`${s} ${u}`:u,properties:i,required:n,additionalProperties:!1},strict:!0}}function _(e,t){if(e.jtdSchema){let r=t??e.jtdSchema.definitions,i=!R(e.jtdSchema,r),n;return i?(n=S(e.jtdSchema,r,0),!t&&r&&Object.keys(r).length>0&&(n.$defs=Object.fromEntries(Object.entries(r).map(([o,s])=>[o,S(s,r,0)])))):(n={},g(n,e.jtdSchema)),{schema:n,strict:i}}if(e.mimeType)return{schema:ne(e.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function ie(e){return T.fromJTD(e)}function oe(e){return T.fromSignal(e)}0&&(module.exports={OpenAIConversion,convertSignalToOpenAISchema,convertToOpenAISchema});
|
|
1
|
+
"use strict";var j=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var K=(e,t)=>{for(var r in t)j(e,r,{get:t[r],enumerable:!0})},L=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of B(t))!V.call(e,n)&&n!==r&&j(e,n,{get:()=>t[n],enumerable:!(i=q(t,n))||i.enumerable});return e};var G=e=>L(j({},"__esModule",{value:!0}),e);var se={};K(se,{OpenAIConversion:()=>T,convertSignalToOpenAISchema:()=>oe,convertToOpenAISchema:()=>ie});module.exports=G(se);var O=require("jtd");var Y=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function H(e){return e.length===0?"/":e.map(t=>/^\d+$/.test(t)?`[${t}]`:`.${t}`).join("")}function E(e,t){let r=e;for(let i=0;i<t.length-1&&(r&&typeof r=="object"&&t[i]in r);i++)r=r[t[i]];return r}function X(e,t){let r=e;for(let i of t)if(r&&typeof r=="object")r=r[i];else return;return r}function z(e,t,r){let{instancePath:i,schemaPath:n}=e,o=H(i),s=n[n.length-1];if(s==="type"){let u=E(t,n)?.type;return u==="boolean"?`${o}: expected boolean`:u==="string"?`${o}: expected string`:u==="timestamp"?`${o}: expected timestamp string`:Y.has(u)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let u=E(t,n)?.enum;return`${o}: expected one of [${u.join(", ")}]`}if(n.length>=2&&n[n.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=X(r,i);return`${o}: unknown variant "${a}"`}return n.length===0?`${o}: unexpected property`:`${o}: validation error at /${n.join("/")}`}function I(e,t,r){let i=r??e.definitions,n=i?{...e,definitions:i}:e;return(0,O.isValidSchema)(n)?(0,O.validate)(n,t,{maxDepth:Z,maxErrors:W}).map(s=>z(s,n,t)):e.ref?[`/: unknown ref "${e.ref}"`]:["/: invalid schema"]}var Z=32,W=100;var h=32,w={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}},$={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'.`,N=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,F="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 m(e,t){return t?[e,"null"]:e}function D(e,t){if(e===t)return!0;if(e==null||t==null||typeof e!=typeof t||typeof e!="object")return!1;if(Array.isArray(e))return Array.isArray(t)&&e.length===t.length&&e.every((o,s)=>D(o,t[s]));if(Array.isArray(t))return!1;let r=e,i=t,n=Object.keys(r);if(n.length!==Object.keys(i).length)return!1;for(let o of n)if(!(o in i)||!D(r[o],i[o]))return!1;return!0}function b(e){return!e||typeof e!="object"?!0:e.type||e.enum||e.elements||e.values||e.discriminator||e.ref||e.properties&&Object.keys(e.properties).length>0||e.optionalProperties&&Object.keys(e.optionalProperties).length>0?!1:(e.properties!==void 0||e.optionalProperties!==void 0)&&e.additionalProperties===!0?!0:!(e.properties!==void 0||e.optionalProperties!==void 0)}function A(e,t){let r=e,i=0;for(;r.ref&&i<h;){let n=t?.[r.ref];if(!n)return!1;r=n,i++}return i>=h?!1:b(r)}function R(e,t,r){let i=e.definitions,n=r(t,e,i,0),o=I(e,n,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:n}}function Q(e,t){if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with 'data' field for MIME type ${e}, got ${typeof t}`]};let r=t;if(typeof r.data!="string")return{success:!1,errors:[`/data: expected base64 string for MIME type ${e}, got ${typeof r.data}`]};let i={success:!0,kind:"binary",mimeType:e,data:new Uint8Array(Buffer.from(r.data,"base64"))};return typeof r.filename=="string"&&r.filename&&(i.filename=r.filename),i}function P(e,t,r){return e.mimeType?Q(e.mimeType,t):e.jtdSchema?R(e.jtdSchema,t,r):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function x(e,t,r){let i=e.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return P(i[0],t,r);if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with format properties, got ${typeof t}`]};let n=t;for(let s=0;s<i.length;s++){let a=`format_${s}`;if(n[a]!==void 0&&n[a]!==null)return P(i[s],n[a],r)}if(e.optional)return{success:!0,kind:"json",data:null};let o=i.map((s,a)=>`format_${a}`);return{success:!1,errors:[`Signal '${e.name??"unnamed"}' is required but no format was provided. Set exactly one of: ${o.join(", ")}.`]}}function M(e,t,r){if(typeof t!="object"||t===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof t]};let i=t,n={},o=[];for(let s of e){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let u=x(s,a,r);if(u.success)u.kind==="binary"?n[s.name]={data:u.data,mimeType:u.mimeType,filename:u.filename}:u.kind==="json"&&(n[s.name]=u.data);else for(let p of u.errors)o.push(`/${s.name}${p.startsWith("/")?"":": "}${p}`)}return o.length>0?{success:!1,errors:o}:{success:!0,kind:"signals",signals:n}}function J(e,t){if(!e||typeof e!="object")return e;let r={...e};return r.ref&&t.has(r.ref)&&(r.ref=t.get(r.ref)),r.properties&&(r.properties=Object.fromEntries(Object.entries(r.properties).map(([i,n])=>[i,J(n,t)]))),r.optionalProperties&&(r.optionalProperties=Object.fromEntries(Object.entries(r.optionalProperties).map(([i,n])=>[i,J(n,t)]))),r.elements&&(r.elements=J(r.elements,t)),r.values&&(r.values=J(r.values,t)),r.mapping&&(r.mapping=Object.fromEntries(Object.entries(r.mapping).map(([i,n])=>[i,J(n,t)]))),r.definitions&&delete r.definitions,r}function U(e){let t={},r=new Map;for(let n=0;n<e.length;n++){let o=e[n].formats;if(o)for(let s=0;s<o.length;s++){let a=o[s].jtdSchema?.definitions;if(!a||Object.keys(a).length===0)continue;let u=new Map,p=!1;for(let[c,f]of Object.entries(a)){let l=t[c];if(l&&!D(l,f)){p=!0;break}}if(p)for(let[c,f]of Object.entries(a)){let l=t[c];if(!(l&&D(l,f)))if(l){let d=1;for(;t[`${c}_${d}`];)d++;let v=`${c}_${d}`;u.set(c,v),t[v]=f}else t[c]=f}else for(let[c,f]of Object.entries(a))t[c]||(t[c]=f);u.size>0&&r.set(`${n}:${s}`,u)}}for(let[,n]of r)for(let[,o]of n)t[o]=J(t[o],n);let i=e.map((n,o)=>{let s=n.formats;if(!s)return n;let a=!1,u=s.map((p,c)=>{if(!p.jtdSchema)return p;let f=r.get(`${o}:${c}`),l=p.jtdSchema.definitions&&Object.keys(p.jtdSchema.definitions).length>0;if(!f&&!l)return p;a=!0;let d=f?J(p.jtdSchema,f):{...p.jtdSchema};return delete d.definitions,{...p,jtdSchema:d}});return a?{...n,formats:u}:n});return{mergedDefs:t,signalsCopy:i}}function C(e){if(Array.isArray(e.type)&&e.type.includes("null")||e.type==="null")return e;if(e.$ref)return{anyOf:[e,{type:"null"}]};if(e.anyOf)return e.anyOf.some(r=>r.type==="null")?e:{...e,anyOf:[...e.anyOf,{type:"null"}]};let t={...e};return t.type&&(t.type=Array.isArray(t.type)?[...t.type,"null"]:[t.type,"null"]),t.enum&&!t.enum.includes(null)&&(t.enum=[...t.enum,null]),t}var ee=new Set(["title","label","name","description"]);function g(e,t){let r=t.metadata;if(!r)return;let i=[],n=typeof r.title=="string"&&r.title.trim()||typeof r.label=="string"&&r.label.trim()||typeof r.name=="string"&&r.name.trim(),o=typeof r.description=="string"?r.description.trim():"";n&&o?i.push(`${n}. ${o}`):n?i.push(n):o&&i.push(o);let s=[];for(let[a,u]of Object.entries(r))ee.has(a)||(typeof u=="string"&&u.trim()?s.push(`[${a}: ${u.trim()}]`):(typeof u=="number"||typeof u=="boolean")&&s.push(`[${a}: ${u}]`));if(s.length>0&&i.push(s.join(" ")),i.length>0){let a=i.join(" ");e.description=e.description?`${e.description} ${a}`:a}}function S(e,t,r){if(r>h)throw new Error(`Schema nesting exceeded ${h} levels`);if(!e||typeof e!="object")return{type:"string",description:k};if(b(e)){let n={type:m("string",e.nullable===!0),description:k};return g(n,e),n}let i=e.nullable===!0;if(e.ref){let n=e.ref;if(!t?.[n])throw new Error(`Unresolved ref "${n}"`);if(i){let o={anyOf:[{$ref:`#/$defs/${n}`},{type:"null"}]};return g(o,e),o}return{$ref:`#/$defs/${n}`}}if(e.type){let n={},o=w[e.type];if(o)n.type=m("integer",i),n.minimum=o.minimum,n.maximum=o.maximum;else switch(e.type){case"boolean":n.type=m("boolean",i);break;case"string":n.type=m("string",i);break;case"timestamp":n.type=m("string",i),n.format="date-time";break;case"float32":n.type=m("number",i),n.minimum=$.minimum,n.maximum=$.maximum;break;case"float64":n.type=m("number",i);break;default:throw new Error(`Unknown JTD type "${e.type}"`)}return g(n,e),n}if(e.enum){let n={type:m("string",i),enum:i?[...e.enum,null]:e.enum};return g(n,e),n}if(e.elements){let n={type:m("array",i),items:S(e.elements,t,r+1)};return g(n,e),n}if(e.properties||e.optionalProperties){let n={},o=[];if(e.properties)for(let[a,u]of Object.entries(e.properties))n[a]=S(u,t,r+1),o.push(a);if(e.optionalProperties)for(let[a,u]of Object.entries(e.optionalProperties)){let p=S(u,t,r+1),c=C(p);c.description=c.description?`(Optional, null if not provided) ${c.description}`:"Optional. Set to null if not provided.",n[a]=c,o.push(a)}let s={type:m("object",i),properties:n,additionalProperties:!1,required:o};return g(s,e),s}if(e.values){let n=S(e.values,t,r+1),o={type:m("array",i),description:F,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:n},required:["key","value"],additionalProperties:!1}};return g(o,e),o}if(e.discriminator&&e.mapping){let n=e.discriminator,o=e.mapping,s=[];for(let[u,p]of Object.entries(o)){let c;b(p)?c={type:"object",properties:{[n]:{type:"string",enum:[u]},_data:{type:"string",description:N}},required:[n,"_data"],additionalProperties:!1}:(c=S(p,t,r+1),c.properties[n]={type:"string",enum:[u]},(c.required??=[]).unshift(n),c.additionalProperties=!1),g(c,p),s.push(c)}i&&s.push({type:"null"});let a={anyOf:s};return g(a,e),a}return{type:"string",description:k}}var te=new Set(Object.keys(w));function y(e,t,r,i){if(i>h||e==null)return e;if(t.ref){let n=r?.[t.ref];return n?y(e,n,r,i+1):e}if(b(t)){if(typeof e=="string")try{return JSON.parse(e)}catch{return e}return e}if(t.type&&te.has(t.type))return typeof e=="number"&&!Number.isInteger(e)?Math.round(e):e;if(t.elements&&Array.isArray(e)){let n=t.elements;return e.map(o=>y(o,n,r,i+1))}if((t.properties||t.optionalProperties)&&typeof e=="object"&&e!==null){let n={...e};if(t.properties)for(let[o,s]of Object.entries(t.properties))o in n&&(n[o]=y(n[o],s,r,i+1));if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))o in n&&(n[o]===null?delete n[o]:n[o]=y(n[o],s,r,i+1));return n}if(t.values&&Array.isArray(e)){let n={};for(let o of e)if(typeof o=="object"&&o!==null){let s=o,a=s.key;n[a]=y(s.value,t.values,r,i+1)}return n}if(t.discriminator&&t.mapping&&typeof e=="object"&&e!==null){let n=e,o=t.discriminator,s=n[o];if(typeof s!="string")return e;let a=t.mapping[s];if(!a)return e;if(b(a)){let p=n._data;if(typeof p=="string")try{let c=JSON.parse(p);if(typeof c=="object"&&c!==null)return{[o]:s,...c}}catch{}return{[o]:s}}let u={[o]:s};if(a.properties)for(let[p,c]of Object.entries(a.properties))p in n&&(u[p]=y(n[p],c,r,i+1));if(a.optionalProperties)for(let[p,c]of Object.entries(a.optionalProperties))p in n&&n[p]!==null&&(u[p]=y(n[p],c,r,i+1));return u}return e}function ne(e){return{type:"object",description:`Binary content (MIME type: ${e}).`,properties:{data:{type:"string",description:`The base64-encoded binary content (MIME type: ${e}).`},filename:{type:["string","null"],description:'Optional filename for the binary content (e.g. "report.pdf"). Set to null if not applicable.'}},required:["data","filename"],additionalProperties:!1}}var T=class e{schema;strict;jtdSchema;descriptor;descriptors;static fromJTD(t){let r=t.definitions,i=!A(t,r),n;return i?(n=S(t,r,0),r&&Object.keys(r).length>0&&(n.$defs=Object.fromEntries(Object.entries(r).map(([o,s])=>[o,S(s,r,0)])))):(n={},g(n,t)),new e(n,i,t,void 0)}static fromSignal(t,r){let{schema:i,strict:n}=re(t,r);return new e(i,n,void 0,t)}static fromSignals(t,r,i){let{mergedDefs:n,signalsCopy:o}=U(t),s={},a=[],u=!0;for(let c of o){if(!c.name)continue;let f=e.fromSignal(c,n);f.strict||(u=!1),c.optional?s[c.name]=C(f.schema):s[c.name]=f.schema,a.push(c.name)}let p={type:"object",properties:s,required:a,additionalProperties:!1};return Object.keys(n).length>0&&(p.$defs=Object.fromEntries(Object.entries(n).map(([c,f])=>[c,S(f,n,0)]))),i&&r?p.description=`${i}. ${r}`:i?p.description=i:r&&(p.description=r),new e(p,u,void 0,void 0,t)}static fromJSON(t){let r=typeof t=="string"?JSON.parse(t):t;if(r.descriptors)return e.fromSignals(r.descriptors,r.description,r.label);if(r.descriptor)return e.fromSignal(r.descriptor);if(r.jtdSchema)return e.fromJTD(r.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(t,r,i,n,o){this.schema=t,this.strict=r,this.jtdSchema=i,this.descriptor=n,this.descriptors=o}convert=t=>this.descriptors?M(this.descriptors,t,y):this.jtdSchema?R(this.jtdSchema,t,y):this.descriptor?x(this.descriptor,t,y):{success:!1,errors:["No source schema available"]};toJSON(){if(this.descriptors){let t={schema:this.schema,strict:this.strict,descriptors:this.descriptors};return this.schema.description&&(t.description=this.schema.description),t}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function re(e,t){let r=e.formats;if(!r||r.length===0)throw new Error("Signal descriptor has no formats");if(r.length===1){let[c]=r,{schema:f,strict:l}=_(c,t);if(e.label||e.description){let d=[];e.label&&d.push(e.label),e.description&&d.push(e.description);let v=d.join(". ");f.description=f.description?`${v} ${f.description}`:v}return{schema:f,strict:l}}let i={},n=[];for(let[c,f]of r.entries()){let{schema:l}=_(f,t),d=`format_${c}`;f.jtdSchema&&(l.description=l.description?`Structured JSON data. ${l.description}`:"Structured JSON data."),i[d]=C(l),n.push(d)}let o=Object.keys(i),s=e.description??"",u=e.optional===!0?"Provide at most ONE of the following format properties, set all others to null.":`You MUST provide exactly ONE of the following format properties: ${o.join(", ")}. Set all others to null.`;return{schema:{type:"object",description:s?`${s} ${u}`:u,properties:i,required:n,additionalProperties:!1},strict:!0}}function _(e,t){if(e.jtdSchema){let r=t??e.jtdSchema.definitions,i=!A(e.jtdSchema,r),n;return i?(n=S(e.jtdSchema,r,0),!t&&r&&Object.keys(r).length>0&&(n.$defs=Object.fromEntries(Object.entries(r).map(([o,s])=>[o,S(s,r,0)])))):(n={},g(n,e.jtdSchema)),{schema:n,strict:i}}if(e.mimeType)return{schema:ne(e.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function ie(e){return T.fromJTD(e)}function oe(e){return T.fromSignal(e)}0&&(module.exports={OpenAIConversion,convertSignalToOpenAISchema,convertToOpenAISchema});
|
|
2
2
|
//# sourceMappingURL=openai.cjs.map
|