293 lines
7.7 KiB
Python
293 lines
7.7 KiB
Python
|
|
"""
|
|||
|
|
Orchestrator Agent - 协调器智能体
|
|||
|
|
负责流程调度与最终验证
|
|||
|
|
"""
|
|||
|
|
from autogen import AssistantAgent
|
|||
|
|
from typing import Dict, Any, Optional, List
|
|||
|
|
import os
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
from config.llm_config import get_agent_llm_config, ORCH_PROMPT
|
|||
|
|
|
|||
|
|
|
|||
|
|
class OrchestratorAgent:
|
|||
|
|
"""协调器 Agent,负责多智能体协同和流程控制"""
|
|||
|
|
|
|||
|
|
def __init__(self, llm_config: Optional[Dict] = None):
|
|||
|
|
"""
|
|||
|
|
初始化 Orchestrator Agent
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
llm_config: LLM 配置
|
|||
|
|
"""
|
|||
|
|
self.llm_config = llm_config or get_agent_llm_config("Orchestrator")
|
|||
|
|
|
|||
|
|
self.agent = AssistantAgent(
|
|||
|
|
name="Orchestrator",
|
|||
|
|
system_message=ORCH_PROMPT,
|
|||
|
|
llm_config=self.llm_config,
|
|||
|
|
description="多智能体系统协调器",
|
|||
|
|
human_input_mode="NEVER"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
self.workspace_dir = Path("workspace")
|
|||
|
|
self.workspace_dir.mkdir(exist_ok=True)
|
|||
|
|
|
|||
|
|
# 流程状态
|
|||
|
|
self.workflow_state: Dict[str, Any] = {
|
|||
|
|
"current_step": 0,
|
|||
|
|
"total_steps": 5,
|
|||
|
|
"status": "pending",
|
|||
|
|
"artifacts": {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def start_workflow(self, user_requirement: str) -> Dict[str, Any]:
|
|||
|
|
"""
|
|||
|
|
启动完整的工作流程
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
user_requirement: 用户需求
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
工作流状态
|
|||
|
|
"""
|
|||
|
|
self.workflow_state = {
|
|||
|
|
"current_step": 1,
|
|||
|
|
"total_steps": 5,
|
|||
|
|
"status": "in_progress",
|
|||
|
|
"user_requirement": user_requirement,
|
|||
|
|
"artifacts": {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return self.workflow_state
|
|||
|
|
|
|||
|
|
def validate_srs(self, srs_content: str) -> Dict[str, Any]:
|
|||
|
|
"""
|
|||
|
|
验证 SRS 文档的完整性
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
srs_content: SRS 文档内容
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
验证结果
|
|||
|
|
"""
|
|||
|
|
prompt = f"""
|
|||
|
|
请验证以下 SRS 文档的完整性:
|
|||
|
|
|
|||
|
|
{self._truncate(srs_content, 3000)}
|
|||
|
|
|
|||
|
|
检查清单:
|
|||
|
|
1. ✅ 包含功能性需求列表
|
|||
|
|
2. ✅ 包含非功能性需求
|
|||
|
|
3. ✅ 包含验收标准
|
|||
|
|
4. ✅ 包含风险分析
|
|||
|
|
5. ✅ 需求具有唯一 ID
|
|||
|
|
|
|||
|
|
请输出验证报告,指出缺失或不完整的部分。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
response = self.agent.generate_reply(
|
|||
|
|
messages=[{"role": "user", "content": prompt}]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
validation_report = response if isinstance(response, str) else str(response)
|
|||
|
|
|
|||
|
|
# 保存验证报告
|
|||
|
|
report_file = self.workspace_dir / "srs_validation.md"
|
|||
|
|
with open(report_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(validation_report)
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
"valid": "✅" in validation_report and "❌" not in validation_report,
|
|||
|
|
"report": validation_report,
|
|||
|
|
"file": str(report_file)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def validate_tests(self, test_code: str, srs_content: str) -> Dict[str, Any]:
|
|||
|
|
"""
|
|||
|
|
验证测试用例的覆盖率
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
test_code: 测试代码
|
|||
|
|
srs_content: SRS 文档
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
验证结果
|
|||
|
|
"""
|
|||
|
|
prompt = f"""
|
|||
|
|
请验证测试用例是否覆盖了所有 SRS 需求:
|
|||
|
|
|
|||
|
|
【SRS 需求】
|
|||
|
|
{self._truncate(srs_content, 2000)}
|
|||
|
|
|
|||
|
|
【测试代码】
|
|||
|
|
{self._truncate(test_code, 2000)}
|
|||
|
|
|
|||
|
|
检查清单:
|
|||
|
|
1. ✅ 每个功能需求都有对应测试
|
|||
|
|
2. ✅ 包含边界情况测试
|
|||
|
|
3. ✅ 包含异常场景测试
|
|||
|
|
4. ✅ 测试可执行且独立
|
|||
|
|
5. ✅ 遵循 TDD 原则
|
|||
|
|
|
|||
|
|
请输出验证报告。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
response = self.agent.generate_reply(
|
|||
|
|
messages=[{"role": "user", "content": prompt}]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
validation_report = response if isinstance(response, str) else str(response)
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
"valid": "✅" in validation_report,
|
|||
|
|
"report": validation_report
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def validate_code(self, code: str, srs_content: str, test_result: Dict) -> Dict[str, Any]:
|
|||
|
|
"""
|
|||
|
|
验证代码质量
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
code: 源代码
|
|||
|
|
srs_content: SRS 文档
|
|||
|
|
test_result: 测试结果
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
验证结果
|
|||
|
|
"""
|
|||
|
|
prompt = f"""
|
|||
|
|
请验证代码质量:
|
|||
|
|
|
|||
|
|
【SRS 需求】
|
|||
|
|
{self._truncate(srs_content, 1500)}
|
|||
|
|
|
|||
|
|
【代码】
|
|||
|
|
{self._truncate(code, 2000)}
|
|||
|
|
|
|||
|
|
【测试结果】
|
|||
|
|
{test_result}
|
|||
|
|
|
|||
|
|
检查清单:
|
|||
|
|
1. ✅ 实现所有功能需求
|
|||
|
|
2. ✅ 通过所有测试用例
|
|||
|
|
3. ✅ 代码符合规范(MISRA-C/PEP8)
|
|||
|
|
4. ✅ 包含完整文档
|
|||
|
|
5. ✅ 无安全漏洞
|
|||
|
|
6. ✅ 性能满足要求
|
|||
|
|
|
|||
|
|
请输出代码质量验证报告。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
response = self.agent.generate_reply(
|
|||
|
|
messages=[{"role": "user", "content": prompt}]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
validation_report = response if isinstance(response, str) else str(response)
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
"valid": test_result.get("success", False) and "✅" in validation_report,
|
|||
|
|
"report": validation_report
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def generate_final_report(self) -> str:
|
|||
|
|
"""
|
|||
|
|
生成最终项目总结报告
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
最终报告内容
|
|||
|
|
"""
|
|||
|
|
prompt = """
|
|||
|
|
请生成项目最终总结报告,包含:
|
|||
|
|
|
|||
|
|
1. 项目概述
|
|||
|
|
2. 交付物清单:
|
|||
|
|
- SRS 文档
|
|||
|
|
- 测试用例
|
|||
|
|
- 源代码
|
|||
|
|
3. 质量指标:
|
|||
|
|
- 测试覆盖率
|
|||
|
|
- 代码质量评分
|
|||
|
|
4. 合规性说明:
|
|||
|
|
- ISO 26262
|
|||
|
|
- MISRA-C
|
|||
|
|
- ASPICE
|
|||
|
|
5. 后续建议
|
|||
|
|
|
|||
|
|
请基于 workspace 目录下的所有文件生成完整报告。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
response = self.agent.generate_reply(
|
|||
|
|
messages=[{"role": "user", "content": prompt}]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
final_report = response if isinstance(response, str) else str(response)
|
|||
|
|
|
|||
|
|
# 保存最终报告
|
|||
|
|
report_file = self.workspace_dir / "FINAL_REPORT.md"
|
|||
|
|
with open(report_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(final_report)
|
|||
|
|
|
|||
|
|
print(f"✅ 最终报告已生成:{report_file}")
|
|||
|
|
return final_report
|
|||
|
|
|
|||
|
|
def request_human_approval(
|
|||
|
|
self,
|
|||
|
|
approval_type: str,
|
|||
|
|
description: str,
|
|||
|
|
data: Dict[str, Any]
|
|||
|
|
) -> bool:
|
|||
|
|
"""
|
|||
|
|
请求人工确认(需要前端配合实现)
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
approval_type: 确认类型
|
|||
|
|
description: 确认描述
|
|||
|
|
data: 相关数据
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
用户是否批准
|
|||
|
|
"""
|
|||
|
|
# 这里只是标记,实际的前端交互由 Streamlit 处理
|
|||
|
|
print(f"\n⚠️ 需要人工确认:{description}")
|
|||
|
|
print(f"类型:{approval_type}")
|
|||
|
|
print(f"数据:{data}\n")
|
|||
|
|
|
|||
|
|
# 在命令行模式下,可以简单询问用户
|
|||
|
|
# 在 GUI 模式下,这会触发前端弹窗
|
|||
|
|
try:
|
|||
|
|
response = input("是否批准?(y/n): ").lower()
|
|||
|
|
return response == 'y'
|
|||
|
|
except:
|
|||
|
|
# 非交互模式下默认批准
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
def _truncate(self, text: str, max_length: int) -> str:
|
|||
|
|
"""截断文本"""
|
|||
|
|
if len(text) <= max_length:
|
|||
|
|
return text
|
|||
|
|
return text[:max_length] + "... [内容已截断]"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def create_orchestrator_agent(llm_config: Optional[Dict] = None) -> AssistantAgent:
|
|||
|
|
"""
|
|||
|
|
创建 Orchestrator Agent(AutoGen 原生格式)
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
llm_config: LLM 配置
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
AutoGen AssistantAgent 实例
|
|||
|
|
"""
|
|||
|
|
config = llm_config or get_agent_llm_config("Orchestrator")
|
|||
|
|
|
|||
|
|
agent = AssistantAgent(
|
|||
|
|
name="Orchestrator",
|
|||
|
|
system_message=ORCH_PROMPT,
|
|||
|
|
llm_config=config,
|
|||
|
|
description="多智能体系统协调器",
|
|||
|
|
human_input_mode="NEVER"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
return agent
|