UNPKG

eve

Version:

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

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