UNPKG

eve

Version:

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

1 lines 8.41 kB
import{existsSync,readFileSync}from"node:fs";import{dirname,join,relative,resolve,sep}from"node:path";import{cp,mkdir,readFile,readdir,rename,rm,stat,writeFile}from"node:fs/promises";import{randomUUID}from"node:crypto";import{copyDevelopmentSourceSnapshot}from"#internal/nitro/dev-runtime-source-snapshot-copy.js";import{createDevelopmentSourceSnapshotPlan}from"#internal/nitro/dev-runtime-source-snapshot.js";const DEV_RUNTIME_ARTIFACTS_DIRECTORY=`dev-runtime`,TERMINAL_WORKFLOW_RUN_STATUSES=new Set([`completed`,`failed`,`cancelled`,`canceled`]);function resolveDevelopmentRuntimeArtifactsPointerPath(e){return join(e,`.eve`,DEV_RUNTIME_ARTIFACTS_DIRECTORY,`current.json`)}function resolveDevelopmentRuntimeArtifactsSnapshotsDirectory(e){return join(e,`.eve`,DEV_RUNTIME_ARTIFACTS_DIRECTORY,`snapshots`)}async function publishDevelopmentRuntimeArtifactsSnapshot(e){let t=await stageDevelopmentRuntimeArtifactsSnapshot(e);return await activateDevelopmentRuntimeArtifactsSnapshot({appRoot:e.project.appRoot,snapshot:t}),t}async function stageDevelopmentRuntimeArtifactsSnapshot(e){let t=join(resolveDevelopmentRuntimeArtifactsSnapshotsDirectory(e.project.appRoot),`${Date.now().toString(36)}-${randomUUID()}`),n=await createDevelopmentSourceSnapshotPlan({appRoot:e.project.appRoot,snapshotRoot:t});try{await copyDevelopmentSourceSnapshot(n),await cp(e.paths.compileDirectoryPath,join(n.runtimeAppRoot,`.eve`,`compile`),{recursive:!0}),await rewriteSnapshotCompiledManifest({appRoot:e.project.appRoot,manifestPath:join(n.runtimeAppRoot,`.eve`,`compile`,`compiled-agent-manifest.json`),runtimeAppRoot:n.runtimeAppRoot}),await validateSnapshotCompiledManifestRoots({manifestPath:join(n.runtimeAppRoot,`.eve`,`compile`,`compiled-agent-manifest.json`),runtimeAppRoot:n.runtimeAppRoot})}catch(e){throw await rm(t,{force:!0,recursive:!0}).catch(()=>{}),e}return{runtimeAppRoot:n.runtimeAppRoot,snapshotRoot:t,snapshotSourceRoot:n.snapshotSourceRoot}}async function activateDevelopmentRuntimeArtifactsSnapshot(e){await writeDevelopmentRuntimeArtifactsPointer(e)}function readDevelopmentRuntimeArtifactsSnapshotRoot(e){let t=readDevelopmentRuntimeArtifactsPointer(e);if(t!==void 0)return t.version===1?t.appRoot:t.runtimeAppRoot}function readDevelopmentRuntimeArtifactsRevision(e){return{revision:readDevelopmentRuntimeArtifactsSnapshotRoot(resolveDevelopmentRuntimeArtifactsPointerPath(e))??e}}function pruneDevelopmentRuntimeArtifactsSnapshotsInBackground(e){pruneDevelopmentRuntimeArtifactsSnapshots({appRoot:e}).catch(e=>{console.warn(`[eve:dev] failed to prune stale runtime snapshots: ${formatErrorMessage(e)}`)})}async function pruneDevelopmentRuntimeArtifactsSnapshots(e){let t=resolveDevelopmentRuntimeArtifactsSnapshotsDirectory(e.appRoot),n=[...collectProtectedSnapshotPaths(readDevelopmentRuntimeArtifactsPointer(resolveDevelopmentRuntimeArtifactsPointerPath(e.appRoot))),...await collectWorkflowDataSnapshotPaths({appRoot:e.appRoot,snapshotsDirectory:t})],i=e.now??Date.now(),a=e.recentWindowMs??9e5,o=e.retainCount??5,s;try{s=await readdir(t,{withFileTypes:!0})}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}let c=(await Promise.all(s.filter(e=>e.isDirectory()).map(async e=>{let n=join(t,e.name);return{path:n,mtimeMs:(await stat(n)).mtimeMs}}))).sort((e,t)=>t.mtimeMs-e.mtimeMs);await Promise.all(c.map(async(e,t)=>{t<o||i-e.mtimeMs<=a||n.some(t=>pathsOverlap(e.path,t))||await rm(e.path,{force:!0,recursive:!0})}))}function readDevelopmentRuntimeArtifactsPointer(n){if(!(n===void 0||!existsSync(n)))try{let e=JSON.parse(readFileSync(n,`utf8`));return e.kind!==`eve-dev-runtime-artifacts-pointer`||typeof e.version!=`number`?void 0:e.version===1&&typeof e.appRoot==`string`&&e.appRoot.length>0?{appRoot:e.appRoot,kind:`eve-dev-runtime-artifacts-pointer`,version:1}:e.version===2&&typeof e.appRoot==`string`&&typeof e.runtimeAppRoot==`string`&&e.runtimeAppRoot.length>0&&typeof e.snapshotRoot==`string`&&e.snapshotRoot.length>0?{appRoot:e.appRoot,kind:`eve-dev-runtime-artifacts-pointer`,runtimeAppRoot:e.runtimeAppRoot,snapshotRoot:e.snapshotRoot,version:2}:void 0}catch{return}}function collectProtectedSnapshotPaths(e){return e===void 0?[]:e.version===1?[e.appRoot]:[e.runtimeAppRoot,e.snapshotRoot]}async function collectWorkflowDataSnapshotPaths(e){let t=join(e.appRoot,`.workflow-data`),n=new Set;return await collectSnapshotPathsFromDirectory({directory:t,snapshotPaths:n,snapshotsDirectory:e.snapshotsDirectory}),[...n]}async function collectSnapshotPathsFromDirectory(e){let t;try{t=await readdir(e.directory,{withFileTypes:!0})}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}await Promise.all(t.map(async t=>{let n=join(e.directory,t.name);if(t.isDirectory()){await collectSnapshotPathsFromDirectory({directory:n,snapshotPaths:e.snapshotPaths,snapshotsDirectory:e.snapshotsDirectory});return}if(!t.isFile()||(await stat(n)).size>1048576)return;let i=await readFile(n,`utf8`);if(shouldScanWorkflowDataSource(i))for(let t of collectSnapshotPathsFromText(i,e.snapshotsDirectory))e.snapshotPaths.add(t)}))}function collectSnapshotPathsFromText(e,t){let n=new Set,i=t.replaceAll(`\\`,`/`),a=RegExp(`${escapeRegExp(i)}/([^/"'\\s]+)`,`gu`),o=e.replaceAll(`\\\\`,`/`).replaceAll(`\\`,`/`);for(let e of o.matchAll(a)){let i=e[1];i!==void 0&&i.length>0&&n.add(join(t,i))}return[...n]}function shouldScanWorkflowDataSource(e){let t=parseJsonObject(e);if(t===void 0)return!0;let n=t.status;return typeof n!=`string`||!TERMINAL_WORKFLOW_RUN_STATUSES.has(n)}function parseJsonObject(e){try{let t=JSON.parse(e);return isObjectRecord(t)?t:void 0}catch{return}}function isObjectRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/gu,`\\$&`)}function pathsOverlap(e,t){return isPathInsideOrEqual(e,t)||isPathInsideOrEqual(t,e)}function formatErrorMessage(e){return e instanceof Error?e.message:String(e)}async function rewriteSnapshotCompiledManifest(e){let t=JSON.parse(await readFile(e.manifestPath,`utf8`)),n=rewriteManifestRoots({appRoot:e.appRoot,runtimeAppRoot:e.runtimeAppRoot,value:t});await writeFile(e.manifestPath,`${JSON.stringify(n,null,2)}\n`)}function rewriteManifestRoots(e){if(Array.isArray(e.value))return e.value.map(t=>rewriteManifestRoots({...e,value:t}));if(e.value===null||typeof e.value!=`object`)return e.value;let t={};for(let[n,r]of Object.entries(e.value)){if(typeof r==`string`&&(n===`appRoot`||n===`agentRoot`)){t[n]=rewritePathWithinAppRoot({appRoot:e.appRoot,path:r,runtimeAppRoot:e.runtimeAppRoot});continue}t[n]=rewriteManifestRoots({appRoot:e.appRoot,runtimeAppRoot:e.runtimeAppRoot,value:r})}return t}function rewritePathWithinAppRoot(e){if(!isPathInsideOrEqual(e.path,e.appRoot))return e.path;let t=relative(e.appRoot,e.path);return t.length===0?e.runtimeAppRoot:join(e.runtimeAppRoot,t)}async function writeDevelopmentRuntimeArtifactsPointer(e){let t=resolveDevelopmentRuntimeArtifactsPointerPath(e.appRoot),r=`${t}.${randomUUID()}.tmp`,i={appRoot:e.appRoot,kind:`eve-dev-runtime-artifacts-pointer`,runtimeAppRoot:e.snapshot.runtimeAppRoot,snapshotRoot:e.snapshot.snapshotRoot,version:2};await mkdir(dirname(t),{recursive:!0}),await writeFile(r,`${JSON.stringify(i,null,2)}\n`);try{await rename(r,t)}catch(e){throw await rm(r,{force:!0}).catch(()=>{}),e}}async function validateSnapshotCompiledManifestRoots(e){let t=collectManifestRootPaths(JSON.parse(await readFile(e.manifestPath,`utf8`)));for(let n of t)if(!isPathInsideOrEqual(n,e.runtimeAppRoot))throw Error(`Development runtime snapshot manifest root "${n}" is outside runtime app root "${e.runtimeAppRoot}".`)}function collectManifestRootPaths(e){if(Array.isArray(e))return e.flatMap(e=>collectManifestRootPaths(e));if(typeof e!=`object`||!e)return[];let t=[];for(let[n,r]of Object.entries(e)){if((n===`appRoot`||n===`agentRoot`)&&typeof r==`string`){t.push(r);continue}t.push(...collectManifestRootPaths(r))}return t}function isPathInsideOrEqual(e,t){let n=resolve(e),r=resolve(t);return n===r||n.startsWith(`${r}${sep}`)}export{activateDevelopmentRuntimeArtifactsSnapshot,pruneDevelopmentRuntimeArtifactsSnapshots,pruneDevelopmentRuntimeArtifactsSnapshotsInBackground,publishDevelopmentRuntimeArtifactsSnapshot,readDevelopmentRuntimeArtifactsRevision,readDevelopmentRuntimeArtifactsSnapshotRoot,resolveDevelopmentRuntimeArtifactsPointerPath,stageDevelopmentRuntimeArtifactsSnapshot};