Move LLM and agent into ext/ai with host tool catalog and call_fn.
Consolidate official AI extensions under ext/ai/, add agent history/context host APIs, dynamic call_fn, and a first-pass TOOL:name loop so runbooks can list zero-arg tools in a table. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c4ffb9473f
commit
92d88fe761
29 files changed
+1019
-558
No files matched your search
@@ -65,11 +65,16 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
|
||||
functions.push(compile_function(i, &flat, path_buf.as_deref())?);
|
||||
}
|
||||
|
||||
let parents: Vec<Option<usize>> = flat.iter().map(|f| f.parent).collect();
|
||||
let children: Vec<Vec<usize>> = flat.iter().map(|f| f.children.clone()).collect();
|
||||
|
||||
Ok(Program {
|
||||
functions,
|
||||
entry,
|
||||
objects,
|
||||
methods,
|
||||
parents,
|
||||
children,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -470,6 +475,17 @@ impl<'a> FnCompiler<'a> {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
"call_fn" => {
|
||||
let name_expr = self
|
||||
.named_or_first(&call, "name")
|
||||
.ok_or_else(|| self.err("call_fn requires name"))?;
|
||||
self.compile_expr(name_expr)?;
|
||||
self.emit(Op::CallFn);
|
||||
if as_stmt {
|
||||
self.emit(Op::Pop);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
other if crate::host::HostFn::from_name(other).is_some() => {
|
||||
let hf = crate::host::HostFn::from_name(other).unwrap();
|
||||
let params = hf.all_params();
|
||||
|
||||
@@ -56,6 +56,8 @@ pub enum Op {
|
||||
GetIndex,
|
||||
/// Call function by index; argc values are on the stack (param order).
|
||||
Call(u16, u8),
|
||||
/// Pop function name text → call zero-arg top-level or lexically visible function.
|
||||
CallFn,
|
||||
/// After a constructor Call: tag top-of-stack map with `_type` = constants[idx].
|
||||
TagInstance(u16),
|
||||
/// Method call: stack has argc args (param order) then receiver on top.
|
||||
@@ -88,6 +90,10 @@ pub struct Program {
|
||||
pub objects: Vec<(String, usize)>,
|
||||
/// (object_fn_idx, method_name) → method fn idx
|
||||
pub methods: Vec<(usize, String, usize)>,
|
||||
/// Parallel to `functions`: parent fn index for dynamic `call_fn` lookup.
|
||||
pub parents: Vec<Option<usize>>,
|
||||
/// Parallel to `functions`: child fn indices.
|
||||
pub children: Vec<Vec<usize>>,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
|
||||
@@ -115,6 +115,8 @@ impl Vm {
|
||||
ip: 0,
|
||||
slots: vec![Value::None; program.functions[entry].locals.len().max(1)],
|
||||
}];
|
||||
self.host
|
||||
.push_call_frame(&program.functions[entry].name.clone());
|
||||
|
||||
loop {
|
||||
let frame = frames.last_mut().unwrap();
|
||||
@@ -127,6 +129,7 @@ impl Vm {
|
||||
let op = fun.code[ip];
|
||||
let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1));
|
||||
frame.ip += 1;
|
||||
self.host.current_line = span.line;
|
||||
|
||||
if self.trace {
|
||||
match op {
|
||||
@@ -347,11 +350,13 @@ impl Vm {
|
||||
for i in (0..argc).rev() {
|
||||
slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
}
|
||||
let fname = program.functions[fid].name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: fid,
|
||||
ip: 0,
|
||||
slots,
|
||||
});
|
||||
self.host.push_call_frame(&fname);
|
||||
}
|
||||
Op::TagInstance(idx) => {
|
||||
let type_name = match fun.constants.get(idx as usize) {
|
||||
@@ -410,11 +415,13 @@ impl Vm {
|
||||
slots[i] = recv.clone();
|
||||
}
|
||||
}
|
||||
let fname = callee.name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: method_fid,
|
||||
ip: 0,
|
||||
slots,
|
||||
});
|
||||
self.host.push_call_frame(&fname);
|
||||
}
|
||||
Op::PluginCall(name_idx, argc) => {
|
||||
let argc = argc as usize;
|
||||
@@ -478,9 +485,35 @@ impl Vm {
|
||||
stack.push(result);
|
||||
}
|
||||
}
|
||||
Op::CallFn => {
|
||||
let name_v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let name = match name_v {
|
||||
Value::Text(s) => s,
|
||||
_ => return Err(self.err_at(span, "call_fn requires text name")),
|
||||
};
|
||||
let current = frames.last().unwrap().fn_idx;
|
||||
let fid = resolve_fn_dynamic(program, current, &name)
|
||||
.map_err(|m| self.err_at(span, m))?;
|
||||
let callee = &program.functions[fid];
|
||||
if !callee.params.is_empty() {
|
||||
return Err(self.err_at(
|
||||
span,
|
||||
format!("call_fn: `{name}` must have no parameters"),
|
||||
));
|
||||
}
|
||||
let slots = vec![Value::None; callee.locals.len().max(1)];
|
||||
let fname = callee.name.clone();
|
||||
frames.push(Frame {
|
||||
fn_idx: fid,
|
||||
ip: 0,
|
||||
slots,
|
||||
});
|
||||
self.host.push_call_frame(&fname);
|
||||
}
|
||||
Op::Return => {
|
||||
let ret = pop(&mut stack).unwrap_or(Value::None);
|
||||
frames.pop();
|
||||
self.host.pop_call_frame();
|
||||
if frames.is_empty() {
|
||||
return Ok(ret);
|
||||
}
|
||||
@@ -569,3 +602,26 @@ fn tag_instance_value(type_name: &str, value: Value) -> Value {
|
||||
]),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result<usize, String> {
|
||||
for &cid in &program.children[current] {
|
||||
if program.functions[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
let mut parent = program.parents[current];
|
||||
while let Some(pid) = parent {
|
||||
for &cid in &program.children[pid] {
|
||||
if program.functions[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
parent = program.parents[pid];
|
||||
}
|
||||
for (i, f) in program.functions.iter().enumerate() {
|
||||
if program.parents[i].is_none() && f.name == name {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
Err(format!("call_fn: unknown function `{name}`"))
|
||||
}
|
||||
Reference in new issue
Block a user