Skip to main content

synaptic_tools/
parallel_executor.rs

1use serde_json::Value;
2use synaptic_core::SynapseError;
3
4use crate::ToolRegistry;
5
6/// Executes multiple tool calls concurrently using `futures::future::join_all`.
7pub struct ParallelToolExecutor {
8    registry: ToolRegistry,
9}
10
11impl ParallelToolExecutor {
12    /// Create a new parallel tool executor backed by the given registry.
13    pub fn new(registry: ToolRegistry) -> Self {
14        Self { registry }
15    }
16
17    /// Execute all tool calls concurrently and return results in the same order.
18    ///
19    /// Each element in `calls` is a `(tool_name, args)` pair.
20    /// Results are returned in the same order as the input.
21    pub async fn execute_all(
22        &self,
23        calls: Vec<(String, Value)>,
24    ) -> Vec<Result<Value, SynapseError>> {
25        let futures: Vec<_> = calls
26            .into_iter()
27            .map(|(name, args)| {
28                let registry = self.registry.clone();
29                async move {
30                    let tool = registry
31                        .get(&name)
32                        .ok_or(SynapseError::ToolNotFound(name))?;
33                    tool.call(args).await
34                }
35            })
36            .collect();
37
38        futures::future::join_all(futures).await
39    }
40}