From f249dda1376dd5a525e74617900351c5bbc61705 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期一, 02 十二月 2024 09:26:18 +0800
Subject: [PATCH] 报告生成

---
 app/api/report.py |   65 ++++++++++++++++++++++++++------
 1 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/app/api/report.py b/app/api/report.py
index 22c9bf6..213fdc7 100644
--- a/app/api/report.py
+++ b/app/api/report.py
@@ -4,13 +4,15 @@
 import asyncio
 import websockets
 from sqlalchemy.orm import Session
+
+from Log import logger
 from app.api import get_current_user_websocket, ResponseList, get_current_user, format_file_url, process_files
 from app.config.config import settings
 from app.models.agent_model import AgentModel, AgentType
 from app.models.base_model import get_db
 from app.models.user_model import UserModel
 from app.service.bisheng import BishengService
-from app.service.token import get_bisheng_token
+from app.service.service_token import get_bisheng_token
 
 router = APIRouter()
 
@@ -57,7 +59,7 @@
 
             # 鐩戝惉姣曟槆鍙戞潵鐨勬秷鎭苟杞彂缁欏鎴风
             async def forward_to_client():
-                last_message = "step"
+                is_answer = False
                 while True:
                     message = await service_websocket.recv()
                     print(f"Received from bisheng: {message}")
@@ -65,18 +67,56 @@
                     files = data.get("files", [])
                     steps = data.get("intermediate_steps", "")
                     msg = data.get("message", "")
+                    category = data.get("category", "")
 
-                    if len(files) != 0 or (steps and last_message == "step") or msg or data["type"] == "close":
-                        if data["type"] == "close":
-                            t = "close"
+                    if category == "question" and steps:
+                        is_answer = False
+                        if not steps:
+                            steps = "\n"
                         else:
-                            t = "stream"
-                        process_files(files, agent_id)
-                        result = {"step_message": steps, "message": msg, "type": t, "files": files}
-                        await websocket.send_json(result)
-                        print(f"Forwarded to client, {chat_id}: {result}")
+                            steps =steps +  "\n"
 
-                    last_message = "step" if steps else "message"
+                        result = {"message": steps, "type": "stream", "files": files}
+                        await websocket.send_json(result)
+                    if category == "answer" and not is_answer:
+                        process_files(files, agent_id)
+                        if not steps.endswith("\n"):
+                            steps+= "\n\n"
+                        result = {"message": steps, "type": "stream", "files": files}
+                        await websocket.send_json(result)
+                    if category == "answer" and is_answer:
+                        process_files(files, agent_id)
+                        result = {"message": "\n", "type": "stream", "files": files}
+                        await websocket.send_json(result)
+                    elif category == "processing":
+                        process_files(files, agent_id)
+                        is_answer = True
+                        result = {"message": msg, "type": "stream", "files": files}
+                        await websocket.send_json(result)
+                    elif files:
+                        process_files(files, agent_id)
+                        result = {"message": "", "type": "stream", "files": files}
+                        await websocket.send_json(result)
+                    elif data["type"] == "close":
+                        process_files(files, agent_id)
+                        result = {"message": "", "type": "close", "files": files}
+                        await websocket.send_json(result)
+                    else:
+                        logger.error("-------------------11111111111111--------------------------")
+                        logger.error(data)
+                    # if len(files) != 0 or (msg and category != "answer") or data["type"] == "close":
+                    #     if data["type"] == "close":
+                    #         t = "close"
+                    #     else:
+                    #         t = "stream"
+                    #     process_files(files, agent_id)
+                    #     result = {"message": msg, "type": t, "files": files}
+                    #     await websocket.send_json(result)
+                    # elif steps and last_message == "step":
+                    #     result = {"step_message": steps, "type": "stream", "files": files}
+                    #     await websocket.send_json(result)
+
+                    # last_message = "message" if msg else "step"
 
             # 鍚姩涓や釜浠诲姟锛屽垎鍒鐞嗗鎴风鍜屾湇鍔$鐨勬秷鎭�
             tasks = [
@@ -90,7 +130,8 @@
                 task.cancel()
                 try:
                     await task
-                except asyncio.CancelledError:
+                except asyncio.CancelledError as e:
+                    print(f"asyncio CancelledError: {e}")
                     pass
 
         except WebSocketDisconnect as e:

--
Gitblit v1.8.0