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