This commit is contained in:
2026-03-12 14:16:27 +08:00
parent 27fba7a7cc
commit cf46f77224
3 changed files with 268 additions and 1 deletions

View File

@@ -411,6 +411,74 @@ def display_chat_flow():
""", unsafe_allow_html=True)
def save_generated_files():
"""从对话中提取并保存生成的文件到 workspace 目录"""
workspace_dir = Path("workspace")
workspace_dir.mkdir(parents=True, exist_ok=True)
files_saved = []
# 遍历所有消息,提取文件内容
for msg in st.session_state.messages:
content = msg.get("content", "")
agent_key = msg.get("agent_key", "")
# PM Agent 生成 SRS.md
if agent_key == "PM_Agent" and ("SRS" in content or "软件需求规格" in content):
srs_file = workspace_dir / "SRS.md"
# 提取 SRS 内容(查找包含 FR- 或 NFR- 的段落)
srs_content = content
if "功能性需求" in content:
srs_content = content[content.find("功能性需求"):]
with open(srs_file, 'w', encoding='utf-8') as f:
f.write(f"# 软件需求规格说明书 (SRS)\n\n")
f.write(f"**生成时间**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
f.write(srs_content)
files_saved.append(str(srs_file))
# QA Agent 生成测试文件
if agent_key == "QA_Agent" and ("test_" in content or "def test_" in content):
test_file = workspace_dir / "test_battery_health.py"
# 提取 Python 代码
if "```python" in content:
code = content.split("```python")[1].split("```")[0]
else:
code = content
with open(test_file, 'w', encoding='utf-8') as f:
f.write(f'"""\n电池健康状态测试用例\n生成时间:{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n"""\n\n')
f.write(code)
files_saved.append(str(test_file))
# Dev Agent 生成源代码
if agent_key == "Dev_Agent" and ("def " in content or "class " in content):
src_file = workspace_dir / "src_battery_health.py"
# 提取 Python 代码
if "```python" in content:
code = content.split("```python")[1].split("```")[0]
else:
code = content
with open(src_file, 'w', encoding='utf-8') as f:
f.write(f'"""\n电池健康状态计算模块\n生成时间:{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n"""\n\n')
f.write(code)
files_saved.append(str(src_file))
# Orchestrator 生成最终报告
if agent_key == "Orchestrator" and ("完成" in content and "SDLC" in content):
report_file = workspace_dir / "FINAL_REPORT.md"
with open(report_file, 'w', encoding='utf-8') as f:
f.write(f"# SDLC 项目最终报告\n\n")
f.write(f"**生成时间**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
f.write("## 项目概述\n\n")
f.write("基于 AutoGen 多智能体系统完成的软件交付项目。\n\n")
f.write("## 生成文件列表\n\n")
for i, saved_file in enumerate(files_saved, 1):
f.write(f"{i}. {saved_file}\n")
f.write(f"\n## 项目总结\n\n{content}\n")
files_saved.append(str(report_file))
return files_saved
def create_agents(api_key: str, base_url: str, model: str):
"""创建所有 Agent"""
llm_config = get_llm_config(model=model, api_key=api_key, base_url=base_url)
@@ -525,6 +593,34 @@ def main():
file_name=f"agent_chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
st.divider()
# 显示生成的文件
st.subheader("📁 生成的文件")
workspace_dir = Path("workspace")
if workspace_dir.exists():
files = list(workspace_dir.glob("*"))
if files:
for file in files:
if file.is_file():
with st.expander(f"📄 {file.name}"):
try:
content = file.read_text(encoding='utf-8')
st.code(content[:500] + ("..." if len(content) > 500 else ""))
st.download_button(
label="⬇️ 下载",
data=content,
file_name=file.name,
mime="text/plain",
key=f"download_{file.name}"
)
except Exception as e:
st.error(f"读取失败:{e}")
else:
st.info("工作目录为空,请先运行工作流")
else:
st.info("工作目录不存在")
# 主界面
display_agent_status_row()
@@ -631,7 +727,15 @@ def main():
# 完成
add_message("Orchestrator", "✅ SDLC 流程完成!所有任务已完成。", "总结完成")
progress_placeholder.success("✅ SDLC 工作流完成!查看上方的对话流了解详情。")
# 保存生成的文件
progress_placeholder.info("💾 正在保存生成的文件...")
saved_files = save_generated_files()
if saved_files:
progress_placeholder.success(f"✅ SDLC 工作流完成!已保存 {len(saved_files)} 个文件到 workspace/ 目录")
else:
progress_placeholder.success("✅ SDLC 工作流完成!查看上方的对话流了解详情。")
st.session_state.is_running = False
st.rerun()