eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.79 kB
JavaScript
import{sleep}from"#compiled/@workflow/core/index.js";import{normalizeSerializableError}from"#execution/workflow-errors.js";import{openWorkflowToolRunOwnerInbox}from"#execution/tools/workflow/owner.js";import{WORKFLOW_CANCELLATION_CLEANUP_MS}from"#execution/tools/workflow/cancellation-policy.js";import{createChannelReader,raceChannelReads}from"#execution/tools/workflow/owner-channels.js";import{createWorkflowBodyRef,executeWorkflowBody}from"#execution/tools/workflow/body.js";import{createBackgroundWorkflowOwner}from"#execution/tools/workflow/workflow-owner-background.js";import{createBlockingWorkflow}from"#execution/tools/workflow/workflow-owner-blocking.js";async function workflowToolRunWorkflow(e){"use workflow";let t=`workflow`in e?await createBackgroundWorkflowOwner(e):createBlockingWorkflow(e);if(t===void 0)return;let n=`workflow`in e?{...e.workflow,execution:`background`}:{...e,execution:e.execution??`blocking`},{signal:r}=t,i=t.kind===`turn`,a=!0,o,s=0,c,l,u;for(;;){if(i&&r.aborted){if(o===void 0)break;l??=sleep(WORKFLOW_CANCELLATION_CLEANUP_MS).then(()=>`cancel`)}if(c!==void 0&&s>=c.reportCount&&t.commands.landed.length===0&&t.commands.failure===void 0){u=c.outcome;break}let e;try{if(i&&o===void 0){let e=openWorkflowToolRunOwnerInbox();o={inbox:e,reader:createChannelReader(`body`,awaitBodyResult(executeWorkflowBody({...n,owner:e.owner},r)))}}let s=[];a&&s.push(t.commands),o!==void 0&&(s.push(o.inbox.reader),c===void 0&&s.push(o.reader)),e=await raceChannelReads(s,l)}catch(e){if(t.commands.failure!==void 0)throw e;u={status:`failed`,error:normalizeSerializableError(e)};break}if(e===`cancel`)break;if(e.next.done){if(e.channel===`control`){a=!1;continue}if(e.channel===`body`){u={status:`failed`,error:`Workflow body ended without an outcome.`};break}if(i&&r.aborted)break;return}if(e.channel===`control`){if(t.kind!==`turn`)throw Error(`Session-owned workflow run received a turn command.`);t.handleCommand(e.next.value);continue}if(e.channel===`commands`){if(t.kind!==`session`)throw Error(`Turn-owned workflow run received a task command.`);let n=e.next.value;if(i&&n.kind===`task-command`&&(n.command.kind===`ready`||n.command.kind===`reject-dispatch`))continue;let r=await t.handleCommand(n);if(r===`stop`)return;r===`start`&&(i=!0);continue}if(e.channel===`body`){c=e.next.value;continue}e.next.value.kind!==`outcome`&&(e.next.value.kind===`report`&&(s+=1),await t.handleMessage(e.next.value))}r.aborted&&(u={status:`cancelled`,reason:r.reason instanceof Error?r.reason.message:String(r.reason??``)}),u!==void 0&&await t.handleMessage({from:createWorkflowBodyRef(n),kind:`outcome`,result:u})}async function*awaitBodyResult(e){yield await e}workflowToolRunWorkflow.workflowId=`workflow//eve//workflowToolRunWorkflow`;export{workflowToolRunWorkflow};