// src/agent/runtime/duplicate_detector.rs // // 同质调用检测器:检测连续重复的工具调用,防止死循环。 /// 同质调用检测器 #[derive(Debug, Default)] pub struct DuplicateDetector { last_call: Option<(String, String)>, // (tool_name, arguments) consecutive_count: usize, } impl DuplicateDetector { /// 记录一次调用,返回是否检测到死循环 pub fn record(&mut self, tool_name: &str, arguments: &str, threshold: usize) -> bool { let key = (tool_name.to_string(), arguments.to_string()); if self.last_call.as_ref() == Some(&key) { self.consecutive_count += 1; if self.consecutive_count >= threshold { return true; } } else { self.last_call = Some(key); self.consecutive_count = 1; } false } }