Main entry point for handling the lifecycle of the WebSocket connection.
(self)
| 270 | self.current_user_text: str = "" |
| 271 | |
| 272 | async def run(self): |
| 273 | """Main entry point for handling the lifecycle of the WebSocket connection.""" |
| 274 | try: |
| 275 | # 1. Setup session (Authentication, permissions, loading models, history, etc.) |
| 276 | success = await self.setup() |
| 277 | if not success: |
| 278 | return |
| 279 | |
| 280 | # 2. Start the message receiving and processing loop |
| 281 | await self.message_loop() |
| 282 | |
| 283 | except WebSocketDisconnect: |
| 284 | logger.info(f"[WS] Client disconnected: {getattr(self.user, 'id', 'unknown')}") |
| 285 | await manager.disconnect(str(self.agent_id), self.websocket) |
| 286 | except Exception as e: |
| 287 | logger.exception(f"[WS] Unexpected error: {e}") |
| 288 | await manager.disconnect(str(self.agent_id), self.websocket) |
| 289 | |
| 290 | async def setup(self) -> bool: |
| 291 | """Accepts connection, authenticates user, verifies agent access, loads models, resolves session & history.""" |