Repository navigation
Expand file tree
/
Copy pathmodel-anchors.cjs
More file actions
26 lines (26 loc) · 2.7 KB
/
Copy pathmodel-anchors.cjs
File metadata and controls
26 lines (26 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'use strict';
const oxc=require('oxc-parser');
const {parseProgram,walk,key,property}=require('./ast.cjs');
function modelAnchors(source){
const schemas=[],handlers=[],entries=[];
const lk=n=>n?.name??n?.value;
const lp=(n,k)=>n?.properties?.find(p=>!p.computed&&lk(p.key)===k)?.value;
const scan={
AssignmentExpression(n){if(n.left?.type!=='IdentifierReference')return;const c=n.right;if(c?.type!=='CallExpression'||lk(c.callee?.property)!=='object')return;const obj=c.arguments[0];if(obj?.type!=='ObjectExpression')return;if(['description','prompt','subagent_type','run_in_background'].every(k=>lp(obj,k)))schemas.push({symbol:n.left.name,start:obj.start,end:obj.end});},
CallExpression(n){if(n.arguments.length===2&&n.arguments[1]?.value==='agentHandler'&&n.arguments[0]?.type==='ArrowFunctionExpression')handlers.push({start:n.arguments[0].start,end:n.arguments[0].end});},
ObjectExpression(n){if(lp(lp(n,'metadata'),'name')?.value==='Agent')entries.push({handler:lk(lp(n,'handler')),start:n.start,end:n.end});}
};
if(!oxc.rawTransferSupported())throw Error('Model patch currently requires Oxc raw-transfer support');
const p=oxc.parseSync('zcode.cjs',source,{experimentalLazy:true});try{if(p.errors.length)throw Error(p.errors.map(e=>e.message).join('\n'));p.visit(new (oxc.experimentalGetLazyVisitor())(scan));}finally{p.dispose();}
if(schemas.length!==1||handlers.length!==1||entries.length!==1)throw Error('Cannot uniquely locate Agent schema/handler/tool entry');
const parseExpression=span=>{const n=parseProgram('('+source.slice(span.start,span.end)+')').body[0].expression.expression;walk(n,{enter(x){x.start+=span.start-1;x.end+=span.start-1;}});return n;};
const schema=parseExpression(schemas[0]),handler=parseExpression(handlers[0]),entry=parseExpression(entries[0]);
const parses=[],launches=[];
walk(handler,{VariableDeclarator(n){if(n.init?.type==='CallExpression'&&key(n.init.callee.property)==='parse'&&n.init.callee.object.name===schemas[0].symbol)parses.push(n);},CallExpression(n){if(key(n.callee.property)==='launch'&&key(n.callee.object?.property)==='subagentPort')launches.push(n);}});
if(parses.length!==1||launches.length!==1||handler.params.length!==2)throw Error('Agent parameter/launch chain changed');
const first=property(schema,'description').value,constructors=[];
walk(first,{CallExpression(n){if(key(n.callee.property)==='string'&&n.callee.object.type==='Identifier')constructors.push(n.callee.object.name);}});
if(constructors.length!==1)throw Error('Zod constructor changed');
return {schema,handler,entry,schemaSymbol:schemas[0].symbol,handlerSymbol:entries[0].handler,parsed:parses[0].id.name,context:handler.params[1].name,launch:launches[0],zod:constructors[0]};
}
module.exports={modelAnchors};