eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 5.19 kB
JavaScript
import{EMPTY_AGENT_HANDLE_STORE,formatAgentStatus,getAgentHandleStore,writeHandles}from"#subagents/handles/store.js";function prepareAgentStart(e,t){let n=getAgentHandleStore(e.state)?.handles??[];if(n.some(e=>e.identity.id===t.identity.id))throw Error(`Agent handle "${t.identity.id}" already exists.`);return writeHandles(e,[...n,{identity:t.identity,operation:t.operation,phase:`starting`,target:t.target}])}function findActiveHandle(e,t){return e.find(e=>(e.phase===`starting`||e.phase===`running`)&&e.operation.id===t)}function confirmAgentStarted(e,t){let n=getAgentHandleStore(e.state)?.handles??[],r=findActiveHandle(n,t.operationId);if(r===void 0)throw Error(`No prepared agent handle for operation "${t.operationId}".`);return r.phase===`running`?e:writeHandles(e,n.map(e=>e===r?{address:t.address,identity:r.identity,operation:r.operation,phase:`running`}:e))}function rejectAgentEffect(e,t){let n=getAgentHandleStore(e.state)?.handles??[],r=findActiveHandle(n,t.operationId);if(r===void 0)return e;if(t.disposition===`retryable`&&r.phase===`running`){let{operation:t}=r;if(t.kind===`continue`)return writeHandles(e,n.map(e=>e===r?{address:r.address,identity:r.identity,lastStatus:t.previousStatus,phase:`parked`}:e))}return writeHandles(e,n.filter(e=>e!==r))}function abandonRunningAgentTurns(e){let t=getAgentHandleStore(e.state)?.handles??[];return t.some(e=>e.phase===`running`)?writeHandles(e,t.map(e=>e.phase===`running`?{address:e.address,identity:e.identity,lastStatus:`(cancelled)`,phase:`parked`}:e)):e}function settleAgentTurn(e,t){let n=getAgentHandleStore(e.state)?.handles??[],r=n.find(e=>e.phase===`running`&&e.operation.id===t.operationId);if(r===void 0||r.phase!==`running`)return{kind:`ignored`,reason:`unknown-operation`};if(t.outcome.kind===`terminal`)return{kind:`settled`,session:writeHandles(e,n.filter(e=>e!==r))};let{result:i}=t.outcome,a=i.kind===`succeeded`?formatAgentStatus(i.output):i.kind===`failed`?formatAgentStatus(i.error):`(cancelled)`;return{kind:`settled`,session:writeHandles(e,n.map(e=>e===r?{address:r.address,identity:r.identity,lastStatus:a,phase:`parked`}:e))}}function applyAgentHandleStoreCommand(e,t){switch(t.kind){case`read`:return{result:{kind:`ready`},store:e};case`reserve`:{let n=e.handles.find(e=>e.identity.id===t.identity.id);if(n!==void 0)return(n.phase===`reserved`||n.phase===`claimed`)&&n.operationId===t.operationId&&n.ownerId===t.ownerId?{result:{handle:n,kind:`ready`},store:e}:{result:{handle:n,kind:`busy`},store:e};let r={callId:t.callId,identity:t.identity,operationId:t.operationId,phase:`reserved`,ownerId:t.ownerId};return{result:{handle:r,kind:`ready`},store:{handles:[...e.handles,r]}}}case`confirm`:{let n=e.handles.find(e=>(e.phase===`claimed`||e.phase===`reserved`)&&e.operationId===t.operationId&&e.ownerId===t.ownerId);return n===void 0?{result:{kind:`unknown`},store:e}:n.phase===`claimed`?{result:{handle:n,kind:`ready`},store:e}:replaceHandle(e,n,{address:t.address,callId:n.callId,identity:n.identity,operationId:n.operationId,phase:`claimed`,ownerId:n.ownerId})}case`claim`:{let n=e.handles.find(e=>e.identity.id===t.agentId);return n===void 0?{result:{kind:`unknown`},store:e}:n.phase===`starting`||n.phase===`running`||n.identity.name!==t.invokedName||n.phase!==`reserved`&&n.address.kind===`agent/remote`!=(t.expectedTarget===`remote`)?{result:{handle:n,kind:`mismatch`},store:e}:n.phase===`claimed`?n.operationId===t.operationId&&n.ownerId===t.ownerId?{result:{handle:n,kind:`ready`},store:e}:{result:{handle:n,kind:`busy`},store:e}:n.phase===`reserved`?{result:{handle:n,kind:`busy`},store:e}:replaceHandle(e,n,{address:n.address,callId:t.callId,identity:n.identity,operationId:t.operationId,phase:`claimed`,ownerId:t.ownerId})}case`remove`:{let n=e.handles.find(e=>e.identity.id===t.agentId);return n===void 0?{result:{kind:`ready`},store:e}:n.phase===`claimed`&&n.ownerId!==t.ownerId?{result:{handle:n,kind:`busy`},store:e}:{result:{kind:`ready`},store:{handles:e.handles.filter(e=>e!==n)}}}case`release-owner`:{let n=e.handles.flatMap(e=>e.phase===`reserved`&&e.ownerId===t.ownerId?[]:e.phase!==`claimed`||e.ownerId!==t.ownerId?[e]:[{address:e.address,identity:e.identity,phase:`available`}]);return{result:{kind:`ready`},store:handlesEqual(e.handles,n)?e:{handles:n}}}}}function abandonAgentInvocationOwners(e,t){let n=getAgentHandleStore(e.state)?.handles??[],r=n.flatMap(e=>e.phase===`reserved`&&t.has(e.ownerId)?[]:e.phase!==`claimed`||!t.has(e.ownerId)?[e]:[{address:e.address,identity:e.identity,lastStatus:`(cancelled)`,phase:`parked`}]);return handlesEqual(n,r)?e:writeHandles(e,r)}function applyTaskAgentHandleCommand(e,t){let n=getAgentHandleStore(e.state)??EMPTY_AGENT_HANDLE_STORE,r=applyAgentHandleStoreCommand(n,t);return{result:r.result,session:r.store===n?e:writeHandles(e,r.store.handles)}}function replaceHandle(e,t,n){return{result:{handle:n,kind:`ready`},store:{handles:e.handles.map(e=>e===t?n:e)}}}function handlesEqual(e,t){return e.length===t.length&&e.every((e,n)=>e===t[n])}export{abandonAgentInvocationOwners,abandonRunningAgentTurns,applyAgentHandleStoreCommand,applyTaskAgentHandleCommand,confirmAgentStarted,prepareAgentStart,rejectAgentEffect,settleAgentTurn};