03_note_tool_operations.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. NoteTool 基本操作示例
  3. 展示 NoteTool 的核心操作:
  4. 1. 创建笔记 (create)
  5. 2. 读取笔记 (read)
  6. 3. 更新笔记 (update)
  7. 4. 搜索笔记 (search)
  8. 5. 列出笔记 (list)
  9. 6. 笔记摘要 (summary)
  10. 7. 删除笔记 (delete)
  11. """
  12. from hello_agents.tools import NoteTool
  13. import re
  14. def extract_note_id(output: str) -> str:
  15. """从 NoteTool 的输出文本中提取 note_id"""
  16. match = re.search(r"ID:\s*(note_[0-9_]+)", output)
  17. if not match:
  18. raise ValueError(f"无法从输出解析 note_id:\n{output}")
  19. return match.group(1)
  20. def main():
  21. print("=" * 80)
  22. print("NoteTool 基本操作示例")
  23. print("=" * 80 + "\n")
  24. # 初始化 NoteTool
  25. notes = NoteTool(workspace="./project_notes")
  26. # 1. 创建笔记
  27. print("1. 创建笔记...")
  28. create_output_1 = notes.run({
  29. "action": "create",
  30. "title": "重构项目 - 第一阶段",
  31. "content": """## 完成情况
  32. 已完成数据模型层的重构,测试覆盖率达到85%。
  33. ## 下一步
  34. 重构业务逻辑层""",
  35. "note_type": "task_state",
  36. "tags": ["refactoring", "phase1"]
  37. })
  38. print(create_output_1 + "\n")
  39. note_id_1 = extract_note_id(create_output_1)
  40. # 创建第二个笔记
  41. create_output_2 = notes.run({
  42. "action": "create",
  43. "title": "依赖冲突问题",
  44. "content": """## 问题描述
  45. 发现某些第三方库版本不兼容,需要解决。
  46. ## 影响范围
  47. 业务逻辑层的3个模块
  48. ## 下一步
  49. 1. 使用虚拟环境隔离
  50. 2. 锁定版本
  51. 3. 使用 pipdeptree 分析依赖树""",
  52. "note_type": "blocker",
  53. "tags": ["dependency", "urgent"]
  54. })
  55. print(create_output_2 + "\n")
  56. note_id_2 = extract_note_id(create_output_2)
  57. # 2. 读取笔记
  58. print("2. 读取笔记...")
  59. note_detail = notes.run({
  60. "action": "read",
  61. "note_id": note_id_1
  62. })
  63. print(note_detail + "\n")
  64. # 3. 更新笔记
  65. print("3. 更新笔记...")
  66. update_result = notes.run({
  67. "action": "update",
  68. "note_id": note_id_1,
  69. "content": """## 完成情况
  70. 已完成数据模型层的重构,测试覆盖率达到85%。
  71. ## 问题
  72. 遇到依赖版本冲突,已记录到单独笔记。
  73. ## 下一步
  74. 先解决依赖冲突,再继续重构业务逻辑层"""
  75. })
  76. print(update_result + "\n")
  77. # 4. 搜索笔记
  78. print("4. 搜索笔记...")
  79. search_results = notes.run({
  80. "action": "search",
  81. "query": "依赖",
  82. "limit": 5
  83. })
  84. print(search_results + "\n")
  85. # 5. 列出笔记
  86. print("5. 列出所有 blocker 类型的笔记...")
  87. blockers = notes.run({
  88. "action": "list",
  89. "note_type": "blocker",
  90. "limit": 10
  91. })
  92. print(blockers + "\n")
  93. # 6. 笔记摘要
  94. print("6. 生成笔记摘要...")
  95. summary_output = notes.run({
  96. "action": "summary"
  97. })
  98. print(summary_output + "\n")
  99. # 7. 删除笔记 (演示,实际使用时谨慎)
  100. print("7. 删除笔记 (演示)...")
  101. # delete_result = notes.run({
  102. # "action": "delete",
  103. # "note_id": note_id_2
  104. # })
  105. # print(delete_result + "\n")
  106. print(f"(已跳过实际删除操作, 笔记ID: {note_id_2})\n")
  107. print("=" * 80)
  108. print("NoteTool 操作演示完成!")
  109. print("=" * 80)
  110. if __name__ == "__main__":
  111. main()