UNPKG

eve

Version:

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

2 lines 3.57 kB
import{checkAgentConfigSource,escapeForQuote,isAstNode,keyMatches,lineAt,parseAgentObject,unwrapExpression}from"./agent-config-ast.js";async function applyAgentConfigStringPath(e,t,n){let r=await parseAgentObject(e);if(r.kind===`bail`)return r;let i=[r.object],a=[],o=r.object;for(let[r,s]of t.slice(0,-1).entries()){let c=findProperty(o,s);if(c.kind===`bail`)return{...c,line:lineAt(e,o.start)};if(c.property===void 0){if(n.kind===`remove`)return{kind:`applied`,nextSource:e};let i=nestedObjectSource(t.slice(r+1),n.value);return{kind:`applied`,nextSource:insertProperty(e,o,s,i)}}let l=propertyObjectValue(c.property);if(l===void 0)return{kind:`bail`,reason:`\`${s}\` is not an object literal that eve can edit safely`,line:lineAt(e,c.property.start)};a.push(c.property),i.push(l),o=l}let s=t.at(-1),c=findProperty(o,s);if(c.kind===`bail`)return{...c,line:lineAt(e,o.start)};if(c.property===void 0)return n.kind===`remove`?{kind:`applied`,nextSource:e}:{kind:`applied`,nextSource:insertProperty(e,o,s,JSON.stringify(n.value))};let l=propertyStringValue(c.property);if(l===void 0)return{kind:`bail`,reason:`\`${t.join(`.`)}\` is not a string literal that eve can edit safely`,line:lineAt(e,c.property.start)};if(n.kind===`set`){if(l.value===n.value)return{kind:`applied`,nextSource:e};let t=l.raw?.[0]===`'`?`'`:`"`,r=`${t}${escapeForQuote(n.value,t)}${t}`;return{kind:`applied`,nextSource:e.slice(0,l.start)+r+e.slice(l.end)}}if(n.removable!==void 0&&!n.removable(l.value))return{kind:`bail`,reason:`\`${t.join(`.`)}\` has the custom value ${JSON.stringify(l.value)}`,line:lineAt(e,c.property.start)};let u=c.property,d=o;for(let e=i.length-1;e>0&&d.properties.length===1;--e)u=a[e-1],d=i[e-1];return{kind:`applied`,nextSource:removeProperty(e,d,u)}}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 r=n[0];return r!==void 0&&r.start!==void 0&&r.end!==void 0?{kind:`ok`,property:r}:{kind:`ok`}}function propertyObjectValue(e){let t=e.value;if(!isAstNode(t))return;let n=unwrapExpression(t);return n.type===`ObjectExpression`&&n.start!==void 0&&n.end!==void 0&&n.properties!==void 0?n:void 0}function propertyStringValue(e){let t=e.value;if(!isAstNode(t))return;let n=unwrapExpression(t);if(n.type!==`Literal`||typeof n.value!=`string`||n.start===void 0||n.end===void 0)return;let r={start:n.start,end:n.end,value:n.value};return n.raw===void 0?r:{...r,raw:n.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};