eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 3.57 kB
JavaScript
import{checkAgentConfigSource,escapeForQuote,isAstNode,keyMatches,lineAt,parseAgentObject,unwrapExpression}from"./agent-config-ast.js";async function applyAgentConfigStringPath(e,n,r){let o=await parseAgentObject(e);if(o.kind===`bail`)return o;let s=[o.object],c=[],l=o.object;for(let[t,a]of n.slice(0,-1).entries()){let o=findProperty(l,a);if(o.kind===`bail`)return{...o,line:lineAt(e,l.start)};if(o.property===void 0){if(r.kind===`remove`)return{kind:`applied`,nextSource:e};let i=nestedObjectSource(n.slice(t+1),r.value);return{kind:`applied`,nextSource:insertProperty(e,l,a,i)}}let u=propertyObjectValue(o.property);if(u===void 0)return{kind:`bail`,reason:`\`${a}\` is not an object literal that eve can edit safely`,line:lineAt(e,o.property.start)};c.push(o.property),s.push(u),l=u}let u=n.at(-1),d=findProperty(l,u);if(d.kind===`bail`)return{...d,line:lineAt(e,l.start)};if(d.property===void 0)return r.kind===`remove`?{kind:`applied`,nextSource:e}:{kind:`applied`,nextSource:insertProperty(e,l,u,JSON.stringify(r.value))};let f=propertyStringValue(d.property);if(f===void 0)return{kind:`bail`,reason:`\`${n.join(`.`)}\` is not a string literal that eve can edit safely`,line:lineAt(e,d.property.start)};if(r.kind===`set`){if(f.value===r.value)return{kind:`applied`,nextSource:e};let n=f.raw?.[0]===`'`?`'`:`"`,i=`${n}${escapeForQuote(r.value,n)}${n}`;return{kind:`applied`,nextSource:e.slice(0,f.start)+i+e.slice(f.end)}}if(r.removable!==void 0&&!r.removable(f.value))return{kind:`bail`,reason:`\`${n.join(`.`)}\` has the custom value ${JSON.stringify(f.value)}`,line:lineAt(e,d.property.start)};let p=d.property,m=l;for(let e=s.length-1;e>0&&m.properties.length===1;--e)p=c[e-1],m=s[e-1];return{kind:`applied`,nextSource:removeProperty(e,m,p)}}function findProperty(e,t){if(e.properties.some(e=>e.type===`SpreadElement`||e.computed===!0))return{kind:`bail`,reason:`\`${t}\` may be supplied by a spread or computed property`};let n=e.properties.filter(e=>e.type===`Property`&&!e.computed&&keyMatches(e.key,t));if(n.length>1)return{kind:`bail`,reason:`\`${t}\` is defined more than once`};let i=n[0];return i!==void 0&&i.start!==void 0&&i.end!==void 0?{kind:`ok`,property:i}:{kind:`ok`}}function propertyObjectValue(e){let t=e.value;if(!isAstNode(t))return;let r=unwrapExpression(t);return r.type===`ObjectExpression`&&r.start!==void 0&&r.end!==void 0&&r.properties!==void 0?r:void 0}function propertyStringValue(e){let t=e.value;if(!isAstNode(t))return;let r=unwrapExpression(t);if(r.type!==`Literal`||typeof r.value!=`string`||r.start===void 0||r.end===void 0)return;let i={start:r.start,end:r.end,value:r.value};return r.raw===void 0?i:{...i,raw:r.raw}}function nestedObjectSource(e,t){let n=JSON.stringify(t);for(let t of[...e].reverse())n=`{ ${t}: ${n} }`;return n}function insertProperty(e,t,n,r){let i=t.end-1,a=t.properties.at(-1);if(a===void 0||a.end===void 0)return e.slice(0,i)+` ${n}: ${r} `+e.slice(i);let o=e.lastIndexOf(`
`,i-1)+1;if(o>t.start){let t=`${e.slice(o,i)} `,s=e.slice(a.end,o),c=e;s.includes(`,`)||(c=c.slice(0,a.end)+`,`+c.slice(a.end));let l=o+ +!s.includes(`,`);return c.slice(0,l)+`${t}${n}: ${r},\n`+c.slice(l)}let s=e.slice(a.end,i).includes(`,`)?` `:`, `;return e.slice(0,i)+`${s}${n}: ${r}`+e.slice(i)}function removeProperty(e,t,n){let r=t.properties.indexOf(n),i=r>0?t.properties[r-1]:void 0,a=r>=0?t.properties[r+1]:void 0;return a?.start===void 0?i?.end===void 0?e.slice(0,t.start+1)+e.slice(t.end-1):e.slice(0,i.end)+e.slice(n.end):e.slice(0,n.start)+e.slice(a.start)}export{applyAgentConfigStringPath,checkAgentConfigSource};