@mochabug/adapt-sdk 0.4.12 → 0.4.14

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 L,validate as G}from"jtd";var H=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function K(e){return e.length===0?"/":e.map(t=>/^\d+$/.test(t)?`[${t}]`:`.${t}`).join("")}function P(e,t){let n=e;for(let i=0;i<t.length-1&&(n&&typeof n=="object"&&t[i]in n);i++)n=n[t[i]];return n}function W(e,t){let n=e;for(let i of t)if(n&&typeof n=="object")n=n[i];else return;return n}function Y(e,t,n){let{instancePath:i,schemaPath:r}=e,o=K(i),s=r[r.length-1];if(s==="type"){let c=P(t,r)?.type;return c==="boolean"?`${o}: expected boolean`:c==="string"?`${o}: expected string`:c==="timestamp"?`${o}: expected timestamp string`:H.has(c)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let c=P(t,r)?.enum;return`${o}: expected one of [${c.join(", ")}]`}if(r.length>=2&&r[r.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=W(n,i);return`${o}: unknown variant "${a}"`}return r.length===0?`${o}: unexpected property`:`${o}: validation error at /${r.join("/")}`}function N(e,t,n){let i=n??e.definitions,r=i?{...e,definitions:i}:e;return L(r)?G(r,t,{maxDepth:z,maxErrors:X}).map(s=>Y(s,r,t)):e.ref?[`/: unknown ref "${e.ref}"`]:["/: invalid schema"]}var z=32,X=100;var b=32,O={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'.`,M=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,U="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 d(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 n=e,i=t,r=Object.keys(n);if(r.length!==Object.keys(i).length)return!1;for(let o of r)if(!(o in i)||!D(n[o],i[o]))return!1;return!0}function T(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 $(e,t){let n=e,i=0;for(;n.ref&&i<b;){let r=t?.[n.ref];if(!r)return!1;n=r,i++}return i>=b?!1:T(n)}function A(e,t,n){let i=e.definitions,r=n(t,e,i,0),o=N(e,r,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:r}}function I(e,t,n){let i=t?`${t}. `:"",r={type:"object",properties:{kind:{type:"string",enum:["value"]},data:e},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 n!==!1&&(r.additionalProperties=!1,o.additionalProperties=!1),{anyOf:[r,o]}}function Z(e,t){let n=e.trim().toLowerCase(),i=t.trim().toLowerCase();return i==="*/*"?!0:i.endsWith("/*")?n.startsWith(i.slice(0,i.indexOf("/")+1)):n===i}function Q(e,t){if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with data and mimeType fields, got ${typeof t}`]};let n=t;if(typeof n.mimeType!="string"||!n.mimeType.includes("/"))return{success:!1,errors:['/mimeType: required concrete MIME type (e.g. "image/png", "application/pdf")']};if(!Z(n.mimeType,e))return{success:!1,errors:[`/mimeType: "${n.mimeType}" does not match accepted type "${e}"`]};let i;if(n.data instanceof Uint8Array)i=n.data;else if(typeof n.data=="string")i=new Uint8Array(Buffer.from(n.data,"base64"));else return{success:!1,errors:[`/data: expected base64 string or Uint8Array, got ${typeof n.data}`]};let r={success:!0,kind:"binary",mimeType:n.mimeType,data:i};return typeof n.filename=="string"&&n.filename&&(r.filename=n.filename),r}function F(e,t,n){return e.mimeType?Q(e.mimeType,t):e.jtdSchema?A(e.jtdSchema,t,n):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function x(e,t,n){let i=e.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return F(i[0],t,n);let r=[];for(let o of i){let s=F(o,t,n);if(s.success)return s;r.push(...s.errors)}return{success:!1,errors:[`No format matched for signal '${e.name??"unnamed"}'. Errors: ${r.join("; ")}`]}}function q(e,t,n){if(typeof t!="object"||t===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof t]};let i=t,r={},o=[];for(let s of e){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let c=x(s,a,n);if(c.success)c.kind==="binary"?r[s.name]={data:c.data,mimeType:c.mimeType,filename:c.filename}:c.kind==="json"&&(r[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:r}}function h(e,t){if(!e||typeof e!="object")return e;let n={...e};return n.ref&&t.has(n.ref)&&(n.ref=t.get(n.ref)),n.properties&&(n.properties=Object.fromEntries(Object.entries(n.properties).map(([i,r])=>[i,h(r,t)]))),n.optionalProperties&&(n.optionalProperties=Object.fromEntries(Object.entries(n.optionalProperties).map(([i,r])=>[i,h(r,t)]))),n.elements&&(n.elements=h(n.elements,t)),n.values&&(n.values=h(n.values,t)),n.mapping&&(n.mapping=Object.fromEntries(Object.entries(n.mapping).map(([i,r])=>[i,h(r,t)]))),n.definitions&&delete n.definitions,n}function _(e){let t={},n=new Map;for(let r=0;r<e.length;r++){let o=e[r].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 c=new Map,f=!1;for(let[p,u]of Object.entries(a)){let l=t[p];if(l&&!D(l,u)){f=!0;break}}if(f)for(let[p,u]of Object.entries(a)){let l=t[p];if(!(l&&D(l,u)))if(l){let m=1;for(;t[`${p}_${m}`];)m++;let E=`${p}_${m}`;c.set(p,E),t[E]=u}else t[p]=u}else for(let[p,u]of Object.entries(a))t[p]||(t[p]=u);c.size>0&&n.set(`${r}:${s}`,c)}}for(let[,r]of n)for(let[,o]of r)t[o]=h(t[o],r);let i=e.map((r,o)=>{let s=r.formats;if(!s)return r;let a=!1,c=s.map((f,p)=>{if(!f.jtdSchema)return f;let u=n.get(`${o}:${p}`),l=f.jtdSchema.definitions&&Object.keys(f.jtdSchema.definitions).length>0;if(!u&&!l)return f;a=!0;let m=u?h(f.jtdSchema,u):{...f.jtdSchema};return delete m.definitions,{...f,jtdSchema:m}});return a?{...r,formats:c}:r});return{mergedDefs:t,signalsCopy:i}}function J(e){return!e||typeof e!="object"?"any":e.ref?e.ref:e.type?e.type:e.enum?`one of: ${e.enum.join(", ")}`:e.elements?`array of ${J(e.elements)}`:e.values?`map of ${J(e.values)}`:e.properties||e.optionalProperties?"object":e.discriminator?`union on '${e.discriminator}'`:"any"}function V(e,t){let n=["Recursive nesting. This field accepts the same object structure as its parent with the following shape:"],i=[],r=[];if(t.properties)for(let[o,s]of Object.entries(t.properties))i.push(`${o} (${J(s)})`);if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))r.push(`${o} (${J(s)})`);if(i.length>0&&n.push(`Required: ${i.join(", ")}.`),r.length>0){let o=r.slice(0,8),s=r.length>8?`, and ${r.length-8} more`:"";n.push(`Optional: ${o.join(", ")}${s}.`)}return t.elements&&n.push(`Array of ${J(t.elements)}.`),t.values&&n.push(`Map with values of type ${J(t.values)}.`),n.join(" ")}function v(e){let t=[];if(!e||typeof e!="object")return t;if(e.ref&&t.push(e.ref),e.properties)for(let n of Object.values(e.properties))t.push(...v(n));if(e.optionalProperties)for(let n of Object.values(e.optionalProperties))t.push(...v(n));if(e.elements&&t.push(...v(e.elements)),e.values&&t.push(...v(e.values)),e.mapping)for(let n of Object.values(e.mapping))t.push(...v(n));return t}function ee(e){let r=new Map,o=new Set;for(let a of Object.keys(e))r.set(a,0);function s(a,c){r.set(a,1),c.push(a);let f=e[a];if(f){for(let p of v(f))if(r.has(p))if(r.get(p)===1){let u=c.indexOf(p);for(let l=u;l<c.length;l++)o.add(c[l])}else r.get(p)===0&&s(p,c)}c.pop(),r.set(a,2)}for(let a of Object.keys(e))r.get(a)===0&&s(a,[]);return o}function j(e,t,n){if(!e||typeof e!="object")return e;if(e.ref&&t.has(e.ref)){let r=n[e.ref];return{metadata:{description:r?V(e.ref,r):`Recursive reference to ${e.ref}.`}}}let i={...e};return i.properties&&(i.properties=Object.fromEntries(Object.entries(i.properties).map(([r,o])=>[r,j(o,t,n)]))),i.optionalProperties&&(i.optionalProperties=Object.fromEntries(Object.entries(i.optionalProperties).map(([r,o])=>[r,j(o,t,n)]))),i.elements&&(i.elements=j(i.elements,t,n)),i.values&&(i.values=j(i.values,t,n)),i.mapping&&(i.mapping=Object.fromEntries(Object.entries(i.mapping).map(([r,o])=>[r,j(o,t,n)]))),i}function C(e){let t=ee(e);if(t.size===0)return e;let n={};for(let[i,r]of Object.entries(e))t.has(i)?n[i]=j(r,t,e):n[i]=r;return n}function te(e,t){e.description=e.description?`${e.description} ${t}`:t}var ne=new Set(["title","label","name","description"]);function g(e,t){let n=t.metadata;if(!n)return;let i=[],r=typeof n.title=="string"&&n.title.trim()||typeof n.label=="string"&&n.label.trim()||typeof n.name=="string"&&n.name.trim(),o=typeof n.description=="string"?n.description.trim():"";r&&o?i.push(`${r}. ${o}`):r?i.push(r):o&&i.push(o);let s=[];for(let[a,c]of Object.entries(n))ne.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(" ");e.description=e.description?`${e.description} ${a}`:a}}function S(e,t,n){if(n>b)throw new Error(`Schema nesting exceeded ${b} levels`);if(!e||typeof e!="object")return{type:"string",description:k};if(T(e)){let r={type:d("string",e.nullable===!0),description:k};return g(r,e),r}let i=e.nullable===!0;if(e.ref){let r=e.ref;if(!t?.[r])throw new Error(`Unresolved ref "${r}"`);if(i){let o={anyOf:[{$ref:`#/$defs/${r}`},{type:"null"}]};return g(o,e),o}return{$ref:`#/$defs/${r}`}}if(e.type){let r={},o,s=O[e.type];if(s)r.type=d("integer",i),o=`Value range: [${s.minimum}, ${s.maximum}].`;else switch(e.type){case"boolean":r.type=d("boolean",i);break;case"string":r.type=d("string",i);break;case"timestamp":r.type=d("string",i),r.format="date-time";break;case"float32":r.type=d("number",i),o=`Value range: [${R.minimum}, ${R.maximum}].`;break;case"float64":r.type=d("number",i);break;default:throw new Error(`Unknown JTD type "${e.type}"`)}return g(r,e),o&&te(r,o),r}if(e.enum){let r={type:d("string",i),enum:i?[...e.enum,null]:e.enum};return g(r,e),r}if(e.elements){let r={type:d("array",i),items:S(e.elements,t,n+1)};return g(r,e),r}if(e.properties||e.optionalProperties){let r={},o=[];if(e.properties)for(let[a,c]of Object.entries(e.properties))r[a]=S(c,t,n+1),o.push(a);if(e.optionalProperties)for(let[a,c]of Object.entries(e.optionalProperties))r[a]=S(c,t,n+1);let s={type:d("object",i),properties:r,additionalProperties:!1};return o.length>0&&(s.required=o),g(s,e),s}if(e.values){let r=S(e.values,t,n+1),o={type:d("array",i),description:U,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:r},required:["key","value"],additionalProperties:!1}};return g(o,e),o}if(e.discriminator&&e.mapping){let r=e.discriminator,o=e.mapping,s=[];for(let[c,f]of Object.entries(o)){let p;T(f)?p={type:"object",properties:{[r]:{const:c},_data:{type:"string",description:M}},required:[r,"_data"],additionalProperties:!1}:(p=S(f,t,n+1),p.properties[r]={const:c},(p.required??=[]).unshift(r),p.additionalProperties=!1),g(p,f),s.push(p)}i&&s.push({type:"null"});let a={anyOf:s};return g(a,e),a}return{type:"string",description:k}}var re=new Set(Object.keys(O));function y(e,t,n,i){if(i>b||e==null)return e;if(t.ref){let r=n?.[t.ref];return r?y(e,r,n,i+1):e}if(T(t)){if(typeof e=="string")try{return JSON.parse(e)}catch{return e}return e}if(t.type&&re.has(t.type))return typeof e=="number"&&!Number.isInteger(e)?Math.round(e):e;if(t.elements&&Array.isArray(e)){let r=t.elements;return e.map(o=>y(o,r,n,i+1))}if((t.properties||t.optionalProperties)&&typeof e=="object"&&e!==null){let r={...e};if(t.properties)for(let[o,s]of Object.entries(t.properties))o in r&&(r[o]=y(r[o],s,n,i+1));if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))o in r&&(r[o]=y(r[o],s,n,i+1));return r}if(t.values&&Array.isArray(e)){let r={};for(let o of e)if(typeof o=="object"&&o!==null){let s=o,a=s.key;r[a]=y(s.value,t.values,n,i+1)}return r}if(t.discriminator&&t.mapping&&typeof e=="object"&&e!==null){let r=e,o=t.discriminator,s=r[o];if(typeof s!="string")return e;let a=t.mapping[s];if(!a)return e;if(T(a)){let f=r._data;if(typeof f=="string")try{let p=JSON.parse(f);if(typeof p=="object"&&p!==null)return{[o]:s,...p}}catch{}return{[o]:s}}let c={[o]:s};if(a.properties)for(let[f,p]of Object.entries(a.properties))f in r&&(c[f]=y(r[f],p,n,i+1));if(a.optionalProperties)for(let[f,p]of Object.entries(a.optionalProperties))f in r&&(c[f]=y(r[f],p,n,i+1));return c}return e}function ie(e){return{type:"object",description:`Binary content (accepted MIME type: ${e}).`,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: ${e}.`}},required:["data","mimeType","filename"],additionalProperties:!1}}var w=class e{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(t){let n=t.definitions,i=n&&Object.keys(n).length>0?C(n):n,r=!$(t,i),o;return r?(o=S(t,i,0),i&&Object.keys(i).length>0&&(o.$defs=Object.fromEntries(Object.entries(i).map(([s,a])=>[s,S(a,i,0)])))):(o={},g(o,t)),new e(o,r,t,void 0)}static fromSignal(t,n,i){let{schema:r,strict:o}=oe(t,n);if(i){let s=I(r,t.description);return new e(s,o,void 0,t)}return new e(r,o,void 0,t)}static fromSignals(t,n,i,r){let{mergedDefs:o,signalsCopy:s}=_(t),a=Object.keys(o).length>0?C(o):o,c={},f=[],p=!0;for(let l of s){if(!l.name)continue;let m=e.fromSignal(l,a,r);m.strict||(p=!1),c[l.name]=m.schema,l.optional||f.push(l.name)}let u={type:"object",properties:c,required:f.length>0?f:void 0,additionalProperties:!1};return Object.keys(a).length>0&&(u.$defs=Object.fromEntries(Object.entries(a).map(([l,m])=>[l,S(m,a,0)]))),i&&n?u.description=`${i}. ${n}`:i?u.description=i:n&&(u.description=n),new e(u,p,void 0,void 0,t,r)}static fromJSON(t){let n=typeof t=="string"?JSON.parse(t):t;if(n.descriptors)return e.fromSignals(n.descriptors,n.description,n.label,n.referenceMode);if(n.descriptor)return e.fromSignal(n.descriptor);if(n.jtdSchema)return e.fromJTD(n.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(t,n,i,r,o,s){this.schema=t,this.strict=n,this.jtdSchema=i,this.descriptor=r,this.descriptors=o,this.referenceMode=s??!1}convert=t=>this.descriptors?q(this.descriptors,t,y):this.jtdSchema?A(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),this.referenceMode&&(t.referenceMode=!0),t}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function oe(e,t){let n=e.formats;if(!n||n.length===0)throw new Error("Signal descriptor has no formats");if(n.length===1){let[a]=n,{schema:c,strict:f}=B(a,t);if(e.label||e.description){let p=[];e.label&&p.push(e.label),e.description&&p.push(e.description);let u=p.join(". ");c.description=c.description?`${u} ${c.description}`:u}return{schema:c,strict:f}}let i=[],r=!0;for(let a of n){let{schema:c,strict:f}=B(a,t);f||(r=!1),a.jtdSchema&&(c.description=c.description?`Structured JSON data. ${c.description}`:"Structured JSON data."),i.push(c)}let o=e.description??"";return{schema:{anyOf:i,description:o||void 0},strict:r}}function B(e,t){if(e.jtdSchema){let n=t??e.jtdSchema.definitions,i=n&&Object.keys(n).length>0?C(n):n,r=!$(e.jtdSchema,i),o;return r?(o=S(e.jtdSchema,i,0),!t&&i&&Object.keys(i).length>0&&(o.$defs=Object.fromEntries(Object.entries(i).map(([s,a])=>[s,S(a,i,0)])))):(o={},g(o,e.jtdSchema)),{schema:o,strict:r}}if(e.mimeType)return{schema:ie(e.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function le(e){return w.fromJTD(e)}function de(e){return w.fromSignal(e)}export{w as AnthropicConversion,de as convertSignalToAnthropicSchema,le as convertToAnthropicSchema};
1
+ import{isSchema as pe,isValidSchema as K,validate as W}from"jtd";var Y=new Set(["int8","uint8","int16","uint16","int32","uint32"]);function z(e){return e.length===0?"/":e.map(t=>/^\d+$/.test(t)?`[${t}]`:`.${t}`).join("")}function M(e,t){let n=e;for(let i=0;i<t.length-1&&(n&&typeof n=="object"&&t[i]in n);i++)n=n[t[i]];return n}function X(e,t){let n=e;for(let i of t)if(n&&typeof n=="object")n=n[i];else return;return n}function Z(e,t,n){let{instancePath:i,schemaPath:r}=e,o=z(i),s=r[r.length-1];if(s==="type"){let c=M(t,r)?.type;return c==="boolean"?`${o}: expected boolean`:c==="string"?`${o}: expected string`:c==="timestamp"?`${o}: expected timestamp string`:Y.has(c)?`${o}: expected integer`:`${o}: expected number`}if(s==="enum"){let c=M(t,r)?.enum;return`${o}: expected one of [${c.join(", ")}]`}if(r.length>=2&&r[r.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(n,i);return`${o}: unknown variant "${a}"`}return r.length===0?`${o}: unexpected property`:`${o}: validation error at /${r.join("/")}`}function U(e,t,n){let i=n??e.definitions,r=i?{...e,definitions:i}:e;return K(r)?W(r,t,{maxDepth:Q,maxErrors:ee}).map(s=>Z(s,r,t)):e.ref?[`/: unknown ref "${e.ref}"`]:["/: invalid schema"]}var Q=32,ee=100;var v=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}},x={minimum:-34028235e31,maximum:34028235e31},O=`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'.`,q=`The variant data encoded as a valid JSON string (e.g. '{"key": "value"}').`,_="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 w(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)=>w(o,t[s]));if(Array.isArray(t))return!1;let n=e,i=t,r=Object.keys(n);if(r.length!==Object.keys(i).length)return!1;for(let o of r)if(!(o in i)||!w(n[o],i[o]))return!1;return!0}function j(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 C(e,t){let n=e,i=0;for(;n.ref&&i<v;){let r=t?.[n.ref];if(!r)return!1;n=r,i++}return i>=v?!1:j(n)}function R(e,t){let n=new Set,i=[...e];for(;i.length>0;){let r=i.pop();if(!(!r||typeof r!="object")){if(r.ref){!n.has(r.ref)&&t[r.ref]&&(n.add(r.ref),i.push(t[r.ref]));continue}if(r.elements&&i.push(r.elements),r.values&&i.push(r.values),r.properties)for(let o of Object.values(r.properties))i.push(o);if(r.optionalProperties)for(let o of Object.values(r.optionalProperties))i.push(o);if(r.mapping)for(let o of Object.values(r.mapping))i.push(o)}}return n}function E(e,t,n){let i=e.definitions,r=n(t,e,i,0),o=U(e,r,i);return o.length>0?{success:!1,errors:o}:{success:!0,kind:"json",data:r}}function V(e,t,n){let i=t?`${t}. `:"",r={type:"object",properties:{kind:{type:"string",enum:["value"]},data:e},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 n!==!1&&(r.additionalProperties=!1,o.additionalProperties=!1),{anyOf:[r,o]}}function te(e,t){let n=e.trim().toLowerCase(),i=t.trim().toLowerCase();return i==="*/*"?!0:i.endsWith("/*")?n.startsWith(i.slice(0,i.indexOf("/")+1)):n===i}function ne(e,t){if(typeof t!="object"||t===null)return{success:!1,errors:[`/: expected object with data and mimeType fields, got ${typeof t}`]};let n=t;if(typeof n.mimeType!="string"||!n.mimeType.includes("/"))return{success:!1,errors:['/mimeType: required concrete MIME type (e.g. "image/png", "application/pdf")']};if(!te(n.mimeType,e))return{success:!1,errors:[`/mimeType: "${n.mimeType}" does not match accepted type "${e}"`]};let i;if(n.data instanceof Uint8Array)i=n.data;else if(typeof n.data=="string")i=new Uint8Array(Buffer.from(n.data,"base64"));else return{success:!1,errors:[`/data: expected base64 string or Uint8Array, got ${typeof n.data}`]};let r={success:!0,kind:"binary",mimeType:n.mimeType,data:i};return typeof n.filename=="string"&&n.filename&&(r.filename=n.filename),r}function I(e,t,n){return e.mimeType?ne(e.mimeType,t):e.jtdSchema?E(e.jtdSchema,t,n):{success:!1,errors:["Signal format has neither jtdSchema nor mimeType"]}}function P(e,t,n){let i=e.formats;if(!i||i.length===0)return{success:!1,errors:["Signal descriptor has no formats"]};if(i.length===1)return I(i[0],t,n);let r=[];for(let o of i){let s=I(o,t,n);if(s.success)return s;r.push(...s.errors)}return{success:!1,errors:[`No format matched for signal '${e.name??"unnamed"}'. Errors: ${r.join("; ")}`]}}function B(e,t,n){if(typeof t!="object"||t===null)return{success:!1,errors:["/: expected object with signal properties, got "+typeof t]};let i=t,r={},o=[];for(let s of e){if(!s.name)continue;let a=i[s.name];if(a==null)continue;let c=P(s,a,n);if(c.success)c.kind==="binary"?r[s.name]={data:c.data,mimeType:c.mimeType,filename:c.filename}:c.kind==="json"&&(r[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:r}}function J(e,t){if(!e||typeof e!="object")return e;let n={...e};return n.ref&&t.has(n.ref)&&(n.ref=t.get(n.ref)),n.properties&&(n.properties=Object.fromEntries(Object.entries(n.properties).map(([i,r])=>[i,J(r,t)]))),n.optionalProperties&&(n.optionalProperties=Object.fromEntries(Object.entries(n.optionalProperties).map(([i,r])=>[i,J(r,t)]))),n.elements&&(n.elements=J(n.elements,t)),n.values&&(n.values=J(n.values,t)),n.mapping&&(n.mapping=Object.fromEntries(Object.entries(n.mapping).map(([i,r])=>[i,J(r,t)]))),n.definitions&&delete n.definitions,n}function L(e){let t={},n=new Map;for(let s=0;s<e.length;s++){let a=e[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 p=new Map,l=!1;for(let[u,d]of Object.entries(f)){let h=t[u];if(h&&!w(h,d)){l=!0;break}}if(l)for(let[u,d]of Object.entries(f)){let h=t[u];if(!(h&&w(h,d)))if(h){let b=1;for(;t[`${u}_${b}`];)b++;let F=`${u}_${b}`;p.set(u,F),t[F]=d}else t[u]=d}else for(let[u,d]of Object.entries(f))t[u]||(t[u]=d);p.size>0&&n.set(`${s}:${c}`,p)}}for(let[,s]of n)for(let[,a]of s)t[a]=J(t[a],s);let i=e.map((s,a)=>{let c=s.formats;if(!c)return s;let f=!1,p=c.map((l,u)=>{if(!l.jtdSchema)return l;let d=n.get(`${a}:${u}`),h=l.jtdSchema.definitions&&Object.keys(l.jtdSchema.definitions).length>0;if(!d&&!h)return l;f=!0;let b=d?J(l.jtdSchema,d):{...l.jtdSchema};return delete b.definitions,{...l,jtdSchema:b}});return f?{...s,formats:p}:s}),r=i.flatMap(s=>(s.formats??[]).filter(a=>a.jtdSchema).map(a=>a.jtdSchema)),o=R(r,t);for(let s of Object.keys(t))o.has(s)||delete t[s];return{mergedDefs:t,signalsCopy:i}}function T(e){return!e||typeof e!="object"?"any":e.ref?e.ref:e.type?e.type:e.enum?`one of: ${e.enum.join(", ")}`:e.elements?`array of ${T(e.elements)}`:e.values?`map of ${T(e.values)}`:e.properties||e.optionalProperties?"object":e.discriminator?`union on '${e.discriminator}'`:"any"}function G(e,t){let n=["Recursive nesting. This field accepts the same object structure as its parent with the following shape:"],i=[],r=[];if(t.properties)for(let[o,s]of Object.entries(t.properties))i.push(`${o} (${T(s)})`);if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))r.push(`${o} (${T(s)})`);if(i.length>0&&n.push(`Required: ${i.join(", ")}.`),r.length>0){let o=r.slice(0,8),s=r.length>8?`, and ${r.length-8} more`:"";n.push(`Optional: ${o.join(", ")}${s}.`)}return t.elements&&n.push(`Array of ${T(t.elements)}.`),t.values&&n.push(`Map with values of type ${T(t.values)}.`),n.join(" ")}function D(e){let t=[];if(!e||typeof e!="object")return t;if(e.ref&&t.push(e.ref),e.properties)for(let n of Object.values(e.properties))t.push(...D(n));if(e.optionalProperties)for(let n of Object.values(e.optionalProperties))t.push(...D(n));if(e.elements&&t.push(...D(e.elements)),e.values&&t.push(...D(e.values)),e.mapping)for(let n of Object.values(e.mapping))t.push(...D(n));return t}function re(e){let r=new Map,o=new Set;for(let a of Object.keys(e))r.set(a,0);function s(a,c){r.set(a,1),c.push(a);let f=e[a];if(f){for(let p of D(f))if(r.has(p))if(r.get(p)===1){let l=c.indexOf(p);for(let u=l;u<c.length;u++)o.add(c[u])}else r.get(p)===0&&s(p,c)}c.pop(),r.set(a,2)}for(let a of Object.keys(e))r.get(a)===0&&s(a,[]);return o}function k(e,t,n){if(!e||typeof e!="object")return e;if(e.ref&&t.has(e.ref)){let r=n[e.ref];return{metadata:{description:r?G(e.ref,r):`Recursive reference to ${e.ref}.`}}}let i={...e};return i.properties&&(i.properties=Object.fromEntries(Object.entries(i.properties).map(([r,o])=>[r,k(o,t,n)]))),i.optionalProperties&&(i.optionalProperties=Object.fromEntries(Object.entries(i.optionalProperties).map(([r,o])=>[r,k(o,t,n)]))),i.elements&&(i.elements=k(i.elements,t,n)),i.values&&(i.values=k(i.values,t,n)),i.mapping&&(i.mapping=Object.fromEntries(Object.entries(i.mapping).map(([r,o])=>[r,k(o,t,n)]))),i}function N(e){let t=re(e);if(t.size===0)return e;let n={};for(let[i,r]of Object.entries(e))t.has(i)?n[i]=k(r,t,e):n[i]=r;return n}function ie(e,t){e.description=e.description?`${e.description} ${t}`:t}var oe=new Set(["title","label","name","description"]);function g(e,t){let n=t.metadata;if(!n)return;let i=[],r=typeof n.title=="string"&&n.title.trim()||typeof n.label=="string"&&n.label.trim()||typeof n.name=="string"&&n.name.trim(),o=typeof n.description=="string"?n.description.trim():"";r&&o?i.push(`${r}. ${o}`):r?i.push(r):o&&i.push(o);let s=[];for(let[a,c]of Object.entries(n))oe.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(" ");e.description=e.description?`${e.description} ${a}`:a}}function S(e,t,n){if(n>v)throw new Error(`Schema nesting exceeded ${v} levels`);if(!e||typeof e!="object")return{type:"string",description:O};if(j(e)){let r={type:m("string",e.nullable===!0),description:O};return g(r,e),r}let i=e.nullable===!0;if(e.ref){let r=e.ref;if(!t?.[r])throw new Error(`Unresolved ref "${r}"`);if(i){let o={anyOf:[{$ref:`#/$defs/${r}`},{type:"null"}]};return g(o,e),o}return{$ref:`#/$defs/${r}`}}if(e.type){let r={},o,s=A[e.type];if(s)r.type=m("integer",i),o=`Value range: [${s.minimum}, ${s.maximum}].`;else switch(e.type){case"boolean":r.type=m("boolean",i);break;case"string":r.type=m("string",i);break;case"timestamp":r.type=m("string",i),r.format="date-time";break;case"float32":r.type=m("number",i),o=`Value range: [${x.minimum}, ${x.maximum}].`;break;case"float64":r.type=m("number",i);break;default:throw new Error(`Unknown JTD type "${e.type}"`)}return g(r,e),o&&ie(r,o),r}if(e.enum){let r={type:m("string",i),enum:i?[...e.enum,null]:e.enum};return g(r,e),r}if(e.elements){let r={type:m("array",i),items:S(e.elements,t,n+1)};return g(r,e),r}if(e.properties||e.optionalProperties){let r={},o=[];if(e.properties)for(let[a,c]of Object.entries(e.properties))r[a]=S(c,t,n+1),o.push(a);if(e.optionalProperties)for(let[a,c]of Object.entries(e.optionalProperties))r[a]=S(c,t,n+1);let s={type:m("object",i),properties:r,additionalProperties:!1};return o.length>0&&(s.required=o),g(s,e),s}if(e.values){let r=S(e.values,t,n+1),o={type:m("array",i),description:_,items:{type:"object",properties:{key:{type:"string",description:"The property name."},value:r},required:["key","value"],additionalProperties:!1}};return g(o,e),o}if(e.discriminator&&e.mapping){let r=e.discriminator,o=e.mapping,s=[];for(let[c,f]of Object.entries(o)){let p;j(f)?p={type:"object",properties:{[r]:{const:c},_data:{type:"string",description:q}},required:[r,"_data"],additionalProperties:!1}:(p=S(f,t,n+1),p.properties[r]={const:c},(p.required??=[]).unshift(r),p.additionalProperties=!1),g(p,f),s.push(p)}i&&s.push({type:"null"});let a={anyOf:s};return g(a,e),a}return{type:"string",description:O}}var se=new Set(Object.keys(A));function y(e,t,n,i){if(i>v||e==null)return e;if(t.ref){let r=n?.[t.ref];return r?y(e,r,n,i+1):e}if(j(t)){if(typeof e=="string")try{return JSON.parse(e)}catch{return e}return e}if(t.type&&se.has(t.type))return typeof e=="number"&&!Number.isInteger(e)?Math.round(e):e;if(t.elements&&Array.isArray(e)){let r=t.elements;return e.map(o=>y(o,r,n,i+1))}if((t.properties||t.optionalProperties)&&typeof e=="object"&&e!==null){let r={...e};if(t.properties)for(let[o,s]of Object.entries(t.properties))o in r&&(r[o]=y(r[o],s,n,i+1));if(t.optionalProperties)for(let[o,s]of Object.entries(t.optionalProperties))o in r&&(r[o]=y(r[o],s,n,i+1));return r}if(t.values&&Array.isArray(e)){let r={};for(let o of e)if(typeof o=="object"&&o!==null){let s=o,a=s.key;r[a]=y(s.value,t.values,n,i+1)}return r}if(t.discriminator&&t.mapping&&typeof e=="object"&&e!==null){let r=e,o=t.discriminator,s=r[o];if(typeof s!="string")return e;let a=t.mapping[s];if(!a)return e;if(j(a)){let f=r._data;if(typeof f=="string")try{let p=JSON.parse(f);if(typeof p=="object"&&p!==null)return{[o]:s,...p}}catch{}return{[o]:s}}let c={[o]:s};if(a.properties)for(let[f,p]of Object.entries(a.properties))f in r&&(c[f]=y(r[f],p,n,i+1));if(a.optionalProperties)for(let[f,p]of Object.entries(a.optionalProperties))f in r&&(c[f]=y(r[f],p,n,i+1));return c}return e}function ae(e){return{type:"object",description:`Binary content (accepted MIME type: ${e}).`,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: ${e}.`}},required:["data","mimeType","filename"],additionalProperties:!1}}var $=class e{schema;strict;jtdSchema;descriptor;descriptors;referenceMode;static fromJTD(t){let n=t.definitions,i=n&&Object.keys(n).length>0?N(n):n,r=!C(t,i),o;if(r){if(o=S(t,i,0),i&&Object.keys(i).length>0){let s=R([t],n??i);o.$defs=Object.fromEntries(Object.entries(i).filter(([a])=>s.has(a)).map(([a,c])=>[a,S(c,i,0)]))}}else o={},g(o,t);return new e(o,r,t,void 0)}static fromSignal(t,n,i){let{schema:r,strict:o}=ce(t,n);if(i){let s=V(r,t.description);return new e(s,o,void 0,t)}return new e(r,o,void 0,t)}static fromSignals(t,n,i,r){let{mergedDefs:o,signalsCopy:s}=L(t),a=Object.keys(o).length>0?N(o):o,c={},f=[],p=!0;for(let u of s){if(!u.name)continue;let d=e.fromSignal(u,a,r);d.strict||(p=!1),c[u.name]=d.schema,u.optional||f.push(u.name)}let l={type:"object",properties:c,required:f.length>0?f:void 0,additionalProperties:!1};return Object.keys(a).length>0&&(l.$defs=Object.fromEntries(Object.entries(a).map(([u,d])=>[u,S(d,a,0)]))),i&&n?l.description=`${i}. ${n}`:i?l.description=i:n&&(l.description=n),new e(l,p,void 0,void 0,t,r)}static fromJSON(t){let n=typeof t=="string"?JSON.parse(t):t;if(n.descriptors)return e.fromSignals(n.descriptors,n.description,n.label,n.referenceMode);if(n.descriptor)return e.fromSignal(n.descriptor);if(n.jtdSchema)return e.fromJTD(n.jtdSchema);throw new Error("Cannot deserialize: missing jtdSchema, descriptor, or descriptors")}constructor(t,n,i,r,o,s){this.schema=t,this.strict=n,this.jtdSchema=i,this.descriptor=r,this.descriptors=o,this.referenceMode=s??!1}convert=t=>this.descriptors?B(this.descriptors,t,y):this.jtdSchema?E(this.jtdSchema,t,y):this.descriptor?P(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),this.referenceMode&&(t.referenceMode=!0),t}return this.descriptor?{schema:this.schema,strict:this.strict,descriptor:this.descriptor}:{schema:this.schema,strict:this.strict,jtdSchema:this.jtdSchema}}};function ce(e,t){let n=e.formats;if(!n||n.length===0)throw new Error("Signal descriptor has no formats");if(n.length===1){let[a]=n,{schema:c,strict:f}=H(a,t);if(e.label||e.description){let p=[];e.label&&p.push(e.label),e.description&&p.push(e.description);let l=p.join(". ");c.description=c.description?`${l} ${c.description}`:l}return{schema:c,strict:f}}let i=[],r=!0;for(let a of n){let{schema:c,strict:f}=H(a,t);f||(r=!1),a.jtdSchema&&(c.description=c.description?`Structured JSON data. ${c.description}`:"Structured JSON data."),i.push(c)}let o=e.description??"";return{schema:{anyOf:i,description:o||void 0},strict:r}}function H(e,t){if(e.jtdSchema){let n=t??e.jtdSchema.definitions,i=n&&Object.keys(n).length>0?N(n):n,r=!C(e.jtdSchema,i),o;if(r){if(o=S(e.jtdSchema,i,0),!t&&i&&Object.keys(i).length>0){let s=R([e.jtdSchema],i);o.$defs=Object.fromEntries(Object.entries(i).filter(([a])=>s.has(a)).map(([a,c])=>[a,S(c,i,0)]))}}else o={},g(o,e.jtdSchema);return{schema:o,strict:r}}if(e.mimeType)return{schema:ae(e.mimeType),strict:!0};throw new Error("Signal format has neither jtdSchema nor mimeType")}function ge(e){return $.fromJTD(e)}function ye(e){return $.fromSignal(e)}export{$ as AnthropicConversion,ye as convertSignalToAnthropicSchema,ge as convertToAnthropicSchema};
2
2
  //# sourceMappingURL=anthropic.mjs.map