protocols.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Trace Storage Protocol - Trace 存储接口定义
  3. 使用 Protocol 定义接口,允许不同的存储实现(内存、PostgreSQL、Neo4j 等)
  4. """
  5. from typing import Protocol, List, Optional, runtime_checkable
  6. from agent.execution.models import Trace, Step
  7. @runtime_checkable
  8. class TraceStore(Protocol):
  9. """Trace + Step 存储接口"""
  10. # ===== Trace 操作 =====
  11. async def create_trace(self, trace: Trace) -> str:
  12. """
  13. 创建新的 Trace
  14. Args:
  15. trace: Trace 对象
  16. Returns:
  17. trace_id
  18. """
  19. ...
  20. async def get_trace(self, trace_id: str) -> Optional[Trace]:
  21. """获取 Trace"""
  22. ...
  23. async def update_trace(self, trace_id: str, **updates) -> None:
  24. """
  25. 更新 Trace
  26. Args:
  27. trace_id: Trace ID
  28. **updates: 要更新的字段
  29. """
  30. ...
  31. async def list_traces(
  32. self,
  33. mode: Optional[str] = None,
  34. agent_type: Optional[str] = None,
  35. uid: Optional[str] = None,
  36. status: Optional[str] = None,
  37. limit: int = 50
  38. ) -> List[Trace]:
  39. """列出 Traces"""
  40. ...
  41. # ===== Step 操作 =====
  42. async def add_step(self, step: Step) -> str:
  43. """
  44. 添加 Step
  45. Args:
  46. step: Step 对象
  47. Returns:
  48. step_id
  49. """
  50. ...
  51. async def get_step(self, step_id: str) -> Optional[Step]:
  52. """获取 Step"""
  53. ...
  54. async def get_trace_steps(self, trace_id: str) -> List[Step]:
  55. """获取 Trace 的所有 Steps(按 sequence 排序)"""
  56. ...
  57. async def get_step_children(self, step_id: str) -> List[Step]:
  58. """获取 Step 的子节点"""
  59. ...