From 671569026e1522574f9612a8536b73131834bac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Sun, 14 Dec 2025 09:43:24 +0800 Subject: [PATCH] tool_id --- routes/chat.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/routes/chat.py b/routes/chat.py index 00e7576..645c449 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -144,7 +144,7 @@ async def enhanced_generate_stream_response( chunk_id = 0 message_tag = "" function_call = {} - function_name = "" + tool_id = "" async for msg, metadata in agent.astream({"messages": messages}, stream_mode="messages"): new_content = "" @@ -153,13 +153,15 @@ 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 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"] + tool_id = msg.tool_call_chunks[0]["id"] + if tool_id not in function_call: + function_call[tool_id] = { + "function_name": msg.tool_call_chunks[0]["name"], + "arguments": "" + } + if tool_id and tool_id in function_call and msg.tool_call_chunks[0]["args"]: + function_call[tool_id]["arguments"] += msg.tool_call_chunks[0]["args"] elif len(msg.content) > 0: preamble_completed.set() await output_queue.put(("preamble_done", None)) @@ -173,8 +175,8 @@ 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") ): - for function_name, args in function_call.items(): - new_content = f"[{message_tag}] {function_name}\n{args}" + for args in function_call.values(): + new_content = f"[{message_tag}] {args['function_name']}\n{args['arguments']}" message_tag = "TOOL_CALL_FINISH" elif isinstance(msg, ToolMessage) and len(msg.content) > 0: message_tag = "TOOL_RESPONSE"