memory_store.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """
  2. agentpaas.engine.memory_store — Persistent cross-run agent memory.
  3. Implements a simplified version of the Letta 3-layer memory model:
  4. Layer 1 — Core Memory (always in context):
  5. Small structured dict: persona, user facts, task context.
  6. Stored in {data_dir}/core_memory.json.
  7. Layer 2 — Recall Memory (recent interactions):
  8. Append-only JSONL log of (run_id, input, output, timestamp).
  9. Stored in {data_dir}/recall_log.jsonl.
  10. Bounded to last MAX_RECALL entries to control size.
  11. Layer 3 — Archival Memory (searchable long-term store):
  12. Delegated to the Knowledge Base system (BM25 indexes).
  13. This module only handles layers 1-2.
  14. Usage in _execute_agent:
  15. ctx.memory = load_memory(agent_dir) # before term.apply()
  16. save_memory(agent_dir, ctx.memory, run_id, input_text, str(result))
  17. """
  18. from __future__ import annotations
  19. import json
  20. import os
  21. from pathlib import Path
  22. from typing import Any, Dict
  23. MAX_RECALL = 50 # keep last N run summaries in recall log
  24. def _memory_dir(agent_dir: str) -> Path:
  25. """Return the memory sub-directory for an agent, creating it if needed."""
  26. p = Path(agent_dir) / "memory"
  27. p.mkdir(parents=True, exist_ok=True)
  28. return p
  29. def load_memory(agent_dir: str) -> Dict[str, Any]:
  30. """Load Core Memory dict from {agent_dir}/memory/core_memory.json.
  31. Returns an empty dict if the file does not exist yet.
  32. The dict is injected into ctx.memory before term execution so that
  33. Memory(agent, store) terms can read/write it within the run.
  34. """
  35. if not agent_dir:
  36. return {}
  37. core_file = _memory_dir(agent_dir) / "core_memory.json"
  38. if not core_file.exists():
  39. return {}
  40. try:
  41. return json.loads(core_file.read_text(encoding="utf-8"))
  42. except Exception:
  43. return {}
  44. def save_memory(
  45. agent_dir: str,
  46. memory: Dict[str, Any],
  47. run_id: str = "",
  48. input_text: str = "",
  49. output_text: str = "",
  50. ) -> None:
  51. """Persist Core Memory and append a Recall entry.
  52. Args:
  53. agent_dir: Agent / instance directory.
  54. memory: ctx.memory dict after run completion.
  55. run_id: Run identifier for recall log.
  56. input_text: User input (truncated to 500 chars in recall).
  57. output_text: Agent output (truncated to 500 chars in recall).
  58. """
  59. if not agent_dir:
  60. return
  61. mem_dir = _memory_dir(agent_dir)
  62. # ── Layer 1: save Core Memory ────────────────────────────────
  63. try:
  64. core_file = mem_dir / "core_memory.json"
  65. tmp = core_file.with_suffix(".json.tmp")
  66. tmp.write_text(
  67. json.dumps(memory, ensure_ascii=False, indent=2),
  68. encoding="utf-8",
  69. )
  70. tmp.replace(core_file)
  71. except Exception:
  72. pass
  73. # ── Layer 2: append Recall log ───────────────────────────────
  74. if not run_id:
  75. return
  76. try:
  77. from agentpaas.db.models import now_utc
  78. entry = json.dumps({
  79. "run_id": run_id,
  80. "input": input_text[:500],
  81. "output": output_text[:500],
  82. "ts": now_utc(),
  83. }, ensure_ascii=False)
  84. recall_file = mem_dir / "recall_log.jsonl"
  85. existing_lines: list[str] = []
  86. if recall_file.exists():
  87. existing_lines = recall_file.read_text(encoding="utf-8").splitlines()
  88. # Trim to MAX_RECALL - 1, then append new entry
  89. trimmed = existing_lines[-(MAX_RECALL - 1):] + [entry]
  90. recall_file.write_text("\n".join(trimmed) + "\n", encoding="utf-8")
  91. except Exception:
  92. pass
  93. def load_recall_summary(agent_dir: str, n: int = 5) -> str:
  94. """Return the last N recall entries as a readable string for prompt injection.
  95. Example output:
  96. [上次对话]
  97. Run run_abc: Q: ... A: ...
  98. Run run_xyz: Q: ... A: ...
  99. """
  100. if not agent_dir:
  101. return ""
  102. recall_file = _memory_dir(agent_dir) / "recall_log.jsonl"
  103. if not recall_file.exists():
  104. return ""
  105. try:
  106. lines = recall_file.read_text(encoding="utf-8").splitlines()
  107. recent = lines[-n:] if len(lines) >= n else lines
  108. entries = []
  109. for line in recent:
  110. try:
  111. d = json.loads(line)
  112. entries.append(
  113. f"Run {d.get('run_id','?')} ({d.get('ts','?')[:10]}): "
  114. f"Q: {d.get('input','')[:100]} A: {d.get('output','')[:100]}"
  115. )
  116. except Exception:
  117. pass
  118. if not entries:
  119. return ""
  120. return "[近期对话记录]\n" + "\n".join(entries)
  121. except Exception:
  122. return ""