use super::{ToolCatalogError, registry::ToolExecutor};
use crate::composition::ToolBridge;
use causa_kernel::{CallControl, InvocationId, Tool, ToolSurface};
use std::collections::HashMap;
use std::sync::Arc;
pub struct BoundTools<'a> {
pub(super) executor: &'a ToolExecutor,
pub(super) surface: ToolSurface,
pub(super) targets: HashMap<String, Arc<dyn Tool>>,
pub(super) invocation_id: InvocationId,
pub(super) control: CallControl,
}
impl ToolExecutor {
pub async fn bind(
&self,
invocation_id: InvocationId,
control: CallControl,
) -> Result<BoundTools<'_>, ToolCatalogError> {
control.check().map_err(ToolCatalogError::Control)?;
let registrations = self.dynamic.read().expect("dynamic registry lock").clone();
let mut definitions = Vec::new();
let mut targets = HashMap::new();
for entry in &self.tools {
control.check().map_err(ToolCatalogError::Control)?;
let definition = entry.tool.definition();
if definition.name != entry.name {
return Err(ToolCatalogError::StaticNameChanged {
registered: entry.name.clone(),
actual: definition.name,
});
}
targets.insert(entry.name.clone(), entry.tool.clone());
definitions.push(definition);
}
for entry in registrations {
control.check().map_err(ToolCatalogError::Control)?;
for definition in entry.list(&control).await? {
let name = definition.name.clone();
if targets.contains_key(&name) {
return Err(ToolCatalogError::DuplicateTool { name });
}
targets.insert(
name,
Arc::new(ToolBridge::new(entry.source.clone(), definition.clone()))
as Arc<dyn Tool>,
);
definitions.push(definition);
}
}
control.check().map_err(ToolCatalogError::Control)?;
Ok(BoundTools {
executor: self,
surface: ToolSurface::from_definitions(definitions),
targets,
invocation_id,
control,
})
}
}
impl BoundTools<'_> {
pub fn surface(&self) -> &ToolSurface {
&self.surface
}
}