Files
marqdo/src/host/dispatch.rs
T
cflmyandCursor b6bb42e460 Implement streaming S0-S2: SSE complete, step/plan events.
Add net/llm SSE primitives and optional stream/echo/trace on step and plan so callers can watch deltas and rounds without changing final return values.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-10 11:03:04 +08:00

730 lines
29 KiB
Rust

//! Host function registry and dispatch.
use std::collections::HashMap;
use crate::host::{agent_rt, foreign, fs, json, math, net, plugin, subtask, sys, time, writeback, HostContext};
use crate::value::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum HostFn {
ReadText = 1,
WriteText = 2,
AppendText = 3,
Exists = 4,
ListDir = 5,
MakeDir = 6,
Remove = 7,
NowUnix = 8,
NowMs = 9,
FormatTime = 10,
SleepMs = 11,
ParseTime = 12,
EnvGet = 13,
EnvSet = 14,
Args = 15,
Cwd = 16,
Exit = 17,
Exec = 18,
JsonParse = 19,
JsonStringify = 20,
JsonGet = 21,
JsonKeys = 22,
HttpGet = 23,
HttpPost = 24,
UrlEncode = 25,
// math
Pi = 26,
EConst = 27,
Num = 28,
MathAdd = 29,
MathSub = 30,
MathMul = 31,
MathDiv = 32,
MathPow = 33,
MathNeg = 34,
MathSin = 35,
MathCos = 36,
MathTan = 37,
MathAsin = 38,
MathAcos = 39,
MathAtan = 40,
MathSqrt = 41,
MathAbs = 42,
MathLn = 43,
MathExp = 44,
MathFloor = 45,
MathCeil = 46,
MathMin = 47,
MathMax = 48,
Random = 49,
RandomInt = 50,
Seed = 51,
Formula = 52,
Simplify = 53,
Expand = 54,
Diff = 55,
Subs = 56,
Eval = 57,
Solve = 58,
Plot = 59,
PlotPoints = 60,
PlotConic = 61,
ForeignSetCmd = 62,
ForeignRun = 63,
ForeignRunLang = 64,
ForeignLangs = 65,
DotenvLoad = 66,
HttpRequest = 67,
JsonQuote = 68,
PluginLoad = 69,
PluginUnload = 70,
PluginList = 71,
ExtNativePath = 72,
MapSet = 76,
ListAppend = 77,
WritebackRecord = 84,
WritebackGet = 85,
WritebackClear = 86,
WritebackList = 87,
SubtaskSpawn = 88,
SubtaskPoll = 89,
SubtaskJoin = 90,
SubtaskKill = 91,
SubtaskWaitAll = 92,
WritebackEnsure = 93,
OuterCallLine = 94,
TextPatch = 95,
ApplyPatchBlocks = 96,
WritebackScanPath = 97,
HttpPostSse = 98,
OpenAiSseParse = 99,
}
impl HostFn {
pub fn from_u16(id: u16) -> Option<Self> {
Some(match id {
1 => Self::ReadText,
2 => Self::WriteText,
3 => Self::AppendText,
4 => Self::Exists,
5 => Self::ListDir,
6 => Self::MakeDir,
7 => Self::Remove,
8 => Self::NowUnix,
9 => Self::NowMs,
10 => Self::FormatTime,
11 => Self::SleepMs,
12 => Self::ParseTime,
13 => Self::EnvGet,
14 => Self::EnvSet,
15 => Self::Args,
16 => Self::Cwd,
17 => Self::Exit,
18 => Self::Exec,
19 => Self::JsonParse,
20 => Self::JsonStringify,
21 => Self::JsonGet,
22 => Self::JsonKeys,
23 => Self::HttpGet,
24 => Self::HttpPost,
25 => Self::UrlEncode,
26 => Self::Pi,
27 => Self::EConst,
28 => Self::Num,
29 => Self::MathAdd,
30 => Self::MathSub,
31 => Self::MathMul,
32 => Self::MathDiv,
33 => Self::MathPow,
34 => Self::MathNeg,
35 => Self::MathSin,
36 => Self::MathCos,
37 => Self::MathTan,
38 => Self::MathAsin,
39 => Self::MathAcos,
40 => Self::MathAtan,
41 => Self::MathSqrt,
42 => Self::MathAbs,
43 => Self::MathLn,
44 => Self::MathExp,
45 => Self::MathFloor,
46 => Self::MathCeil,
47 => Self::MathMin,
48 => Self::MathMax,
49 => Self::Random,
50 => Self::RandomInt,
51 => Self::Seed,
52 => Self::Formula,
53 => Self::Simplify,
54 => Self::Expand,
55 => Self::Diff,
56 => Self::Subs,
57 => Self::Eval,
58 => Self::Solve,
59 => Self::Plot,
60 => Self::PlotPoints,
61 => Self::PlotConic,
62 => Self::ForeignSetCmd,
63 => Self::ForeignRun,
64 => Self::ForeignRunLang,
65 => Self::ForeignLangs,
66 => Self::DotenvLoad,
67 => Self::HttpRequest,
68 => Self::JsonQuote,
69 => Self::PluginLoad,
70 => Self::PluginUnload,
71 => Self::PluginList,
72 => Self::ExtNativePath,
76 => Self::MapSet,
77 => Self::ListAppend,
84 => Self::WritebackRecord,
85 => Self::WritebackGet,
86 => Self::WritebackClear,
87 => Self::WritebackList,
88 => Self::SubtaskSpawn,
89 => Self::SubtaskPoll,
90 => Self::SubtaskJoin,
91 => Self::SubtaskKill,
92 => Self::SubtaskWaitAll,
93 => Self::WritebackEnsure,
94 => Self::OuterCallLine,
95 => Self::TextPatch,
96 => Self::ApplyPatchBlocks,
97 => Self::WritebackScanPath,
98 => Self::HttpPostSse,
99 => Self::OpenAiSseParse,
_ => return None,
})
}
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"host_read_text" => Self::ReadText,
"host_write_text" => Self::WriteText,
"host_append_text" => Self::AppendText,
"host_exists" => Self::Exists,
"host_list_dir" => Self::ListDir,
"host_make_dir" => Self::MakeDir,
"host_remove" => Self::Remove,
"host_now_unix" => Self::NowUnix,
"host_now_ms" => Self::NowMs,
"host_format_time" => Self::FormatTime,
"host_sleep_ms" => Self::SleepMs,
"host_parse_time" => Self::ParseTime,
"host_env_get" => Self::EnvGet,
"host_env_set" => Self::EnvSet,
"host_args" => Self::Args,
"host_cwd" => Self::Cwd,
"host_exit" => Self::Exit,
"host_exec" => Self::Exec,
"host_json_parse" => Self::JsonParse,
"host_json_stringify" => Self::JsonStringify,
"host_json_get" => Self::JsonGet,
"host_json_keys" => Self::JsonKeys,
// Aliases so optional `headers` / `content_type` bind like plot.
"host_http_get" | "http_get" => Self::HttpGet,
"host_http_post" | "http_post" => Self::HttpPost,
"host_http_post_sse" | "http_post_sse" => Self::HttpPostSse,
"host_openai_sse_parse" | "openai_sse_parse" => Self::OpenAiSseParse,
"host_url_encode" => Self::UrlEncode,
"host_pi" => Self::Pi,
"host_e" => Self::EConst,
"host_num" => Self::Num,
"host_math_add" => Self::MathAdd,
"host_math_sub" => Self::MathSub,
"host_math_mul" => Self::MathMul,
"host_math_div" => Self::MathDiv,
"host_math_pow" => Self::MathPow,
"host_math_neg" => Self::MathNeg,
"host_math_sin" => Self::MathSin,
"host_math_cos" => Self::MathCos,
"host_math_tan" => Self::MathTan,
"host_math_asin" => Self::MathAsin,
"host_math_acos" => Self::MathAcos,
"host_math_atan" => Self::MathAtan,
"host_math_sqrt" => Self::MathSqrt,
"host_math_abs" => Self::MathAbs,
"host_math_ln" => Self::MathLn,
"host_math_exp" => Self::MathExp,
"host_math_floor" => Self::MathFloor,
"host_math_ceil" => Self::MathCeil,
"host_math_min" => Self::MathMin,
"host_math_max" => Self::MathMax,
"host_random" => Self::Random,
"host_random_int" => Self::RandomInt,
"host_seed" => Self::Seed,
"host_formula" => Self::Formula,
"host_simplify" => Self::Simplify,
"host_expand" => Self::Expand,
"host_diff" => Self::Diff,
"host_subs" => Self::Subs,
"host_eval" => Self::Eval,
"host_solve" => Self::Solve,
// Friendly aliases so L1 `plot` / `绘图` get host optionals (`grid`, `path`, …)
// instead of thin wrappers that only forward required params.
"host_plot" | "plot" | "绘图" => Self::Plot,
"host_plot_points" | "plot_points" | "绘点列" => Self::PlotPoints,
"host_plot_conic" | "plot_conic" | "绘圆锥" => Self::PlotConic,
"host_foreign_set_cmd" => Self::ForeignSetCmd,
"host_foreign_run" => Self::ForeignRun,
"host_foreign_run_lang" => Self::ForeignRunLang,
"host_foreign_langs" => Self::ForeignLangs,
"host_dotenv_load" | "load_dotenv" => Self::DotenvLoad,
"host_http_request" | "http_request" => Self::HttpRequest,
"host_json_quote" | "quote" => Self::JsonQuote,
"host_plugin_load" | "plugin_load" => Self::PluginLoad,
"host_plugin_unload" | "plugin_unload" => Self::PluginUnload,
"host_plugin_list" | "plugin_list" => Self::PluginList,
"host_ext_native_path" | "ext_native_path" => Self::ExtNativePath,
"host_map_set" | "map_set" => Self::MapSet,
"host_list_append" | "list_append" => Self::ListAppend,
"host_writeback_record" | "writeback_record" => Self::WritebackRecord,
"host_writeback_get" | "writeback_get" => Self::WritebackGet,
"host_writeback_clear" | "writeback_clear" => Self::WritebackClear,
"host_writeback_list" | "writeback_list" => Self::WritebackList,
"host_writeback_ensure" | "writeback_ensure" => Self::WritebackEnsure,
"host_outer_call_line" | "outer_call_line" => Self::OuterCallLine,
"host_text_patch" | "text_patch" => Self::TextPatch,
"host_apply_patch_blocks" | "apply_patch_blocks" => Self::ApplyPatchBlocks,
"host_writeback_scan_path" | "writeback_scan_path" => Self::WritebackScanPath,
"host_subtask_spawn" | "subtask_spawn" => Self::SubtaskSpawn,
"host_subtask_poll" | "subtask_poll" => Self::SubtaskPoll,
"host_subtask_join" | "subtask_join" => Self::SubtaskJoin,
"host_subtask_kill" | "subtask_kill" => Self::SubtaskKill,
"host_subtask_wait_all" | "subtask_wait_all" => Self::SubtaskWaitAll,
_ => return None,
})
}
pub fn as_u16(self) -> u16 {
self as u16
}
pub fn name(self) -> &'static str {
match self {
Self::ReadText => "host_read_text",
Self::WriteText => "host_write_text",
Self::AppendText => "host_append_text",
Self::Exists => "host_exists",
Self::ListDir => "host_list_dir",
Self::MakeDir => "host_make_dir",
Self::Remove => "host_remove",
Self::NowUnix => "host_now_unix",
Self::NowMs => "host_now_ms",
Self::FormatTime => "host_format_time",
Self::SleepMs => "host_sleep_ms",
Self::ParseTime => "host_parse_time",
Self::EnvGet => "host_env_get",
Self::EnvSet => "host_env_set",
Self::Args => "host_args",
Self::Cwd => "host_cwd",
Self::Exit => "host_exit",
Self::Exec => "host_exec",
Self::JsonParse => "host_json_parse",
Self::JsonStringify => "host_json_stringify",
Self::JsonGet => "host_json_get",
Self::JsonKeys => "host_json_keys",
Self::HttpGet => "host_http_get",
Self::HttpPost => "host_http_post",
Self::HttpPostSse => "host_http_post_sse",
Self::OpenAiSseParse => "host_openai_sse_parse",
Self::UrlEncode => "host_url_encode",
Self::Pi => "host_pi",
Self::EConst => "host_e",
Self::Num => "host_num",
Self::MathAdd => "host_math_add",
Self::MathSub => "host_math_sub",
Self::MathMul => "host_math_mul",
Self::MathDiv => "host_math_div",
Self::MathPow => "host_math_pow",
Self::MathNeg => "host_math_neg",
Self::MathSin => "host_math_sin",
Self::MathCos => "host_math_cos",
Self::MathTan => "host_math_tan",
Self::MathAsin => "host_math_asin",
Self::MathAcos => "host_math_acos",
Self::MathAtan => "host_math_atan",
Self::MathSqrt => "host_math_sqrt",
Self::MathAbs => "host_math_abs",
Self::MathLn => "host_math_ln",
Self::MathExp => "host_math_exp",
Self::MathFloor => "host_math_floor",
Self::MathCeil => "host_math_ceil",
Self::MathMin => "host_math_min",
Self::MathMax => "host_math_max",
Self::Random => "host_random",
Self::RandomInt => "host_random_int",
Self::Seed => "host_seed",
Self::Formula => "host_formula",
Self::Simplify => "host_simplify",
Self::Expand => "host_expand",
Self::Diff => "host_diff",
Self::Subs => "host_subs",
Self::Eval => "host_eval",
Self::Solve => "host_solve",
Self::Plot => "host_plot",
Self::PlotPoints => "host_plot_points",
Self::PlotConic => "host_plot_conic",
Self::ForeignSetCmd => "host_foreign_set_cmd",
Self::ForeignRun => "host_foreign_run",
Self::ForeignRunLang => "host_foreign_run_lang",
Self::ForeignLangs => "host_foreign_langs",
Self::DotenvLoad => "host_dotenv_load",
Self::HttpRequest => "host_http_request",
Self::JsonQuote => "host_json_quote",
Self::PluginLoad => "host_plugin_load",
Self::PluginUnload => "host_plugin_unload",
Self::PluginList => "host_plugin_list",
Self::ExtNativePath => "host_ext_native_path",
Self::MapSet => "host_map_set",
Self::ListAppend => "host_list_append",
Self::WritebackRecord => "host_writeback_record",
Self::WritebackGet => "host_writeback_get",
Self::WritebackClear => "host_writeback_clear",
Self::WritebackList => "host_writeback_list",
Self::WritebackEnsure => "host_writeback_ensure",
Self::OuterCallLine => "host_outer_call_line",
Self::TextPatch => "host_text_patch",
Self::ApplyPatchBlocks => "host_apply_patch_blocks",
Self::WritebackScanPath => "host_writeback_scan_path",
Self::SubtaskSpawn => "host_subtask_spawn",
Self::SubtaskPoll => "host_subtask_poll",
Self::SubtaskJoin => "host_subtask_join",
Self::SubtaskKill => "host_subtask_kill",
Self::SubtaskWaitAll => "host_subtask_wait_all",
}
}
pub fn required_params(self) -> &'static [&'static str] {
match self {
Self::ReadText | Self::Exists | Self::ListDir | Self::MakeDir | Self::Remove => {
&["path"]
}
Self::WriteText | Self::AppendText => &["path", "text"],
Self::NowUnix | Self::NowMs | Self::Args | Self::Cwd => &[],
Self::FormatTime => &["unix", "pattern"],
Self::ParseTime => &["text", "pattern"],
Self::SleepMs => &["ms"],
Self::EnvGet => &["name"],
Self::EnvSet => &["name", "value"],
Self::Exit => &["code"],
Self::Exec => &["cmd"],
Self::JsonParse | Self::UrlEncode | Self::JsonQuote => &["text"],
Self::JsonStringify | Self::JsonKeys => &["value"],
Self::JsonGet => &["value", "key"],
Self::HttpGet => &["url"],
Self::HttpPost => &["url", "body"],
Self::HttpPostSse => &["url", "body"],
Self::OpenAiSseParse => &["text"],
Self::HttpRequest => &["method", "url"],
Self::DotenvLoad => &[],
Self::Pi | Self::EConst | Self::Random => &[],
Self::Num | Self::MathNeg | Self::MathSin | Self::MathCos | Self::MathTan
| Self::MathAsin | Self::MathAcos | Self::MathAtan | Self::MathSqrt
| Self::MathAbs | Self::MathLn | Self::MathExp | Self::MathFloor | Self::MathCeil
| Self::Seed | Self::Formula | Self::Simplify | Self::Expand => &["value"],
Self::MathAdd | Self::MathSub | Self::MathMul | Self::MathDiv | Self::MathPow
| Self::MathMin | Self::MathMax => &["a", "b"],
Self::RandomInt => &["min", "max"],
Self::Diff => &["formula", "var"],
Self::Subs | Self::Eval => &["formula", "var", "value"],
Self::Solve => &["formula", "var"],
Self::Plot => &["formula", "var", "min", "max"],
Self::PlotPoints => &["xs", "ys"],
Self::PlotConic => &["kind", "a"],
Self::ForeignSetCmd => &["lang", "cmd"],
Self::ForeignRun => &["code"],
Self::ForeignRunLang => &["lang", "source"],
Self::ForeignLangs => &[],
Self::PluginLoad => &["path"],
Self::PluginUnload | Self::PluginList => &[],
Self::ExtNativePath => &["name"],
Self::OuterCallLine => &[],
Self::TextPatch => &["path", "find", "replace"],
Self::ApplyPatchBlocks => &["path", "text"],
Self::WritebackScanPath => &["path"],
Self::MapSet => &["map", "key", "value"],
Self::ListAppend => &["list", "item"],
Self::WritebackRecord => &["value"],
Self::WritebackGet | Self::WritebackClear => &[],
Self::WritebackList => &[],
Self::WritebackEnsure => &["key"],
Self::SubtaskSpawn => &[],
Self::SubtaskPoll | Self::SubtaskJoin | Self::SubtaskKill => &["id"],
Self::SubtaskWaitAll => &[],
}
}
pub fn optional_params(self) -> &'static [&'static str] {
match self {
Self::JsonStringify => &["indent"],
Self::HttpGet => &["headers"],
Self::HttpPost => &["content_type", "headers"],
Self::HttpPostSse => &["content_type", "headers", "echo"],
Self::OpenAiSseParse => &["echo"],
Self::HttpRequest => &["body", "content_type", "headers"],
Self::DotenvLoad => &["path"],
Self::Exec => &["args"],
Self::Solve => &["min", "max"],
Self::Plot => &["steps", "path", "derivative", "grid"],
Self::PlotPoints => &["path", "grid"],
Self::PlotConic => &["b", "h", "k", "path", "grid"],
Self::ForeignSetCmd => &["args"],
Self::ForeignRun | Self::ForeignRunLang => &["stdin"],
Self::WritebackRecord | Self::WritebackGet | Self::WritebackClear => {
&["at_end", "key", "line"]
}
Self::WritebackEnsure => &["placeholder", "line"],
Self::SubtaskSpawn => &["path", "fn", "args", "code", "lang", "source", "stdin", "quiet"],
_ => &[],
}
}
pub fn all_params(self) -> Vec<&'static str> {
let mut v = self.required_params().to_vec();
v.extend_from_slice(self.optional_params());
v
}
}
pub fn call_host(
ctx: &mut HostContext,
fn_id: HostFn,
bound: &HashMap<String, Value>,
) -> Result<Value, String> {
match fn_id {
HostFn::ReadText => fs::read_text(ctx, require(bound, "path")?),
HostFn::WriteText => {
fs::write_text(ctx, require(bound, "path")?, require(bound, "text")?)
}
HostFn::AppendText => {
fs::append_text(ctx, require(bound, "path")?, require(bound, "text")?)
}
HostFn::Exists => fs::exists(ctx, require(bound, "path")?),
HostFn::ListDir => fs::list_dir(ctx, require(bound, "path")?),
HostFn::MakeDir => fs::make_dir(ctx, require(bound, "path")?),
HostFn::Remove => fs::remove(ctx, require(bound, "path")?),
HostFn::NowUnix => time::now_unix(),
HostFn::NowMs => time::now_ms(),
HostFn::FormatTime => {
time::format_time(require(bound, "unix")?, require(bound, "pattern")?)
}
HostFn::SleepMs => time::sleep_ms(ctx, require(bound, "ms")?),
HostFn::ParseTime => {
time::parse_time(require(bound, "text")?, require(bound, "pattern")?)
}
HostFn::EnvGet => sys::env_get(require(bound, "name")?),
HostFn::EnvSet => sys::env_set(require(bound, "name")?, require(bound, "value")?),
HostFn::DotenvLoad => sys::dotenv_load(ctx, bound.get("path")),
HostFn::Args => sys::args(ctx),
HostFn::Cwd => sys::cwd(ctx),
HostFn::Exit => sys::exit(ctx, require(bound, "code")?),
HostFn::Exec => sys::exec(ctx, require(bound, "cmd")?, bound.get("args")),
HostFn::JsonParse => json::parse(require(bound, "text")?),
HostFn::JsonStringify => {
json::stringify(require(bound, "value")?, bound.get("indent"))
}
HostFn::JsonGet => json::get(require(bound, "value")?, require(bound, "key")?),
HostFn::JsonKeys => json::keys(require(bound, "value")?),
HostFn::JsonQuote => json::quote(require(bound, "text")?),
HostFn::HttpGet => net::http_get(ctx, require(bound, "url")?, bound.get("headers")),
HostFn::HttpPost => net::http_post(
ctx,
require(bound, "url")?,
require(bound, "body")?,
bound.get("content_type"),
bound.get("headers"),
),
HostFn::HttpPostSse => net::http_post_sse(
ctx,
require(bound, "url")?,
require(bound, "body")?,
bound.get("content_type"),
bound.get("headers"),
bound.get("echo"),
),
HostFn::OpenAiSseParse => {
net::openai_sse_parse(require(bound, "text")?, bound.get("echo"))
}
HostFn::HttpRequest => net::http_request(
ctx,
require(bound, "method")?,
require(bound, "url")?,
bound.get("body"),
bound.get("content_type"),
bound.get("headers"),
),
HostFn::UrlEncode => net::url_encode(require(bound, "text")?),
HostFn::Pi => math::pi(),
HostFn::EConst => math::e_const(),
HostFn::Num => math::to_num(require(bound, "value")?),
HostFn::MathAdd => math::binary_num("add", require(bound, "a")?, require(bound, "b")?),
HostFn::MathSub => math::binary_num("sub", require(bound, "a")?, require(bound, "b")?),
HostFn::MathMul => math::binary_num("mul", require(bound, "a")?, require(bound, "b")?),
HostFn::MathDiv => math::binary_num("div", require(bound, "a")?, require(bound, "b")?),
HostFn::MathPow => math::binary_num("pow", require(bound, "a")?, require(bound, "b")?),
HostFn::MathMin => math::binary_num("min", require(bound, "a")?, require(bound, "b")?),
HostFn::MathMax => math::binary_num("max", require(bound, "a")?, require(bound, "b")?),
HostFn::MathNeg => math::unary_num("neg", require(bound, "value")?),
HostFn::MathSin => math::unary_num("sin", require(bound, "value")?),
HostFn::MathCos => math::unary_num("cos", require(bound, "value")?),
HostFn::MathTan => math::unary_num("tan", require(bound, "value")?),
HostFn::MathAsin => math::unary_num("asin", require(bound, "value")?),
HostFn::MathAcos => math::unary_num("acos", require(bound, "value")?),
HostFn::MathAtan => math::unary_num("atan", require(bound, "value")?),
HostFn::MathSqrt => math::unary_num("sqrt", require(bound, "value")?),
HostFn::MathAbs => math::unary_num("abs", require(bound, "value")?),
HostFn::MathLn => math::unary_num("ln", require(bound, "value")?),
HostFn::MathExp => math::unary_num("exp", require(bound, "value")?),
HostFn::MathFloor => math::unary_num("floor", require(bound, "value")?),
HostFn::MathCeil => math::unary_num("ceil", require(bound, "value")?),
HostFn::Random => math::random(ctx),
HostFn::RandomInt => {
math::random_int(ctx, require(bound, "min")?, require(bound, "max")?)
}
HostFn::Seed => math::seed(ctx, require(bound, "value")?),
HostFn::Formula => math::formula_parse(require(bound, "value")?),
HostFn::Simplify => math::simplify(require(bound, "value")?),
HostFn::Expand => math::expand(require(bound, "value")?),
HostFn::Diff => math::diff(require(bound, "formula")?, require(bound, "var")?),
HostFn::Subs => math::subs(
require(bound, "formula")?,
require(bound, "var")?,
require(bound, "value")?,
),
HostFn::Eval => math::eval_at(
require(bound, "formula")?,
require(bound, "var")?,
require(bound, "value")?,
),
HostFn::Solve => math::solve(
require(bound, "formula")?,
require(bound, "var")?,
bound.get("min"),
bound.get("max"),
),
HostFn::Plot => math::plot(
ctx,
require(bound, "formula")?,
require(bound, "var")?,
require(bound, "min")?,
require(bound, "max")?,
bound.get("steps"),
bound.get("path"),
bound.get("derivative"),
bound.get("grid"),
),
HostFn::PlotPoints => math::plot_points(
ctx,
require(bound, "xs")?,
require(bound, "ys")?,
bound.get("path"),
bound.get("grid"),
),
HostFn::PlotConic => math::plot_conic(
ctx,
require(bound, "kind")?,
require(bound, "a")?,
bound.get("b"),
bound.get("h"),
bound.get("k"),
bound.get("path"),
bound.get("grid"),
),
HostFn::ForeignSetCmd => foreign::set_cmd(
ctx,
require(bound, "lang")?,
require(bound, "cmd")?,
bound.get("args"),
),
HostFn::ForeignRun => foreign::run(ctx, require(bound, "code")?, bound.get("stdin")),
HostFn::ForeignRunLang => foreign::run_lang(
ctx,
require(bound, "lang")?,
require(bound, "source")?,
bound.get("stdin"),
),
HostFn::ForeignLangs => foreign::langs(ctx),
HostFn::PluginLoad => plugin::load(ctx, require(bound, "path")?),
HostFn::PluginUnload => plugin::unload(ctx),
HostFn::PluginList => plugin::list(ctx),
HostFn::ExtNativePath => {
let name = require(bound, "name")?;
let s = match name {
Value::Text(t) => t.as_str(),
_ => return Err("name must be text".into()),
};
Ok(match crate::ext_cli::installed_native_path(s) {
Some(p) => Value::Text(p.to_string_lossy().into_owned()),
None => Value::None,
})
}
HostFn::MapSet => {
agent_rt::map_set(require(bound, "map")?, require(bound, "key")?, require(bound, "value")?)
}
HostFn::ListAppend => {
agent_rt::list_append(require(bound, "list")?, require(bound, "item")?)
}
HostFn::OuterCallLine => Ok(Value::Int(
ctx.call_site_lines
.first()
.copied()
.unwrap_or(ctx.current_line) as i64,
)),
HostFn::TextPatch => fs::text_patch(
ctx,
require(bound, "path")?,
require(bound, "find")?,
require(bound, "replace")?,
),
HostFn::ApplyPatchBlocks => {
fs::apply_patch_blocks(ctx, require(bound, "path")?, require(bound, "text")?)
}
HostFn::WritebackScanPath => writeback::scan_path(ctx, require(bound, "path")?),
HostFn::WritebackRecord => writeback::record(
ctx,
require(bound, "value")?,
bound.get("at_end"),
bound.get("key"),
bound.get("line"),
),
HostFn::WritebackGet => writeback::get(
ctx,
bound.get("at_end"),
bound.get("key"),
bound.get("line"),
),
HostFn::WritebackClear => writeback::clear(
ctx,
bound.get("at_end"),
bound.get("key"),
bound.get("line"),
),
HostFn::WritebackList => writeback::list(ctx),
HostFn::WritebackEnsure => {
let placeholder = bound
.get("placeholder")
.cloned()
.unwrap_or_else(|| Value::Text("pending".into()));
writeback::ensure_slot(
ctx,
require(bound, "key")?,
&placeholder,
bound.get("line"),
)
}
HostFn::SubtaskSpawn => subtask::spawn(ctx, bound),
HostFn::SubtaskPoll => subtask::poll(ctx, require(bound, "id")?),
HostFn::SubtaskJoin => subtask::join(ctx, require(bound, "id")?),
HostFn::SubtaskKill => subtask::kill(ctx, require(bound, "id")?),
HostFn::SubtaskWaitAll => subtask::wait_all(ctx),
}
}
fn require<'a>(bound: &'a HashMap<String, Value>, key: &str) -> Result<&'a Value, String> {
bound
.get(key)
.ok_or_else(|| format!("host call missing `{key}`"))
}