UNPKG

eve

Version:

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

1 lines 2.65 kB
import{isObject}from"#shared/guards.js";import{loadContext}from"#context/container.js";import{tool}from"ai";import{authorizationPendingModelText,isAuthorizationPendingModelOutput,isAuthorizationSignal,modelFacingAuthorizationOutput}from"#harness/authorization.js";import{isCodeModeToolExecutionOptions}from"#runtime/framework-tools/code-mode-connection-auth.js";import{stashToolInterrupt}from"#harness/tool-interrupts.js";import{ASK_QUESTION_TOOL_NAME}from"#runtime/framework-tools/ask-question.js";import{WEB_SEARCH_TOOL_DEFINITION}from"#runtime/framework-tools/web-search.js";import{resolveWebSearchBackend,resolveWebSearchProviderTool}from"#harness/provider-tools.js";function buildToolSet(e){let t={},a=e.capabilities?.requestInput===!0,o=e.disabledProviderTools;for(let s of e.tools.values()){if(s.name===ASK_QUESTION_TOOL_NAME&&!a||o?.has(s.name))continue;let c=s.toModelOutput;t[s.name]=tool({description:s.description,execute:wrapToolExecute(s),inputSchema:s.inputSchema,needsApproval:buildNeedsApprovalFn(s,e),outputSchema:s.outputSchema,...s.execute===void 0?c===void 0?{}:{toModelOutput:({output:e})=>c(e)}:{toModelOutput:({output:e})=>isAuthorizationPendingModelOutput(e)?{type:`text`,value:authorizationPendingModelText(e.connections)}:c===void 0?typeof e==`string`?{type:`text`,value:e}:{type:`json`,value:e??null}:c(e)}})}return t}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,capabilities:e.capabilities,disabledProviderTools:e.disabledProviderTools,tools:t})}function wrapToolExecute(e){let n=e.execute;if(n!==void 0)return async(e,r)=>{let i=await n(e);return!isAuthorizationSignal(i)||isCodeModeToolExecutionOptions(r)?i:(stashToolInterrupt(loadContext(),r.toolCallId,i),modelFacingAuthorizationOutput(i))}}async function buildToolSetWithProviderTools(e){let t=e.disabledProviderTools,n={...buildToolSet({approvedTools:e.approvedTools,capabilities:e.capabilities,disabledProviderTools:t,tools:e.tools})};if(!t?.has(WEB_SEARCH_TOOL_DEFINITION.name)){let t=e.tools.get(WEB_SEARCH_TOOL_DEFINITION.name);if(t!==void 0&&t.execute===void 0){let t=resolveWebSearchBackend(e.modelReference);t===null?delete n[WEB_SEARCH_TOOL_DEFINITION.name]:n[WEB_SEARCH_TOOL_DEFINITION.name]=await resolveWebSearchProviderTool(t)}}return n}function buildNeedsApprovalFn(t,n){return async r=>{if(t.needsApproval===void 0)return!1;let i=isObject(r)?r:void 0;return t.needsApproval({approvedTools:n.approvedTools??new Set,toolInput:i,toolName:t.name})}}export{buildToolSet,buildToolSetFromDefinitions,buildToolSetWithProviderTools,wrapToolExecute};