eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 4.61 kB
JavaScript
import{isEveDevEnvironment}from"#internal/application/optional-package-install.js";import{waitForDevelopmentSandboxPrewarm}from"#execution/sandbox/development-prewarm.js";import{buildCallbackContext}from"#context/build-callback-context.js";import{trackActiveSandboxHandle}from"#execution/sandbox/active-handles.js";import{SandboxTemplateNotProvisionedError}from"#public/definitions/sandbox-backend.js";import{getRuntimeCompiledArtifactsSandboxAppRoot}from"#runtime/compiled-artifacts-source.js";import{createRuntimeSandboxKeys}from"#runtime/sandbox/keys.js";import{createRuntimeSandboxTemplatePlan}from"#runtime/sandbox/template-plan.js";import{prewarmAppSandboxes}from"#execution/sandbox/prewarm.js";import{waitForSandboxTemplatePrewarmLock}from"#execution/sandbox/template-prewarm-lock.js";async function ensureSandboxAccess(e){let t=e.state?.initialized??!1,n=e.state?.session??null,r=getRuntimeCompiledArtifactsSandboxAppRoot(e.compiledArtifactsSource)??process.cwd(),i=e.registry.sandbox,a;function getHandle(){return a===void 0&&(a=createHandle().catch(e=>{throw a=void 0,e})),a}async function createHandle(){if(i===null)return null;let a=i.inheritance,o=a?.definition??i.definition,s=o.backend,c=createRuntimeSandboxTemplatePlan({definition:o,workspaceResourceRoot:a?.workspaceResourceRoot??i.workspaceResourceRoot}),l=await createRuntimeSandboxKeys({backendName:s.name,compiledArtifactsSource:e.compiledArtifactsSource,nodeId:a?.nodeId??e.nodeId,sessionId:e.sessionId,sourceId:o.sourceId,templatePlan:c});l.templateKey!==null&&(await waitForDevelopmentSandboxPrewarm({appRoot:r,compiledArtifactsSource:e.compiledArtifactsSource,log:t=>logDevelopmentSandbox(`eve: sandbox template "${formatNodeLabel(e.nodeId)}" (${s.name}): ${t}`)}),await waitForSandboxTemplatePrewarmLock({appRoot:r,backendName:s.name,log:t=>logDevelopmentSandbox(`eve: sandbox template "${formatNodeLabel(e.nodeId)}" (${s.name}): ${t}`),templateKey:l.templateKey}));let u=n!==null&&n.backendName===s.name&&n.sessionKey===l.sessionKey?n:null;u===null&&(t=!1);let d={existingMetadata:u?.metadata,runtimeContext:{appRoot:r},sessionKey:l.sessionKey,tags:e.tags,templateKey:l.templateKey},f=await withDevelopmentSandboxProgress(`eve: opening sandbox session "${formatNodeLabel(e.nodeId)}" on backend "${s.name}"...`,`eve: opening sandbox session "${formatNodeLabel(e.nodeId)}" on backend "${s.name}"`,async()=>await createBackendHandleWithPrewarmRetry({appRoot:r,backend:s,compiledArtifactsSource:e.compiledArtifactsSource,createInput:d}));return trackActiveSandboxHandle({backendName:s.name,handle:f,sessionKey:l.sessionKey}),t||=(await runOnSession(async()=>{await o.onSession?.({ctx:{session:buildCallbackContext().session},use:f.useSessionFn})}),!0),f}async function runOnSession(t){if(e.runOnSession!==void 0){await e.runOnSession(t);return}await t()}return{async captureState(){if(a!==void 0){let e=await a;e!==null&&(n=await e.captureState())}return{initialized:t,session:n}},async delete(r){if(e.ownsSandbox===!1)throw Error(`Only the owning session can delete a shared sandbox. Delete it from the parent session instead.`);let i=await getHandle();if(i===null)throw Error(`The sandbox is not available in the current authored runtime context.`);await i.delete(r),a=void 0,t=!1,n=null},async get(){let e=await getHandle();return e===null?null:e.session},async stop(){let e=await getHandle();if(e===null)throw Error(`The sandbox is not available in the current authored runtime context.`);await e.stop()}}}async function createBackendHandleWithPrewarmRetry(e){try{return await e.backend.create(e.createInput)}catch(t){if(e.createInput.templateKey===null||e.compiledArtifactsSource.kind!==`disk`||!SandboxTemplateNotProvisionedError.is(t))throw t;return await prewarmAppSandboxes({appRoot:e.appRoot,compiledArtifactsSource:e.compiledArtifactsSource,log:e=>logDevelopmentSandbox(e)}),await waitForSandboxTemplatePrewarmLock({appRoot:e.appRoot,backendName:e.backend.name,log:e=>logDevelopmentSandbox(`eve: ${e}`),templateKey:e.createInput.templateKey}),logDevelopmentSandbox(`eve: sandbox template is ready; retrying sandbox creation...`),await e.backend.create(e.createInput)}}function logDevelopmentSandbox(e){isEveDevEnvironment()&&console.log(e)}async function withDevelopmentSandboxProgress(e,t,n){if(logDevelopmentSandbox(e),!isEveDevEnvironment())return await n();let r=Date.now(),i=setInterval(()=>{logDevelopmentSandbox(`${t} (${Math.round((Date.now()-r)/1e3)}s elapsed)...`)},5e3);i.unref?.();try{return await n()}finally{clearInterval(i)}}function formatNodeLabel(e){return e===`__root__`?`root`:e}export{ensureSandboxAccess};