Compare commits
2 Commits
5c4f36ff0d
...
c257a98a37
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c257a98a37 | ||
|
|
a822a2a1d1 |
@ -314,7 +314,15 @@ class ToolOutputLengthMiddleware(AgentMiddleware):
|
|||||||
if isinstance(result, ToolMessage):
|
if isinstance(result, ToolMessage):
|
||||||
# Process ToolMessage
|
# Process ToolMessage
|
||||||
content = result.text
|
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
|
# Truncate the content
|
||||||
truncated_content, metadata = self.truncate_content(content, tool_name)
|
truncated_content, metadata = self.truncate_content(content, tool_name)
|
||||||
|
|
||||||
@ -355,7 +363,16 @@ class ToolOutputLengthMiddleware(AgentMiddleware):
|
|||||||
for msg in messages:
|
for msg in messages:
|
||||||
if isinstance(msg, ToolMessage):
|
if isinstance(msg, ToolMessage):
|
||||||
tool_msg_tool_name = getattr(msg, 'name', tool_name)
|
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
|
# Truncate the ToolMessage content
|
||||||
truncated_content, metadata = self.truncate_content(msg.content, tool_msg_tool_name)
|
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)
|
# Handle different return types (same logic as sync version)
|
||||||
if isinstance(result, ToolMessage):
|
if isinstance(result, ToolMessage):
|
||||||
content = result.text
|
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_content, metadata = self.truncate_content(content, tool_name)
|
||||||
|
|
||||||
truncated_message = ToolMessage(
|
truncated_message = ToolMessage(
|
||||||
@ -458,7 +483,16 @@ class ToolOutputLengthMiddleware(AgentMiddleware):
|
|||||||
for msg in messages:
|
for msg in messages:
|
||||||
if isinstance(msg, ToolMessage):
|
if isinstance(msg, ToolMessage):
|
||||||
tool_msg_tool_name = getattr(msg, 'name', tool_name)
|
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_content, metadata = self.truncate_content(msg.content, tool_msg_tool_name)
|
||||||
|
|
||||||
truncated_msg = ToolMessage(
|
truncated_msg = ToolMessage(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user