use agent_sdk_foundation::events::AgentEvent;
use agent_sdk_foundation::llm;
use agent_sdk_foundation::types::{ToolInvocation, ToolResult, ToolTier};
use async_trait::async_trait;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ToolDecision {
Allow,
Block(String),
RequiresConfirmation(String),
}
#[async_trait]
pub trait AgentHooks: Send + Sync {
async fn pre_tool_use(&self, invocation: &ToolInvocation) -> ToolDecision {
match invocation.tier {
ToolTier::Observe => ToolDecision::Allow,
ToolTier::Confirm => {
ToolDecision::RequiresConfirmation(format!("Confirm {}?", invocation.tool_name))
}
}
}
async fn post_tool_use(&self, _tool_name: &str, _result: &ToolResult) {
}
async fn on_event(&self, _event: &AgentEvent) {
}
async fn on_error(&self, _error: &anyhow::Error) -> bool {
false
}
async fn on_context_compact(&self, _messages: &[llm::Message]) -> Option<String> {
None
}
}
#[derive(Clone, Copy, Default)]
pub struct DefaultHooks;
#[async_trait]
impl AgentHooks for DefaultHooks {}
#[derive(Clone, Copy, Default)]
pub struct AllowAllHooks;
#[async_trait]
impl AgentHooks for AllowAllHooks {
async fn pre_tool_use(&self, _invocation: &ToolInvocation) -> ToolDecision {
ToolDecision::Allow
}
}
#[derive(Clone, Copy, Default)]
pub struct LoggingHooks;
#[async_trait]
impl AgentHooks for LoggingHooks {
async fn pre_tool_use(&self, invocation: &ToolInvocation) -> ToolDecision {
log::debug!(
"Pre-tool use tool={} input={:?} tier={:?}",
invocation.tool_name,
invocation.requested_input,
invocation.tier,
);
DefaultHooks.pre_tool_use(invocation).await
}
async fn post_tool_use(&self, tool_name: &str, result: &ToolResult) {
log::debug!(
"Post-tool use tool={tool_name} success={} duration_ms={:?}",
result.success,
result.duration_ms
);
}
async fn on_event(&self, event: &AgentEvent) {
log::debug!("Agent event {event:?}");
}
async fn on_error(&self, error: &anyhow::Error) -> bool {
log::error!("Agent error {error:?}");
false
}
}