""" Agent 管理 API 路由 """ from fastapi import APIRouter, HTTPException from pydantic import BaseModel from typing import List, Optional import time router = APIRouter() # 内存存储,实际应用应该使用持久化存储 agents_db = {} class Agent(BaseModel): agent_id: str name: str role: str model: str description: Optional[str] = None status: str = "idle" created_at: float = 0 class AgentCreate(BaseModel): agent_id: str name: str role: str = "developer" model: str description: Optional[str] = None # Agent状态存储 agent_states_db = {} @router.get("") @router.get("/") async def list_agents(): """获取所有 Agent 列表""" # 合并数据库和默认agent default_agents = [ { "agent_id": "claude-001", "name": "Claude Code", "role": "developer", "model": "claude-opus-4.6", "status": "working", "description": "主开发 Agent", "created_at": time.time() - 86400 }, { "agent_id": "kimi-001", "name": "Kimi CLI", "role": "architect", "model": "kimi-k2", "status": "idle", "description": "架构设计 Agent", "created_at": time.time() - 72000 }, { "agent_id": "opencode-001", "name": "OpenCode", "role": "reviewer", "model": "opencode-v1", "status": "idle", "description": "代码审查 Agent", "created_at": time.time() - 36000 } ] # 使用数据库中的agent(覆盖默认的) agents_map = {a["agent_id"]: a for a in default_agents} agents_map.update(agents_db) return {"agents": list(agents_map.values())} @router.post("/register") async def register_agent(agent: AgentCreate): """注册新 Agent""" agent_data = { "agent_id": agent.agent_id, "name": agent.name, "role": agent.role, "model": agent.model, "description": agent.description or "", "status": "idle", "created_at": time.time() } agents_db[agent.agent_id] = agent_data return agent_data @router.get("/{agent_id}") async def get_agent(agent_id: str): """获取指定 Agent 信息""" if agent_id in agents_db: return agents_db[agent_id] raise HTTPException(status_code=404, detail="Agent not found") @router.delete("/{agent_id}") async def delete_agent(agent_id: str): """删除 Agent""" if agent_id in agents_db: del agents_db[agent_id] return {"message": "Agent deleted"} raise HTTPException(status_code=404, detail="Agent not found") @router.get("/{agent_id}/state") async def get_agent_state(agent_id: str): """获取 Agent 状态""" # 如果存在真实状态,返回真实状态 if agent_id in agent_states_db: return agent_states_db[agent_id] # 默认mock状态 default_states = { "claude-001": { "agent_id": agent_id, "task": "修复用户登录bug", "progress": 65, "working_files": ["src/auth/login.py", "src/auth/jwt.py"], "status": "working", "last_update": time.time() - 120 }, "kimi-001": { "agent_id": agent_id, "task": "等待会议开始", "progress": 0, "working_files": [], "status": "waiting", "last_update": time.time() - 300 }, "opencode-001": { "agent_id": agent_id, "task": "代码审查", "progress": 30, "working_files": ["src/components/Button.tsx"], "status": "working", "last_update": time.time() - 60 } } return default_states.get(agent_id, { "agent_id": agent_id, "task": "空闲", "progress": 0, "working_files": [], "status": "idle", "last_update": time.time() }) @router.post("/{agent_id}/state") async def update_agent_state(agent_id: str, data: dict): """更新 Agent 状态""" agent_states_db[agent_id] = { "agent_id": agent_id, "task": data.get("task", ""), "progress": data.get("progress", 0), "working_files": data.get("working_files", []), "status": data.get("status", "idle"), "last_update": time.time() } return {"success": True}