protocols.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Storage Protocols - 存储接口定义
  3. 使用 Protocol 定义接口,允许不同的存储实现(内存、PostgreSQL、Neo4j 等)
  4. TraceStore 已移动到 agent.execution.protocols
  5. """
  6. from typing import Protocol, List, Optional, Dict, Any, runtime_checkable
  7. from agent.memory.models import Experience, Skill
  8. @runtime_checkable
  9. class MemoryStore(Protocol):
  10. """Experience + Skill 存储接口"""
  11. # ===== Experience 操作 =====
  12. async def add_experience(self, exp: Experience) -> str:
  13. """添加 Experience"""
  14. ...
  15. async def get_experience(self, exp_id: str) -> Optional[Experience]:
  16. """获取 Experience"""
  17. ...
  18. async def search_experiences(
  19. self,
  20. scope: str,
  21. context: str,
  22. limit: int = 10
  23. ) -> List[Experience]:
  24. """
  25. 搜索相关 Experience
  26. Args:
  27. scope: 范围(如 "agent:researcher")
  28. context: 当前上下文,用于语义匹配
  29. limit: 最大返回数量
  30. """
  31. ...
  32. async def update_experience_stats(
  33. self,
  34. exp_id: str,
  35. success: bool
  36. ) -> None:
  37. """更新 Experience 使用统计"""
  38. ...
  39. # ===== Skill 操作 =====
  40. async def add_skill(self, skill: Skill) -> str:
  41. """添加 Skill"""
  42. ...
  43. async def get_skill(self, skill_id: str) -> Optional[Skill]:
  44. """获取 Skill"""
  45. ...
  46. async def get_skill_tree(self, scope: str) -> List[Skill]:
  47. """获取技能树"""
  48. ...
  49. async def search_skills(
  50. self,
  51. scope: str,
  52. context: str,
  53. limit: int = 5
  54. ) -> List[Skill]:
  55. """搜索相关 Skills"""
  56. ...
  57. @runtime_checkable
  58. class StateStore(Protocol):
  59. """短期状态存储接口(用于 Task State,通常用 Redis)"""
  60. async def get(self, key: str) -> Optional[Dict[str, Any]]:
  61. """获取状态"""
  62. ...
  63. async def set(
  64. self,
  65. key: str,
  66. value: Dict[str, Any],
  67. ttl: Optional[int] = None
  68. ) -> None:
  69. """
  70. 设置状态
  71. Args:
  72. key: 键
  73. value: 值
  74. ttl: 过期时间(秒)
  75. """
  76. ...
  77. async def update(self, key: str, **updates) -> None:
  78. """部分更新"""
  79. ...
  80. async def delete(self, key: str) -> None:
  81. """删除"""
  82. ...