UNPKG

eve

Version:

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

1 lines 4.18 kB
import{isObject}from"#shared/guards.js";import{parseJsonValue}from"#shared/json.js";import{loadContext}from"#context/container.js";import{buildCallbackContext}from"#context/build-callback-context.js";import{tool}from"ai";import{authorizationPendingModelText,isAuthorizationPendingModelOutput,isAuthorizationSignal,modelFacingAuthorizationOutput}from"#harness/authorization.js";import{withToolOutputSerializationError}from"#harness/tool-output-serialization.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";const toolApprovals=new WeakMap;function buildToolSet(e){let t={},n=e.capabilities?.requestInput===!0,r=e.disabledProviderTools;for(let s of e.tools.values()){if(s.name===ASK_QUESTION_TOOL_NAME&&!n||r?.has(s.name))continue;let c=s.toModelOutput,l=buildApprovalFn(s,e),u=tool({description:s.description,execute:wrapToolExecute(s),inputSchema:s.inputSchema,outputSchema:s.outputSchema,...s.execute===void 0?c===void 0?{}:{toModelOutput:async({output:e,toolCallId:t})=>normalizeToolModelOutput({output:await c(e),toolCallId:t,toolName:s.name})}:{toModelOutput:async({output:e,toolCallId:t})=>isAuthorizationPendingModelOutput(e)?{type:`text`,value:authorizationPendingModelText(e.connections)}:c===void 0?typeof e==`string`?{type:`text`,value:e}:normalizeToolModelOutput({output:{type:`json`,value:e??null},toolCallId:t,toolName:s.name}):normalizeToolModelOutput({output:await c(e),toolCallId:t,toolName:s.name})}});t[s.name]=u,s.approval!==void 0&&toolApprovals.set(u,l)}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 t=e.execute;if(t!==void 0)return async(r,i)=>{let a=await t(r,i);return isAuthorizationSignal(a)?(stashToolInterrupt(loadContext(),i.toolCallId,a),modelFacingAuthorizationOutput(a)):normalizeToolJsonOutput({boundary:`execute`,output:a,toolCallId:i.toolCallId,toolName:e.name})}}function normalizeToolJsonOutput(e){let n=e.output===void 0?null:e.output;return withToolOutputSerializationError(e,()=>(parseJsonValue(n),n))}function normalizeToolModelOutput(e){return withToolOutputSerializationError({boundary:`toModelOutput`,toolCallId:e.toolCallId,toolName:e.toolName},()=>{if(e.output===null||typeof e.output!=`object`)throw TypeError(`Expected a tool model output object.`);let t=e.output;if(t.type===`text`){if(typeof t.value!=`string`)throw TypeError(`Expected text model output to include a string "value".`);return{type:`text`,value:t.value}}if(t.type===`json`)return{type:`json`,value:normalizeToolJsonOutput({boundary:`toModelOutput`,output:t.value,toolCallId:e.toolCallId,toolName:e.toolName})};throw TypeError(`Expected tool model output type to be "text" or "json".`)})}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 buildApprovalFn(t,n){return async(i,a)=>{if(t.approval===void 0)return;let o=isObject(i)?i:void 0,s=await t.approval({...buildCallbackContext(),approvedTools:n.approvedTools??new Set,callId:a,toolInput:o,toolName:t.name});return typeof s==`boolean`?s?`user-approval`:`not-applicable`:s}}function buildToolApproval(e){return async({toolCall:t})=>{let n=e[t.toolName];return n===void 0?void 0:await toolApprovals.get(n)?.(t.input,t.toolCallId)}}export{buildToolApproval,buildToolSet,buildToolSetFromDefinitions,buildToolSetWithProviderTools,wrapToolExecute};