eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 2 kB
JavaScript
import{parseWithNitroRolldownAst}from"#internal/bundler/nitro-rolldown.js";async function parseAgentObject(t){let n;try{n=await parseWithNitroRolldownAst(`agent.ts`,t)}catch(e){let n=e;return{kind:`bail`,reason:`agent.ts does not parse: ${n.message??`unknown parse error`}`,line:parseErrorLine(t,n)}}if((n.errors?.length??0)>0){let e=n.errors?.[0];return{kind:`bail`,reason:`agent.ts does not parse: ${e?.message??`unknown parse error`}`,line:parseErrorLine(t,e)}}let r=findDefineAgentObject(n.program??n);return r===void 0?{kind:`bail`,reason:"no `export default defineAgent({ ... })` call found",line:1}:{kind:`ok`,object:r}}async function checkAgentConfigSource(e){let t=await parseAgentObject(e);return t.kind===`bail`?t.reason:void 0}function findDefineAgentObject(e){for(let t of e.body??[]){if(t.type!==`ExportDefaultDeclaration`||t.declaration==null)continue;let e=unwrapExpression(t.declaration);if(e.type!==`CallExpression`||e.callee?.type!==`Identifier`||e.callee.name!==`defineAgent`)continue;let n=e.arguments?.[0];if(n===void 0||n.type===`SpreadElement`)continue;let r=unwrapExpression(n);if(r.type===`ObjectExpression`&&r.start!==void 0&&r.end!==void 0&&r.properties!==void 0)return r}}function unwrapExpression(e){let t=e;for(;t.type===`ParenthesizedExpression`||t.type===`TSAsExpression`||t.type===`TSSatisfiesExpression`;){if(t.expression==null)return t;t=t.expression}return t}function keyMatches(e,t){return e?.type===`Identifier`?e.name===t:e?.type===`Literal`&&typeof e.value==`string`&&e.value===t}function isAstNode(e){return typeof e==`object`&&!!e&&typeof e.type==`string`}function escapeForQuote(e,t){return e.replaceAll(`\\`,`\\\\`).replaceAll(t,`\\${t}`)}function lineAt(e,t){let n=1;for(let r=0;r<t&&r<e.length;r+=1)e[r]===`
`&&(n+=1);return n}function parseErrorLine(e,t){return typeof t?.loc?.line==`number`?t.loc.line:lineAt(e,t?.labels?.[0]?.start??t?.start??0)}export{checkAgentConfigSource,escapeForQuote,isAstNode,keyMatches,lineAt,parseAgentObject,unwrapExpression};