UNPKG

eve

Version:

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

1 lines 1.75 kB
import{sessionCommandHookToken}from"#execution/session-command-token.js";import{walkCauseChain}from"#shared/errors.js";import{cancelRun,getRawHookByToken,getWorld}from"#internal/workflow/runtime.js";import{EntityConflictError,HookNotFoundError,RunExpiredError,WorkflowRunNotFoundError}from"#compiled/@workflow/errors/index.js";import{resumeSessionInbox}from"#execution/wire/session-inbox-resume.js";async function settleContinuationConflictStep(e){"use step";try{e.command!==void 0&&await forwardCommand({command:e.command,continuationToken:e.continuationToken,ownerSessionId:e.ownerSessionId})}finally{e.activityCollectorRunId!==void 0&&await cancelCollector(e.activityCollectorRunId)}}async function forwardCommand(e){try{await resumeSessionInbox(e.continuationToken,e.command)}catch(t){if(!HookNotFoundError.is(t))throw t;let n=await resolveOwnerSessionId(e);await resumeSessionInbox(sessionCommandHookToken(n),e.command)}}async function resolveOwnerSessionId(e){if(e.ownerSessionId!==void 0)return e.ownerSessionId;try{let t=await getRawHookByToken(e.continuationToken);if(typeof t.runId==`string`&&t.runId.length>0)return t.runId}catch(e){if(!HookNotFoundError.is(e))throw e}let t=e.command.requestId===void 0?``:` ${JSON.stringify(e.command.requestId)}`;throw Error(`Unable to forward losing candidate delivery${t}: continuation owner could not be resolved.`)}async function cancelCollector(e){try{await cancelRun(await getWorld(),e,{cancelReason:`Session candidate did not acquire continuation ownership`})}catch(e){if(!isInactiveRun(e))throw e}}function isInactiveRun(e){for(let t of walkCauseChain(e))if(WorkflowRunNotFoundError.is(t)||RunExpiredError.is(t)||EntityConflictError.is(t))return!0;return!1}export{settleContinuationConflictStep};