eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
3 lines • 6.3 kB
JavaScript
import{cp,mkdir,readdir,rename,writeFile}from"node:fs/promises";import{dirname,join,relative}from"node:path";import{SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS}from"#discover/filesystem.js";import{bundleExtensionDistributionGraph}from"#internal/authored-module-loader.js";import{EXTENSION_COMPATIBILITY_MANIFEST_FILENAME}from"#compiler/extension-compatibility.js";import{emitExtensionDeclarations}from"#internal/nitro/host/extension-declarations.js";async function emitExtensionDistribution(e){let t=await collectExtensionSourceFiles(e.sourceRoot),n=e.manifest.skills.filter(e=>e.sourceKind===`skill-package`).map(t=>relative(e.sourceRoot,t.rootPath).replaceAll(`\\`,`/`)),r=t.filter(e=>isAuthoredModule(e.logicalPath)&&!n.some(t=>e.logicalPath.startsWith(`${t}/`)));await copyDistributionDataFiles({files:t,moduleLogicalPaths:new Set(r.map(e=>e.logicalPath)),stagedDistRoot:e.stagedDistRoot});let i=await createDistributionEntries({...e,moduleFiles:r}),[a,o]=await Promise.allSettled([bundleExtensionDistributionGraph({entries:i,packageRoot:e.appRoot,runtimeDependencies:e.runtimeDependencies}),emitExtensionDeclarations({appRoot:e.appRoot,declarationsRoot:e.declarationsRoot,moduleLogicalPaths:r.map(e=>e.logicalPath),sourceRoot:e.sourceRoot})]);if(a.status===`rejected`)throw a.reason;if(o.status===`rejected`)throw o.reason;let s=a.value;for(let[t,n]of s.files){let r=join(e.stagedOutDir,t);await mkdir(dirname(r),{recursive:!0}),await writeFile(r,n,`utf8`)}let c=relative(e.appRoot,e.sourceRoot);try{await cp(join(e.declarationsRoot,c),e.stagedDistRoot,{recursive:!0})}catch(e){throw isFileSystemError(e,`ENOENT`)?Error(`TypeScript emitted no declarations for "${c}". Ensure the package tsconfig.json \`include\` covers every module under \`eve.extension.source\`.`,{cause:e}):e}return await emitDeclarationBarrels(e),s.imports}var ExtensionOutputRestoreError=class extends Error{preservedOutputPath;constructor(e,t){super(`Publishing the extension build output failed, and the previous output could not be restored. It is preserved at "${e}".`,t),this.name=`ExtensionOutputRestoreError`,this.preservedOutputPath=e}};async function replaceExtensionBuildOutput(e){await mkdir(dirname(e.outDir),{recursive:!0});let t=join(e.transactionRoot,`previous-output`),n=!1;try{await rename(e.outDir,t),n=!0}catch(e){if(!isFileSystemError(e,`ENOENT`))throw e}try{await rename(e.stagedOutDir,e.outDir)}catch(r){if(n)try{await rename(t,e.outDir)}catch{throw new ExtensionOutputRestoreError(t,{cause:r})}throw r}}async function collectExtensionSourceFiles(e,t=e){let n=[];for(let r of await readdir(t,{withFileTypes:!0})){let i=join(t,r.name);if(r.isDirectory())n.push(...await collectExtensionSourceFiles(e,i));else if(r.isFile()){let t=relative(e,i).replaceAll(`\\`,`/`);if(t===EXTENSION_COMPATIBILITY_MANIFEST_FILENAME)throw Error(`The extension source cannot contain "${EXTENSION_COMPATIBILITY_MANIFEST_FILENAME}"; eve reserves it for generated compatibility metadata.`);n.push({absolutePath:i,logicalPath:t})}else throw Error(`Extension source entry "${i}" must be a regular file or directory.`)}return n.sort((e,t)=>e.logicalPath.localeCompare(t.logicalPath))}function isAuthoredModule(e){return!/\.d\.[cm]?ts$/.test(e)&&SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS.some(t=>e.endsWith(t))}async function copyDistributionDataFiles(e){await Promise.all(e.files.filter(t=>!e.moduleLogicalPaths.has(t.logicalPath)).map(async t=>{let n=join(e.stagedDistRoot,t.logicalPath);await mkdir(dirname(n),{recursive:!0}),await cp(t.absolutePath,n)}))}async function createDistributionEntries(e){let t=relative(e.stagedOutDir,e.stagedDistRoot).replaceAll(`\\`,`/`),n=e.moduleFiles.map(e=>({name:`${t}/${stripAuthoredModuleExtension(e.logicalPath)}`,path:e.absolutePath})),r=new Set;for(let e of n){if(r.has(e.name))throw Error(`Multiple extension modules emit the same path "${e.name}.mjs".`);r.add(e.name)}let i=join(e.transactionRoot,`barrels`);return n.push(await stageRuntimeBarrel({barrelPath:join(i,`index.mjs`),name:`index`,reexports:[{name:`default`,path:join(e.sourceRoot,e.declarationModule.logicalPath)},{name:e.shortName,path:join(e.sourceRoot,e.declarationModule.logicalPath)}]}),await stageRuntimeBarrel({barrelPath:join(i,`tools.mjs`),name:`tools/index`,reexports:e.manifest.tools.map(t=>({name:toolExportName(t.logicalPath),path:join(e.sourceRoot,t.logicalPath)}))})),n}async function stageRuntimeBarrel(e){await mkdir(dirname(e.barrelPath),{recursive:!0});let t=e.reexports.map(t=>reexportLine(t.name,relativeImport(dirname(e.barrelPath),t.path)));return await writeFile(e.barrelPath,`${t.join(`
`)}\n`,`utf8`),{name:e.name,path:e.barrelPath}}async function emitDeclarationBarrels(e){await mkdir(join(e.stagedOutDir,`tools`),{recursive:!0});let t=relativeImport(e.stagedOutDir,join(e.stagedDistRoot,e.declarationModule.logicalPath));await writeDeclarationBarrel({path:join(e.stagedOutDir,`index.d.ts`),reexports:[{name:`default`,specifier:t},{name:e.shortName,specifier:t}]}),await writeDeclarationBarrel({path:join(e.stagedOutDir,`tools`,`index.d.ts`),reexports:e.manifest.tools.map(t=>({name:toolExportName(t.logicalPath),specifier:relativeImport(join(e.stagedOutDir,`tools`),join(e.stagedDistRoot,t.logicalPath))}))})}async function writeDeclarationBarrel(e){let t=e.reexports.map(e=>reexportLine(e.name,toDeclarationSpecifier(e.specifier)));await writeFile(e.path,[`// Generated by eve. Do not edit by hand.`,``,...t,``].join(`
`),`utf8`)}function stripAuthoredModuleExtension(e){for(let t of SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS)if(e.endsWith(t))return e.slice(0,e.length-t.length);return e}function toolExportName(e){let t=stripAuthoredModuleExtension(e).replace(/^tools\//,``).replace(/[^A-Za-z0-9_$]/g,`_`);return/^[A-Za-z_$]/.test(t)?t:`_${t}`}function relativeImport(e,t){let n=relative(e,t).replaceAll(`\\`,`/`);return n.startsWith(`.`)?n:`./${n}`}function reexportLine(e,t){return e==="default"?`export { default } from ${JSON.stringify(t)};`:`export { default as ${e} } from ${JSON.stringify(t)};`}function toDeclarationSpecifier(e){return e.replace(/\.mts$/,`.mjs`).replace(/\.cts$/,`.cjs`).replace(/\.tsx?$/,`.js`)}function isFileSystemError(e,t){return e instanceof Error&&`code`in e&&e.code===t}export{ExtensionOutputRestoreError,emitExtensionDistribution,replaceExtensionBuildOutput};