adk_tool/
toolset.rs

1use adk_core::{ReadonlyContext, Result, Tool, ToolPredicate, Toolset};
2use async_trait::async_trait;
3use std::sync::Arc;
4
5pub struct BasicToolset {
6    name: String,
7    tools: Vec<Arc<dyn Tool>>,
8    predicate: Option<ToolPredicate>,
9}
10
11impl BasicToolset {
12    pub fn new(name: impl Into<String>, tools: Vec<Arc<dyn Tool>>) -> Self {
13        Self { name: name.into(), tools, predicate: None }
14    }
15
16    pub fn with_predicate(mut self, predicate: ToolPredicate) -> Self {
17        self.predicate = Some(predicate);
18        self
19    }
20}
21
22#[async_trait]
23impl Toolset for BasicToolset {
24    fn name(&self) -> &str {
25        &self.name
26    }
27
28    async fn tools(&self, _ctx: Arc<dyn ReadonlyContext>) -> Result<Vec<Arc<dyn Tool>>> {
29        if let Some(predicate) = &self.predicate {
30            Ok(self.tools.iter().filter(|tool| predicate(tool.as_ref())).cloned().collect())
31        } else {
32            Ok(self.tools.clone())
33        }
34    }
35}
36
37/// Creates a predicate that allows only tools with names in the provided list
38pub fn string_predicate(allowed_tools: Vec<String>) -> ToolPredicate {
39    let allowed_set: std::collections::HashSet<String> = allowed_tools.into_iter().collect();
40    Box::new(move |tool: &dyn Tool| allowed_set.contains(tool.name()))
41}