jack 5 days ago
parent
commit
29f4912a84
1 changed files with 21 additions and 10 deletions
  1. 21 10
      app/core/runner/utils/message_util.py

+ 21 - 10
app/core/runner/utils/message_util.py

@@ -52,24 +52,35 @@ def is_tool_call(message: ChatCompletionMessage) -> bool:
 
 
 def merge_tool_call_delta(tool_calls, tool_call_delta):
-    if len(tool_calls) - 1 >= tool_call_delta.index:
-        tool_call = tool_calls[tool_call_delta.index]
-        tool_call.function.arguments += (
-            ""
-            if tool_call_delta.function.arguments is None
-            else tool_call_delta.function.arguments
+    index = getattr(tool_call_delta, "index", None)
+    if index is None:
+        # 尝试通过 id 匹配已有工具调用
+        if tool_call_delta.id:
+            for i, tc in enumerate(tool_calls):
+                if tc.id == tool_call_delta.id:
+                    index = i
+                    break
+        # 若仍找不到,根据列表长度决定
+        if index is None:
+            index = 0 if len(tool_calls) == 0 else len(tool_calls) - 1
+
+    if len(tool_calls) - 1 >= index:
+        tool_call = tool_calls[index]
+        # 确保 arguments 为字符串,避免 None 导致拼接失败
+        tool_call.function.arguments = (tool_call.function.arguments or "") + (
+            tool_call_delta.function.arguments or ""
         )
     else:
         tool_call = ChatCompletionMessageToolCall(
             id=tool_call_delta.id,
             function=Function(
                 name=tool_call_delta.function.name,
-                arguments=tool_call_delta.function.arguments,
+                arguments=tool_call_delta.function.arguments or "",
             ),
             type=tool_call_delta.type,
         )
         tool_calls.append(tool_call)
-    print(
-        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaatttttttttttttttttttttttttttttmmmmmmmmmmmmmmmmmmmmmmmmmm"
-    )
+
+    # 调试输出可保留,但建议移除或降级为 debug
+    print("=" * 40)
     print(tool_calls)