use std::sync::Arc;
use sim_kernel::{CapabilityName, Cx, Result, ShapeRef, Symbol, Value};
use sim_lib_server::ServerAddress;
use crate::tools::{Tool, register_tool};
use crate::util::installed_codecs;
pub struct ToolSpec {
pub symbol: Symbol,
pub description: String,
pub args_shape: ShapeRef,
pub result_shape: Option<ShapeRef>,
pub category: Symbol,
pub capabilities: Vec<CapabilityName>,
pub function: Value,
}
impl Tool {
pub fn local(cx: &mut Cx, spec: ToolSpec) -> Self {
Self {
symbol: spec.symbol,
description: spec.description,
args_shape: spec.args_shape,
result_shape: spec.result_shape,
category: spec.category,
capabilities: spec.capabilities,
function: spec.function,
address: ServerAddress::Local,
codecs: installed_codecs(cx),
}
}
}
pub fn install_tool(cx: &mut Cx, tool: Arc<Tool>) -> Result<Value> {
if let Some(existing) = cx.registry().value_by_symbol(&tool.symbol).cloned()
&& existing.object().downcast_ref::<Tool>().is_some()
{
return Ok(existing);
}
let value = cx.factory().opaque(tool.clone())?;
register_tool(cx, tool, value.clone())?;
Ok(value)
}