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(