eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.76 kB
JavaScript
import{join,relative,resolve}from"node:path";import{pathToFileURL}from"node:url";import{ROOT_COMPILED_AGENT_NODE_ID}from"#compiler/manifest.js";import{collectRuntimeModuleBindingsForManifest,compiledModuleMapSchema,resolveCompiledModuleExtensionScopeNamespace}from"#compiler/module-map.js";import{memoizeModuleNamespaceFactories}from"#compiler/source-graph.js";import{loadFrameworkProgrammaticModule}from"#framework/sources/registry.js";import{formatValidationError}from"#runtime/validation.js";import{loadAuthoredModuleNamespace}from"#internal/authored-module-loader.js";import{loadCompiledManifest}from"#runtime/loaders/manifest.js";import{readMaterializedAuthoredModuleIndex}from"#internal/materialized-authored-modules.js";const EXT_CONFIG_SCOPE=Symbol.for(`eve.ext-config-scope`);async function loadCompiledModuleMapFromAuthoredSource(e){return await hydrateCompiledModuleMapFromManifest(await loadCompiledManifest({compiledArtifactsSource:e.compiledArtifactsSource}),e.compiledArtifactsSource.appRoot,e.authoredAppRoot)}async function hydrateCompiledModuleMapFromManifest(e,t,n=e.appRoot){let r=await readMaterializedAuthoredModuleIndex(t);if(r!==void 0)return await loadMaterializedCompiledModuleMap({moduleMapPath:r.moduleMap,runtimeAppRoot:t});let i={},a=[{manifest:e,nodeId:ROOT_COMPILED_AGENT_NODE_ID},...[...e.subagents].sort((e,t)=>e.nodeId.localeCompare(t.nodeId)).map(e=>({manifest:e.agent,nodeId:e.nodeId}))];for(let t of a)i[t.nodeId]={modules:await hydrateCompiledNodeScope(t.manifest,t=>resolve(n,relative(e.appRoot,t)))};return{nodes:i}}async function hydrateCompiledNodeScope(e,t){let n=new Map(e.extensionMounts.map(e=>[e.mountSourceId,e.packageNamespace])),r=globalThis,i={};for(let{binding:a,sourceId:o}of collectRuntimeModuleBindingsForManifest(e)){let e=n.get(o);e!==void 0&&(r[EXT_CONFIG_SCOPE]=e);try{i[o]=a.backing.kind===`programmatic`?await loadFrameworkProgrammaticModule(a.backing,Object.fromEntries(Object.entries(a.backing.dependencies??{}).map(([e,t])=>[e,i[t]]))):memoizeModuleNamespaceFactories(await loadAuthoredModuleNamespace(t(a.backing.sourcePath),{externalDependencies:a.backing.externalDependencies,extensionScopeNamespace:resolveCompiledModuleExtensionScopeNamespace(a)}))}finally{e!==void 0&&(r[EXT_CONFIG_SCOPE]=void 0)}}return i}async function loadMaterializedCompiledModuleMap(e){let t=join(e.runtimeAppRoot,`.eve`,`compile`,e.moduleMapPath),n=await import(`${pathToFileURL(t).href}?generation=${encodeURIComponent(e.moduleMapPath)}`),r=compiledModuleMapSchema.safeParse(n.moduleMap??n.default);if(!r.success)throw Error(`Expected materialized authored module map "${t}" to export a valid compiled eve module map. ${formatValidationError(r.error)}`);return r.data}export{loadCompiledModuleMapFromAuthoredSource};