zhaoqingang
2025-01-07 46e223745cebfedb78563ea3b671ec0f2cadbf0e
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,26 +59,70 @@
            # 监听毕昇发来的消息并转发给客户端
            async def forward_to_client():
                last_message = "step"
                is_answer = False
                while True:
                    message = await service_websocket.recv()
                    print(f"Received from bisheng: {message}")
                    data = json.loads(message)
                    files = data.get("files", [])
                    steps = data.get("intermediate_steps", "")
                    msg = data.get("message", "")
                    if len(files) != 0 or (steps and last_message == "step") or msg or data["type"] == "close":
                        if data["type"] == "close":
                            t = "close"
                        else:
                            t = "stream"
                    try:
                        message = await service_websocket.recv()
                        # print(f"Received from bisheng: {message}")
                        data = json.loads(message)
                        files = data.get("files", [])
                        steps = data.get("intermediate_steps", "")
                        msg = data.get("message", "")
                        category = data.get("category", "")
                        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}")
                        if category == "question" and steps:
                            is_answer = False
                            if not steps:
                                steps = "\n"
                            else:
                                steps = steps + "\n"
                            result = {"message": steps, "type": "stream", "files": files}
                            await websocket.send_json(result)
                        if category == "answer" and not is_answer:
                            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 data["type"] == "close":
                            # process_files(files, agent_id)
                            result = {"message": "", "type": "close", "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)
                    last_message = "step" if steps else "message"
                        elif category == "system" and steps:
                            result = {"message": steps, "type": "stream", "files": files}
                            await websocket.send_json(result)
                        else:
                            logger.error("-------------------11111111111111--------------------------")
                            logger.error(data)
                    except Exception as e:
                        logger.error(e)
                        await websocket.send_json({"message": "连接异常!", "type": "close", "files": []})
                    # 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 +136,8 @@
                task.cancel()
                try:
                    await task
                except asyncio.CancelledError:
                except asyncio.CancelledError as e:
                    print(f"asyncio CancelledError: {e}")
                    pass
        except WebSocketDisconnect as e: