UNPKG

eve

Version:

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

1 lines 2.2 kB
import{getHarnessEmissionState}from"#harness/emission.js";import{projectToDurableSession}from"#execution/session.js";import{hasProxyInputRequests}from"#harness/proxy-input-requests.js";import{migrateDurableSessionSnapshot}from"#execution/durable-session-migrations/snapshot.js";const EVE_SESSION_STREAM_NAMESPACE=`eve.session`,DURABLE_SESSION_VERSION=1,DURABLE_SESSION_READ_TIMEOUT_MS=1e4;function projectSessionState(t){return{continuationToken:t.session.continuationToken,emissionState:getHarnessEmissionState(t.session.state),hasProxyInputRequests:hasProxyInputRequests(t.session.state),sessionId:t.session.sessionId,version:1}}async function readDurableSession(e){if(e.snapshot!==void 0)return migrateDurableSessionSnapshot(e.snapshot).session;let{getRun:t}=await import(`#compiled/@workflow/core/runtime.js`),n=t(e.sessionId).getReadable({namespace:EVE_SESSION_STREAM_NAMESPACE,startIndex:-1}).getReader(),r=`eve durable session tail read failed`,i;try{let t=await Promise.race([n.read().then(e=>({kind:`read`,read:e})),new Promise(e=>{i=setTimeout(()=>e({kind:`timeout`}),DURABLE_SESSION_READ_TIMEOUT_MS)})]);if(t.kind===`timeout`)throw r=`eve durable session tail read timed out after ${DURABLE_SESSION_READ_TIMEOUT_MS}ms`,new DurableSessionReadTimeoutError(e);if(t.read.done||t.read.value===void 0)throw r=`eve durable session tail read returned no snapshot`,Error(`No durable session snapshot found in stream "${EVE_SESSION_STREAM_NAMESPACE}" for run ${e.sessionId}.`);return r=`eve durable session tail read complete`,migrateDurableSessionSnapshot(t.read.value).session}finally{i!==void 0&&clearTimeout(i),await n.cancel(r).catch(()=>{}),n.releaseLock()}}var DurableSessionReadTimeoutError=class extends Error{constructor(e){super(`Timed out reading durable session snapshot from stream "${EVE_SESSION_STREAM_NAMESPACE}" for run ${e.sessionId} after ${DURABLE_SESSION_READ_TIMEOUT_MS}ms.`),this.name=`DurableSessionReadTimeoutError`}};function createDurableSessionState(e){let n={session:projectToDurableSession(e.session),version:1};return{...projectSessionState({session:e.session}),snapshot:n}}export{DURABLE_SESSION_VERSION,createDurableSessionState,projectSessionState,readDurableSession};