mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 00:51:17 +08:00
* chore: remove dead Python modules and helpers Delete unreferenced P0 code identified by the dead-code audit: - services/llm/registry.py and its test-only coverage - logging/adapters package (LlamaIndex log forwarding) - textbook_struct.chapter_rebuild.detect_frames - load_config_with_main_async and its async file helper - memory.trace.iter_by_ids / latest_ts - TokenTracker.format_summary - WeixinChannel._api_get - ArxivSearchTool.extract_arxiv_id_from_url - FileLibraryStore migration scaffolding (_run_migrations et al.) - eight small dead helpers in services, api/utils, and knowledge - redundant runtime C2CMessage import in the QQ channel * chore: drop the prompt-language shim and a dead storage helper The book agents were the last in-tree users of blocks/_language.py, which only re-exported services.prompt.language. Import the real module and delete the shim. storage._storage_path_from_version_entry had no caller. Verified: ruff F401/F811/F841/F821 on changed files; py_compile. --------- Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
"""
|
|
Progress Broadcaster - Manages WebSocket broadcasting of knowledge base progress
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
from typing import Optional
|
|
|
|
from fastapi import WebSocket
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ProgressBroadcaster:
|
|
"""Manages WebSocket broadcasting of knowledge base progress"""
|
|
|
|
_instance: Optional["ProgressBroadcaster"] = None
|
|
_connections: dict[str, set[WebSocket]] = {} # kb_name -> Set[WebSocket]
|
|
_lock = asyncio.Lock()
|
|
|
|
@classmethod
|
|
def get_instance(cls) -> "ProgressBroadcaster":
|
|
"""Get singleton instance"""
|
|
if cls._instance is None:
|
|
cls._instance = cls()
|
|
return cls._instance
|
|
|
|
async def connect(self, kb_name: str, websocket: WebSocket):
|
|
"""Connect WebSocket to specified knowledge base"""
|
|
async with self._lock:
|
|
if kb_name not in self._connections:
|
|
self._connections[kb_name] = set()
|
|
self._connections[kb_name].add(websocket)
|
|
logger.debug(
|
|
f"Connected WebSocket for KB '{kb_name}' (total: {len(self._connections[kb_name])})"
|
|
)
|
|
|
|
async def disconnect(self, kb_name: str, websocket: WebSocket):
|
|
"""Disconnect WebSocket connection"""
|
|
async with self._lock:
|
|
if kb_name in self._connections:
|
|
self._connections[kb_name].discard(websocket)
|
|
if not self._connections[kb_name]:
|
|
del self._connections[kb_name]
|
|
logger.debug(f"Disconnected WebSocket for KB '{kb_name}'")
|
|
|
|
async def broadcast(self, kb_name: str, progress: dict):
|
|
"""Broadcast progress update to all WebSocket connections for specified knowledge base"""
|
|
async with self._lock:
|
|
if kb_name not in self._connections:
|
|
return
|
|
|
|
# Create list of connections to remove (closed connections)
|
|
to_remove = []
|
|
|
|
for websocket in self._connections[kb_name]:
|
|
try:
|
|
await websocket.send_json({"type": "progress", "data": progress})
|
|
except Exception as e:
|
|
# Connection closed or error, mark for removal
|
|
logger.debug(f"Error sending to WebSocket for KB '{kb_name}': {e}")
|
|
to_remove.append(websocket)
|
|
|
|
# Remove closed connections
|
|
for ws in to_remove:
|
|
self._connections[kb_name].discard(ws)
|
|
|
|
if not self._connections[kb_name]:
|
|
del self._connections[kb_name]
|