eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.48 kB
JavaScript
import{z}from"#compiled/zod/index.js";import{toErrorMessage}from"#shared/errors.js";import{parseJsonObject}from"#shared/json.js";import{asSchema}from"ai";const rehydratedSchemas={input:new WeakMap,output:new WeakMap};function toInputSchema(e){return toSchema(e,`input`)}function toOutputSchema(e){return toSchema(e,`output`)}function serializeInputSchema(e){return serializeSchema(e,`input`)}function serializeOutputSchema(e){return serializeSchema(e,`output`)}function isToolSchema(e){let t=getStandardSchemaProperties(e);if(t===void 0)return!1;let n=t.jsonSchema;return typeof t.validate==`function`&&typeof n==`object`&&!!n&&typeof n.input==`function`&&typeof n.output==`function`}const UNSPECIFIED_INPUT_SCHEMA=z.fromJSONSchema({});function toSchema(e,t){if(e==null||isToolSchema(e))return e;let n=rehydratedSchemas[t],r=n.get(e);return r===void 0&&(r=rehydrateJsonSchema(toJsonObject(e,t)),n.set(e,r)),r}function rehydrateJsonSchema(e){try{return z.fromJSONSchema(e,{defaultTarget:`draft-7`})}catch{}try{return z.fromJSONSchema(e,{defaultTarget:`draft-2020-12`})}catch(t){return console.warn(`[eve] Tool schema uses JSON Schema features outside local validation support; passing input through unvalidated: ${toErrorMessage(t)}`),toPassthroughSchema(e)}}function toPassthroughSchema(e){let emit=()=>structuredClone(e);return{"~standard":{version:1,vendor:`eve`,validate:e=>({value:e}),jsonSchema:{input:emit,output:emit}}}}function serializeSchema(e,t){return e==null?e:toJsonObject(e,t)}function toJsonObject(e,t){let n=getStandardSchemaProperties(e),r=n?.jsonSchema,i=typeof r==`object`&&r?r[t]:void 0,a=typeof n?.vendor==`string`?n.vendor:`unknown`;if(n!==void 0&&typeof i!=`function`&&a===`zod`){if(t===`input`){let t=asSchema(e),{$schema:n,...r}=parseJsonObject(t.jsonSchema);return r}throw Error(`Zod 3 cannot emit an output JSON Schema. Upgrade to Zod 4 or provide a plain JSON Schema object.`)}if(n!==void 0&&typeof i!=`function`)throw Error(`Standard Schema vendor "${a}" does not support JSON Schema conversion. Provide a Standard Schema implementation with JSON Schema conversion or a plain JSON Schema object.`);let{$schema:o,...s}=parseJsonObject(n===void 0?e:i({target:`draft-07`}));return s}function getStandardSchemaProperties(e){if(typeof e!=`object`||!e||!(`~standard`in e))return;let t=e[`~standard`];return typeof t==`object`&&t?t:void 0}export{UNSPECIFIED_INPUT_SCHEMA,isToolSchema,serializeInputSchema,serializeOutputSchema,toInputSchema,toOutputSchema};