use std::sync::Arc;
use anyhow::Result;
use tracing::info;
use super::{WatchdogFramework, WatchdogRule, TriggerCondition, ToolExecutionEvent};
pub struct WatchdogClient {
framework: Arc<WatchdogFramework>,
agent_id: String,
}
impl WatchdogClient {
pub fn new(framework: Arc<WatchdogFramework>, agent_id: impl Into<String>) -> Self {
Self {
framework,
agent_id: agent_id.into(),
}
}
pub async fn register_rule(
&self,
rule_id: impl Into<String>,
tool_id: impl Into<String>,
condition: TriggerCondition,
) -> Result<()> {
let rule = WatchdogRule::new(
rule_id,
tool_id,
condition,
self.agent_id.clone(),
);
self.framework.register_rule(rule)?;
info!("Agent {} registered watchdog rule", self.agent_id);
Ok(())
}
pub async fn unregister_rule(&self, rule_id: &str) -> bool {
self.framework.remove_rule(rule_id).is_some()
}
pub async fn set_rule_enabled(&self, rule_id: &str, enabled: bool) -> bool {
self.framework.set_rule_enabled(rule_id, enabled)
}
pub async fn has_rule(&self, rule_id: &str) -> bool {
self.framework.has_rule(rule_id)
}
pub async fn list_rules(&self) -> Vec<WatchdogRule> {
self.framework.list_rules()
}
pub fn framework(&self) -> &Arc<WatchdogFramework> {
&self.framework
}
pub async fn handle_event(&self, event: &ToolExecutionEvent) -> Result<Vec<String>> {
self.framework.process_event(event).await
}
}