06_three_day_workflow.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. CodebaseMaintainer 三天工作流演示
  3. 完整展示长程智能体在三天内的工作流程:
  4. - 第一天: 探索代码库
  5. - 第二天: 分析代码质量
  6. - 第三天: 规划重构任务
  7. - 一周后: 检查进度
  8. """
  9. from hello_agents import HelloAgentsLLM
  10. from datetime import datetime
  11. import json
  12. import time
  13. # 导入 CodebaseMaintainer
  14. import sys
  15. sys.path.append('.')
  16. from codebase_maintainer import CodebaseMaintainer
  17. def day_1_exploration(maintainer):
  18. """第一天: 探索代码库"""
  19. print("\n" + "=" * 80)
  20. print("第一天: 探索代码库")
  21. print("=" * 80 + "\n")
  22. # 1. 初步探索
  23. print("### 1. 初步探索项目结构 ###")
  24. response = maintainer.explore()
  25. print(f"\n助手总结:\n{response[:500]}...\n")
  26. # 2. 深入分析某个模块
  27. print("### 2. 深入分析数据模型 ###")
  28. response = maintainer.run("请分析 app/models/ 目录下的数据模型设计")
  29. print(f"\n助手总结:\n{response[:500]}...\n")
  30. # 模拟时间流逝
  31. time.sleep(1)
  32. def day_2_analysis(maintainer):
  33. """第二天: 分析代码质量"""
  34. print("\n" + "=" * 80)
  35. print("第二天: 分析代码质量")
  36. print("=" * 80 + "\n")
  37. # 1. 整体质量分析
  38. print("### 1. 整体代码质量分析 ###")
  39. response = maintainer.analyze()
  40. print(f"\n助手总结:\n{response[:500]}...\n")
  41. # 2. 查看具体问题
  42. print("### 2. 深入分析问题方法 ###")
  43. response = maintainer.run(
  44. "请查看 order_service.py 的 process_order 方法,给出重构建议"
  45. )
  46. print(f"\n助手总结:\n{response[:500]}...\n")
  47. # 模拟时间流逝
  48. time.sleep(1)
  49. def day_3_planning(maintainer):
  50. """第三天: 规划重构任务"""
  51. print("\n" + "=" * 80)
  52. print("第三天: 规划重构任务")
  53. print("=" * 80 + "\n")
  54. # 1. 回顾进度
  55. print("### 1. 回顾当前进度 ###")
  56. response = maintainer.plan_next_steps()
  57. print(f"\n助手总结:\n{response[:500]}...\n")
  58. # 2. 手动创建详细的重构计划
  59. print("### 2. 创建详细重构计划 ###")
  60. maintainer.create_note(
  61. title="本周重构计划 - Week 1",
  62. content="""## 目标
  63. 完成数据模型层的优化
  64. ## 任务清单
  65. - [ ] 为 User.email 添加唯一约束
  66. - [ ] 为 Order 添加 created_at, updated_at 字段
  67. - [ ] 编写数据库迁移脚本
  68. - [ ] 更新相关测试用例
  69. ## 时间安排
  70. - 周一: 设计迁移脚本
  71. - 周二-周三: 执行迁移并测试
  72. - 周四: 更新测试用例
  73. - 周五: Code Review
  74. ## 风险
  75. - 数据库迁移可能影响线上环境,需要在非高峰期执行
  76. - 现有数据中可能存在重复email,需要先清理
  77. """,
  78. note_type="task_state",
  79. tags=["refactoring", "week1", "high_priority"]
  80. )
  81. print("✅ 已创建详细的重构计划\n")
  82. # 模拟时间流逝
  83. time.sleep(1)
  84. def week_later_review(maintainer):
  85. """一周后: 检查进度"""
  86. print("\n" + "=" * 80)
  87. print("一周后: 检查进度")
  88. print("=" * 80 + "\n")
  89. # 1. 查看笔记摘要
  90. print("### 1. 笔记摘要 ###")
  91. summary = maintainer.note_tool.run({"action": "summary"})
  92. print("📊 笔记摘要:")
  93. print(json.dumps(summary, indent=2, ensure_ascii=False))
  94. print()
  95. # 2. 生成完整报告
  96. print("### 2. 会话报告 ###")
  97. report = maintainer.generate_report()
  98. print("\n📄 会话报告:")
  99. print(json.dumps(report, indent=2, ensure_ascii=False))
  100. def demonstrate_cross_session_continuity():
  101. """演示跨会话的连贯性"""
  102. print("\n" + "=" * 80)
  103. print("演示跨会话的连贯性")
  104. print("=" * 80 + "\n")
  105. # 第一次会话
  106. print("### 第一次会话 (session_1) ###")
  107. maintainer_1 = CodebaseMaintainer(
  108. project_name="my_flask_app",
  109. codebase_path="./my_flask_app",
  110. llm=HelloAgentsLLM()
  111. )
  112. # 创建一些笔记
  113. maintainer_1.create_note(
  114. title="数据模型问题",
  115. content="User.email 缺少唯一约束",
  116. note_type="blocker",
  117. tags=["database", "urgent"]
  118. )
  119. stats_1 = maintainer_1.get_stats()
  120. print(f"会话1统计: {stats_1['activity']}\n")
  121. # 模拟会话结束
  122. time.sleep(1)
  123. # 第二次会话 (新的会话ID,但笔记被保留)
  124. print("### 第二次会话 (session_2) ###")
  125. maintainer_2 = CodebaseMaintainer(
  126. project_name="my_flask_app", # 同一个项目
  127. codebase_path="./my_flask_app",
  128. llm=HelloAgentsLLM()
  129. )
  130. # 检索之前的笔记
  131. response = maintainer_2.run(
  132. "我们之前发现了什么问题?现在应该如何处理?"
  133. )
  134. print(f"\n助手回答:\n{response[:300]}...\n")
  135. stats_2 = maintainer_2.get_stats()
  136. print(f"会话2统计: {stats_2['activity']}\n")
  137. # 展示笔记摘要
  138. summary = maintainer_2.note_tool.run({"action": "summary"})
  139. print("📊 跨会话笔记摘要:")
  140. print(json.dumps(summary, indent=2, ensure_ascii=False))
  141. def demonstrate_tool_synergy():
  142. """演示三大工具的协同"""
  143. print("\n" + "=" * 80)
  144. print("演示三大工具的协同")
  145. print("=" * 80 + "\n")
  146. maintainer = CodebaseMaintainer(
  147. project_name="synergy_demo",
  148. codebase_path="./demo_project",
  149. llm=HelloAgentsLLM()
  150. )
  151. # 1. TerminalTool 发现问题
  152. print("### 1. TerminalTool 发现项目结构 ###")
  153. structure = maintainer.execute_command("ls -la")
  154. print(f"项目结构:\n{structure[:200]}...\n")
  155. # 2. NoteTool 记录发现
  156. print("### 2. NoteTool 记录发现 ###")
  157. maintainer.create_note(
  158. title="项目结构分析",
  159. content=f"项目包含以下主要目录:\n{structure}",
  160. note_type="conclusion",
  161. tags=["structure", "analysis"]
  162. )
  163. print("✅ 已记录到笔记\n")
  164. # 3. MemoryTool 存储关键信息 (通过对话)
  165. print("### 3. MemoryTool 存储关键信息 ###")
  166. response = maintainer.run("项目的主要结构是什么?")
  167. print(f"助手回答:\n{response[:200]}...\n")
  168. # 4. ContextBuilder 整合所有信息
  169. print("### 4. ContextBuilder 整合所有信息 ###")
  170. response = maintainer.run(
  171. "基于我们之前的分析,项目有哪些需要改进的地方?"
  172. )
  173. print(f"助手回答:\n{response[:300]}...\n")
  174. # 展示统计信息
  175. stats = maintainer.get_stats()
  176. print("📊 工具使用统计:")
  177. print(f" - 执行的命令: {stats['activity']['commands_executed']}")
  178. print(f" - 创建的笔记: {stats['activity']['notes_created']}")
  179. print(f" - 发现的问题: {stats['activity']['issues_found']}")
  180. def main():
  181. """主函数"""
  182. print("=" * 80)
  183. print("CodebaseMaintainer 三天工作流演示")
  184. print("=" * 80)
  185. # 初始化助手
  186. maintainer = CodebaseMaintainer(
  187. project_name="my_flask_app",
  188. codebase_path="./my_flask_app",
  189. llm=HelloAgentsLLM()
  190. )
  191. # 执行三天工作流
  192. day_1_exploration(maintainer)
  193. day_2_analysis(maintainer)
  194. day_3_planning(maintainer)
  195. week_later_review(maintainer)
  196. # 额外演示
  197. print("\n\n" + "=" * 80)
  198. print("额外演示")
  199. print("=" * 80)
  200. demonstrate_cross_session_continuity()
  201. demonstrate_tool_synergy()
  202. print("\n" + "=" * 80)
  203. print("完整演示结束!")
  204. print("=" * 80)
  205. if __name__ == "__main__":
  206. main()