| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- """
- Storage Protocols - 存储接口定义
- 使用 Protocol 定义接口,允许不同的存储实现(内存、PostgreSQL、Neo4j 等)
- TraceStore 已移动到 agent.execution.protocols
- """
- from typing import Protocol, List, Optional, Dict, Any, runtime_checkable
- from agent.memory.models import Experience, Skill
- @runtime_checkable
- class MemoryStore(Protocol):
- """Experience + Skill 存储接口"""
- # ===== Experience 操作 =====
- async def add_experience(self, exp: Experience) -> str:
- """添加 Experience"""
- ...
- async def get_experience(self, exp_id: str) -> Optional[Experience]:
- """获取 Experience"""
- ...
- async def search_experiences(
- self,
- scope: str,
- context: str,
- limit: int = 10
- ) -> List[Experience]:
- """
- 搜索相关 Experience
- Args:
- scope: 范围(如 "agent:researcher")
- context: 当前上下文,用于语义匹配
- limit: 最大返回数量
- """
- ...
- async def update_experience_stats(
- self,
- exp_id: str,
- success: bool
- ) -> None:
- """更新 Experience 使用统计"""
- ...
- # ===== Skill 操作 =====
- async def add_skill(self, skill: Skill) -> str:
- """添加 Skill"""
- ...
- async def get_skill(self, skill_id: str) -> Optional[Skill]:
- """获取 Skill"""
- ...
- async def get_skill_tree(self, scope: str) -> List[Skill]:
- """获取技能树"""
- ...
- async def search_skills(
- self,
- scope: str,
- context: str,
- limit: int = 5
- ) -> List[Skill]:
- """搜索相关 Skills"""
- ...
- @runtime_checkable
- class StateStore(Protocol):
- """短期状态存储接口(用于 Task State,通常用 Redis)"""
- async def get(self, key: str) -> Optional[Dict[str, Any]]:
- """获取状态"""
- ...
- async def set(
- self,
- key: str,
- value: Dict[str, Any],
- ttl: Optional[int] = None
- ) -> None:
- """
- 设置状态
- Args:
- key: 键
- value: 值
- ttl: 过期时间(秒)
- """
- ...
- async def update(self, key: str, **updates) -> None:
- """部分更新"""
- ...
- async def delete(self, key: str) -> None:
- """删除"""
- ...
|