UNPKG

eve

Version:

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

1 lines 5.05 kB
import{createLogger,logError}from"#internal/logging.js";import{callAdapterEventHandler}from"#channel/adapter.js";import{AuthKey,CapabilitiesKey,ChannelInstrumentationKey,InitiatorAuthKey}from"#context/keys.js";import{toErrorMessage}from"#shared/errors.js";import{createSubagentCalledEvent,encodeMessageStreamEvent,timestampHandleMessageStreamEvent}from"#protocol/message.js";import{BundleKey,ChannelKey}from"#runtime/sessions/runtime-context-keys.js";import{deserializeContext}from"#context/serialize.js";import{createDurableSessionState,readDurableSession}from"#execution/durable-session-store.js";import{resolveRemoteAgentForAction,startRemoteAgentSession}from"#execution/remote-agent-dispatch.js";import{createWorkflowRuntime,workflowEntryReference}from"#execution/workflow-runtime.js";import{getPendingRuntimeActionBatch,recordPendingSubagentChild}from"#harness/runtime-actions.js";import{hydrateDurableSession}from"#execution/session.js";import{buildAdapterContext}from"#channel/adapter-context.js";import{buildSubagentRunInput}from"#execution/subagent-tool.js";import{resolveSubagentDepth}from"#harness/subagent-depth.js";const log=createLogger(`execution.dispatch-runtime-actions`);async function dispatchRuntimeActionsStep(e){"use step";let f=await readDurableSession(e.sessionState),p=getPendingRuntimeActionBatch(f.state);if(p===void 0||p.actions.length===0)return{results:[],sessionState:e.sessionState};let m=await deserializeContext(e.serializedContext),h=m.require(BundleKey),g=hydrateDurableSession({compactionOverrides:{thresholdPercent:h.resolvedAgent.config.compaction?.thresholdPercent},durable:f,turnAgent:h.turnAgent}),_=m.require(ChannelKey),v=m.get(AuthKey)??null,y=m.get(CapabilitiesKey),b=m.get(ChannelInstrumentationKey),x=m.get(InitiatorAuthKey)??null,S=e.parentWritable.getWriter(),C=buildAdapterContext(_,m),w=resolveSubagentDepth(g),T=p.actions.filter(e=>e.kind===`subagent-call`).length,E=g,D=[];try{for(let r of p.actions){if(isRecursiveAgentAction(r,h.subagentRegistry.subagentsByNodeId)&&(g.rootSessionId!==void 0||w.currentDepth>0)){log.warn(`recursive agent call blocked outside the root session`,{callId:r.callId,currentDepth:w.currentDepth,nodeId:r.nodeId,subagentName:r.subagentName}),D.push(createRecursiveAgentRootOnlyResult(r));continue}let i,a,o,u;switch(r.kind){case`subagent-call`:{let n=h.subagentRegistry.subagentsByNodeId.get(r.nodeId),o=n?.definition.kind===`subagent`?{description:n.definition.description,type:`local`}:{type:`runtime`},s=createWorkflowRuntime({compiledArtifactsSource:h.compiledArtifactsSource,nodeId:r.nodeId}),{childContinuationToken:c,runInput:l}=buildSubagentRunInput({action:r,auth:v,batchEvent:p.event,capabilities:y,channelMetadata:b,fanoutSize:T,initiatorAuth:x,parentContinuationToken:e.parentContinuationToken,session:g,source:o});try{i=(await s.run(l)).sessionId}catch(e){logError(log,`local subagent start failed`,e,{callId:r.callId,nodeId:r.nodeId,subagentName:r.subagentName}),D.push({callId:r.callId,isError:!0,kind:`subagent-result`,output:{code:`SUBAGENT_START_FAILED`,message:toErrorMessage(e)},subagentName:r.subagentName});continue}E=recordPendingSubagentChild({callId:r.callId,child:{continuationToken:c,kind:`local`,sessionId:i},session:E}),a=r.name,u=r.subagentName;break}case`remote-agent-call`:{let n;try{n=resolveRemoteAgentForAction({nodeId:r.nodeId,remoteAgentName:r.remoteAgentName,registry:h.subagentRegistry.subagentsByNodeId}),i=await startRemoteAgentSession({action:r,callbackBaseUrl:e.callbackBaseUrl,callbackToken:e.parentContinuationToken,remote:n,session:g})}catch(e){logError(log,`remote agent start failed`,e,{remoteAgentName:r.remoteAgentName,nodeId:r.nodeId,callId:r.callId}),D.push(createRemoteAgentStartFailureResult({action:r,error:e}));continue}a=r.name,o={url:n.url},u=r.remoteAgentName,E=recordPendingSubagentChild({callId:r.callId,child:{kind:`remote`,sessionId:i},session:E});break}default:throw Error(`Unsupported runtime action kind "${r.kind}" in workflow runtime.`)}let d=await callAdapterEventHandler(_,createSubagentCalledEvent({callId:r.callId,childSessionId:i,name:a,remote:o,sequence:p.event.sequence,sessionId:g.sessionId,toolName:u,turnId:p.event.turnId,workflowId:workflowEntryReference.workflowId}),C);await S.write(encodeMessageStreamEvent(timestampHandleMessageStreamEvent(d)))}}finally{S.releaseLock()}return{results:D,sessionState:E===g?e.sessionState:createDurableSessionState({session:E})}}function createRemoteAgentStartFailureResult(e){return{callId:e.action.callId,isError:!0,kind:`subagent-result`,output:{code:`REMOTE_AGENT_START_FAILED`,message:toErrorMessage(e.error)},subagentName:e.action.remoteAgentName}}function createRecursiveAgentRootOnlyResult(e){return{callId:e.callId,isError:!0,kind:`subagent-result`,output:{code:`RECURSIVE_AGENT_ROOT_ONLY`,message:`The built-in "agent" tool is only available to the root session.`},subagentName:e.subagentName}}function isRecursiveAgentAction(e,t){return e.kind===`subagent-call`&&e.subagentName===`agent`&&!t.has(e.nodeId)}export{dispatchRuntimeActionsStep};