一个基于MCP架构、采用双LLM策略的AI Agent系统,可以通过自然语言对话生成、修改和修复React前端应用。
┌─────────────────────────────────────────────────────────────────┐ │ AI Agent IDE │ ├──────────────────┬────────────────────┬──────────────────────────┤ │ 🤖 Chat Panel │ 📁 File Tree │ 👁️ Preview Panel │ │ │ + Code Editor │ │ │ • 输入需求 │ │ • 实时页面预览 │ │ • AI响应 │ • 文件浏览 │ • 刷新/新窗口打开 │ │ • 历史记录 │ • 代码查看 │ • 版本历史 │ │ │ │ │ └──────────────────┴────────────────────┴──────────────────────────┘
git clone <repository-url>
cd ai-agent-frontend-generator
cp backend/.env.example backend/.env
nano backend/.env # 填入你的API密钥
编辑 .env 文件:
DEEPSEEK_API_KEY=sk-your-deepseek-key-here GEMINI_API_KEY=your-gemini-key-here
后端:
cd backend
pip install -r requirements.txt
前端:
cd frontend
npm install
启动后端:
cd backend
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000
启动前端(新终端):
cd frontend
npm run dev
打开浏览器访问:http://localhost:3000
API文档:http://localhost:8000/docs
在聊天面板输入:
创建一个计数器,包含增加、减少和重置按钮
系统会自动:
将计数器的按钮改为蓝色背景,增加阴影效果
如果构建失败,系统会:
/workspace ├── backend/ # 后端服务 │ ├── api/ # API路由 │ │ ├── chat.py # 聊天与错误修复 │ │ ├── project.py # 项目状态 │ │ ├── version.py # 版本控制 │ │ ├── download.py # 下载服务 │ │ └── preview.py # 预览代理 │ ├── services/ # 业务逻辑 │ │ ├── llm_service.py # LLM调用 │ │ ├── docker_service.py # Docker操作 │ │ ├── git_service.py # Git操作 │ │ └── workspace_service.py # 工作区管理 │ ├── config.py # 配置管理 │ ├── models.py # 数据模型 │ ├── main.py # 应用入口 │ ├── requirements.txt # Python依赖 │ └── Dockerfile # Docker镜像 │ ├── frontend/ # 前端应用 │ ├── src/ │ │ ├── components/ # React组件 │ │ ├── api/ # API客户端 │ │ ├── store/ # 状态管理 │ │ └── utils/ # 工具函数 │ ├── package.json # Node依赖 │ └── Dockerfile # Docker镜像 │ ├── docs.md # 产品设计文档 ├── ARCHITECTURE.md # 架构文档 ├── DEV_GUIDE.md # 开发指南 ├── USER_GUIDE.md # 使用指南 └── README.md # 本文件
系统采用模型-上下文-协议(MCP)架构,实现清晰的关注点分离:
┌──────────────┐ │ AI Agent │ ← "大脑":理解意图、规划任务 │ (DeepSeek) │ └──────┬───────┘ │ JSON请求 ┌──────▼───────┐ │ MCP Server │ ← "四肢":安全执行具体操作 │ - Docker │ │ - Git │ │ - File Ops │ └──────────────┘
DeepSeek-R1 - 需求分析师
Gemini-Flash-2.5 - 代码工匠
启动后端后访问:http://localhost:8000/docs
| 方法 | 端点 | 描述 |
|---|---|---|
| POST | /api/v1/chat | 发送消息生成代码 |
| POST | /api/v1/fix-error | 修复构建错误 |
| GET | /api/v1/project-state | 获取项目状态 |
| GET | /api/v1/file-content | 获取文件内容 |
| GET | /api/v1/versions | 获取版本历史 |
| POST | /api/v1/checkout | 切换版本 |
| GET | /api/v1/download | 下载项目 |
| GET | /api/v1/preview/:id | 预览页面 |
在 backend/.env 中配置:
# API Keys(必需) DEEPSEEK_API_KEY=sk-your-key GEMINI_API_KEY=your-key # Server Configuration HOST=0.0.0.0 PORT=8000 # Docker Configuration DOCKER_BASE_IMAGE=node:20-alpine VITE_PREVIEW_PORT=4173 # Storage WORKSPACE_DIR=/tmp/ai-agent-workspace
backend/services/ 创建新服务:# backend/services/format_service.py
class FormatService:
async def format_code(session_id: str, file_path: str):
container = docker_service.containers[session_id]
exit_code, output = container.exec_run(
f"npx prettier --write /app/{file_path}"
)
return exit_code == 0
WorkspaceService 中集成:from .format_service import format_service
async def format_project_files(self, session_id: str):
# 实现格式化逻辑
pass
// frontend/src/components/NewComponent.jsx
import React from 'react'
export default function NewComponent({ prop1, prop2 }) {
return (
<div className="new-component">
{/* 组件内容 */}
</div>
)
}
查看后端日志:
cd backend
python -m uvicorn main:app --reload --log-level debug
查看前端网络请求:
测试API端点:
# 健康检查
curl http://localhost:8000/health
# 测试聊天
curl -X POST http://localhost:8000/api/v1/chat \
-H "Content-Type: application/json" \
-d '{"sessionId":"test","prompt":"测试消息"}'
在聊天面板输入你的需求:
创建一个待办事项列表,可以添加、删除和标记完成
系统会自动生成相应的React组件和样式。
将待办事项列表的背景改为浅灰色
构建失败时系统会自动:
在版本控制面板:
为了获得更好的结果,建议:
A: 系统会自动尝试修复。如果多次失败:
A:
A: 构建日志会显示在后端终端窗口中,或者查看:
tail -f /tmp/backend.log
A: 支持所有现代React特性:
A: 当前版本主要生成JavaScript/JSX。可以在提示词中明确要求TypeScript。
A: 可以在工作目录中手动删除对应的session文件夹:
rm -rf /tmp/ai-agent-workspace/session-*
欢迎贡献代码!
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 MIT 许可证 - 详见 LICENSE 文件
Made with ❤️ by AI Agent Team
⭐ 如果觉得这个项目有用,请给个Star!