causa_runtime/executor/
catalog.rs1use super::{ToolCatalogError, registry::ToolExecutor};
4use crate::composition::ToolBridge;
5use causa_kernel::{CallControl, InvocationId, ProcessorData, Tool, ToolDescription, 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) tools: Vec<ToolDescription>,
18 pub(super) targets: HashMap<String, Arc<dyn Tool>>,
19 pub(super) invocation_id: InvocationId,
20 pub(super) control: CallControl,
21 pub(super) data: Option<ProcessorData>,
22}
23
24impl ToolExecutor {
25 pub async fn bind(
32 &self,
33 invocation_id: InvocationId,
34 control: CallControl,
35 ) -> Result<BoundTools<'_>, ToolCatalogError> {
36 control.check().map_err(ToolCatalogError::Control)?;
37 let registrations = self.dynamic.read().expect("dynamic registry lock").clone();
38 let mut tools = Vec::new();
39 let mut targets = HashMap::new();
40 for entry in &self.tools {
41 control.check().map_err(ToolCatalogError::Control)?;
42 let definition = entry.tool.definition();
43 if definition.name != entry.name {
44 return Err(ToolCatalogError::StaticNameChanged {
45 registered: entry.name.clone(),
46 actual: definition.name,
47 });
48 }
49 targets.insert(entry.name.clone(), entry.tool.clone());
50 tools.push(ToolDescription::new(definition).with_hints(entry.tool.hints()));
51 }
52 for entry in registrations {
53 control.check().map_err(ToolCatalogError::Control)?;
54 for description in entry.describe(&control).await? {
55 let name = description.definition.name.clone();
56 if targets.contains_key(&name) {
57 return Err(ToolCatalogError::DuplicateTool { name });
58 }
59 let bridge = ToolBridge::new(entry.source.clone(), description.definition.clone())
60 .with_hints(description.hints.clone());
61 targets.insert(name, Arc::new(bridge) as Arc<dyn Tool>);
62 tools.push(description);
63 }
64 }
65 control.check().map_err(ToolCatalogError::Control)?;
66 let surface = ToolSurface::from_definitions(
67 tools.iter().map(|tool| tool.definition.clone()).collect(),
68 );
69 Ok(BoundTools {
70 executor: self,
71 surface,
72 tools,
73 targets,
74 invocation_id,
75 control,
76 data: None,
77 })
78 }
79}
80
81impl BoundTools<'_> {
82 pub fn surface(&self) -> &ToolSurface {
84 &self.surface
85 }
86
87 pub fn with_processor_data(mut self, data: ProcessorData) -> Self {
92 self.data = Some(data);
93 self
94 }
95}