sim_lib_agent/
tool_projection.rs1use std::sync::Arc;
2
3use sim_kernel::{CapabilityName, Cx, Result, ShapeRef, Symbol, Value};
4use sim_lib_server::ServerAddress;
5
6use crate::tools::{Tool, register_tool};
7use crate::util::installed_codecs;
8
9pub struct ToolSpec {
11 pub symbol: Symbol,
13 pub description: String,
15 pub args_shape: ShapeRef,
17 pub result_shape: Option<ShapeRef>,
19 pub category: Symbol,
21 pub capabilities: Vec<CapabilityName>,
23 pub function: Value,
25}
26
27impl Tool {
28 pub fn local(cx: &mut Cx, spec: ToolSpec) -> Self {
30 Self {
31 symbol: spec.symbol,
32 description: spec.description,
33 args_shape: spec.args_shape,
34 result_shape: spec.result_shape,
35 category: spec.category,
36 capabilities: spec.capabilities,
37 function: spec.function,
38 address: ServerAddress::Local,
39 codecs: installed_codecs(cx),
40 }
41 }
42}
43
44pub fn install_tool(cx: &mut Cx, tool: Arc<Tool>) -> Result<Value> {
46 if let Some(existing) = cx.registry().value_by_symbol(&tool.symbol).cloned()
47 && existing.object().downcast_ref::<Tool>().is_some()
48 {
49 return Ok(existing);
50 }
51 let value = cx.factory().opaque(tool.clone())?;
52 register_tool(cx, tool, value.clone())?;
53 Ok(value)
54}