feat: 实现多主机架构的核心组件

新增路由器、主机客户端和共享协议模块,支持多主机部署模式:
- 路由器作为中央节点管理主机连接和消息路由
- 主机客户端作为工作节点运行本地代理
- 共享协议定义通信消息格式
- 新增独立运行模式standalone.py
- 更新配置系统支持路由模式
This commit is contained in:
Yuyao Huang (Sam)
2026-03-28 14:08:47 +08:00
parent 8ecc701d5e
commit 64297e5e27
17 changed files with 1338 additions and 6 deletions
+12 -1
View File
@@ -30,6 +30,7 @@ class BackgroundTask:
result: Optional[str] = None
error: Optional[str] = None
notify_chat_id: Optional[str] = None
user_id: Optional[str] = None
@property
def elapsed(self) -> float:
@@ -44,12 +45,18 @@ class TaskRunner:
def __init__(self) -> None:
self._tasks: Dict[str, BackgroundTask] = {}
self._lock = asyncio.Lock()
self._notification_handler: Optional[Callable] = None
def set_notification_handler(self, handler: Optional[Callable]) -> None:
"""Set custom notification handler for M3 mode (host client -> router)."""
self._notification_handler = handler
async def submit(
self,
coro: Callable[[], Any],
description: str,
notify_chat_id: Optional[str] = None,
user_id: Optional[str] = None,
) -> str:
"""Submit a coroutine as a background task."""
task_id = str(uuid.uuid4())[:8]
@@ -59,6 +66,7 @@ class TaskRunner:
started_at=time.time(),
status=TaskStatus.PENDING,
notify_chat_id=notify_chat_id,
user_id=user_id,
)
async with self._lock:
@@ -94,7 +102,10 @@ class TaskRunner:
logger.exception("Task %s failed: %s", task_id, exc)
if task.notify_chat_id:
await self._send_notification(task)
if self._notification_handler:
await self._notification_handler(task)
else:
await self._send_notification(task)
async def _send_notification(self, task: BackgroundTask) -> None:
"""Send Feishu notification about task completion."""