use crate::hooks::HookRegistry;
use crate::protocol::{HookEventKind, HookOutput};
#[derive(Debug, Clone)]
pub struct ToolAllowList {
pub allowed: Vec<String>,
}
impl ToolAllowList {
pub fn new(tools: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self { allowed: tools.into_iter().map(Into::into).collect() }
}
#[must_use]
pub fn allows(&self, tool: &str) -> bool {
self.allowed.iter().any(|t| t == tool)
}
pub fn install(self, hooks: &mut HookRegistry) {
hooks.on(HookEventKind::PreToolUse, None, move |payload| {
let verdict = match payload.tool_name.as_deref() {
Some(tool) if !self.allows(tool) => Some(format!(
"Tool {tool} is not allowed here. Allowed tools: {}",
self.allowed.join(", ")
)),
_ => None,
};
async move { verdict.map_or_else(HookOutput::allow, HookOutput::block) }
});
}
}