Code Agent
根据 specs_agent 生成的任务清单自动生成 Agent 项目代码。
功能特性
- 任务清单解析:解析 specs_agent 生成的 Markdown 格式任务清单
- 代码自动生成:根据任务清单和项目规范生成完整的 Agent 项目
- 模板 + AI 混合生成:标准文件使用模板,业务逻辑使用 AI 生成
- 规范约束:支持 steering_agent 提供的项目规范
工作流程
specs_agent 任务清单 + steering_agent 项目规范
↓
code_agent
↓
完整 Agent 项目代码
快速开始
环境变量
# 必需
export OPENAI_API_KEY=your-api-key
# 可选
export OPENAI_BASE_URL=https://litellm.taiji.io/v1
export MODEL_NAME=taiji/gpt-4o-mini
export API_KEY=your-api-key # API 访问密钥
export API_HOST=0.0.0.0
export API_PORT=8000
本地运行
# 安装依赖
pip install -r requirements.txt
# 启动服务
python run_api_server.py
Docker 运行
# 构建镜像
docker build -t code-agent .
# 运行容器
docker run -p 8000:8000 \
-e OPENAI_API_KEY=your-key \
code-agent
API 端点
| 端点 | 方法 | 描述 |
|---|---|---|
/ |
GET | 服务状态 |
/health |
GET | 健康检查 |
/mcp |
POST | MCP JSON-RPC |
/mcp/sse |
GET/POST | MCP SSE 流式 |
/api/v1/parse |
POST | 解析任务清单 |
/api/v1/generate |
POST | 生成完整项目 |
/api/v1/tools |
GET | 获取工具列表 |
MCP 工具
| 工具名称 | 功能 |
|---|---|
parse_task_list |
解析 specs_agent 生成的任务清单 |
generate_project |
根据任务清单生成完整的 Agent 项目 |
使用示例
1. 解析任务清单
curl -X POST http://localhost:8000/api/v1/parse \
-H "Content-Type: application/json" \
-H "api-key: your-key" \
-d '{
"task_list_markdown": "# 任务清单: 数据去重 Agent\n\n## 任务列表\n\n### Task-001: 创建项目结构\n**类型**: 创建文件\n**文件**: `src/__init__.py`\n**依赖**: 无"
}'
2. 生成完整项目
curl -X POST http://localhost:8000/api/v1/generate \
-H "Content-Type: application/json" \
-H "api-key: your-key" \
-d '{
"task_list": "# 任务清单: 数据去重 Agent\n...",
"project_context": "{\"naming_conventions\": {\"files\": \"snake_case\"}}"
}'
3. 完整流程示例
import requests
import json
# 1. 从 specs_agent 获取任务清单
task_list = """
# 任务清单: 数据去重 Agent
## 任务列表
### Task-001: 创建项目目录结构
**类型**: 创建文件
**文件**:
- `src/__init__.py`
- `src/server/__init__.py`
**依赖**: 无
### Task-002: 实现 MCP 工具
**类型**: 创建文件
**文件**: `src/server/mcp_server.py`
**依赖**: Task-001
**描述**: 实现数据去重的 MCP 工具
**代码要点**:
```python
@server.tool()
async def deduplicate(data: List[str]) -> str:
...
执行顺序
- Task-001 → Task-002 """
2. 从 steering_agent 获取项目规范(可选)
project_context = { "naming_conventions": { "files": "snake_case", "classes": "PascalCase", "functions": "snake_case" }, "code_patterns": { "async_required": True, "error_handling": "try-except with JSON response" } }
3. 调用 code_agent 生成项目
response = requests.post( "http://localhost:8000/api/v1/generate", headers={"api-key": "your-key"}, json={ "task_list": task_list, "project_context": json.dumps(project_context) } )
result = response.json()
if result["success"]: print(f"✅ 生成成功!共 {len(result['files'])} 个文件") for file in result["files"]: print(f" - {file['path']} ({file['type']})") else: print(f"❌ 生成失败: {result['error']}")
## 生成结果格式
```json
{
"success": true,
"project_name": "dedup_agent",
"files": [
{
"path": "dedup_agent/src/__init__.py",
"content": "\"\"\"数据去重 Agent 源代码包\"\"\"",
"type": "template",
"task_id": "Task-001"
},
{
"path": "dedup_agent/src/server/mcp_server.py",
"content": "...(完整代码)...",
"type": "ai_generated",
"task_id": "Task-002"
}
],
"tasks_completed": 2,
"tasks_total": 2,
"error": null
}
与其他 Agent 的协作
用户 → specs_agent → 任务清单
↓
用户 → steering_agent → 项目规范
↓
code_agent
↓
完整项目代码
项目结构
code_agent/
├── Dockerfile
├── README.md
├── USAGE.md
├── requirements.txt
├── run_api_server.py
└── src/
├── __init__.py
└── server/
├── __init__.py
├── api_server.py # FastAPI + MCP HTTP
├── mcp_server.py # MCP 工具定义
├── parser.py # 任务清单解析器
├── generator.py # 代码生成器
└── templates/ # 代码模板
├── __init__.py
├── base_templates.py
└── prompts.py
许可证
MIT License