zhangqian
2024-10-29 976f9731921853d3098729e4ba8ef6b2d377e9e7
app/api/report.py
@@ -65,8 +65,8 @@
                    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:
@@ -75,6 +75,8 @@
                        result = {"step_message": steps, "message": msg, "type": t, "files": files}
                        await websocket.send_json(result)
                        print(f"Forwarded to client, {chat_id}: {result}")
                    last_message = "step" if steps else "message"
            # 启动两个任务,分别处理客户端和服务端的消息
            tasks = [
@@ -91,8 +93,22 @@
                except asyncio.CancelledError:
                    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)