eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 4.48 kB
JavaScript
import{tool}from"ai";import{isObject}from"#shared/guards.js";import{loadContext}from"#context/container.js";import{buildCallbackContext}from"#context/build-callback-context.js";import{resolveApprovalPolicy}from"#approval/definition.js";import{authorizationPendingModelText,isAuthorizationPendingModelOutput,isAuthorizationSignal,modelFacingAuthorizationOutput}from"#harness/authorization.js";import{createBackgroundToolCallBatch,executeBackgroundToolCall}from"#harness/background-tools.js";import{normalizeToolJsonOutput,normalizeToolModelOutput}from"#harness/tool-model-output.js";import{stashToolInterrupt}from"#harness/tool-interrupts.js";import{resolveWebSearchBackend,resolveWebSearchProviderTool}from"#harness/provider-tools.js";import{isAsyncIterable}from"#shared/async-iterable.js";const toolApprovals=new WeakMap;function buildToolSet(e){let t={},n=e.backgroundBatch??createBackgroundToolCallBatch(),r=e.disabledProviderTools;for(let i of e.tools.values()){if(r?.has(i.name))continue;n.setTool(i.name,i.execution===`background`&&i.execute!==void 0?{executeInput:i.executeInput,name:i.name,nodeId:i.nodeId,workflowId:requireBackgroundWorkflowId(i)}:void 0);let a=i.toModelOutput,o=buildApprovalFn(i,e),s=tool({description:i.description,execute:wrapToolExecute(i,n),inputSchema:i.inputSchema,...i.execution===`background`?{onInputAvailable:({input:e,toolCallId:t})=>{if(i.execute===void 0)throw Error(`Background tool "${i.name}" has no execute function.`);n.register({callId:t,input:e,toolName:i.name})}}:{},outputSchema:i.outputSchema,...i.execute===void 0?a===void 0?{}:{toModelOutput:async({output:e,toolCallId:t})=>normalizeToolModelOutput({output:await a(e),toolCallId:t,toolName:i.name})}:{toModelOutput:async({output:e,toolCallId:t})=>isAuthorizationPendingModelOutput(e)?{type:`text`,value:authorizationPendingModelText(e.connections)}:a===void 0?typeof e==`string`?{type:`text`,value:e}:normalizeToolModelOutput({output:{type:`json`,value:e??null},toolCallId:t,toolName:i.name}):normalizeToolModelOutput({output:await a(e),toolCallId:t,toolName:i.name})}});t[i.name]=s,i.approval!==void 0&&toolApprovals.set(s,o)}return t}function requireBackgroundWorkflowId(e){if(e.workflowId===void 0)throw Error(`Background tool "${e.name}" must be defined with defineWorkflowTool().`);return e.workflowId}function buildToolSetFromDefinitions(e){let t=new Map;for(let n of e.tools)t.has(n.name)||t.set(n.name,n);return buildToolSet({approvedTools:e.approvedTools,backgroundBatch:e.backgroundBatch,disabledProviderTools:e.disabledProviderTools,tools:t})}function wrapToolExecute(e,t=createBackgroundToolCallBatch()){let n=e.execute;if(n!==void 0)return(r,i)=>{let a;try{e.execution===`background`?(t.register({callId:i.toolCallId,input:r,toolName:e.name}),a=executeBackgroundToolCall({batch:t,definition:e,options:i,toolInput:r})):a=n(r,i)}catch(e){return Promise.reject(e)}return isAsyncIterable(a)?normalizeToolExecuteIterable(a,e.name,i):Promise.resolve(a).then(t=>normalizeToolExecuteOutput(t,e.name,i))}}async function*normalizeToolExecuteIterable(e,t,n){for await(let r of e)yield normalizeToolExecuteOutput(r,t,n)}function normalizeToolExecuteOutput(e,t,n){return isAuthorizationSignal(e)?(stashToolInterrupt(loadContext(),n.toolCallId,e),modelFacingAuthorizationOutput(e)):normalizeToolJsonOutput({boundary:`execute`,output:e,toolCallId:n.toolCallId,toolName:t})}async function buildToolSetWithProviderTools(e){let t=e.disabledProviderTools,n={...buildToolSet({approvedTools:e.approvedTools,backgroundBatch:e.backgroundBatch,disabledProviderTools:t,tools:e.tools})};for(let r of e.tools.values()){let i=r.behavior?.handling;if(i?.kind===`provider-tool`&&r.execute===void 0&&!t?.has(r.name)){let t=resolveWebSearchBackend(e.modelReference,i.provider);t===null?delete n[r.name]:n[r.name]=await resolveWebSearchProviderTool(t)}}return n}function buildApprovalFn(e,t){return async(n,r,i)=>{if(e.approval===void 0)return;let a=isObject(n)?n:void 0,o=await resolveApprovalPolicy(e.approval)({...buildCallbackContext(),abortSignal:i??new AbortController().signal,approvedTools:t.approvedTools??new Set,callId:r,toolInput:a,toolName:e.name});return typeof o==`boolean`?o?`user-approval`:`not-applicable`:o}}function buildToolApproval(e,t){return async({toolCall:n})=>{let r=e[n.toolName];return r===void 0?void 0:await toolApprovals.get(r)?.(n.input,n.toolCallId,t)}}export{buildToolApproval,buildToolSet,buildToolSetFromDefinitions,buildToolSetWithProviderTools,wrapToolExecute};