eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.3 kB
JavaScript
import{createLogger,logError}from"#internal/logging.js";import{cancelRun,getRun,getWorld,resumeHook}from"#internal/workflow/runtime.js";import{isTaskWorkflowTargetGone}from"#execution/tasks/workflow-target.js";import{WORKFLOW_CANCELLATION_SETTLE_MS}from"#execution/tools/workflow/cancellation-policy.js";const log=createLogger(`execution.workflow-tool-run`);async function cancelWorkflowToolRun(e,t){let n={kind:`cancel`,reason:t},r=!1;try{await resumeHook(e.hookToken,n),r=!0}catch(t){isTaskWorkflowTargetGone(t)||logError(log,`failed to signal a workflow tool run to cancel; cancelling it outright`,t,{runId:e.runId})}try{await settleWorkflowToolRunCancellation(e.runId,t,r)}catch(t){if(isTaskWorkflowTargetGone(t))return;logError(log,`failed to cancel workflow tool run; it may run to completion`,t,{runId:e.runId})}}async function settleWorkflowToolRunCancellation(e,t,n=!0){if(n){let t=Date.now()+WORKFLOW_CANCELLATION_SETTLE_MS;for(;Date.now()<t;){try{let t=await getRun(e).status;if(t!==`pending`&&t!==`running`)return}catch(e){if(isTaskWorkflowTargetGone(e))return;throw e}await new Promise(e=>setTimeout(e,250))}}try{await cancelRun(await getWorld(),e,{cancelReason:t})}catch(e){if(isTaskWorkflowTargetGone(e))return;throw e}}export{cancelWorkflowToolRun,settleWorkflowToolRunCancellation};