Compare commits

..

2 Commits

Author SHA1 Message Date
朱潮
c257a98a37 Merge branch 'master' into bot_manager 2026-02-23 23:23:44 +08:00
朱潮
a822a2a1d1 优化空的工具调用 2026-02-23 23:23:38 +08:00

View File

@ -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(