memory.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # app/services/agent/memory.py
  2. import json
  3. from typing import List, Dict, Any, Optional
  4. from app.core.redis import get_redis_client
  5. from app.core.logger import logger
  6. SESSION_KEY_PREFIX = "agent:session:"
  7. DEFAULT_SESSION_TTL = 86400 # 24小时过期
  8. class AgentMemory:
  9. """基于 Redis 的 Agent 会话上下文记忆管理器"""
  10. @classmethod
  11. def _make_key(cls, session_id: str) -> str:
  12. return f"{SESSION_KEY_PREFIX}{session_id.strip()}"
  13. @classmethod
  14. def sanitize_messages(cls, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
  15. """
  16. 清洗消息列表,确保严格符合 OpenAI / DeepSeek API 的结构约束:
  17. 1. 任何 role=='tool' 的消息前面必须紧跟一个包含该 tool_call_id 的 assistant 消息;
  18. 2. 任何带有 tool_calls 的 assistant 消息必须紧随其所有对应的 tool 返回消息;
  19. 3. 剔除孤立的 tool 消息或截断导致的残缺调用块,防止 DeepSeek HTTP 400 报错。
  20. """
  21. clean: List[Dict[str, Any]] = []
  22. i = 0
  23. n = len(messages)
  24. while i < n:
  25. msg = messages[i]
  26. role = msg.get("role")
  27. if role == "tool":
  28. # 孤立的 tool 消息,前一个不是合法 assistant tool_calls,直接丢弃
  29. if not clean or clean[-1].get("role") != "assistant" or not clean[-1].get("tool_calls"):
  30. i += 1
  31. continue
  32. matching_ids = {tc.get("id") for tc in clean[-1].get("tool_calls", [])}
  33. if msg.get("tool_call_id") not in matching_ids:
  34. i += 1
  35. continue
  36. clean.append(msg)
  37. i += 1
  38. elif role == "assistant" and msg.get("tool_calls"):
  39. expected_ids = {tc.get("id") for tc in msg.get("tool_calls", []) if tc.get("id")}
  40. j = i + 1
  41. tool_msgs = []
  42. while j < n and messages[j].get("role") == "tool":
  43. tool_msgs.append(messages[j])
  44. j += 1
  45. found_ids = {tm.get("tool_call_id") for tm in tool_msgs}
  46. if expected_ids and expected_ids.issubset(found_ids):
  47. clean.append(msg)
  48. clean.extend(tool_msgs)
  49. i = j
  50. else:
  51. if msg.get("content"):
  52. clean.append({"role": "assistant", "content": msg["content"]})
  53. i = j
  54. else:
  55. clean.append(msg)
  56. i += 1
  57. return clean
  58. @classmethod
  59. async def get_history(cls, session_id: Optional[str], max_messages: int = 20) -> List[Dict[str, Any]]:
  60. """获取指定 session 的历史消息(经自动清洗与安全校验)"""
  61. if not session_id:
  62. return []
  63. try:
  64. redis = await get_redis_client()
  65. key = cls._make_key(session_id)
  66. raw = await redis.get(key)
  67. if not raw:
  68. return []
  69. history = json.loads(raw)
  70. if isinstance(history, list):
  71. sliced = history[-max_messages:]
  72. return cls.sanitize_messages(sliced)
  73. return []
  74. except Exception as e:
  75. logger.warning(f"[AgentMemory] Failed to read history for session {session_id}: {e}")
  76. return []
  77. @classmethod
  78. async def append_messages(
  79. cls,
  80. session_id: Optional[str],
  81. new_messages: List[Dict[str, Any]],
  82. ttl: int = DEFAULT_SESSION_TTL,
  83. max_stored_messages: int = 40,
  84. ) -> None:
  85. """向 session 追加新消息并刷新 TTL"""
  86. if not session_id or not new_messages:
  87. return
  88. try:
  89. redis = await get_redis_client()
  90. key = cls._make_key(session_id)
  91. existing = await cls.get_history(session_id, max_messages=max_stored_messages)
  92. combined = existing + new_messages
  93. # 控制保存的历史最大长度,防止 token 溢出
  94. trimmed = combined[-max_stored_messages:]
  95. await redis.setex(key, ttl, json.dumps(trimmed, ensure_ascii=False))
  96. except Exception as e:
  97. logger.warning(f"[AgentMemory] Failed to append messages for session {session_id}: {e}")
  98. @classmethod
  99. async def clear_session(cls, session_id: str) -> bool:
  100. """清除指定 session 历史"""
  101. if not session_id:
  102. return False
  103. try:
  104. redis = await get_redis_client()
  105. key = cls._make_key(session_id)
  106. await redis.delete(key)
  107. return True
  108. except Exception as e:
  109. logger.warning(f"[AgentMemory] Failed to delete session {session_id}: {e}")
  110. return False