eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 2.5 kB
JavaScript
import{readdir,stat}from"node:fs/promises";import{basename,isAbsolute,relative,resolve,sep}from"node:path";import{PROJECT_NAME_ERROR,parseProjectName}from"#setup/project-name.js";import{classifyAgentRootEntry,getDirectoryEntryType}from"#discover/filesystem.js";const ENVIRONMENT_ONLY_ENTRIES=new Set([`.DS_Store`,`.editorconfig`,`.gitattributes`,`.gitignore`,`.git`,`.idea`,`.vscode`]);async function pathKind(e){let t=await stat(e).catch(e=>{if(e.code!==`ENOENT`)throw e});return t===void 0?`missing`:t.isDirectory()?`directory`:`other`}function isEnvironmentOnly(e){return e.every(e=>ENVIRONMENT_ONLY_ENTRIES.has(e))}async function isAgentRoot(e){return(await readdir(e,{withFileTypes:!0}).catch(()=>[])).some(e=>{let t=classifyAgentRootEntry(e.name,getDirectoryEntryType(e));return t!==`unknown`&&t!==`ignored-directory`&&t!==`lib-directory`})}async function isExistingEveProject(e,t){return await isAgentRoot(e)?!0:(t.includes(`package.json`)||t.includes(`vercel.json`))&&t.includes(`agent`)&&await isAgentRoot(resolve(e,`agent`))}function listEntries(e){return e.map(e=>` - ${e}`).join(`
`)}function assertTargetStaysWithinParent(e,t,n){if(t===void 0||isAbsolute(t))return;let r=relative(e,n);if(r===`..`||r.startsWith(`..${sep}`))throw Error(PROJECT_NAME_ERROR)}async function resolveInitTarget(e){let t=resolve(e.parentDirectory),n=e.target!==void 0,r=resolve(t,e.target??`.`);assertTargetStaysWithinParent(t,e.target,r);let i=r===t,a=await pathKind(r);if(a===`other`)throw Error(`Cannot initialize an agent because "${r}" is not a directory.`);if(a===`missing`)return{createInPlace:!1,failurePolicy:`remove`,kind:`fresh`,overwriteExisting:!1,preservedEntries:[],projectName:parseProjectName(basename(r)),projectPath:r};let o=(await readdir(r)).sort();if(o.length===0||isEnvironmentOnly(o)){let e=i?`.`:parseProjectName(basename(r));return{createInPlace:i||o.length>0,failurePolicy:`clear`,kind:`fresh`,overwriteExisting:!1,preservedEntries:o,projectName:e,projectPath:r}}if(await isExistingEveProject(r,o))throw Error(`An eve project already exists at "${r}". Run an existing-project command from that directory instead.`);if(o.includes(`package.json`)){if(!n||e.target!==`.`)throw Error(`Adding eve to an existing package requires an explicit \`eve init .\` from "${r}".`);return{kind:`existing`,projectPath:r}}throw Error(`Cannot initialize an agent in the non-empty directory "${r}". Move or remove these entries, or choose an empty target:\n${listEntries(o)}`)}export{resolveInitTarget};