From 319e3e93bbb4807da58db2ac72bb8f3029738dd7 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期二, 03 十二月 2024 13:48:40 +0800 Subject: [PATCH] dify报告合并 --- app/api/report.py | 103 +++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 83 insertions(+), 20 deletions(-) diff --git a/app/api/report.py b/app/api/report.py index 1bc08dc..0d33194 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,24 +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": - last_message = "step" if steps else "message" - 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) + + 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 = [ @@ -88,11 +136,26 @@ task.cancel() try: await task - except asyncio.CancelledError: + except asyncio.CancelledError as e: + print(f"asyncio CancelledError: {e}") pass - except WebSocketDisconnect: - print(f"Client {chat_id} disconnected") + except WebSocketDisconnect as e: + print(f"WebSocket connection closed with code {e.code}: {e.reason}") + await websocket.close() + await service_websocket.close() + except Exception as e: + print(f"Exception occurred: {e}") + finally: + print("Cleaning up resources of bisheng report") + # 鍙栨秷鎵�鏈変换鍔� + for task in tasks: + if not task.done(): + task.cancel() + try: + await task + except asyncio.CancelledError: + pass @router.get("/variables/list", response_model=ResponseList) -- Gitblit v1.8.0