eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.43 kB
JavaScript
import{jsonSchema}from"ai";import{WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,parseWorkflowProgramOutput,readWorkflowProgramCallInterrupt}from"#execution/dynamic-workflow/schema.js";import{continueWorkflowSandboxInterrupt,createParkingHostTool,createWorkflowSandboxTool,getWorkflowSandboxPendingInterrupts,unwrapWorkflowSandboxResult}from"#shared/workflow-sandbox.js";const agentBridgeSchema=jsonSchema({additionalProperties:!1,properties:{input:{additionalProperties:!1,properties:{agentId:{type:`string`},message:{type:`string`},outputSchema:{type:`object`}},required:[`message`],type:`object`},target:{type:`string`}},required:[`target`,`input`],type:`object`});async function runWorkflowProgramStep(e){"use step";let t=buildWorkflowProgramTools(),n=e.program.continuationSecurity,r;if(e.resume===void 0){let i=await createWorkflowSandboxTool({bridgeRequestLimit:WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,continuationSecurity:n,hostTools:t});if(i.execute===void 0)throw Error(`Workflow program has no sandbox executor.`);r=await i.execute({js:wrapWorkflowProgram(e.program.js)},{toolCallId:e.callId})}else{let i=e.resume.interrupt;r=i;for(let a of e.resume.resolutions){r=await continueWorkflowSandboxInterrupt({bridgeRequestLimit:WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,continuationSecurity:n,interrupt:i,resolution:a,tools:t});let e=await unwrapWorkflowSandboxResult(r,n);if(e.status===`completed`)break;let o=getWorkflowSandboxPendingInterrupts(e.interrupt)[0];if(o===void 0)throw Error(`Workflow program continuation contains no pending agent call.`);i=o}}let i=await unwrapWorkflowSandboxResult(r,n);if(i.status===`completed`)return{output:parseWorkflowProgramOutput(i.output),status:`completed`};let a=getWorkflowSandboxPendingInterrupts(i.interrupt);if(a.length===0)throw Error(`Workflow program continuation contains no pending agent call.`);for(let e of a)readWorkflowProgramCallInterrupt(e);return{interrupt:a[0],pending:a,status:`interrupted`}}function buildWorkflowProgramTools(){return{agent:createParkingHostTool({description:`Invoke one allowlisted child agent.`,inputSchema:agentBridgeSchema,interrupt:e=>({kind:WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,task:void 0,toolInput:e,toolName:`agent`})})}}function wrapWorkflowProgram(e){return`return await (async (ctx) => {\n${e}\n})(Object.freeze({\n agent: (target, input) => tools.agent({ target, input }),\n}));`}export{runWorkflowProgramStep};