Files
PlantAI-rag-new/agent/legacy-rag/api/websocket.py
T
jerryandClaude Fable 5 2f82c4a8dc P4/M5: agent/legacy-rag 源码迁移(449 模块 191K 行)+ 影子 7911/7912 冒烟
- 按生产 7900 容器黄金清单迁移(compile.manifest 449 条目,0 缺失)
- 入口双态:api.app(生产同源 3 路由)/ api.main(裁剪 11 路由族 91 路径)
- 修复:advanced/__init__ 空文件(包标记误覆盖,已从主项目恢复)
- 修复:api/rag_routes.py 跨环境 site-packages 注入改 LEGACY_GF_SITE_PACKAGES 门控
- 修复:scripts/utils 缺失(动态加载 reasoning_engine)
- 影子验证:kg/stats 数据连通(6.2s,与 7900 同源 normalized 后端)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 14:20:41 +08:00

79 lines
2.1 KiB
Python
Executable File

import json
from typing import Any
from fastapi import WebSocket
class ConnectionManager:
def __init__(self):
self._connections: dict[str, list[WebSocket]] = {}
async def connect(self, websocket: WebSocket, session_id: str) -> None:
await websocket.accept()
if session_id not in self._connections:
self._connections[session_id] = []
self._connections[session_id].append(websocket)
def disconnect(self, websocket: WebSocket, session_id: str) -> None:
if session_id in self._connections:
self._connections[session_id].remove(websocket)
if not self._connections[session_id]:
del self._connections[session_id]
async def send_message(self, session_id: str, message: dict[str, Any]) -> None:
if session_id in self._connections:
for connection in self._connections[session_id]:
await connection.send_json(message)
async def broadcast(self, message: dict[str, Any]) -> None:
for connections in self._connections.values():
for connection in connections:
await connection.send_json(message)
manager = ConnectionManager()
async def send_progress(
session_id: str,
current_agent: str | None,
status: str,
progress: float,
message: str,
) -> None:
await manager.send_message(
session_id,
{
"type": "progress",
"session_id": session_id,
"data": {
"current_agent": current_agent,
"status": status,
"progress": progress,
"message": message,
},
},
)
async def send_result(session_id: str, result: dict[str, Any]) -> None:
await manager.send_message(
session_id,
{
"type": "result",
"session_id": session_id,
"data": result,
},
)
async def send_error(session_id: str, error: str) -> None:
await manager.send_message(
session_id,
{
"type": "error",
"session_id": session_id,
"data": {"error": error},
},
)