zeph_tools/
tool_filter.rs1use crate::executor::{ToolCall, ToolError, ToolExecutor, ToolOutput};
5use crate::registry::ToolDef;
6
7#[derive(Debug)]
13pub struct ToolFilter<E: ToolExecutor> {
14 inner: E,
15 suppressed: &'static [&'static str],
16}
17
18impl<E: ToolExecutor> ToolFilter<E> {
19 #[must_use]
20 pub fn new(inner: E, suppressed: &'static [&'static str]) -> Self {
21 Self { inner, suppressed }
22 }
23}
24
25impl<E: ToolExecutor> ToolExecutor for ToolFilter<E> {
26 async fn execute(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
27 self.inner.execute(response).await
28 }
29
30 async fn execute_confirmed(&self, response: &str) -> Result<Option<ToolOutput>, ToolError> {
31 self.inner.execute_confirmed(response).await
32 }
33
34 fn tool_definitions(&self) -> Vec<ToolDef> {
35 self.inner
36 .tool_definitions()
37 .into_iter()
38 .filter(|d| !self.suppressed.contains(&d.id.as_ref()))
39 .collect()
40 }
41
42 async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
43 if self.suppressed.contains(&call.tool_id.as_str()) {
44 return Ok(None);
45 }
46 self.inner.execute_tool_call(call).await
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[derive(Debug)]
55 struct StubExecutor;
56 impl ToolExecutor for StubExecutor {
57 async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
58 Ok(None)
59 }
60 fn tool_definitions(&self) -> Vec<ToolDef> {
61 vec![
62 ToolDef {
63 id: "read".into(),
64 description: "read a file".into(),
65 schema: schemars::schema_for!(String),
66 invocation: crate::registry::InvocationHint::ToolCall,
67 },
68 ToolDef {
69 id: "glob".into(),
70 description: "find files".into(),
71 schema: schemars::schema_for!(String),
72 invocation: crate::registry::InvocationHint::ToolCall,
73 },
74 ToolDef {
75 id: "edit".into(),
76 description: "edit a file".into(),
77 schema: schemars::schema_for!(String),
78 invocation: crate::registry::InvocationHint::ToolCall,
79 },
80 ]
81 }
82 async fn execute_tool_call(
83 &self,
84 call: &ToolCall,
85 ) -> Result<Option<ToolOutput>, ToolError> {
86 Ok(Some(ToolOutput {
87 tool_name: call.tool_id.clone(),
88 summary: "stub".to_owned(),
89 blocks_executed: 1,
90 filter_stats: None,
91 diff: None,
92 streamed: false,
93 terminal_id: None,
94 locations: None,
95 raw_response: None,
96 }))
97 }
98 }
99
100 #[test]
101 fn suppressed_tools_hidden_from_definitions() {
102 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
103 let defs = filter.tool_definitions();
104 let ids: Vec<&str> = defs.iter().map(|d| d.id.as_ref()).collect();
105 assert!(!ids.contains(&"read"));
106 assert!(!ids.contains(&"glob"));
107 assert!(ids.contains(&"edit"));
108 }
109
110 #[tokio::test]
111 async fn suppressed_tool_call_returns_none() {
112 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
113 let call = ToolCall {
114 tool_id: "read".to_owned(),
115 params: serde_json::Map::new(),
116 };
117 let result = filter.execute_tool_call(&call).await.unwrap();
118 assert!(result.is_none());
119 }
120
121 #[tokio::test]
122 async fn allowed_tool_call_passes_through() {
123 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
124 let call = ToolCall {
125 tool_id: "edit".to_owned(),
126 params: serde_json::Map::new(),
127 };
128 let result = filter.execute_tool_call(&call).await.unwrap();
129 assert!(result.is_some());
130 }
131}