UNPKG

eve

Version:

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

1 lines • 2.07 kB
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=>{e.session!==void 0&&e.serializedContext!==void 0&&await cancelAgentInvocationOwner({ownerId:e.entry.task.taskId,serializedContext:e.serializedContext,session:e.session})};async function cancelAgentInvocationOwnerStep(e){"use step";try{await cancelAgentInvocationOwner({ownerId:e.ownerId,serializedContext:e.serializedContext,session:readDurableSession(e.sessionState)})}catch(t){logError(log,`failed to cancel workflow-owned agent turn`,t,{ownerId:e.ownerId})}}async function cancelAgentInvocationOwner(e){let t=(getAgentHandleStore(e.session.state)?.handles??[]).filter(t=>t.phase===`claimed`&&t.ownerId===e.ownerId);if(t.length===0)return;let n,r=(await Promise.allSettled(t.map(async t=>{if(t.address.kind!==`agent/remote`){await requestWorkflowTurnCancellation({sessionId:t.address.sessionId});return}n??=deserializeContext(e.serializedContext);let r=await n,i=r.require(BundleKey),a=getDynamicSubagentSelection(r,t.identity.nodeId),o=resolveRemoteAgentForAction({dynamicRemoteAgent:a?.kind===`remote`?a.remoteAgent:void 0,nodeId:t.identity.nodeId,registry:i.subagentRegistry.subagentsByNodeId,remoteAgentName:t.identity.name});await cancelRemoteAgentTurn({remote:{...o,url:t.address.url},sessionId:t.address.sessionId})}))).filter(e=>e.status===`rejected`);if(r.length>0)throw AggregateError(r.map(e=>e.reason),`Failed to cancel owned agent turns.`)}export{cancelAgentInvocationOwnerStep,cancelBackgroundAgentTask};