UNPKG

eve

Version:

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

1 lines 1.83 kB
import{expectFunction,expectObjectRecord}from"#internal/authored-module.js";import{toErrorMessage}from"#shared/errors.js";import{isToolSchema,toInputSchema,toOutputSchema}from"#shared/tool-schema.js";import{ResolveAgentError,loadResolvedModuleExport}from"#runtime/resolve-helpers.js";import{registerDefinitionSource,stampDefinitionKey}from"#public/tool-result-narrowing.js";async function resolveToolDefinition(a,o,s){try{let n=expectObjectRecord(await loadResolvedModuleExport({definition:a,kindLabel:`tool`,moduleMap:o,nodeId:s}),describe(a,`to return an object`)),i={kind:`tool`,logicalPath:a.logicalPath,name:a.name},c=`tool-source:${a.sourceId}`;stampDefinitionKey(n,c),registerDefinitionSource(c,i),registerDefinitionSource(`tool:${n.description}`,i);let l=expectFunction(n.execute,describe(a,`to provide an execute function`)),u=isToolSchema(n.inputSchema)?n.inputSchema:toInputSchema(a.inputSchema),d=isToolSchema(n.outputSchema)?n.outputSchema:toOutputSchema(a.outputSchema);return{description:a.description,execute:l,exportName:a.exportName,inputSchema:u,logicalPath:a.logicalPath,name:a.name,outputSchema:d,sourceId:a.sourceId,sourceKind:`module`,...extractOptionalHooks(n,a)}}catch(e){throw e instanceof ResolveAgentError?e:new ResolveAgentError(`Failed to attach the tool execute function from "${a.logicalPath}": ${toErrorMessage(e)}`,{logicalPath:a.logicalPath,sourceId:a.sourceId})}}function extractOptionalHooks(t,n){let r={};return t.approval!==void 0&&(r.approval=expectFunction(t.approval,describe(n,`to provide an approval function`))),t.toModelOutput!==void 0&&(r.toModelOutput=expectFunction(t.toModelOutput,describe(n,`to provide a toModelOutput function`))),r}function describe(e,t){return`Expected the tool export "${e.exportName??`default`}" from "${e.logicalPath}" ${t}.`}export{resolveToolDefinition};