UNPKG

eve

Version:

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

28 lines (20 loc) 2.75 kB
import{readFile,readdir,rm}from"node:fs/promises";import{join,relative}from"node:path";const LEGACY_SELF_MODIFICATION_ROOT=`agent/subagents/self-modification`,LEGACY_FILES=[`agent.ts`,`config.ts`,`sandbox.ts`,`extensions/selfmod.ts`],LEGACY_DEFAULTS={"agent.ts":[`import { defineSelfModificationAgent } from "eve/self-modification/agent"; import config from "./config"; export default defineSelfModificationAgent({ config, // To use a specific model instead of eve's default, add: // model: "provider/model", }); `],"sandbox.ts":[`import { defineSelfModificationSandbox } from "eve/self-modification/sandbox"; import config from "./config"; export default defineSelfModificationSandbox({ config }); `],"config.ts":[`import { defineSelfModificationConfig } from "eve/self-modification/config"; export default defineSelfModificationConfig({}); `,`import { defineSelfModificationConfig } from "eve/self-modification/config"; export default defineSelfModificationConfig({ local: { enabled: true }, }); `],"extensions/selfmod.ts":[`import selfModification from "eve/self-modification"; import config from "../config"; export default selfModification(config); `]};async function detectLegacySelfModificationScaffold(e){let t=join(e,LEGACY_SELF_MODIFICATION_ROOT),n=await listFiles(t);if(!n.includes(`agent.ts`))return;let r=await readText(join(t,`agent.ts`));if(r===void 0||!r.includes(`defineSelfModificationAgent`))return;let i=new Set(LEGACY_FILES),a=JSON.stringify(await Promise.all(n.map(async e=>[e,await readText(join(t,e))]))),o=n.some(e=>!i.has(e))||(await Promise.all(Object.entries(LEGACY_DEFAULTS).map(async([e,n])=>{let r=await readText(join(t,e));return r===void 0||!n.includes(r)}))).some(Boolean)||!r.includes(`from "eve/self-modification/agent"`)||(await readText(join(t,`sandbox.ts`)))?.includes(`from "eve/self-modification/sandbox"`)!==!0;return{root:t,paths:n.map(e=>join(LEGACY_SELF_MODIFICATION_ROOT,e)),customized:o,signature:a}}async function removeLegacySelfModificationScaffold(e,t){let n=await detectLegacySelfModificationScaffold(e);if(n===void 0||n.root!==t.root||n.customized!==t.customized||n.signature!==t.signature)throw Error(`The self-modification scaffold changed while setup was waiting for cleanup approval.`);await rm(t.root,{recursive:!0,force:!0})}async function listFiles(e){try{return(await readdir(e,{recursive:!0,withFileTypes:!0})).filter(e=>e.isFile()).map(t=>relative(e,join(t.parentPath,t.name)).replaceAll(`\\`,`/`)).sort()}catch(e){if(e.code===`ENOENT`)return[];throw e}}async function readText(e){try{return await readFile(e,`utf8`)}catch(e){if(e.code===`ENOENT`)return;throw e}}export{LEGACY_SELF_MODIFICATION_ROOT,detectLegacySelfModificationScaffold,removeLegacySelfModificationScaffold};