use std::collections::HashMap;
#[derive(Default)]
pub struct ToolErrorTracker {
tool_errors: HashMap<String, HashMap<String, usize>>,
max_consecutive_errors: usize,
}
impl ToolErrorTracker {
pub fn new(max_errors: usize) -> Self {
Self {
tool_errors: HashMap::new(),
max_consecutive_errors: max_errors,
}
}
pub fn record_error(&mut self, tool_name: &str) -> bool {
let server_map = self.tool_errors.entry(tool_name.to_string()).or_default();
let curr_server = "current_server".to_string();
let count = server_map.entry(curr_server).or_insert(0);
*count += 1;
*count >= self.max_consecutive_errors
}
pub fn record_success(&mut self, tool_name: &str) {
if let Some(server_map) = self.tool_errors.get_mut(tool_name) {
server_map.clear(); }
}
pub fn get_error_count(&self, tool_name: &str) -> usize {
if let Some(server_map) = self.tool_errors.get(tool_name) {
if let Some(count) = server_map.get("current_server") {
return *count;
}
}
0
}
pub fn max_consecutive_errors(&self) -> usize {
self.max_consecutive_errors
}
}