eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.46 kB
JavaScript
import{createLogger,logError}from"#internal/logging.js";import{BundleKey}from"#runtime/sessions/runtime-context-keys.js";import{requestWorkflowTurnCancellation}from"#execution/workflow-runtime.js";import{deserializeContext}from"#context/serialize.js";import{readDurableSession}from"#execution/durable-session-store.js";import{cancelRemoteAgentTurn,resolveRemoteAgentForAction}from"#subagents/remote-dispatch.js";import{getAgentHandleStore}from"#subagents/handles/store.js";import{getDynamicSubagentSelection}from"#context/dynamic-subagent-lifecycle.js";const log=createLogger(`execution.agent-invocation-cancel`),cancelBackgroundAgentTask=async e=>{if(e.session===void 0||e.serializedContext===void 0)return;let t=e.session,n=getAgentHandleStore(t.state)?.handles.find(t=>t.phase===`claimed`&&t.ownerId===e.entry.taskId);if(n===void 0||n.phase!==`claimed`)return;if(n.address.kind!==`agent/remote`){await requestWorkflowTurnCancellation({sessionId:n.address.sessionId});return}let r=await deserializeContext(e.serializedContext),i=r.require(BundleKey),a=getDynamicSubagentSelection(r,n.identity.nodeId),o=resolveRemoteAgentForAction({dynamicRemoteAgent:a?.kind===`remote`?a.remoteAgent:void 0,nodeId:n.identity.nodeId,registry:i.subagentRegistry.subagentsByNodeId,remoteAgentName:n.identity.name});await cancelRemoteAgentTurn({remote:{...o,url:n.address.url},sessionId:n.address.sessionId})};async function cancelAgentInvocationOwnerStep(e){"use step";let t=await readDurableSession(e.sessionState),n=(getAgentHandleStore(t.state)?.handles??[]).filter(t=>t.phase===`claimed`&&t.ownerId===e.ownerId);if(n.length===0)return;let r=n.some(e=>e.address.kind===`agent/remote`)?await deserializeContext(e.serializedContext):void 0;await Promise.all(n.map(async t=>{try{if(t.address.kind!==`agent/remote`){await requestWorkflowTurnCancellation({sessionId:t.address.sessionId});return}let e=r.require(BundleKey),n=getDynamicSubagentSelection(r,t.identity.nodeId),i=resolveRemoteAgentForAction({dynamicRemoteAgent:n?.kind===`remote`?n.remoteAgent:void 0,nodeId:t.identity.nodeId,registry:e.subagentRegistry.subagentsByNodeId,remoteAgentName:t.identity.name});await cancelRemoteAgentTurn({remote:{...i,url:t.address.url},sessionId:t.address.sessionId})}catch(n){logError(log,`failed to cancel workflow-owned agent turn`,n,{agentId:t.identity.id,childSessionId:t.address.sessionId,ownerId:e.ownerId})}}))}export{cancelAgentInvocationOwnerStep,cancelBackgroundAgentTask};