From 477c49587ceb1dd9938b6293f146ebeafc973bfa Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 25 May 2026 08:50:44 +0800 Subject: [PATCH] feat(agent): log tool execution result summary and truncate if too long --- app/agent/tools/base.py | 11 +++++++++-- app/agent/tools/manager.py | 15 ++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/agent/tools/base.py b/app/agent/tools/base.py index 8391d7de..26c73feb 100644 --- a/app/agent/tools/base.py +++ b/app/agent/tools/base.py @@ -212,8 +212,15 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta): # 执行具体工具逻辑 try: result = await self.run(**kwargs) - result_len = len(str(result)) if result is not None else 0 - logger.debug(f"Tool {self.name} executed, raw result length: {result_len}") + + # 记录工具执行结果摘要日志 + str_result = serialize_tool_result_for_agent(result) + if len(str_result) > 500: + summary = str_result[:500] + f"...(已截断,总长度: {len(str_result)})" + else: + summary = str_result + logger.info(f"Agent工具 {self.name} 执行完成,结果摘要: {summary}") + except Exception as e: error_message = f"工具执行异常 ({type(e).__name__}): {str(e)}" logger.error(f"Tool {self.name} execution failed: {e}", exc_info=True) diff --git a/app/agent/tools/manager.py b/app/agent/tools/manager.py index 65d5f171..ec8a5db6 100644 --- a/app/agent/tools/manager.py +++ b/app/agent/tools/manager.py @@ -260,11 +260,16 @@ class MoviePilotToolsManager: # 调用工具的run方法。HTTP/MCP 工具调用不会经过 BaseTool._arun, # 因此这里也必须复用同一套返回值格式化和兜底截断逻辑。 result = await tool_instance.run(**normalized_arguments) - return format_tool_result_for_agent( - result, - tool_name=tool_name, - max_chars=getattr(tool_instance, "result_max_chars", None), - ) + + # 记录工具执行结果摘要日志 + str_result = format_tool_result_for_agent(result, tool_name=tool_name, max_chars=getattr(tool_instance, "result_max_chars", None)) + if len(str_result) > 500: + summary = str_result[:500] + f"...(已截断,总长度: {len(str_result)})" + else: + summary = str_result + logger.info(f"Agent工具 {tool_name} 执行完成,结果摘要: {summary}") + + return str_result except Exception as e: logger.error(f"调用工具 {tool_name} 时发生错误: {e}", exc_info=True) error_msg = json.dumps(