完整实现 Swarm 多智能体协作系统

- 新增 CLIPluginAdapter 统一接口 (backend/app/core/agent_adapter.py)
- 新增 LLM 服务层,支持 Anthropic/OpenAI/DeepSeek/Ollama (backend/app/services/llm_service.py)
- 新增 Agent 执行引擎,支持文件锁自动管理 (backend/app/services/agent_executor.py)
- 新增 NativeLLMAgent 原生 LLM 适配器 (backend/app/adapters/native_llm_agent.py)
- 新增进程管理器 (backend/app/services/process_manager.py)
- 新增 Agent 控制 API (backend/app/routers/agents_control.py)
- 新增 WebSocket 实时通信 (backend/app/routers/websocket.py)
- 更新前端 AgentsPage,支持启动/停止 Agent
- 测试通过:Agent 启动、批量操作、栅栏同步

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Claude Code
2026-03-09 17:32:11 +08:00
commit dc398d7c7b
118 changed files with 23120 additions and 0 deletions

View File

@@ -0,0 +1,166 @@
"""
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("/")
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}