Преглед изворни кода

Update 06_three_day_workflow.py

Tao Sun пре 7 месеци
родитељ
комит
82914339d3
1 измењених фајлова са 90 додато и 77 уклоњено
  1. 90 77
      code/chapter9/06_three_day_workflow.py

+ 90 - 77
code/chapter9/06_three_day_workflow.py

@@ -2,10 +2,11 @@
 CodebaseMaintainer 三天工作流演示
 
 完整展示长程智能体在三天内的工作流程:
-- 第一天: 探索代码库
-- 第二天: 分析代码质量
-- 第三天: 规划重构任务
+- 第一天: 探索代码库(Agent 自主探索)
+- 第二天: 分析代码质量(Agent 自主分析)
+- 第三天: 规划重构任务(Agent 自主规划)
 - 一周后: 检查进度
+
 """
 
 import os
@@ -34,18 +35,27 @@ from codebase_maintainer import CodebaseMaintainer
 
 
 def day_1_exploration(maintainer):
-    """第一天: 探索代码库"""
+    """第一天: 探索代码库(Agentic 方式)
+    
+    在这个阶段,我们只给 Agent 高层次的目标,
+    Agent 会自主决定:
+    - 使用哪些 shell 命令探索代码库
+    - 查看哪些文件
+    - 是否记录笔记
+    """
     print("\n" + "=" * 80)
-    print("第一天: 探索代码库")
+    print("第一天: 探索代码库(Agent 自主探索)")
     print("=" * 80 + "\n")
 
-    # 1. 初步探索
+    # 1. 初步探索 - Agent 自主决定如何探索
     print("### 1. 初步探索项目结构 ###")
+    print("💡 提示:Agent 会自主决定使用哪些命令(如 find, ls, cat)\n")
     response = maintainer.explore()
     print(f"\n助手总结:\n{response[:500]}...\n")
 
-    # 2. 深入分析某个模块
+    # 2. 深入分析某个模块 - Agent 自主决定分析方法
     print("### 2. 分析数据处理模块 ###")
+    print("💡 提示:Agent 会自主决定如何分析这个文件\n")
     response = maintainer.run("请查看 data_processor.py 文件,分析其代码设计")
     print(f"\n助手总结:\n{response[:500]}...\n")
 
@@ -54,18 +64,26 @@ def day_1_exploration(maintainer):
 
 
 def day_2_analysis(maintainer):
-    """第二天: 分析代码质量"""
+    """第二天: 分析代码质量(Agentic 方式)
+    
+    Agent 会自主决定:
+    - 使用什么方法分析代码质量(grep TODO? 统计行数? 检查复杂度?)
+    - 是否需要创建笔记记录问题
+    - 如何组织分析结果
+    """
     print("\n" + "=" * 80)
-    print("第二天: 分析代码质量")
+    print("第二天: 分析代码质量(Agent 自主分析)")
     print("=" * 80 + "\n")
 
-    # 1. 整体质量分析
-    print("### 1. 查找所有 TODO 注释 ###")
+    # 1. 整体质量分析 - Agent 自主决定分析方法
+    print("### 1. 分析代码质量 ###")
+    print("💡 提示:Agent 会自主决定如何分析(如 grep TODO, wc -l, 复杂度分析)\n")
     response = maintainer.analyze()
     print(f"\n助手总结:\n{response[:500]}...\n")
 
-    # 2. 查看具体问题
+    # 2. 查看具体问题 - Agent 自主深入分析
     print("### 2. 分析 API 客户端代码 ###")
+    print("💡 提示:Agent 会自主决定如何分析这个文件的质量\n")
     response = maintainer.run(
         "请分析 api_client.py 的代码质量,特别是错误处理部分,给出改进建议"
     )
@@ -76,43 +94,33 @@ def day_2_analysis(maintainer):
 
 
 def day_3_planning(maintainer):
-    """第三天: 规划重构任务"""
+    """第三天: 规划重构任务(Agentic 方式)
+    
+    Agent 会自主决定:
+    - 回顾哪些历史笔记
+    - 如何组织任务规划
+    - 是否需要创建新的笔记
+    - 如何安排优先级
+    """
     print("\n" + "=" * 80)
-    print("第三天: 规划重构任务")
+    print("第三天: 规划重构任务(Agent 自主规划)")
     print("=" * 80 + "\n")
 
-    # 1. 回顾进度
-    print("### 1. 回顾当前进度 ###")
+    # 1. 回顾进度 - Agent 自主查看历史笔记并规划
+    print("### 1. 回顾当前进度并规划下一步 ###")
+    print("💡 提示:Agent 会自主查看历史笔记,分析当前进度,并制定计划\n")
     response = maintainer.plan_next_steps()
     print(f"\n助手总结:\n{response[:500]}...\n")
 
-    # 2. 手动创建详细的重构计划
-    print("### 2. 创建详细重构计划 ###")
-    maintainer.create_note(
-        title="本周重构计划 - Week 1",
-        content="""## 目标
-完成代码质量改进和 TODO 清理
-
-## 任务清单
-- [ ] 实现 data_processor.py 中的数据验证逻辑
-- [ ] 添加 api_client.py 的重试和错误处理机制
-- [ ] 优化 utils.py 的格式化逻辑
-- [ ] 补充 models.py 的用户验证方法
-
-## 时间安排
-- 周一: 实现数据验证逻辑
-- 周二: 完善错误处理
-- 周三-周四: 优化工具函数
-- 周五: Code Review 和测试
-
-## 风险
-- 新增验证逻辑可能影响现有功能
-- 需要充分的单元测试覆盖
-""",
-        note_type="task_state",
-        tags=["refactoring", "week1", "high_priority"]
+    # 2. 询问 Agent 创建详细计划(Agent 会自主决定是否使用 NoteTool)
+    print("### 2. 让 Agent 创建详细的重构计划 ###")
+    print("💡 提示:Agent 会自主决定如何创建和组织重构计划\n")
+    response = maintainer.run(
+        "请基于我们的分析,创建一个详细的本周重构计划。"
+        "计划应该包括:目标、具体任务清单、时间安排和风险。"
+        "请使用 NoteTool 创建一个 task_state 类型的笔记来记录这个计划。"
     )
-    print("✅ 已创建详细的重构计划\n")
+    print(f"\n助手总结:\n{response[:500]}...\n")
 
     # 模拟时间流逝
     time.sleep(1)
@@ -148,7 +156,8 @@ def demonstrate_cross_session_continuity():
     print("### 第一次会话 (session_1) ###")
     maintainer_1 = CodebaseMaintainer(
         project_name="demo_codebase",
-        codebase_path="./codebase",
+        #实际使用的时候替换代码路径
+        codebase_path="/Users/suntao/Documents/GitHub/hello-agents/code/chapter9/codebase",
         llm=HelloAgentsLLM()
     )
 
@@ -170,7 +179,8 @@ def demonstrate_cross_session_continuity():
     print("### 第二次会话 (session_2) ###")
     maintainer_2 = CodebaseMaintainer(
         project_name="demo_codebase",  # 同一个项目
-        codebase_path="./codebase",
+        #实际使用的时候替换代码路径
+        codebase_path="/Users/suntao/Documents/GitHub/hello-agents/code/chapter9/codebase",
         llm=HelloAgentsLLM()
     )
 
@@ -190,67 +200,70 @@ def demonstrate_cross_session_continuity():
 
 
 def demonstrate_tool_synergy():
-    """演示三大工具的协同"""
+    """演示三大工具的协同(Agentic 方式)
+    
+    在这个演示中:
+    - 我们不再手动调用工具
+    - 而是让 Agent 自主决定使用哪些工具
+    - Agent 会根据任务自动协同使用多个工具
+    """
     print("\n" + "=" * 80)
-    print("演示三大工具的协同")
+    print("演示三大工具的协同(Agent 自主协调)")
     print("=" * 80 + "\n")
 
     maintainer = CodebaseMaintainer(
         project_name="synergy_demo",
-        codebase_path="./codebase",
+        #实际使用的时候替换代码路径
+        codebase_path="/Users/suntao/Documents/GitHub/hello-agents/code/chapter9/codebase",
         llm=HelloAgentsLLM()
     )
 
-    # 1. TerminalTool 发现问题
-    print("### 1. TerminalTool 查找 TODO 注释 ###")
-    todos = maintainer.execute_command("grep -rn 'TODO' --include='*.py' .")
-    print(f"发现的 TODO:\n{todos[:300]}...\n")
-
-    # 2. NoteTool 记录发现
-    print("### 2. NoteTool 记录发现 ###")
-    maintainer.create_note(
-        title="待实现功能清单",
-        content=f"通过代码扫描发现以下待实现功能:\n{todos[:500]}",
-        note_type="conclusion",
-        tags=["todo", "analysis"]
-    )
-    print("✅ 已记录到笔记\n")
-
-    # 3. MemoryTool 存储关键信息 (通过对话)
-    print("### 3. MemoryTool 存储关键信息 ###")
-    response = maintainer.run("代码库中有哪些待实现的功能?")
-    print(f"助手回答:\n{response[:200]}...\n")
-
-    # 4. ContextBuilder 整合所有信息
-    print("### 4. ContextBuilder 整合所有信息 ###")
+    # Agent 自主分析并记录
+    print("### Agent 自主分析代码库中的 TODO 项 ###")
+    print("💡 提示:Agent 会自主决定:\n")
+    print("   1. 使用 TerminalTool 查找 TODO")
+    print("   2. 使用 NoteTool 记录发现")
+    print("   3. 使用 MemoryTool 记住关键信息\n")
+    
     response = maintainer.run(
-        "基于我们的代码分析,应该优先实现哪些 TODO 功能?"
+        "请分析代码库中的所有 TODO 项,并将发现记录到笔记中。"
+        "然后告诉我应该优先实现哪些功能。"
     )
-    print(f"助手回答:\n{response[:300]}...\n")
+    print(f"助手回答:\n{response[:500]}...\n")
 
     # 展示统计信息
     stats = maintainer.get_stats()
     print("📊 工具使用统计:")
+    print(f"  - 工具调用次数: {stats['activity']['tool_calls']}")
     print(f"  - 执行的命令: {stats['activity']['commands_executed']}")
     print(f"  - 创建的笔记: {stats['activity']['notes_created']}")
-    print(f"  - 发现的问题: {stats['activity']['issues_found']}")
 
 
 def main():
     """主函数"""
     print("=" * 80)
-    print("CodebaseMaintainer 三天工作流演示")
+    print("CodebaseMaintainer 三天工作流演示(Agentic 版本)")
     print("=" * 80)
     
-    print("\n💡 使用我们在 chapter9 创建的示例代码库")
+    print("\n✨ 核心特性:Agent 自主决策")
+    print("💡 使用我们在 chapter9 创建的示例代码库")
     print("📁 代码库路径: ./codebase")
-    print("📦 包含文件: data_processor.py, api_client.py, utils.py, models.py\n")
+    print("📦 包含文件: data_processor.py, api_client.py, utils.py, models.py")
+    print("\n🔧 Agent 可用工具:")
+    print("   - TerminalTool: 执行 shell 命令")
+    print("   - NoteTool: 创建和管理笔记")
+    print("   - MemoryTool: 记忆管理")
+    print("\n⚡ Agent 会自主决定:")
+    print("   - 使用哪些工具")
+    print("   - 执行什么命令")
+    print("   - 如何组织信息\n")
 
     # 初始化助手
     maintainer = CodebaseMaintainer(
         project_name="demo_codebase",
-        codebase_path="./codebase",
-        llm=HelloAgentsLLM()
+        #实际使用的时候替换代码路径
+        codebase_path="/Users/suntao/Documents/GitHub/hello-agents/code/chapter9/codebase",
+        llm=HelloAgentsLLM(provider="deepseek",model="deepseek-chat",api_key="sk-1264257bc4114a73998498ad1436f8f0",base_url="https://api.deepseek.com")
     )
 
     # 执行三天工作流