Skip to content

feat: WebSocket 实时通知系统 — 替换轮询,事件驱动刷新#19

Merged
robscc merged 1 commit intomainfrom
feat/websocket-notifications
Mar 18, 2026
Merged

feat: WebSocket 实时通知系统 — 替换轮询,事件驱动刷新#19
robscc merged 1 commit intomainfrom
feat/websocket-notifications

Conversation

@robscc
Copy link
Owner

@robscc robscc commented Mar 18, 2026

Summary

  • 新增 NotificationBusbackend/agentpal/services/notification_bus.py):基于 asyncio.Queue 的 fan-out pub/sub 总线,支持 256 消息缓冲、慢消费者丢弃、指数退避重连
  • 新增 WebSocket 端点 GET /api/v1/notifications/wsnotifications.py):每 30 秒无消息时发送心跳 {"type": "ping"} 保持连接
  • SubAgent & CronScheduler 集成:任务完成/失败时通过 notification_bus.publish() 广播事件,失败不影响主流程
  • 前端 useNotifications Hook + NotificationProvider:连接 WS 端点,按 INVALIDATION_MAP invalidate React Query 缓存;指数退避自动重连(1s → 30s)
  • 移除定时轮询useTasks(5s)、useCron(10s)、DashboardPage(30s)改为 WS 事件驱动 + staleTime,减少无效请求

Test plan

  • backend/tests/unit/test_notification_bus.py — 覆盖 subscribe/publish/unsubscribe/慢消费者/广播等 9 个场景
  • backend/tests/integration/test_notifications_ws.py — WebSocket 连接、消息收发、心跳
  • 手动验证:启动前后端,触发 SubAgent 任务或 Cron,观察前端 Tasks/Cron 页面无需刷新自动更新

🤖 Generated with Claude Code

新增全局 NotificationBus(asyncio fan-out pub/sub),SubAgent 任务完成/失败和
Cron 执行结束时向所有 WebSocket 订阅方广播事件。前端通过 useNotifications Hook
连接 /api/v1/notifications/ws,按类型 invalidate React Query 缓存,
移除 useTasks / useCron / DashboardPage 的定时轮询,降低无效请求。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@robscc robscc merged commit 4be3e4c into main Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant