用途
这是一个供全栈多智能体应用使用的架构规范、目录布局、通信协议与约定参考。它用来说明如何组织一个由 FastAPI 后端与 AG2 多智能体编排、React 前端构成的项目。
目录布局
后端:
backend/
├── app/
│ ├── main.py # FastAPI app — REST endpoints + WebSocket handler
│ ├── agents.py # AG2 agent team definition and run_team() entry point
│ └── schemas.py # Pydantic models shared between endpoints and agents
└── tests/
└── test_api.py
前端:
frontend/
├── src/
│ ├── main.tsx # React entry point
│ ├── App.tsx # Root component
│ └── components/
│ └── Chat.tsx # WebSocket chat UI
├── index.html
├── vite.config.ts # Vite config with API/WS proxy to backend
└── package.json
技术栈
- 后端:FastAPI、AG2(多智能体编排)、Pydantic、uvicorn
- 前端:React 19、TypeScript、Vite
- 通信:WebSocket 用于实时智能体消息,REST 用于健康检查/元数据
通信协议
前端与后端通过位于 /ws/chat 的单一 WebSocket 通信。
消息格式(通过 WebSocket 传输 JSON):
客户端 → 服务端:
{"message": "user text here"}
服务端 → 客户端(每条智能体响应一条):
{"agent": "planner", "content": "...", "type": "agent_message"}
服务端 → 客户端(所有智能体完成时):
{"agent": "", "content": "", "type": "done"}
消息类型:agent_message、status、error、done。
REST 端点
- GET /api/health — 返回
{"status": "ok", "agents": ["planner", "coder", "reviewer"]}
智能体团队
后端运行一个 3 智能体团队,使用 AG2 的 RoundRobinPattern:
- planner — 将用户请求拆解为编号步骤
- coder — 用代码实现计划
- reviewer — 审查正确性与安全性
agents.py 中的 run_team(message) 函数是唯一入口点。它返回 {"agent": name, "content": text} 字典组成的列表。
关键约定
- 所有 API 模型都放在
schemas.py— 从那里导入,绝不内联定义 - 智能体定义保留在
agents.py—main.py只导入run_team和AGENT_NAMES - 前端 Vite 配置在开发时将
/api和/ws代理到后端 — 组件中没有硬编码 URL - Pydantic 模型使用
model_dump_json()进行序列化(v2 API)
限制
该资料未提供具体收费、价格、平台支持、性能效果或安装步骤等信息,暂未提供。