UNPKG

eve

Version:

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

1 lines 6.66 kB
import{dirname,isAbsolute,join,resolve}from"node:path";import{SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS}from"#discover/filesystem.js";import{createDiscoverErrorDiagnostic}from"#discover/diagnostics.js";import{EXTENSION_COMPATIBILITY_MANIFEST_FILENAME,findUnsupportedExtensionCapabilities,parseExtensionCompatibilityManifest}from"#compiler/extension-compatibility.js";import{parseExtensionMountSpecifier}from"#discover/extension-specifier.js";import{parseBuiltInExtensionPackageRoots,parseExtensionPackageRoots}from"#shared/extension-package-contract.js";const DISCOVER_EXTENSION_MOUNT_UNRESOLVED=`discover/extension-mount-unresolved`,DISCOVER_EXTENSION_MOUNT_AMBIGUOUS=`discover/extension-mount-ambiguous`,DISCOVER_EXTENSION_MOUNT_MISSING_DECLARATION=`discover/extension-mount-missing-declaration`,DISCOVER_EXTENSION_NESTED_MOUNT_UNSUPPORTED=`discover/extension-nested-mount-unsupported`,DISCOVER_EXTENSION_PACKAGE_INVALID=`discover/extension-package-invalid`,DISCOVER_EXTENSION_COMPATIBILITY_INVALID=`discover/extension-compatibility-invalid`,DISCOVER_EXTENSION_CAPABILITY_INCOMPATIBLE=`discover/extension-capability-incompatible`,DISCOVER_EXTENSION_AGENT_CONFIG_UNSUPPORTED=`discover/extension-agent-config-unsupported`,DISCOVER_EXTENSION_SANDBOX_UNSUPPORTED=`discover/extension-sandbox-unsupported`,DISCOVER_EXTENSION_MEMORY_UNSUPPORTED=`discover/extension-memory-unsupported`,DISCOVER_EXTENSION_INSTRUMENTATION_UNSUPPORTED=`discover/extension-instrumentation-unsupported`;function mountNamespace(e){let t=e.slice(e.lastIndexOf(`/`)+1);for(let e of SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS)if(t.toLowerCase().endsWith(e))return t.slice(0,t.length-e.length);return t}function mountRefNamespace(e){let t=e.replace(/^extensions\//,``),n=t.indexOf(`/`);return n===-1?mountNamespace(e):t.slice(0,n)}function packageStateNamespace(e){return e.replace(/^@/,``).replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``)||`extension`}async function locateExtensionMount(e){let t=await locateExtensionMountPackage(e);if(t.location===void 0)return{diagnostics:t.diagnostics};let{location:n}=t;if(n.builtIn)return{location:{namespace:n.namespace,specifier:n.specifier,packageName:n.packageName,packageRoot:n.packageRoot,sourceRoot:n.distRoot,externalDependencies:[]},diagnostics:[]};let r=join(n.distRoot,EXTENSION_COMPATIBILITY_MANIFEST_FILENAME),i;try{i=parseExtensionCompatibilityManifest(await e.source.readTextFile(r),r)}catch(e){return{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_COMPATIBILITY_INVALID,message:`Extension "${n.packageName}" has no valid compatibility manifest at "${r}". Rebuild or reinstall the extension. ${e instanceof Error?e.message:String(e)}`,sourcePath:r})]}}let a=findUnsupportedExtensionCapabilities(i);return a.length>0?{diagnostics:a.map(e=>createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_CAPABILITY_INCOMPATIBLE,message:`Extension "${n.packageName}" requires ${e.capability} contract v${e.requiredVersion}, but this eve supports ${e.capability} contract ${formatSupportedVersions(e.supportedVersions)}. Upgrade eve or install an extension release that requires a supported ${e.capability} contract.`,sourcePath:r}))}:{location:{namespace:n.namespace,specifier:n.specifier,packageName:n.packageName,packageRoot:n.packageRoot,sourceRoot:n.distRoot,externalDependencies:i.build?.externalDependencies??[]},diagnostics:[]}}async function locateExtensionMountPackage(e){let t=join(e.agentRoot,e.mount.logicalPath),{namespace:n}=e,r;try{r=await e.source.readTextFile(t)}catch{return{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_MOUNT_UNRESOLVED,message:`Could not read extension mount "${e.mount.logicalPath}".`,sourcePath:t})]}}let i=parseExtensionMountSpecifier(r);if(i===null)return{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_MOUNT_UNRESOLVED,message:`Extension mount "${e.mount.logicalPath}" must default-export a mounted extension, e.g. \`export default crm({ ... })\` or \`export { default } from "@acme/crm"\`.`,sourcePath:t})]};let a=await resolvePackageRoot({source:e.source,appRoot:e.appRoot,mountDirectory:dirname(t),specifier:i});if(a===null)return{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_MOUNT_UNRESOLVED,message:`Could not resolve extension package "${i}" mounted by "${e.mount.logicalPath}".`,sourcePath:t})]};let o=join(a,`package.json`),s;try{s=JSON.parse(await e.source.readTextFile(o))}catch{return{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_PACKAGE_INVALID,message:`Extension package "${i}" has no readable package.json at "${o}".`,sourcePath:o})]}}let c=parseBuiltInExtensionPackageRoots(s.eve?.builtInExtensions,packageSpecifierSubpath(i)),l=c??parseExtensionPackageRoots(s.eve?.extension);return l===null?{diagnostics:[createDiscoverErrorDiagnostic({code:DISCOVER_EXTENSION_PACKAGE_INVALID,message:`Package "${i}" is not an eve extension: its package.json must declare \`eve.extension.dist\`.`,sourcePath:o})]}:{location:{namespace:n,specifier:i,packageName:typeof s.name==`string`&&s.name.length>0?s.name:i,packageRoot:a,...l.source===void 0?{}:{authoredSourceRoot:resolve(a,l.source)},builtIn:c!==null,distRoot:resolve(a,l.dist)},diagnostics:[]}}function formatSupportedVersions(e){return e.length===0?`versions: none`:`versions: ${e.map(e=>`v${e}`).join(`, `)}`}async function resolvePackageRoot(e){if(e.specifier.startsWith(`.`)){let t=resolve(e.mountDirectory,e.specifier);return await hasPackageJson(e.source,t)?t:null}if(isAbsolute(e.specifier))return await hasPackageJson(e.source,e.specifier)?e.specifier:null;let t=bareSpecifierPackagePath(e.specifier),n=resolve(e.appRoot);for(;;){let r=join(n,`node_modules`,t);if(await hasPackageJson(e.source,r))return r;let i=dirname(n);if(i===n)return null;n=i}}function packageSpecifierSubpath(e){let t=e.slice(bareSpecifierPackagePath(e).length);return t.length===0?`.`:`.${t}`}function bareSpecifierPackagePath(e){let t=e.split(`/`);return e.startsWith(`@`)?t.slice(0,2).join(`/`):t[0]??e}async function hasPackageJson(e,t){return await e.stat(join(t,`package.json`))===`file`}export{DISCOVER_EXTENSION_AGENT_CONFIG_UNSUPPORTED,DISCOVER_EXTENSION_CAPABILITY_INCOMPATIBLE,DISCOVER_EXTENSION_COMPATIBILITY_INVALID,DISCOVER_EXTENSION_INSTRUMENTATION_UNSUPPORTED,DISCOVER_EXTENSION_MEMORY_UNSUPPORTED,DISCOVER_EXTENSION_MOUNT_AMBIGUOUS,DISCOVER_EXTENSION_MOUNT_MISSING_DECLARATION,DISCOVER_EXTENSION_MOUNT_UNRESOLVED,DISCOVER_EXTENSION_NESTED_MOUNT_UNSUPPORTED,DISCOVER_EXTENSION_PACKAGE_INVALID,DISCOVER_EXTENSION_SANDBOX_UNSUPPORTED,locateExtensionMount,locateExtensionMountPackage,mountNamespace,mountRefNamespace,packageStateNamespace};