UNPKG

eve

Version:

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

1 lines 5.5 kB
import"#protocol/activity.js";import{normalizePresentationText}from"#shared/presentation-text.js";const MAX_ACTIVITY_EVENT_IDS=1e3,MAX_ACTIVITY_PENDING_SETTLEMENTS=500,MAX_ACTIVITY_ENTITIES=500;function createActivitySnapshot(){return{actions:{},blockers:{},pendingSettlements:{},revision:0,seenEventIds:[],version:1,work:{}}}function reduceActivityBatch(e,t){let n=e,r=e.seenEventIds,i=!1,a=!1;for(let e of t.events){if(r.includes(e.eventId))continue;let t=reduceEvent(n,e);t!==n&&(a=!0,r=appendBounded(r,e.eventId,MAX_ACTIVITY_EVENT_IDS),i||=presentationDiffers(n,t),n=t)}return a?i?{...n,revision:e.revision+1,seenEventIds:r}:{...n,revision:e.revision,seenEventIds:r}:e}function presentationDiffers(e,t){return e.actions!==t.actions||e.blockers!==t.blockers||e.work!==t.work}function reduceEvent(e,t){switch(t.kind){case`work.started`:return startWork(e,t);case`work.settled`:return settleWork(e,t);case`action.started`:return startAction(e,t);case`action.settled`:return settleAction(e,t);case`action.label.updated`:return updateActionLabel(e,t);case`blocker.started`:return startBlocker(e,t);case`blocker.settled`:return settleBlocker(e,t)}}function startWork(e,t){if(e.work[t.work.id]!==void 0)return e;let n=pendingFor(e,`work`,t.work.id),r=t.work.parentId===void 0?void 0:e.work[t.work.parentId],i=n?.outcome??(!isBackgroundWorkBoundary(e,t.work)&&r!==void 0&&r.phase!==`running`?`cancelled`:`running`),a={...t.work,name:t.work.name===void 0?void 0:normalizePresentationText(t.work.name),phase:i,settledAt:n?.settledAt??(i===`cancelled`?r?.settledAt:void 0),startedAt:t.startedAt},o={...e,pendingSettlements:removeKey(e.pendingSettlements,pendingKey(`work`,a.id)),work:replaceBounded(e.work,a.id,a)};return a.phase===`running`?o:settleWorkTree(o,{outcome:a.phase,settledAt:a.settledAt??t.startedAt,workId:a.id})}function settleWork(e,t){let n=e.work[t.workId];return n===void 0?retainPending(e,`work`,t.workId,t):n.phase===`running`?settleWorkTree(e,{outcome:t.outcome,settledAt:t.settledAt,workId:t.workId}):e}function startAction(e,t){if(e.actions[t.action.id]!==void 0)return e;let n=pendingFor(e,`action`,t.action.id),r=e.work[t.action.parentWorkId],i=n?.outcome??(r!==void 0&&r.phase!==`running`?`cancelled`:`running`);return{...e,actions:replaceBounded(e.actions,t.action.id,{...t.action,name:normalizePresentationText(t.action.name),phase:i,settledAt:n?.settledAt??(i===`cancelled`?r?.settledAt:void 0),startedAt:t.startedAt}),pendingSettlements:removeKey(e.pendingSettlements,pendingKey(`action`,t.action.id))}}function updateActionLabel(e,t){let n=e.actions[t.actionId];if(n===void 0)return e;let r=normalizePresentationText(t.label);return n.label===r?e:{...e,actions:replaceBounded(e.actions,t.actionId,{...n,label:r})}}function settleAction(e,t){let n=e.actions[t.actionId];return n===void 0?retainPending(e,`action`,t.actionId,t):n.phase===`running`?{...e,actions:replaceBounded(e.actions,t.actionId,{...n,phase:t.outcome,settledAt:t.settledAt})}:e}function startBlocker(e,t){if(e.blockers[t.blocker.id]!==void 0)return e;let n=pendingFor(e,`blocker`,t.blocker.id),r=e.work[t.blocker.parentWorkId],i=n?.outcome??(r!==void 0&&r.phase!==`running`?`cancelled`:`blocked`);return{...e,blockers:replaceBounded(e.blockers,t.blocker.id,{...t.blocker,label:t.blocker.label===void 0?void 0:normalizePresentationText(t.blocker.label),phase:i,settledAt:n?.settledAt??(i===`cancelled`?r?.settledAt:void 0),startedAt:t.startedAt}),pendingSettlements:removeKey(e.pendingSettlements,pendingKey(`blocker`,t.blocker.id))}}function settleBlocker(e,t){let n=e.blockers[t.blockerId];return n===void 0?retainPending(e,`blocker`,t.blockerId,t):n.phase===`blocked`?{...e,blockers:replaceBounded(e.blockers,t.blockerId,{...n,phase:t.outcome,settledAt:t.settledAt})}:e}function retainPending(e,t,n,r){let i=pendingKey(t,n);return e.pendingSettlements[i]===void 0?{...e,pendingSettlements:replaceBounded(e.pendingSettlements,i,{entityKind:t,eventId:r.eventId,outcome:r.outcome,settledAt:r.settledAt},500)}:e}function pendingFor(e,t,n){return e.pendingSettlements[pendingKey(t,n)]}function pendingKey(e,t){return`${e}:${t}`}function settleWorkTree(e,t){let n=new Set([t.workId]),r=!0;for(;r;){r=!1;for(let t of Object.values(e.work))t.parentId===void 0||!n.has(t.parentId)||n.has(t.id)||isBackgroundWorkBoundary(e,t)||(n.add(t.id),r=!0)}return{...e,actions:mapActivityStates(e.actions,e=>n.has(e.parentWorkId)&&e.phase===`running`?{...e,phase:`cancelled`,settledAt:t.settledAt}:e),blockers:mapActivityStates(e.blockers,e=>n.has(e.parentWorkId)&&e.phase===`blocked`?{...e,phase:`cancelled`,settledAt:t.settledAt}:e),work:mapActivityStates(e.work,e=>e.id===t.workId?{...e,phase:t.outcome,settledAt:t.settledAt}:n.has(e.id)&&e.phase===`running`?{...e,phase:`cancelled`,settledAt:t.settledAt}:e)}}function isBackgroundWorkBoundary(e,t){return t.kind===`task`?!0:t.callId===void 0||t.parentId===void 0?!1:e.actions[`action:${t.parentId}:${t.callId}`]!==void 0}function mapActivityStates(e,t){let n=e;for(let[r,i]of Object.entries(e)){let a=t(i);a!==i&&(n===e&&(n={...e}),n[r]=a)}return n}function replaceBounded(e,t,n,r=500){let i={...e,[t]:n},a=Object.keys(i).length-r;for(let e of Object.keys(i).slice(0,Math.max(0,a)))delete i[e];return i}function removeKey(e,t){if(e[t]===void 0)return e;let n={...e};return delete n[t],n}function appendBounded(e,t,n){return[...e,t].slice(-n)}export{MAX_ACTIVITY_ENTITIES,MAX_ACTIVITY_EVENT_IDS,MAX_ACTIVITY_PENDING_SETTLEMENTS,createActivitySnapshot,reduceActivityBatch};