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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! tool.* — Lua-side tool registry for LLM function calling.
use crate::bridge::obs;
use mlua::prelude::*;
/// Register tool.* Lua API.
///
/// Tool registry is maintained in Lua globals (_TOOL_REGISTRY table).
/// Rust provides thin helpers: register/call/list/schema.
pub fn register(lua: &Lua) -> LuaResult<()> {
let script_name: String = lua
.globals()
.get::<Option<String>>("_SCRIPT_NAME")?
.unwrap_or_else(|| "unknown".to_string());
// Initialize registry in Lua globals
let registry = lua.create_table()?;
lua.globals().set("_TOOL_REGISTRY", registry)?;
let tool_tbl = lua.create_table()?;
// tool.register(name, schema, handler_fn)
let script_name_register = script_name.clone();
tool_tbl.set(
"register",
lua.create_function(
move |lua, (name, schema, handler): (String, LuaValue, LuaFunction)| {
let registry: LuaTable = lua.globals().get("_TOOL_REGISTRY")?;
let entry = lua.create_table()?;
entry.set("name", name.clone())?;
entry.set("schema", schema)?;
entry.set("handler", handler)?;
registry.set(name.clone(), entry)?;
tracing::info!(
target: "lua",
script = %script_name_register,
"{}",
obs::obs_line(
"tool",
"tool_register",
&obs::obs_context(None),
&[("tool", name.as_str())],
),
);
Ok(())
},
)?,
)?;
// tool.call(name, input) -> result or error
//
// Async so that handlers may invoke async stdlib functions (e.g.
// `std.sql.query`, `http.request`, `mcp.call`) via coroutine yield.
// Purely synchronous handlers still work unchanged.
let script_name_call = script_name.clone();
tool_tbl.set(
"call",
lua.create_async_function(move |lua, (name, input): (String, LuaValue)| {
let script_name = script_name_call.clone();
async move {
tracing::info!(
target: "lua",
script = %script_name,
"{}",
obs::obs_line(
"tool",
"tool_call",
&obs::obs_context(None),
&[("tool", name.as_str())],
),
);
let registry: LuaTable = lua.globals().get("_TOOL_REGISTRY")?;
let entry: Option<LuaTable> = registry.get(name.clone())?;
match entry {
None => {
tracing::warn!(
target: "lua",
script = %script_name,
"{}",
obs::obs_line(
"tool",
"tool_result",
&obs::obs_context(None),
&[("tool", name.as_str()), ("ok", "false")],
),
);
Err(LuaError::external(format!("tool not found: {name}")))
}
Some(e) => {
let handler: LuaFunction = e.get("handler")?;
match handler.call_async::<LuaValue>(input).await {
Ok(v) => {
tracing::info!(
target: "lua",
script = %script_name,
"{}",
obs::obs_line(
"tool",
"tool_result",
&obs::obs_context(None),
&[("tool", name.as_str()), ("ok", "true")],
),
);
Ok(v)
}
Err(e) => {
tracing::warn!(
target: "lua",
script = %script_name,
"{}",
obs::obs_line(
"tool",
"tool_result",
&obs::obs_context(None),
&[("tool", name.as_str()), ("ok", "false")],
),
);
Err(e)
}
}
}
}
}
})?,
)?;
// tool.list() -> array of names
tool_tbl.set(
"list",
lua.create_function(|lua, ()| {
let registry: LuaTable = lua.globals().get("_TOOL_REGISTRY")?;
let names = lua.create_table()?;
for (idx, pair) in (1..).zip(registry.pairs::<String, LuaTable>()) {
let (name, _) = pair?;
names.set(idx, name)?;
}
Ok(names)
})?,
)?;
// tool.schema() -> JSON array of Anthropic tool definitions
// Each entry: { name = "...", description = "...", input_schema = {...} }
tool_tbl.set(
"schema",
lua.create_function(|lua, ()| {
let registry: LuaTable = lua.globals().get("_TOOL_REGISTRY")?;
let arr = lua.create_table()?;
for (idx, pair) in (1..).zip(registry.pairs::<String, LuaTable>()) {
let (_, entry) = pair?;
let name: String = entry.get("name")?;
let schema: LuaTable = entry.get("schema")?;
let description: String = schema.get("description")?;
let input_schema: LuaValue = schema.get("input_schema")?;
let tool_def = lua.create_table()?;
tool_def.set("name", name)?;
tool_def.set("description", description)?;
tool_def.set("input_schema", input_schema)?;
arr.set(idx, tool_def)?;
}
Ok(arr)
})?,
)?;
lua.globals().set("tool", tool_tbl)?;
Ok(())
}