note_tool.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. """NoteTool - 结构化笔记工具
  2. 为Agent提供结构化笔记能力,支持:
  3. - 创建/读取/更新/删除笔记
  4. - 按类型组织(任务状态、结论、阻塞项、行动计划等)
  5. - 持久化存储(Markdown格式,带YAML前置元数据)
  6. - 搜索与过滤
  7. - 与MemoryTool集成(可选)
  8. 使用场景:
  9. - 长时程任务的状态跟踪
  10. - 关键结论与依赖记录
  11. - 待办事项与行动计划
  12. - 项目知识沉淀
  13. 笔记格式示例:
  14. ```markdown
  15. ---
  16. id: note_20250118_120000_0
  17. title: 项目进展
  18. type: task_state
  19. tags: [milestone, phase1]
  20. created_at: 2025-01-18T12:00:00
  21. updated_at: 2025-01-18T12:00:00
  22. ---
  23. # 项目进展
  24. 已完成需求分析,下一步:设计方案
  25. ## 关键里程碑
  26. - [x] 需求收集
  27. - [ ] 方案设计
  28. ```
  29. """
  30. from typing import Dict, Any, List, Optional
  31. from datetime import datetime
  32. from pathlib import Path
  33. import json
  34. import os
  35. import re
  36. from ..base import Tool, ToolParameter
  37. class NoteTool(Tool):
  38. """笔记工具
  39. 为Agent提供结构化笔记管理能力,支持多种笔记类型:
  40. - task_state: 任务状态
  41. - conclusion: 关键结论
  42. - blocker: 阻塞项
  43. - action: 行动计划
  44. - reference: 参考资料
  45. - general: 通用笔记
  46. 用法示例:
  47. ```python
  48. note_tool = NoteTool(workspace="./project_notes")
  49. # 创建笔记
  50. note_tool.run({
  51. "action": "create",
  52. "title": "项目进展",
  53. "content": "已完成需求分析,下一步:设计方案",
  54. "note_type": "task_state",
  55. "tags": ["milestone", "phase1"]
  56. })
  57. # 读取笔记
  58. notes = note_tool.run({"action": "list", "note_type": "task_state"})
  59. ```
  60. """
  61. def __init__(
  62. self,
  63. workspace: str = "./notes",
  64. auto_backup: bool = True,
  65. max_notes: int = 1000
  66. ):
  67. super().__init__(
  68. name="note",
  69. description="笔记工具 - 创建、读取、更新、删除结构化笔记,支持任务状态、结论、阻塞项等类型"
  70. )
  71. self.workspace = Path(workspace)
  72. self.auto_backup = auto_backup
  73. self.max_notes = max_notes
  74. # 确保工作目录存在
  75. self.workspace.mkdir(parents=True, exist_ok=True)
  76. # 笔记索引文件
  77. self.index_file = self.workspace / "notes_index.json"
  78. self._load_index()
  79. def _load_index(self):
  80. """加载笔记索引"""
  81. if self.index_file.exists():
  82. with open(self.index_file, 'r', encoding='utf-8') as f:
  83. self.notes_index = json.load(f)
  84. else:
  85. self.notes_index = {
  86. "notes": [],
  87. "metadata": {
  88. "created_at": datetime.now().isoformat(),
  89. "total_notes": 0
  90. }
  91. }
  92. self._save_index()
  93. def _save_index(self):
  94. """保存笔记索引"""
  95. with open(self.index_file, 'w', encoding='utf-8') as f:
  96. json.dump(self.notes_index, f, ensure_ascii=False, indent=2)
  97. def _generate_note_id(self) -> str:
  98. """生成笔记ID"""
  99. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  100. count = len(self.notes_index["notes"])
  101. return f"note_{timestamp}_{count}"
  102. def _get_note_path(self, note_id: str) -> Path:
  103. """获取笔记文件路径"""
  104. return self.workspace / f"{note_id}.md"
  105. def _note_to_markdown(self, note: Dict[str, Any]) -> str:
  106. """将笔记对象转换为Markdown格式"""
  107. # YAML前置元数据
  108. frontmatter = "---\n"
  109. frontmatter += f"id: {note['id']}\n"
  110. frontmatter += f"title: {note['title']}\n"
  111. frontmatter += f"type: {note['type']}\n"
  112. if note.get('tags'):
  113. tags_str = json.dumps(note['tags'])
  114. frontmatter += f"tags: {tags_str}\n"
  115. frontmatter += f"created_at: {note['created_at']}\n"
  116. frontmatter += f"updated_at: {note['updated_at']}\n"
  117. frontmatter += "---\n\n"
  118. # Markdown内容
  119. content = f"# {note['title']}\n\n"
  120. content += note['content']
  121. return frontmatter + content
  122. def _markdown_to_note(self, markdown_text: str) -> Dict[str, Any]:
  123. """将Markdown文本解析为笔记对象"""
  124. # 提取YAML前置元数据
  125. frontmatter_match = re.match(r'^---\s*\n(.*?)\n---\s*\n', markdown_text, re.DOTALL)
  126. if not frontmatter_match:
  127. raise ValueError("无效的笔记格式:缺少YAML前置元数据")
  128. frontmatter_text = frontmatter_match.group(1)
  129. content_start = frontmatter_match.end()
  130. # 解析YAML(简化版)
  131. note = {}
  132. for line in frontmatter_text.split('\n'):
  133. if ':' in line:
  134. key, value = line.split(':', 1)
  135. key = key.strip()
  136. value = value.strip()
  137. # 处理特殊字段
  138. if key == 'tags':
  139. try:
  140. note[key] = json.loads(value)
  141. except:
  142. note[key] = []
  143. else:
  144. note[key] = value
  145. # 提取内容(去掉标题行)
  146. markdown_content = markdown_text[content_start:].strip()
  147. # 移除第一行的 # 标题
  148. lines = markdown_content.split('\n')
  149. if lines and lines[0].startswith('# '):
  150. markdown_content = '\n'.join(lines[1:]).strip()
  151. note['content'] = markdown_content
  152. # 添加元数据
  153. note['metadata'] = {
  154. 'word_count': len(markdown_content),
  155. 'status': 'active'
  156. }
  157. return note
  158. def run(self, parameters: Dict[str, Any]) -> str:
  159. """执行工具"""
  160. if not self.validate_parameters(parameters):
  161. return "❌ 参数验证失败"
  162. action = parameters.get("action")
  163. if action == "create":
  164. return self._create_note(parameters)
  165. elif action == "read":
  166. return self._read_note(parameters)
  167. elif action == "update":
  168. return self._update_note(parameters)
  169. elif action == "delete":
  170. return self._delete_note(parameters)
  171. elif action == "list":
  172. return self._list_notes(parameters)
  173. elif action == "search":
  174. return self._search_notes(parameters)
  175. elif action == "summary":
  176. return self._get_summary()
  177. else:
  178. return f"❌ 不支持的操作: {action}"
  179. def get_parameters(self) -> List[ToolParameter]:
  180. """获取工具参数定义"""
  181. return [
  182. ToolParameter(
  183. name="action",
  184. type="string",
  185. description=(
  186. "操作类型: create(创建), read(读取), update(更新), "
  187. "delete(删除), list(列表), search(搜索), summary(摘要)"
  188. ),
  189. required=True
  190. ),
  191. ToolParameter(
  192. name="title",
  193. type="string",
  194. description="笔记标题(create/update时必需)",
  195. required=False
  196. ),
  197. ToolParameter(
  198. name="content",
  199. type="string",
  200. description="笔记内容(create/update时必需)",
  201. required=False
  202. ),
  203. ToolParameter(
  204. name="note_type",
  205. type="string",
  206. description=(
  207. "笔记类型: task_state(任务状态), conclusion(结论), "
  208. "blocker(阻塞项), action(行动计划), reference(参考), general(通用)"
  209. ),
  210. required=False,
  211. default="general"
  212. ),
  213. ToolParameter(
  214. name="tags",
  215. type="array",
  216. description="标签列表(可选)",
  217. required=False
  218. ),
  219. ToolParameter(
  220. name="note_id",
  221. type="string",
  222. description="笔记ID(read/update/delete时必需)",
  223. required=False
  224. ),
  225. ToolParameter(
  226. name="query",
  227. type="string",
  228. description="搜索关键词(search时必需)",
  229. required=False
  230. ),
  231. ToolParameter(
  232. name="limit",
  233. type="integer",
  234. description="返回结果数量限制(默认10)",
  235. required=False,
  236. default=10
  237. ),
  238. ]
  239. def _create_note(self, params: Dict[str, Any]) -> str:
  240. """创建笔记"""
  241. title = params.get("title")
  242. content = params.get("content")
  243. note_type = params.get("note_type", "general")
  244. tags = params.get("tags", [])
  245. if not title or not content:
  246. return "❌ 创建笔记需要提供 title 和 content"
  247. # 检查笔记数量限制
  248. if len(self.notes_index["notes"]) >= self.max_notes:
  249. return f"❌ 笔记数量已达上限 ({self.max_notes})"
  250. # 生成笔记ID
  251. note_id = self._generate_note_id()
  252. # 创建笔记对象
  253. note = {
  254. "id": note_id,
  255. "title": title,
  256. "content": content,
  257. "type": note_type,
  258. "tags": tags if isinstance(tags, list) else [],
  259. "created_at": datetime.now().isoformat(),
  260. "updated_at": datetime.now().isoformat(),
  261. "metadata": {
  262. "word_count": len(content),
  263. "status": "active"
  264. }
  265. }
  266. # 保存笔记文件(Markdown格式)
  267. note_path = self._get_note_path(note_id)
  268. markdown_content = self._note_to_markdown(note)
  269. with open(note_path, 'w', encoding='utf-8') as f:
  270. f.write(markdown_content)
  271. # 更新索引
  272. self.notes_index["notes"].append({
  273. "id": note_id,
  274. "title": title,
  275. "type": note_type,
  276. "tags": tags if isinstance(tags, list) else [],
  277. "created_at": note["created_at"]
  278. })
  279. self.notes_index["metadata"]["total_notes"] = len(self.notes_index["notes"])
  280. self._save_index()
  281. return f"✅ 笔记创建成功\nID: {note_id}\n标题: {title}\n类型: {note_type}"
  282. def _read_note(self, params: Dict[str, Any]) -> str:
  283. """读取笔记"""
  284. note_id = params.get("note_id")
  285. if not note_id:
  286. return "❌ 读取笔记需要提供 note_id"
  287. note_path = self._get_note_path(note_id)
  288. if not note_path.exists():
  289. return f"❌ 笔记不存在: {note_id}"
  290. with open(note_path, 'r', encoding='utf-8') as f:
  291. markdown_text = f.read()
  292. note = self._markdown_to_note(markdown_text)
  293. return self._format_note(note)
  294. def _update_note(self, params: Dict[str, Any]) -> str:
  295. """更新笔记"""
  296. note_id = params.get("note_id")
  297. if not note_id:
  298. return "❌ 更新笔记需要提供 note_id"
  299. note_path = self._get_note_path(note_id)
  300. if not note_path.exists():
  301. return f"❌ 笔记不存在: {note_id}"
  302. # 读取现有笔记
  303. with open(note_path, 'r', encoding='utf-8') as f:
  304. markdown_text = f.read()
  305. note = self._markdown_to_note(markdown_text)
  306. # 更新字段
  307. if "title" in params:
  308. note["title"] = params["title"]
  309. if "content" in params:
  310. note["content"] = params["content"]
  311. note["metadata"]["word_count"] = len(params["content"])
  312. if "note_type" in params:
  313. note["type"] = params["note_type"]
  314. if "tags" in params:
  315. note["tags"] = params["tags"] if isinstance(params["tags"], list) else []
  316. note["updated_at"] = datetime.now().isoformat()
  317. # 保存更新(Markdown格式)
  318. markdown_content = self._note_to_markdown(note)
  319. with open(note_path, 'w', encoding='utf-8') as f:
  320. f.write(markdown_content)
  321. # 更新索引
  322. for idx_note in self.notes_index["notes"]:
  323. if idx_note["id"] == note_id:
  324. idx_note["title"] = note["title"]
  325. idx_note["type"] = note["type"]
  326. idx_note["tags"] = note["tags"]
  327. break
  328. self._save_index()
  329. return f"✅ 笔记更新成功: {note_id}"
  330. def _delete_note(self, params: Dict[str, Any]) -> str:
  331. """删除笔记"""
  332. note_id = params.get("note_id")
  333. if not note_id:
  334. return "❌ 删除笔记需要提供 note_id"
  335. note_path = self._get_note_path(note_id)
  336. if not note_path.exists():
  337. return f"❌ 笔记不存在: {note_id}"
  338. # 删除文件
  339. note_path.unlink()
  340. # 更新索引
  341. self.notes_index["notes"] = [
  342. n for n in self.notes_index["notes"] if n["id"] != note_id
  343. ]
  344. self.notes_index["metadata"]["total_notes"] = len(self.notes_index["notes"])
  345. self._save_index()
  346. return f"✅ 笔记已删除: {note_id}"
  347. def _list_notes(self, params: Dict[str, Any]) -> str:
  348. """列出笔记"""
  349. note_type = params.get("note_type")
  350. limit = params.get("limit", 10)
  351. # 过滤笔记
  352. filtered_notes = self.notes_index["notes"]
  353. if note_type:
  354. filtered_notes = [n for n in filtered_notes if n["type"] == note_type]
  355. # 限制数量
  356. filtered_notes = filtered_notes[:limit]
  357. if not filtered_notes:
  358. return "📝 暂无笔记"
  359. result = f"📝 笔记列表(共 {len(filtered_notes)} 条)\n\n"
  360. for note in filtered_notes:
  361. result += f"• [{note['type']}] {note['title']}\n"
  362. result += f" ID: {note['id']}\n"
  363. if note.get('tags'):
  364. result += f" 标签: {', '.join(note['tags'])}\n"
  365. result += f" 创建时间: {note['created_at']}\n\n"
  366. return result
  367. def _search_notes(self, params: Dict[str, Any]) -> str:
  368. """搜索笔记"""
  369. query = params.get("query", "").lower()
  370. limit = params.get("limit", 10)
  371. if not query:
  372. return "❌ 搜索需要提供 query"
  373. # 搜索匹配的笔记
  374. matched_notes = []
  375. for idx_note in self.notes_index["notes"]:
  376. note_path = self._get_note_path(idx_note["id"])
  377. if note_path.exists():
  378. with open(note_path, 'r', encoding='utf-8') as f:
  379. markdown_text = f.read()
  380. try:
  381. note = self._markdown_to_note(markdown_text)
  382. except Exception as e:
  383. print(f"⚠️ 解析笔记失败 {idx_note['id']}: {e}")
  384. continue
  385. # 检查标题、内容、标签是否匹配
  386. if (query in note["title"].lower() or
  387. query in note["content"].lower() or
  388. any(query in tag.lower() for tag in note.get("tags", []))):
  389. matched_notes.append(note)
  390. # 限制数量
  391. matched_notes = matched_notes[:limit]
  392. if not matched_notes:
  393. return f"📝 未找到匹配 '{query}' 的笔记"
  394. result = f"🔍 搜索结果(共 {len(matched_notes)} 条)\n\n"
  395. for note in matched_notes:
  396. result += self._format_note(note, compact=True) + "\n"
  397. return result
  398. def _get_summary(self) -> str:
  399. """获取笔记摘要"""
  400. total = len(self.notes_index["notes"])
  401. # 按类型统计
  402. type_counts = {}
  403. for note in self.notes_index["notes"]:
  404. note_type = note["type"]
  405. type_counts[note_type] = type_counts.get(note_type, 0) + 1
  406. result = f"📊 笔记摘要\n\n"
  407. result += f"总笔记数: {total}\n\n"
  408. result += "按类型统计:\n"
  409. for note_type, count in sorted(type_counts.items()):
  410. result += f" • {note_type}: {count}\n"
  411. return result
  412. def _format_note(self, note: Dict[str, Any], compact: bool = False) -> str:
  413. """格式化笔记输出"""
  414. if compact:
  415. return (
  416. f"[{note['type']}] {note['title']}\n"
  417. f"ID: {note['id']}\n"
  418. f"内容: {note['content'][:100]}{'...' if len(note['content']) > 100 else ''}"
  419. )
  420. else:
  421. result = f"📝 笔记详情\n\n"
  422. result += f"ID: {note['id']}\n"
  423. result += f"标题: {note['title']}\n"
  424. result += f"类型: {note['type']}\n"
  425. if note.get('tags'):
  426. result += f"标签: {', '.join(note['tags'])}\n"
  427. result += f"创建时间: {note['created_at']}\n"
  428. result += f"更新时间: {note['updated_at']}\n"
  429. result += f"\n内容:\n{note['content']}\n"
  430. return result