This commit is contained in:
朱潮 2025-12-14 09:30:34 +08:00
commit 367e292854

View File

@ -143,8 +143,8 @@ async def enhanced_generate_stream_response(
logger.info(f"Starting agent stream response")
chunk_id = 0
message_tag = ""
function_call = {}
function_name = ""
tool_args = ""
async for msg, metadata in agent.astream({"messages": messages}, stream_mode="messages"):
new_content = ""
@ -153,10 +153,13 @@ async def enhanced_generate_stream_response(
if msg.tool_call_chunks: # 检查工具调用块
if message_tag != "TOOL_CALL":
message_tag = "TOOL_CALL"
function_name = ""
if msg.tool_call_chunks[0]["name"]:
function_name = msg.tool_call_chunks[0]["name"]
if msg.tool_call_chunks[0]["args"]:
tool_args += msg.tool_call_chunks[0]["args"]
if function_name and msg.tool_call_chunks[0]["args"]:
if function_name not in function_call:
function_call[function_name] = ""
function_call[function_name] += msg.tool_call_chunks[0]["args"]
elif len(msg.content) > 0:
preamble_completed.set()
await output_queue.put(("preamble_done", None))
@ -170,7 +173,9 @@ async def enhanced_generate_stream_response(
("finish_reason" in msg.response_metadata and msg.response_metadata["finish_reason"] == "tool_calls") or \
("stop_reason" in msg.response_metadata and msg.response_metadata["stop_reason"] == "tool_use")
):
new_content = f"[{message_tag}] {function_name}\n{tool_args}"
for function_name, args in function_call.items():
new_content = f"[{message_tag}] {function_name}\n{args}"
message_tag = "TOOL_CALL_FINISH"
elif isinstance(msg, ToolMessage) and len(msg.content) > 0:
message_tag = "TOOL_RESPONSE"
new_content = f"[{message_tag}] {msg.name}\n{msg.text}"