Browse Source

修复编译后接收worker信息报错的问题

longjoedyy 3 weeks ago
parent
commit
e5dd63ba0c
2 changed files with 40 additions and 13 deletions
  1. 3 1
      core/async_chat_service.py
  2. 37 12
      core/worker_manager.py

+ 3 - 1
core/async_chat_service.py

@@ -214,7 +214,9 @@ class AsyncChatService:
     async def get_task_result(self, task_id: str) -> Dict[str, Any]:
         """获取任务结果"""
         task_info = chat_result_manager.get_task(task_id)
-        # chat_logger.info(f"获取任务结果 - 任务ID={task_id}, 状态={task_info['status']}")
+        # chat_logger.info(
+        #     f"获取任务结果 - 任务ID={task_id}, 状态={task_info['status']},error_message={task_info['error_message']}"
+        # )
         if not task_info:
             return {
                 "success": False,

+ 37 - 12
core/worker_manager.py

@@ -327,18 +327,43 @@ def _execute_and_get_result(
 
         result = worker.execute(enhanced_query, backend_url, token)
         chat_logger.info(f"Worker{worker_type}执行成功:问题{query}")
-        ai_messages = [
-            msg
-            for msg in result.get("messages", [])
-            if hasattr(msg, "type") and msg.type == "ai"
-        ]
-
-        if ai_messages:
-            # 获取最后一条 AI 消息的内容
-            last_ai_content = ai_messages[-1].content
-            return last_ai_content
-        else:
-            return ""
+        last_ai_content = ""
+        all_messages = result["messages"]
+        for i, msg in enumerate(all_messages):
+            msg_data = {
+                "index": i,
+                "type": getattr(msg, "type", "unknown"),
+                "content": "",
+            }
+            # 获取内容
+            if hasattr(msg, "content"):
+                content = msg.content
+                if isinstance(content, str):
+                    msg_data["content"] = content
+                else:
+                    msg_data["content"] = str(content)
+
+            # 收集AI消息
+            if msg_data["type"] == "ai":
+                last_ai_content = msg_data["content"]
+
+        return last_ai_content
+
+        # ai_messages = [
+        #     msg
+        #     for msg in result.get("messages", [])
+        #     if hasattr(msg, "type") and msg.type == "ai"
+        # ]
+
+        # if ai_messages:
+        #     # 获取最后一条 AI 消息的内容
+        #     last_ai_content = ai_messages[-1].content
+        #     chat_logger.info(
+        #         f"Worker{worker_type}执行成功:last_ai_content={last_ai_content}"
+        #     )
+        #     return last_ai_content
+        # else:
+        #     return ""
 
     except Exception as e:
         chat_logger.error(