UNPKG

eve

Version:

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

1 lines 1.06 kB
import{createActionsRequestedEvent}from"#protocol/message.js";import{collectActionPresentation}from"#harness/action-presentation.js";function createProviderStreamActionBatch(e){let t=new Map,n=Promise.resolve(),r,i,a=!1,o,emitPendingActions=async()=>{if(a){t.clear();return}if(t.size===0)return;let n=[...t.values()];t.clear(),await e.emitFn(createActionsRequestedEvent({actions:n.map(({action:e})=>e),presentation:collectActionPresentation(n),sequence:e.state.sequence,stepIndex:e.state.stepIndex,turnId:e.state.turnId}))},scheduleFlush=()=>{if(a||i!==void 0)return;let e,t=new Promise(t=>{e=t});o=e,i=setTimeout(()=>{i=void 0,o=void 0,e?.()},0),n=n.then(()=>t).then(emitPendingActions).catch(e=>{r??=e})},releaseFlushTimer=()=>{if(i===void 0)return;clearTimeout(i),i=void 0;let e=o;o=void 0,e?.()};return{async cancel(){a=!0,t.clear(),releaseFlushTimer(),await n},observe(e){a||(t.set(e.action.callId,e),scheduleFlush())},async flush(){if(!a){if(releaseFlushTimer(),await n,r!==void 0)throw r;await emitPendingActions()}}}}export{createProviderStreamActionBatch};