From a822a2a1d1acf7897516bdff8137fc9f00acb949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Mon, 23 Feb 2026 23:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A9=BA=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/tool_output_length_middleware.py | 42 +++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/agent/tool_output_length_middleware.py b/agent/tool_output_length_middleware.py index 34a939f..96b6c1e 100644 --- a/agent/tool_output_length_middleware.py +++ b/agent/tool_output_length_middleware.py @@ -314,7 +314,15 @@ class ToolOutputLengthMiddleware(AgentMiddleware): if isinstance(result, ToolMessage): # Process ToolMessage content = result.text - if content and len(content) > self.max_length: + # 如果内容为空字符串,填充为 "empty" + if content is None or content == '': + logger.info(f"Tool output for '{tool_name}' is empty, filling with 'empty'") + return ToolMessage( + content="empty", + tool_call_id=result.tool_call_id, + name=result.name + ) + if len(content) > self.max_length: # Truncate the content truncated_content, metadata = self.truncate_content(content, tool_name) @@ -355,7 +363,16 @@ class ToolOutputLengthMiddleware(AgentMiddleware): for msg in messages: if isinstance(msg, ToolMessage): tool_msg_tool_name = getattr(msg, 'name', tool_name) - if self.should_process_tool(tool_msg_tool_name) and len(msg.content) > self.max_length: + msg_content = msg.content if isinstance(msg.content, str) else str(msg.content) if msg.content else '' + # 如果内容为空字符串,填充为 "empty" + if msg_content == '': + logger.info(f"Tool output for '{tool_msg_tool_name}' in Command is empty, filling with 'empty'") + updated_messages.append(ToolMessage( + content="empty", + tool_call_id=msg.tool_call_id, + name=msg.name + )) + elif self.should_process_tool(tool_msg_tool_name) and len(msg_content) > self.max_length: # Truncate the ToolMessage content truncated_content, metadata = self.truncate_content(msg.content, tool_msg_tool_name) @@ -424,7 +441,15 @@ class ToolOutputLengthMiddleware(AgentMiddleware): # Handle different return types (same logic as sync version) if isinstance(result, ToolMessage): content = result.text - if content and len(content) > self.max_length: + # 如果内容为空字符串,填充为 "empty" + if content is None or content == '': + logger.info(f"Tool output for '{tool_name}' is empty, filling with 'empty'") + return ToolMessage( + content="empty", + tool_call_id=result.tool_call_id, + name=result.name + ) + if len(content) > self.max_length: truncated_content, metadata = self.truncate_content(content, tool_name) truncated_message = ToolMessage( @@ -458,7 +483,16 @@ class ToolOutputLengthMiddleware(AgentMiddleware): for msg in messages: if isinstance(msg, ToolMessage): tool_msg_tool_name = getattr(msg, 'name', tool_name) - if self.should_process_tool(tool_msg_tool_name) and len(msg.content) > self.max_length: + msg_content = msg.content if isinstance(msg.content, str) else str(msg.content) if msg.content else '' + # 如果内容为空字符串,填充为 "empty" + if msg_content == '': + logger.info(f"Tool output for '{tool_msg_tool_name}' in Command is empty, filling with 'empty'") + updated_messages.append(ToolMessage( + content="empty", + tool_call_id=msg.tool_call_id, + name=msg.name + )) + elif self.should_process_tool(tool_msg_tool_name) and len(msg_content) > self.max_length: truncated_content, metadata = self.truncate_content(msg.content, tool_msg_tool_name) truncated_msg = ToolMessage(