fix: 多工具时协程不阻塞,仅需等待最慢那个

This commit is contained in:
Sakura-RanChen
2025-11-18 11:10:18 +08:00
parent 0b0fd142a3
commit 48313bc302
+11 -8
View File
@@ -872,20 +872,23 @@ class ConnectionHandler:
f"检测到 {len(tool_calls_list)} 个工具调用" f"检测到 {len(tool_calls_list)} 个工具调用"
) )
# 执行所有工具并收集结果 # 收集所有工具调用的 Future
tool_results = [] futures_with_data = []
for tool_call_data in tool_calls_list: for tool_call_data in tool_calls_list:
self.logger.bind(tag=TAG).debug( self.logger.bind(tag=TAG).debug(
f"function_name={tool_call_data['name']}, function_id={tool_call_data['id']}, function_arguments={tool_call_data['arguments']}" f"function_name={tool_call_data['name']}, function_id={tool_call_data['id']}, function_arguments={tool_call_data['arguments']}"
) )
# 使用统一工具处理器处理所有工具调用 future = asyncio.run_coroutine_threadsafe(
result = asyncio.run_coroutine_threadsafe( self.func_handler.handle_llm_function_call(self, tool_call_data),
self.func_handler.handle_llm_function_call(
self, tool_call_data
),
self.loop, self.loop,
).result() )
futures_with_data.append((future, tool_call_data))
# 等待协程结束(实际等待时长为最慢的那个)
tool_results = []
for future, tool_call_data in futures_with_data:
result = future.result()
tool_results.append((result, tool_call_data)) tool_results.append((result, tool_call_data))
# 统一处理所有工具调用结果 # 统一处理所有工具调用结果