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 claim_source: None,
97 }))
98 }
99 }
100
101 #[test]
102 fn suppressed_tools_hidden_from_definitions() {
103 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
104 let defs = filter.tool_definitions();
105 let ids: Vec<&str> = defs.iter().map(|d| d.id.as_ref()).collect();
106 assert!(!ids.contains(&"read"));
107 assert!(!ids.contains(&"glob"));
108 assert!(ids.contains(&"edit"));
109 }
110
111 #[tokio::test]
112 async fn suppressed_tool_call_returns_none() {
113 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
114 let call = ToolCall {
115 tool_id: "read".to_owned(),
116 params: serde_json::Map::new(),
117 caller_id: None,
118 };
119 let result = filter.execute_tool_call(&call).await.unwrap();
120 assert!(result.is_none());
121 }
122
123 #[tokio::test]
124 async fn allowed_tool_call_passes_through() {
125 let filter = ToolFilter::new(StubExecutor, &["read", "glob"]);
126 let call = ToolCall {
127 tool_id: "edit".to_owned(),
128 params: serde_json::Map::new(),
129 caller_id: None,
130 };
131 let result = filter.execute_tool_call(&call).await.unwrap();
132 assert!(result.is_some());
133 }
134}