UNPKG

eve

Version:

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

1 lines • 1.97 kB
import{readFile,rename,writeFile}from"node:fs/promises";import{join}from"node:path";import{applyModelSelectionToSource}from"#source-change/apply-model-selection.js";import{checkAgentConfigSource}from"#source-change/agent-config-string-path.js";import{applyAgentModelSettingsToSource}from"#source-change/apply-agent-model-settings.js";function createStaticSourceChange(e){return{updateModelName:t=>updateAgentModelName(e,t),updateModelSettings:t=>updateAgentModelSettings(e,t)}}async function updateAgentModelSettings(e,t){let n=e.configModule;if(n===void 0)return{kind:`bail`,reason:`agent has no agent.ts config module to edit`,at:{logicalPath:`agent.ts`,line:1}};let r=join(e.agentRoot,n.logicalPath),i=await readFile(r,`utf8`),a=await applyAgentModelSettingsToSource(i,t);if(a.kind===`bail`)return{kind:`bail`,reason:a.reason,at:{logicalPath:n.logicalPath,line:a.line}};let o=await editedSourceBail(i,a.nextSource,n.logicalPath);return o===void 0?(await writeSourceIfChanged(r,i,a.nextSource),{kind:`applied`,changed:a.changed}):o}async function updateAgentModelName(e,t){let n=e.configModule;if(n===void 0)return{kind:`bail`,reason:`agent has no agent.ts config module to edit`,at:{logicalPath:`agent.ts`,line:1}};let r=join(e.agentRoot,n.logicalPath),i=await readFile(r,`utf8`),a=await applyModelSelectionToSource(i,t);if(a.kind===`bail`)return{kind:`bail`,reason:a.reason,at:{logicalPath:n.logicalPath,line:a.line}};let o=await editedSourceBail(i,a.nextSource,n.logicalPath);return o===void 0?(await writeSourceIfChanged(r,i,a.nextSource),{kind:`applied`,from:a.from,to:a.to}):o}async function writeSourceIfChanged(e,t,n){if(n===t)return;let r=`${e}.${process.pid}.eve-tmp`;await writeFile(r,n,`utf8`),await rename(r,e)}async function editedSourceBail(e,t,n){if(t===e)return;let r=await checkAgentConfigSource(t);if(r!==void 0)return{kind:`bail`,reason:`the edit produced source eve refuses to write (${r})`,at:{logicalPath:n,line:1}}}export{createStaticSourceChange};