UNPKG

eve

Version:

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

1 lines 5.71 kB
import{existsSync}from"node:fs";import{basename,join}from"node:path";import{LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH,resolveLocalWorkflowWorldDataDirectory}from"#internal/workflow/local-world-data-directory.js";import{turnWorkflowReference}from"#execution/workflow-runtime.js";import{DEVELOPMENT_WORKFLOW_DELIVERY_HEADER,DEVELOPMENT_WORKFLOW_STREAM_ROUTE,DEVELOPMENT_WORKFLOW_TRANSPORT_HEADER,DEVELOPMENT_WORKFLOW_WORLD_ROUTE,DEVELOPMENT_WORLD_OPERATIONS}from"#internal/workflow/development-world-protocol.js";import{deriveEveWorkflowQueuePrefix}from"#internal/workflow/queue-namespace.js";import{timingSafeEqualStrings}from"#internal/nitro/dev-client-address.js";import{decodeDevelopmentWorldValue,encodeDevelopmentWorldValue,serializeDevelopmentWorldError}from"#internal/workflow/development-world-codec.js";import{createWorld}from"#compiled/@workflow/world-local/index.js";function createParentDevelopmentWorkflowWorld(e){return new LocalParentDevelopmentWorkflowWorld(e)}var LocalParentDevelopmentWorkflowWorld=class{#e;#t;#n;#r;#i;#a=!1;#o=!1;constructor(e){if(e.transportSecret.length<16)throw Error(`Development Workflow transport secret is too short to be trusted.`);this.#e=e.agentName,this.#t=e.appRoot,this.#n=e.resolveActiveGenerationId,this.#r=e.transportSecret,this.#i=createWorld({dataDir:resolveLocalWorkflowWorldDataDirectory(e.appRoot),recoverActiveRuns:!1})}async start(){if(this.#o)return;let e=await this.#s(),t=new Set;for(let n of e)this.#p(n)||t.add(n);t.size>0&&console.error(`[eve:dev] ${String(t.size)} active local Workflow run(s) reference development generations that no longer exist (${[...t].join(`, `)}). Their deliveries are quarantined; remove "${LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH}" to discard the app's active local Workflow runs.`),await this.#i.start?.(),this.#o=!0,await reenqueueActiveDevelopmentRuns({enqueue:this.#u.bind(this),prefix:deriveEveWorkflowQueuePrefix(this.#e),quarantinedGenerationIds:t,world:this.#i})}async close(){this.#a||(this.#a=!0,this.#o=!1,await this.#i.close?.())}async handleRequest(e){let t=new URL(e.url);if(t.pathname===DEVELOPMENT_WORKFLOW_WORLD_ROUTE)return await this.#c(e);if(t.pathname===DEVELOPMENT_WORKFLOW_STREAM_ROUTE)return await this.#l(e,t)}async#s(){let e=new Set;for(let t of[`pending`,`running`]){let n;do{let i;try{i=await this.#i.runs.list({pagination:{cursor:n,limit:1e3},resolveData:`none`,status:t})}catch(e){throw Error(`Failed to read the app's active local Workflow runs. Remove "${LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH}" to discard them.`,{cause:e})}for(let t of i.data)t.workflowName===turnWorkflowReference.workflowId&&e.add(t.deploymentId);n=i.hasMore?i.cursor??void 0:void 0}while(n!==void 0)}return e}async#c(e){if(!this.#f(e)||e.method!==`POST`)return Response.json({error:`Workflow World request is not trusted.`},{status:401});try{let t=decodeDevelopmentWorldValue(await e.text()),n=await this.#d(t);return new Response(encodeDevelopmentWorldValue(n))}catch(e){return new Response(encodeDevelopmentWorldValue(serializeDevelopmentWorldError(e)),{status:500})}}async#l(e,t){if(!this.#f(e)||e.method!==`GET`)return Response.json({error:`Workflow World request is not trusted.`},{status:401});let n=t.searchParams.get(`runId`),r=t.searchParams.get(`name`),i=t.searchParams.get(`startIndex`);if(n===null||r===null)return Response.json({error:`Workflow stream request is malformed.`},{status:400});let a=i===null||i===``?void 0:Number(i);if(i===``||a!==void 0&&!Number.isInteger(a))return Response.json({error:`Workflow stream start index is invalid.`},{status:400});try{return new Response(await this.#i.streams.get(n,r,a))}catch(e){return new Response(encodeDevelopmentWorldValue(serializeDevelopmentWorldError(e)),{status:500})}}async#u(...e){let[t,n,r]=e;return await this.#i.queue(t,n,{...r,headers:{...r?.headers,[DEVELOPMENT_WORKFLOW_DELIVERY_HEADER]:this.#r}})}async#d(e){if(!isDevelopmentWorldCall(e))throw Error(`Development Workflow World call is malformed.`);let t=[...e.arguments];if(e.operation===`getDeploymentId`||e.operation===`resolveLatestDeploymentId`)return this.#n();if(e.operation===`queue`)return await this.#u(...t);if(e.operation===`streams.writeMulti`&&this.#i.streams.writeMulti===void 0){for(let e of t[2])await this.#i.streams.write(t[0],t[1],e);return}let n=e.operation.indexOf(`.`),r=this.#i[e.operation.slice(0,n)],i=typeof r==`object`&&r?r[e.operation.slice(n+1)]:void 0;if(typeof i==`function`)return await Reflect.apply(i,r,t)}#f(e){let t=e.headers.get(DEVELOPMENT_WORKFLOW_TRANSPORT_HEADER);return t!==null&&timingSafeEqualStrings(t,this.#r)}#p(t){return isValidGenerationId(t)?existsSync(join(this.#t,`.eve`,`dev-runtime`,`snapshots`,t,`generation.json`)):!1}};function isValidGenerationId(e){return e.length>0&&e!==`.`&&e!==`..`&&basename(e)===e}const DEVELOPMENT_WORLD_OPERATION_SET=new Set(DEVELOPMENT_WORLD_OPERATIONS);function isDevelopmentWorldCall(e){return isObject(e)&&typeof e.operation==`string`&&DEVELOPMENT_WORLD_OPERATION_SET.has(e.operation)&&Array.isArray(e.arguments)}function isObject(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}async function reenqueueActiveDevelopmentRuns(e){for(let t of[`pending`,`running`]){let n;do{let i;try{i=await e.world.runs.list({pagination:{cursor:n},resolveData:`none`,status:t})}catch(e){throw Error(`Failed to read the app's active local Workflow runs. Remove "${LOCAL_WORKFLOW_WORLD_DATA_DIRECTORY_RELATIVE_PATH}" to discard them.`,{cause:e})}for(let t of i.data)e.quarantinedGenerationIds.has(t.deploymentId)||await e.enqueue(`${e.prefix}${t.workflowName}`,{runId:t.runId});n=i.hasMore?i.cursor??void 0:void 0}while(n!==void 0)}}export{createParentDevelopmentWorkflowWorld};