UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.89 kB
import{z}from"#compiled/zod/index.js";import{toErrorMessage}from"#shared/errors.js";import{parseJsonObject}from"#shared/json.js";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){if(typeof e!=`object`||!e||!(`~standard`in e))return!1;let t=e[`~standard`];if(typeof t!=`object`||!t)return!1;let n=t,r=n.jsonSchema;return typeof n.validate==`function`&&typeof r==`object`&&!!r&&typeof r.input==`function`&&typeof r.output==`function`}const UNSPECIFIED_INPUT_SCHEMA=z.fromJSONSchema({});function toSchema(e,t){if(e==null||isToolSchema(e))return e;let n=rehydratedSchemas[t],i=n.get(e);return i===void 0&&(i=rehydrateJsonSchema(toJsonObject(e,t)),n.set(e,i)),i}function rehydrateJsonSchema(n){try{return z.fromJSONSchema(n,{defaultTarget:`draft-7`})}catch{}try{return z.fromJSONSchema(n,{defaultTarget:`draft-2020-12`})}catch(e){return console.warn(`[eve] Tool schema uses JSON Schema features outside local validation support; passing input through unvalidated: ${toErrorMessage(e)}`),toPassthroughSchema(n)}}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{$schema:r,...i}=isStandardJsonSchema(e)?parseJsonObject(e[`~standard`].jsonSchema[t]({target:`draft-07`})):parseJsonObject(e);return i}function isStandardJsonSchema(e){return typeof e==`object`&&!!e&&`~standard`in e}export{UNSPECIFIED_INPUT_SCHEMA,isToolSchema,serializeInputSchema,serializeOutputSchema,toInputSchema,toOutputSchema};