安全访问,同时兼容 dict 和对象两种类型。这样即使某个 chunk 缺少 name 字段也不会崩溃。

This commit is contained in:
朱潮 2026-04-04 23:31:46 +08:00
parent 314a693793
commit b1b98caad7

View File

@ -102,10 +102,12 @@ async def enhanced_generate_stream_response(
message_tag = "TOOL_CALL"
if config.tool_response:
for tool_call_chunk in msg.tool_call_chunks:
if tool_call_chunk["name"]:
new_content = f"[{message_tag}] {tool_call_chunk['name']}\n"
if tool_call_chunk['args']:
new_content += tool_call_chunk['args']
chunk_name = tool_call_chunk.get("name") if isinstance(tool_call_chunk, dict) else getattr(tool_call_chunk, "name", None)
chunk_args = tool_call_chunk.get("args") if isinstance(tool_call_chunk, dict) else getattr(tool_call_chunk, "args", None)
if chunk_name:
new_content = f"[{message_tag}] {chunk_name}\n"
if chunk_args:
new_content += chunk_args
# 处理文本内容
elif msg.content:
preamble_completed.set()