From 2181de7a5ff4cfbd6f54c9b8bf282b178b3ba80f Mon Sep 17 00:00:00 2001 From: Windpicker-owo <3431391539@qq.com> Date: Tue, 19 Aug 2025 11:33:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/tool_history.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/common/tool_history.py b/src/common/tool_history.py index f7667328..b558a8d8 100644 --- a/src/common/tool_history.py +++ b/src/common/tool_history.py @@ -105,6 +105,34 @@ class ToolHistoryManager: except Exception as e: logger.error(f"记录工具调用时发生错误: {e}") + def find_cached_result(self, tool_name: str, args: Dict[str, Any]) -> Optional[Dict[str, Any]]: + """查找匹配的缓存记录 + + Args: + tool_name: 工具名称 + args: 工具调用参数 + + Returns: + Optional[Dict[str, Any]]: 如果找到匹配的缓存记录则返回结果,否则返回None + """ + # 检查是否启用历史记录 + if not global_config.tool.history.enable_history: + return None + + # 清理输入参数中的敏感信息以便比较 + sanitized_input_args = self._sanitize_args(args) + + # 按时间倒序遍历历史记录 + for record in reversed(self._history): + if (record["tool_name"] == tool_name and + record["status"] == "completed" and + record["ttl_count"] < record.get("ttl", 5)): + # 比较参数是否匹配 + if self._sanitize_args(record["arguments"]) == sanitized_input_args: + logger.info(f"工具 {tool_name} 命中缓存记录") + return record["result"] + return None + def _sanitize_args(self, args: Dict[str, Any]) -> Dict[str, Any]: """清理参数中的敏感信息""" sensitive_keys = ['api_key', 'token', 'password', 'secret'] @@ -297,6 +325,12 @@ def wrap_tool_executor(): async def wrapped_execute_tool_call(self, tool_call, tool_instance=None): start_time = time.time() + + # 首先检查缓存 + if cached_result := history_manager.find_cached_result(tool_call.func_name, tool_call.args): + logger.info(f"{getattr(self, 'log_prefix', '')}使用缓存结果,跳过工具 {tool_call.func_name} 执行") + return cached_result + try: result = await original_execute(self, tool_call, tool_instance) execution_time = time.time() - start_time