diff --git a/src/common/tool_history.py b/src/common/tool_history.py index dd873c9e..e6be7547 100644 --- a/src/common/tool_history.py +++ b/src/common/tool_history.py @@ -51,8 +51,15 @@ class ToolHistoryManager: def _clean_expired_records(self): """清理已过期的记录""" + original_count = len(self._history) self._history = [record for record in self._history if record.get("ttl_count", 0) < record.get("ttl", 5)] - self._save_history() + cleaned_count = original_count - len(self._history) + + if cleaned_count > 0: + logger.info(f"清理了 {cleaned_count} 条过期的工具历史记录,剩余 {len(self._history)} 条") + self._save_history() + else: + logger.debug("没有需要清理的过期工具历史记录") def record_tool_call(self, tool_name: str, @@ -77,6 +84,9 @@ class ToolHistoryManager: if not global_config.tool.history.enable_history or ttl <= 0: return + # 先清理过期记录 + self._clean_expired_records() + try: # 创建记录 record = { @@ -190,6 +200,8 @@ class ToolHistoryManager: Returns: 符合条件的历史记录列表 """ + # 先清理过期记录 + self._clean_expired_records() def _parse_time(time_str: Optional[Union[datetime, str]]) -> Optional[datetime]: if isinstance(time_str, datetime): return time_str