zhaoqingang
2024-11-19 35f8639455992d24f5d0b962bd6fa79d7fcb8f31
app/api/report.py
@@ -10,7 +10,7 @@
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()
@@ -65,16 +65,21 @@
                    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"
                    category = data.get("category", "")
                    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 = {"step_message": steps, "message": msg, "type": t, "files": files}
                        result = {"message": msg, "type": t, "files": files}
                        await websocket.send_json(result)
                        print(f"Forwarded to client, {chat_id}: {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 +93,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)