protocols.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """
  2. Trace Storage Protocol - Trace 存储接口定义
  3. 使用 Protocol 定义接口,允许不同的存储实现(内存、PostgreSQL、Neo4j 等)
  4. """
  5. from typing import Protocol, List, Optional, Dict, Any, runtime_checkable
  6. from agent.execution.models import Trace, Message
  7. from agent.goal.models import GoalTree, Goal
  8. @runtime_checkable
  9. class TraceStore(Protocol):
  10. """Trace + Message + GoalTree 存储接口"""
  11. # ===== Trace 操作 =====
  12. async def create_trace(self, trace: Trace) -> str:
  13. """
  14. 创建新的 Trace
  15. Args:
  16. trace: Trace 对象
  17. Returns:
  18. trace_id
  19. """
  20. ...
  21. async def get_trace(self, trace_id: str) -> Optional[Trace]:
  22. """获取 Trace"""
  23. ...
  24. async def update_trace(self, trace_id: str, **updates) -> None:
  25. """
  26. 更新 Trace
  27. Args:
  28. trace_id: Trace ID
  29. **updates: 要更新的字段
  30. """
  31. ...
  32. async def list_traces(
  33. self,
  34. mode: Optional[str] = None,
  35. agent_type: Optional[str] = None,
  36. uid: Optional[str] = None,
  37. status: Optional[str] = None,
  38. limit: int = 50
  39. ) -> List[Trace]:
  40. """列出 Traces"""
  41. ...
  42. # ===== GoalTree 操作 =====
  43. async def get_goal_tree(self, trace_id: str) -> Optional[GoalTree]:
  44. """
  45. 获取 GoalTree
  46. Args:
  47. trace_id: Trace ID
  48. Returns:
  49. GoalTree 对象,如果不存在返回 None
  50. """
  51. ...
  52. async def update_goal_tree(self, trace_id: str, tree: GoalTree) -> None:
  53. """
  54. 更新完整 GoalTree
  55. Args:
  56. trace_id: Trace ID
  57. tree: GoalTree 对象
  58. """
  59. ...
  60. async def add_goal(self, trace_id: str, goal: Goal) -> None:
  61. """
  62. 添加 Goal 到 GoalTree
  63. Args:
  64. trace_id: Trace ID
  65. goal: Goal 对象
  66. """
  67. ...
  68. async def update_goal(self, trace_id: str, goal_id: str, **updates) -> None:
  69. """
  70. 更新 Goal 字段
  71. Args:
  72. trace_id: Trace ID
  73. goal_id: Goal ID
  74. **updates: 要更新的字段(如 status, summary, self_stats, cumulative_stats)
  75. """
  76. ...
  77. # ===== Message 操作 =====
  78. async def add_message(self, message: Message) -> str:
  79. """
  80. 添加 Message
  81. 自动更新关联 Goal 的 stats(self_stats 和祖先的 cumulative_stats)
  82. Args:
  83. message: Message 对象
  84. Returns:
  85. message_id
  86. """
  87. ...
  88. async def get_message(self, message_id: str) -> Optional[Message]:
  89. """获取 Message"""
  90. ...
  91. async def get_trace_messages(
  92. self,
  93. trace_id: str
  94. ) -> List[Message]:
  95. """
  96. 获取 Trace 的所有 Messages(按 sequence 排序)
  97. Args:
  98. trace_id: Trace ID
  99. Returns:
  100. Message 列表
  101. """
  102. ...
  103. async def get_messages_by_goal(
  104. self,
  105. trace_id: str,
  106. goal_id: str
  107. ) -> List[Message]:
  108. """
  109. 获取指定 Goal 关联的所有 Messages
  110. Args:
  111. trace_id: Trace ID
  112. goal_id: Goal ID
  113. Returns:
  114. Message 列表
  115. """
  116. ...
  117. async def update_message(self, message_id: str, **updates) -> None:
  118. """
  119. 更新 Message 字段(用于状态变更、错误记录等)
  120. Args:
  121. message_id: Message ID
  122. **updates: 要更新的字段
  123. """
  124. ...
  125. # ===== 事件流操作(用于 WebSocket 断线续传)=====
  126. async def get_events(
  127. self,
  128. trace_id: str,
  129. since_event_id: int = 0
  130. ) -> List[Dict[str, Any]]:
  131. """
  132. 获取事件流(用于 WS 断线续传)
  133. Args:
  134. trace_id: Trace ID
  135. since_event_id: 从哪个事件 ID 开始(0 表示全部)
  136. Returns:
  137. 事件列表(按 event_id 排序)
  138. """
  139. ...
  140. async def append_event(
  141. self,
  142. trace_id: str,
  143. event_type: str,
  144. payload: Dict[str, Any]
  145. ) -> int:
  146. """
  147. 追加事件,返回 event_id
  148. Args:
  149. trace_id: Trace ID
  150. event_type: 事件类型
  151. payload: 事件数据
  152. Returns:
  153. event_id: 新事件的 ID
  154. """
  155. ...