eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 6.48 kB
JavaScript
import{withSandboxTemplatePrewarmLock}from"./template-prewarm-lock.js";import{resolvePackageSourceFilePath}from"#internal/application/package.js";import{join}from"node:path";import{toErrorMessage}from"#shared/errors.js";import{ROOT_RUNTIME_AGENT_NODE_ID}from"#runtime/graph.js";import{createBundledRuntimeCompiledArtifactsSource,createDiskRuntimeCompiledArtifactsSource,getRuntimeCompiledArtifactsSandboxAppRoot}from"#runtime/compiled-artifacts-source.js";import{loadCompiledModuleMapFromAuthoredSource}from"#internal/authored-module-map-loader.js";import{createAuthoredSourceRuntimeCompiledArtifactsSource}from"#internal/application/runtime-compiled-artifacts-source.js";import{loadCompileMetadata}from"#runtime/loaders/compile-metadata.js";import{withBundledCompiledArtifacts}from"#runtime/loaders/bundled-artifacts.js";import{loadCompiledManifest}from"#runtime/loaders/manifest.js";import{resolveRuntimeCompilerArtifactPaths}from"#runtime/loaders/artifact-paths.js";import{resolveRuntimeAgentGraph}from"#runtime/resolve-agent-graph.js";import{createRuntimeSandboxTemplateKey}from"#runtime/sandbox/keys.js";import{createRuntimeSandboxTemplatePlan}from"#runtime/sandbox/template-plan.js";import{materializeWorkspaceDirectory}from"#runtime/workspace/seed-files.js";async function prewarmSandboxes(t){let n=await collectPrewarmTargets(t);if(n.length===0)return;let i=createPrewarmSignature(n);if(t.shouldPrewarmSignature?.(i)===!1)return;let a=t.dispatch??(async({backend:e,input:t})=>await e.prewarm(t));t.log?.(`eve: initializing ${formatSandboxTemplateCount(n.length)}...`);let o=(await Promise.all(n.map(async({backend:n,label:i,input:o})=>{let logBackendProgress=e=>{shouldLogSandboxPrewarmProgress(e)&&t.log?.(`eve: sandbox template "${i}" (${n.name}): ${e}`)},s;try{s=await withSandboxTemplatePrewarmLock({appRoot:o.runtimeContext.appRoot,backendName:n.name,templateKey:o.templateKey},async()=>await a({backend:n,input:{...o,log:t.log===void 0?void 0:logBackendProgress}}))}catch(e){let a=formatPrewarmFailureForEnvironment({backendName:n.name,error:e});throw t.log?.(`eve: failed to initialize sandbox template "${i}" on backend "${n.name}": ${toErrorMessage(a)}`),a}return s}))).filter(e=>e.reused).length;t.log?.(`eve: initialized ${formatSandboxTemplateCount(n.length)} (${o} reused, ${n.length-o} built).`),t.onPrewarmSignature?.(i)}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,onPrewarmSignature:e.onPrewarmSignature,shouldPrewarmSignature:e.shouldPrewarmSignature})}async function prewarmBuiltAppSandboxes(e){let r=join(e.appRoot,`.output`),i=createDiskRuntimeCompiledArtifactsSource(r,{moduleMapLoaderPath:resolvePackageSourceFilePath(`src/internal/authored-module-map-loader.ts`),sandboxAppRoot:e.appRoot}),[s,c,l]=await Promise.all([loadCompileMetadata({compiledArtifactsSource:i}),loadCompiledManifest({compiledArtifactsSource:i}),loadCompiledModuleMapFromAuthoredSource({compiledArtifactsSource:i})]);await withBundledCompiledArtifacts({manifest:c,metadata:s??void 0,moduleMap:l,sessionId:`built-app-prewarm`},async()=>{let t=createBundledRuntimeCompiledArtifactsSource(),n=await resolveRuntimeAgentGraph({manifest:c,moduleMap:l});await prewarmSandboxes({appRoot:e.appRoot,compileDirectoryPath:resolveRuntimeCompilerArtifactPaths(r).compileDirectoryPath,compiledArtifactsSource:t,dispatch:e.dispatch,graph:n,log:e.log})})}async function collectPrewarmTargets(e){let t={appRoot:e.appRoot},n=[];return await Promise.all(collectNodeSandboxes(e.graph).map(async({definition:r,nodeId:i,workspaceResourceRoot:a})=>{let o=createRuntimeSandboxTemplatePlan({definition:r,workspaceResourceRoot:a}),s=await createRuntimeSandboxTemplateKey({backendName:r.backend.name,compiledArtifactsSource:e.compiledArtifactsSource,nodeId:i,sourceId:r.sourceId,templatePlan:o});s!==null&&n.push({backend:r.backend,label:formatLabel(i),input:{bootstrap:r.bootstrap,seedFiles:await loadResourceRootSeedFiles({compileDirectoryPath:e.compileDirectoryPath,workspaceResourceRoot:a}),runtimeContext:t,templateKey:s},signature:`${r.backend.name}:${i}:${s}`})})),n.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===null?[]:[{...n,nodeId:e}]})}function formatLabel(e){return e===ROOT_RUNTIME_AGENT_NODE_ID?`root`:e}function createPrewarmSignature(e){return e.map(e=>e.signature).sort().join(`
`)}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()||!isLocalSandboxBackend(e.backendName)?e.error:Error(`The ${e.backendName} sandbox backend is not available when deploying on Vercel. Vercel build containers cannot run local Docker containers or microsandbox VMs. Use defaultBackend() so eve selects Vercel Sandbox on Vercel, or configure a Vercel-compatible backend explicitly, such as vercel(). Original ${e.backendName} error: ${toErrorMessage(e.error)}`,{cause:e.error})}function isVercelEnvironment(){return!!process.env.VERCEL?.trim()}function isLocalSandboxBackend(e){return e===`docker`||e===`microsandbox`}export{prewarmAppSandboxes,prewarmBuiltAppSandboxes,prewarmSandboxes};