use crate::error::HookError;
use crate::hook::{Hook, HookAction, HookContext, HookPoint};
use async_trait::async_trait;
use std::sync::Mutex;
#[derive(Debug, Clone)]
pub struct RecordedEvent {
pub point: HookPoint,
pub tokens_used: u64,
pub turns_completed: u32,
}
pub struct LoggingHook {
points: Vec<HookPoint>,
events: Mutex<Vec<RecordedEvent>>,
}
impl LoggingHook {
pub fn new() -> Self {
Self {
points: vec![
HookPoint::PreInference,
HookPoint::PostInference,
HookPoint::PreToolUse,
HookPoint::PostToolUse,
HookPoint::ExitCheck,
],
events: Mutex::new(Vec::new()),
}
}
pub fn events(&self) -> Vec<RecordedEvent> {
self.events.lock().unwrap().clone()
}
}
impl Default for LoggingHook {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Hook for LoggingHook {
fn points(&self) -> &[HookPoint] {
&self.points
}
async fn on_event(&self, ctx: &HookContext) -> Result<HookAction, HookError> {
self.events.lock().unwrap().push(RecordedEvent {
point: ctx.point,
tokens_used: ctx.tokens_used,
turns_completed: ctx.turns_completed,
});
Ok(HookAction::Continue)
}
}