UNPKG

eve

Version:

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

1 lines • 2.26 kB
import{isObject}from"#shared/guards.js";import{SESSION_INBOX_SESSION_ID_METADATA_KEY,isSessionInboxAddress,sessionCommandHookToken,sessionHandoffMarkerToken,sessionInboxHookToken}from"#execution/session-inbox/address.js";import{getHookByToken,resumeHook}from"#internal/workflow/runtime.js";import{HookNotFoundError}from"#compiled/@workflow/errors/index.js";import{UnsupportedLegacySessionError,resolveLegacyInbox,resumeLegacyInbox}from"#execution/legacy-session/inbox.js";async function resumeSessionInbox(e,t){let n=logicalToken(e),r=Date.now()+5e3;for(;;){let i;try{i=await resumeHook(sessionInboxHookToken(n),t)}catch(e){if(!HookNotFoundError.is(e))throw e;if(await isHandoffInProgress(n,r))continue;return await resumeLegacyInbox(n,t).catch(rethrowUnsupportedAsNotFound)}let a;return{ownerRunId:i.runId,get sessionId(){return a??=(async()=>{try{return typeof e==`string`?requireSessionId(await i.metadata):e.sessionId}catch(e){throw new AcceptedSessionIdentityError(e)}})()}}}}var AcceptedSessionIdentityError=class extends Error{constructor(e){super(`Session command accepted, but its session identity could not be resolved.`,{cause:e}),this.name=`AcceptedSessionIdentityError`}};function requireSessionId(e){let t=isObject(e)?e[SESSION_INBOX_SESSION_ID_METADATA_KEY]:void 0;if(typeof t!=`string`||t.length===0)throw Error(`Session hook metadata is missing its session ID.`);return t}async function resolveSessionInbox(e){try{return{sessionId:requireSessionId(await(await getHookByToken(sessionInboxHookToken(e))).metadata)}}catch(t){if(!HookNotFoundError.is(t))throw t;return{sessionId:(await resolveLegacyInbox(e)).sessionId}}}function rethrowUnsupportedAsNotFound(e){throw e instanceof UnsupportedLegacySessionError?new HookNotFoundError(e.message):e}function logicalToken(e){if(typeof e==`string`)return e;if(!isSessionInboxAddress(e))throw Error(`Session inbox target has an invalid address.`);return sessionCommandHookToken(e.sessionId)}async function isHandoffInProgress(e,t){if(Date.now()>=t)return!1;try{await getHookByToken(sessionHandoffMarkerToken(e))}catch(e){if(HookNotFoundError.is(e))return!1;throw e}return await new Promise(e=>setTimeout(e,20)),!0}export{AcceptedSessionIdentityError,requireSessionId,resolveSessionInbox,resumeSessionInbox};