eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 5.9 kB
JavaScript
import{join,relative,resolve}from"node:path";import{toErrorMessage}from"#shared/errors.js";import{resolveSandboxCacheDirectory}from"#internal/application/paths.js";import{getSandboxEnvironmentRuntime}from"#shared/sandbox-environment.js";import{createSandboxProviderResources}from"#shared/sandbox-provider.js";import{writeSandboxPreparedArtifactsManifest}from"#runtime/sandbox/prepared-artifacts.js";import{resolveRuntimeCompilerArtifactPaths}from"#runtime/loaders/artifact-paths.js";import{loadCompiledModuleMapFromAuthoredSource}from"#internal/authored-module-map-loader.js";import{createAuthoredSourceRuntimeCompiledArtifactsSource}from"#internal/application/runtime-compiled-artifacts-source.js";import{getRuntimeCompiledArtifactsSandboxAppRoot}from"#runtime/compiled-artifacts-source.js";import{ROOT_RUNTIME_AGENT_NODE_ID,getResolvedRuntimeAgentNode}from"#runtime/graph.js";import{loadCompiledManifest}from"#runtime/loaders/manifest.js";import{resolveRuntimeAgentGraph}from"#runtime/resolve-agent-graph.js";import{createSandboxProviderFiles}from"#execution/sandbox/provider-files.js";import{createDevelopmentSandboxProviderHost}from"#execution/sandbox/provider-host-development.js";import{materializeWorkspaceDirectory}from"#runtime/workspace/seed-files.js";const diskPreparedArtifactStore={async write(e){await writeSandboxPreparedArtifactsManifest(e)}};async function prewarmSandboxes(e){let t=await collectPrewarmTargets(e);if(t.length===0)return;let n=e.preparedArtifactStore??diskPreparedArtifactStore,r=e.dispatch??(async({context:e,provider:t})=>await t.implementation.prepare(e));e.log?.(`eve: initializing ${formatSandboxTemplateCount(t.length)}...`);let i=await Promise.allSettled(t.map(async({context:t,label:n,nodeId:i,provider:a})=>{let logProviderProgress=t=>{shouldLogSandboxPrewarmProgress(t)&&e.log?.(`eve: sandbox template "${n}" (${a.providerName}): ${t}`)};try{return{nodeId:i,provider:a,result:await r({context:{...t,log:e.log===void 0?void 0:logProviderProgress},provider:a})}}catch(t){let r=formatPrewarmFailureForEnvironment({providerName:a.providerName,error:t});throw e.log?.(`eve: failed to initialize sandbox template "${n}" on provider "${a.providerName}": ${toErrorMessage(r)}`),r}})),a=i.find(e=>e.status===`rejected`);if(a?.status===`rejected`)throw a.reason;let o=i.flatMap(e=>e.status===`fulfilled`?[{artifact:e.value.result,nodeId:e.value.nodeId,providerName:e.value.provider.providerName}]:[]);await n.write({compileDirectoryPath:e.compileDirectoryPath,entries:o}),e.log?.(`eve: initialized ${formatSandboxTemplateCount(t.length)}.`)}async function prewarmAppSandboxes(e){let t=e.compiledArtifactsSource??createAuthoredSourceRuntimeCompiledArtifactsSource(e.appRoot);if(t.kind!==`disk`)throw Error(`prewarmAppSandboxes requires disk-backed compiled artifacts.`);let n=await(e.loadAgentGraph??loadGraphFromArtifacts)({compiledArtifactsSource:t});await prewarmSandboxes({appRoot:getRuntimeCompiledArtifactsSandboxAppRoot(t)??e.appRoot,compileDirectoryPath:resolveRuntimeCompilerArtifactPaths(t.appRoot).compileDirectoryPath,compiledArtifactsSource:t,dispatch:e.dispatch,graph:n,log:e.log,preparedArtifactStore:e.preparedArtifactStore})}async function collectPrewarmTargets(e){let t=[];return await Promise.all(collectNodeSandboxes(e.graph).map(async({definition:n,nodeId:r,workspaceResourceRoot:i})=>{let a=getResolvedRuntimeAgentNode(e.graph,r).agent,o=join(resolve(e.appRoot,relative(a.metadata.appRoot,a.metadata.agentRoot)),`sandbox`),s=getSandboxEnvironmentRuntime(n.environment),c=await loadResourceRootSeedFiles({compileDirectoryPath:e.compileDirectoryPath,workspaceResourceRoot:i});t.push({context:{files:createSandboxProviderFiles(o),host:createDevelopmentSandboxProviderHost(e.appRoot),resources:createSandboxProviderResources({resourcesKey:i.contentHash,resourcesPath:i.contentHash===void 0?void 0:`${e.compileDirectoryPath}/${i.logicalPath}`,seedFiles:c}),sourceRevision:n.revisionHash,storagePath:resolveSandboxCacheDirectory(e.appRoot)},label:formatLabel(r),nodeId:r,provider:s})})),t.sort((e,t)=>e.label.localeCompare(t.label))}async function loadResourceRootSeedFiles(e){return e.workspaceResourceRoot.contentHash===void 0&&e.workspaceResourceRoot.rootEntries.length===0?[]:(await materializeWorkspaceDirectory(`${e.compileDirectoryPath}/${e.workspaceResourceRoot.logicalPath}`)).map(e=>({content:e.content,path:e.path}))}async function loadGraphFromArtifacts(e){let[t,n]=await Promise.all([loadCompiledManifest({compiledArtifactsSource:e.compiledArtifactsSource}),loadCompiledModuleMapFromAuthoredSource({compiledArtifactsSource:e.compiledArtifactsSource})]);return await resolveRuntimeAgentGraph({manifest:t,moduleMap:n})}function collectNodeSandboxes(e){return[...e.nodesByNodeId.entries()].flatMap(([e,t])=>{let n=t.sandboxRegistry.sandbox;return n.definition.kind===`parent`?[]:[{...n,definition:n.definition,nodeId:e}]})}function formatLabel(e){return e===ROOT_RUNTIME_AGENT_NODE_ID?`root`:e}function formatSandboxTemplateCount(e){return`${e} sandbox ${e===1?`template`:`templates`}`}function shouldLogSandboxPrewarmProgress(e){return!e.startsWith(`checking `)&&!e.startsWith(`reusing `)&&e!==`loading microsandbox runtime`&&e!==`microsandbox runtime ready`}function formatPrewarmFailureForEnvironment(e){return!isVercelEnvironment()||!isLocalSandboxProvider(e.providerName)?e.error:Error(`The ${e.providerName} sandbox provider is not available when deploying on Vercel. Vercel build containers cannot run local Docker containers or microsandbox VMs. Use DefaultSandbox.environment() so eve selects Vercel Sandbox on Vercel, or configure VercelSandbox.environment() explicitly. Original ${e.providerName} error: ${toErrorMessage(e.error)}`,{cause:e.error})}function isVercelEnvironment(){return!!process.env.VERCEL?.trim()}function isLocalSandboxProvider(e){return e===`docker`||e===`microsandbox`}export{prewarmAppSandboxes,prewarmSandboxes};