Files
crewai/agents/dev_agent.py
ZhuJW 8584821f36 fix
2026-03-13 20:53:44 +08:00

145 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Dev Agent - 开发工程师智能体
负责根据 SRS 和测试用例编写业务代码
"""
from typing import Dict, Any
from crewai import Agent, Task
from models.qwen_config import get_llm
def create_dev_agent() -> Agent:
"""
创建开发工程师智能体
Returns:
Agent: Dev 智能体实例
"""
return Agent(
role="全栈软件工程师",
goal="根据软件需求规格说明书 (SRS) 和测试用例实现高质量的业务代码",
backstory="""你是一位拥有 10 年经验的全栈软件工程师,擅长:
1. 快速理解需求并设计合理的系统架构
2. 编写清晰、可维护、高性能的代码
3. 遵循 TDD测试驱动开发实践
你的代码实现必须通过所有测试用例,并满足性能和安全要求。""",
verbose=True,
allow_delegation=False,
llm=get_llm()
)
def create_dev_task(srs_document: str, test_plan: str) -> Task:
"""
创建代码实现任务
Args:
srs_document: 软件需求规格说明书
test_plan: 测试方案和测试用例
Returns:
Task: Dev 任务实例
"""
return Task(
description=f"""
请根据以下 SRS 文档和测试方案,实现完整的业务代码:
【SRS 文档】
{srs_document}
【测试方案】
{test_plan}
【输出要求】
请按照以下结构输出代码实现:
## 1. 项目结构设计
- 1.1 目录结构
- 1.2 模块划分
- 1.3 依赖关系
## 2. 核心业务代码
实现所有功能性需求,包含:
```python
# 示例:主业务逻辑
class <ServiceName>:
\"\"\"<服务>业务逻辑类\"\"\"
def __init__(self):
# 初始化
def <method_name>(self, params) -> ReturnType:
\"\"\"
方法描述
Args:
params: 参数说明
Returns:
返回值说明
\"\"\"
# 实现逻辑
pass
```
## 3. 数据模型定义
- 3.1 Pydantic 模型
- 3.2 数据库模型(如需要)
- 3.3 DTO/VO 对象
## 4. API 接口定义(如适用)
- 4.1 RESTful 端点
- 4.2 请求/响应格式
- 4.3 错误处理
## 5. 配置文件
- 5.1 环境变量配置
- 5.2 日志配置
- 5.3 其他配置项
## 6. 使用说明
- 6.1 安装步骤
- 6.2 运行命令
- 6.3 配置说明
【注意事项】
- 遵循 PEP 8 编码规范
- 使用类型提示 (Type Hints)
- 包含必要的文档字符串
- 实现适当的错误处理
- 确保代码可通过所有测试用例
""",
expected_output="完整的业务代码实现和使用说明",
agent=create_dev_agent()
)
def execute_dev_stage(srs_document: str, test_plan: str) -> Dict[str, Any]:
"""
执行 Dev 阶段任务
Args:
srs_document: 软件需求规格说明书
test_plan: 测试方案和测试用例
Returns:
Dict[str, Any]: 包含代码实现和执行结果
"""
dev_agent = create_dev_agent()
dev_task = create_dev_task(srs_document, test_plan)
crew = Crew(
agents=[dev_agent],
tasks=[dev_task],
verbose=True
)
result = crew.kickoff()
return {
"stage": "代码实现",
"implementation": result.raw,
"status": "completed"
}