causa_runtime/executor/
catalog.rs1use super::{ToolCatalogError, registry::ToolExecutor};
4use crate::composition::ToolBridge;
5use causa_kernel::{CallControl, InvocationId, Tool, ToolSurface};
6use std::collections::HashMap;
7use std::sync::Arc;
8
9pub struct BoundTools<'a> {
15 pub(super) executor: &'a ToolExecutor,
16 pub(super) surface: ToolSurface,
17 pub(super) targets: HashMap<String, Arc<dyn Tool>>,
18 pub(super) invocation_id: InvocationId,
19 pub(super) control: CallControl,
20}
21
22impl ToolExecutor {
23 pub async fn bind(
27 &self,
28 invocation_id: InvocationId,
29 control: CallControl,
30 ) -> Result<BoundTools<'_>, ToolCatalogError> {
31 control.check().map_err(ToolCatalogError::Control)?;
32 let registrations = self.dynamic.read().expect("dynamic registry lock").clone();
33 let mut definitions = Vec::new();
34 let mut targets = HashMap::new();
35 for entry in &self.tools {
36 control.check().map_err(ToolCatalogError::Control)?;
37 let definition = entry.tool.definition();
38 if definition.name != entry.name {
39 return Err(ToolCatalogError::StaticNameChanged {
40 registered: entry.name.clone(),
41 actual: definition.name,
42 });
43 }
44 targets.insert(entry.name.clone(), entry.tool.clone());
45 definitions.push(definition);
46 }
47 for entry in registrations {
48 control.check().map_err(ToolCatalogError::Control)?;
49 for definition in entry.list(&control).await? {
50 let name = definition.name.clone();
51 if targets.contains_key(&name) {
52 return Err(ToolCatalogError::DuplicateTool { name });
53 }
54 targets.insert(
55 name,
56 Arc::new(ToolBridge::new(entry.source.clone(), definition.clone()))
57 as Arc<dyn Tool>,
58 );
59 definitions.push(definition);
60 }
61 }
62 control.check().map_err(ToolCatalogError::Control)?;
63 Ok(BoundTools {
64 executor: self,
65 surface: ToolSurface::from_definitions(definitions),
66 targets,
67 invocation_id,
68 control,
69 })
70 }
71}
72
73impl BoundTools<'_> {
74 pub fn surface(&self) -> &ToolSurface {
76 &self.surface
77 }
78}