eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.85 kB
JavaScript
import{createEveCallbackRoutePath}from"#protocol/routes.js";import{createLogger,logError}from"#internal/logging.js";import{createWorkflowRuntime}from"#execution/workflow-runtime.js";import{continueRemoteAgentSession,isAmbiguousRemoteAgentContinueError,isRetryableRemoteAgentContinueError,resolveRemoteAgentForAction}from"#subagents/remote-dispatch.js";import{AGENT_UNREACHABLE}from"#subagents/agent-handle-errors.js";import{createWorkflowCallbackUrl}from"#execution/workflow-callback-url.js";import{readTaskIdFromInboxToken}from"#tasks/task-inbox-token.js";import{isRuntimeNoActiveSessionError}from"#execution/runtime-errors.js";import{normalizeRequestedOutputSchema}from"#subagents/invocation.js";import{err,ok}from"#shared/result.js";const log=createLogger(`execution.agent-handle-dispatch`);function isAgentHandleAction(e){return e.kind===`subagent-call`||e.kind===`remote-agent-call`}async function dispatchToClaimedAgentAddress(e){return await dispatchToAgentAddress(e)}async function dispatchToAgentAddress(e){let{action:t,handle:n}=e,r=n.identity.id,i=await deliverToAgentAddress({action:t,address:n.address,auth:e.auth,bundle:e.bundle,identity:n.identity,parentToken:e.parentToken,taskId:e.taskId});if(!i.ok){let{cause:a,permanent:o}=i.error;return logError(log,`task agent delivery failed`,a,{agentId:r,callId:t.callId,nodeId:n.identity.nodeId,permanent:o,subagentName:n.identity.name}),{deliveryAmbiguous:i.error.deliveryAmbiguous,deliveryPermanent:o,kind:`error`,result:createAgentErrorResult({action:t,code:AGENT_UNREACHABLE,message:o?`Agent "${n.identity.name}" with id "${r}" is no longer reachable.`:`Agent "${n.identity.name}" with id "${r}" is temporarily unreachable. Try again.`}),session:e.currentSession}}return{address:n.address,callId:t.callId,kind:`called`,name:t.name,session:e.currentSession,toolName:n.identity.name}}async function deliverToAgentAddress(e){let{action:t,address:n,bundle:r,identity:i}=e;if(n.kind===`agent/remote`){let a;try{a=resolveRemoteAgentForAction({nodeId:i.nodeId,remoteAgentName:i.name,registry:r.subagentRegistry.subagentsByNodeId})}catch(e){return err({cause:e,deliveryAmbiguous:!1,permanent:!0})}try{await continueRemoteAgentSession({auth:e.auth,callback:{callId:t.callId,subagentName:i.name,taskId:e.taskId??readTaskIdFromInboxToken(e.parentToken),token:e.parentToken,url:createWorkflowCallbackUrl(n.callbackBaseUrl,createEveCallbackRoutePath(e.parentToken))},message:readSubagentMessage(t),outputSchema:normalizeRequestedOutputSchema(t.input.outputSchema),remote:{...a,url:n.url},sessionId:n.sessionId})}catch(e){return err({cause:e,deliveryAmbiguous:isAmbiguousRemoteAgentContinueError(e),permanent:!isRetryableRemoteAgentContinueError(e)})}return ok(void 0)}let a=createWorkflowRuntime({compiledArtifactsSource:r.compiledArtifactsSource,nodeId:i.nodeId});try{if((await a.dispatchSession({command:{auth:e.auth,caller:{callId:t.callId,replyTo:{kind:`hook`,token:e.parentToken},subagentName:i.name,taskId:e.taskId??readTaskIdFromInboxToken(e.parentToken)},kind:`send`,payload:{message:readSubagentMessage(t),outputSchema:normalizeRequestedOutputSchema(t.input.outputSchema)}},sessionId:n.sessionId})).status===`session_not_active`)return err({cause:Error(`Agent session "${n.sessionId}" is no longer active.`),deliveryAmbiguous:!1,permanent:!0})}catch(e){let t=isRuntimeNoActiveSessionError(e);return err({cause:e,deliveryAmbiguous:!t,permanent:t})}return ok(void 0)}function createAgentErrorResult(e){return{callId:e.action.callId,isError:!0,kind:`subagent-result`,origin:`dispatch`,output:{code:e.code,message:e.message},subagentName:e.action.kind===`remote-agent-call`?e.action.remoteAgentName:e.action.subagentName}}function readSubagentMessage(e){return typeof e.input.message==`string`?e.input.message:``}export{createAgentErrorResult,dispatchToClaimedAgentAddress,isAgentHandleAction};