UNPKG

eve

Version:

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

1 lines 2.51 kB
import{isSessionCommandHookToken,sessionCommandHookToken}from"#execution/session-command-token.js";import{isObject}from"#shared/guards.js";import{SESSION_INBOX_WIRE_VERSION_METADATA_KEY,SessionInboxWireError,WORKFLOW_TASK_AUTHORIZATION_METADATA_KEY,isSessionInboxAddress,isSessionInboxWireVersion}from"#execution/wire/session-inbox-contract.js";import{getHookByToken,getRawHookByToken,resumeHook}from"#internal/workflow/runtime.js";import{HookNotFoundError}from"#compiled/@workflow/errors/index.js";import{sessionInboxWire}from"#execution/wire/session-inbox-encoder.js";async function resumeSessionInbox(e,t){if(typeof e!=`string`){if(!isSessionInboxAddress(e))throw new SessionInboxWireError(`Session inbox target has an invalid address.`);if(!isSessionInboxWireVersion(e.version))throw new SessionInboxWireError(`Session inbox target declares unsupported wire version ${JSON.stringify(e.version)}.`);return await resumeHook(sessionCommandHookToken(e.sessionId),sessionInboxWire.encode(t,{version:e.version}))}let n=e;if(isStableInboxFastPathCompatible(n,t))return await resumeHook(n,sessionInboxWire.encode(t,{variant:`send`,version:0}));let r=await getHookByToken(n),i=await resolveSessionInboxWireTarget(r);return await resumeHook(r,sessionInboxWire.encode(t,i))}function isStableInboxFastPathCompatible(e,t){return!isSessionCommandHookToken(e)||t.kind===`cancel`&&t.tasks===!0?!1:!(`caller`in t&&t.caller?.activityObserver!==void 0)}async function sessionDriverSupportsWorkflowTaskAuthorization(e){let t=await(await getHookByToken(sessionCommandHookToken(e))).metadata;return isObject(t)&&t[WORKFLOW_TASK_AUTHORIZATION_METADATA_KEY]===!0}async function resolveSessionInboxWireTarget(e){let t=await e.metadata;if(isObject(t)&&SESSION_INBOX_WIRE_VERSION_METADATA_KEY in t){let e=t[SESSION_INBOX_WIRE_VERSION_METADATA_KEY];if(isSessionInboxWireVersion(e))return{version:e};throw new SessionInboxWireError(`Session inbox target declares unsupported wire version ${JSON.stringify(e)}.`)}let n=sessionCommandHookToken(e.runId);if(e.token===n)return{variant:`send`,version:0};try{let t=await getRawHookByToken(n);if(t.runId!==e.runId)throw new SessionInboxWireError(`Stable session inbox ${JSON.stringify(n)} belongs to run ${JSON.stringify(t.runId)}, expected ${JSON.stringify(e.runId)}.`);return{variant:`send`,version:0}}catch(e){if(HookNotFoundError.is(e))return{variant:`deliver`,version:0};throw e}}export{resolveSessionInboxWireTarget,resumeSessionInbox,sessionDriverSupportsWorkflowTaskAuthorization};