Files
multiAgentTry/backend/app/routers/heartbeats.py
Claude Code 1719d1f1f9 重构 API 路由并新增工作流编排功能
后端:
- 重构 agents, heartbeats, locks, meetings, resources, roles, workflows 路由
- 新增 orchestrator 和 providers 路由
- 新增 CLI 调用器和流程编排服务
- 添加日志配置和依赖项

前端:
- 更新 AgentsPage、SettingsPage、WorkflowPage 页面
- 扩展 api.ts 新增 API 接口

其他:
- 清理测试 agent 数据文件
- 新增示例工作流和项目审计报告

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 16:36:25 +08:00

65 lines
1.8 KiB
Python

"""
心跳管理 API 路由
接入 HeartbeatService 服务,监控 Agent 活跃状态
"""
from fastapi import APIRouter
from pydantic import BaseModel
from typing import Optional
from ..services.heartbeat import get_heartbeat_service
router = APIRouter()
class HeartbeatUpdate(BaseModel):
status: str = "idle"
current_task: Optional[str] = ""
progress: Optional[int] = 0
@router.get("")
@router.get("/")
async def list_heartbeats():
"""获取所有 Agent 心跳"""
service = get_heartbeat_service()
all_hb = await service.get_all_heartbeats()
heartbeats = {}
for agent_id, hb in all_hb.items():
heartbeats[agent_id] = {
"agent_id": hb.agent_id,
"last_heartbeat": hb.last_heartbeat,
"status": hb.status,
"current_task": hb.current_task,
"progress": hb.progress,
"elapsed_display": hb.elapsed_display,
"is_timeout": hb.is_timeout()
}
return {"heartbeats": heartbeats}
@router.post("/{agent_id}")
async def update_heartbeat(agent_id: str, data: HeartbeatUpdate = None):
"""更新 Agent 心跳"""
service = get_heartbeat_service()
if data is None:
data = HeartbeatUpdate()
await service.update_heartbeat(
agent_id=agent_id,
status=data.status,
current_task=data.current_task or "",
progress=data.progress or 0
)
return {"success": True}
@router.get("/timeouts")
async def check_timeouts(timeout_seconds: int = 60):
"""检查超时的 Agent"""
service = get_heartbeat_service()
timeout_agents = await service.check_timeout(timeout_seconds)
return {
"timeout_seconds": timeout_seconds,
"timeout_agents": timeout_agents,
"count": len(timeout_agents)
}