refactor(memory): Memory V2 P3 - 舍弃后台抽取 + Gateway 端点收口

P3 在 P1/P2 基础上完成 Memory V2 收尾:

舍弃后台抽取:
- 删除 updater/queue/storage/prompt/summarization_hook 五个模块
- 移除 background_extraction 配置段、扁平字段兼容、storage 字段
- 理由:Hindsight auto_retain 已自动留存每轮对话,后台抽取冗余

Hindsight 召回注入改用 wrap_model_call:
- 由 before_model 改为 wrap_model_call —— 召回只注入单次模型请求、
  不落持久 state,从根上消除 <memory-context> 泄漏给前端的风险
- builtin frozen snapshot 仍走系统提示词

Gateway 端点收口为 /api/memory/v2*:
- 删除旧 facts/history 端点 (/api/memory、/memory/facts 等)
- 新增 entries 增删改 (POST/PUT/DELETE /memory/v2/entries)
- 新增 POST /memory/v2/search —— Hindsight recall/reflect 检索代理
- client.py memory 方法重写为 V2 等价方法

文档同步更新 CLAUDE.md 与 MEMORY_V2_DESIGN_ZH.md。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
xuhdandClaude Opus 4.7 committed 2026-05-15 13:08:18 +08:00
1 parent f9d7610679
commit 21a76f5e7c
14 files changed
+954 -3085

No files matched your search

@@ -765,27 +765,22 @@ class DeerFlowClient:
]
}
def get_memory(self) -> dict:
"""Get current memory data.
def get_memory(self, agent: str | None = None) -> dict:
"""读取 Memory V2 本地记忆 (USER.md / MEMORY.md)。
Args:
agent: 可选 agent 名;省略表示默认 agent。
Returns:
Memory data dict (see src/agents/memory/updater.py for structure).
``{user_entries, memory_entries, user_usage, memory_usage}``。
"""
from deerflow.agents.memory.updater import get_memory_data
return get_memory_data(user_id=get_effective_user_id())
def export_memory(self) -> dict:
"""Export current memory data for backup or transfer."""
from deerflow.agents.memory.updater import get_memory_data
return get_memory_data(user_id=get_effective_user_id())
def import_memory(self, memory_data: dict) -> dict:
"""Import and persist full memory data."""
from deerflow.agents.memory.updater import import_memory_data
return import_memory_data(memory_data, user_id=get_effective_user_id())
provider = self._build_memory_provider(agent)
return {
"user_entries": provider.read("user"),
"memory_entries": provider.read("memory"),
"user_usage": provider.usage("user"),
"memory_usage": provider.usage("memory"),
}
def get_model(self, name: str) -> dict | None:
"""Get a specific model's configuration by name.
@@ -949,83 +944,73 @@ class DeerFlowClient:
return get_or_new_skill_storage().install_skill_from_archive(skill_path)
# ------------------------------------------------------------------
# Public API — memory management
# Public API — memory management (Memory V2)
# ------------------------------------------------------------------
def reload_memory(self) -> dict:
"""Reload memory data from file, forcing cache invalidation.
def _build_memory_provider(self, agent: str | None = None):
"""按生效配置构建并初始化一个 V2 builtin provider。"""
from deerflow.agents.memory.providers.builtin import BuiltinFileProvider
from deerflow.config.memory_config import get_effective_memory_config
from deerflow.config.paths import get_paths
Returns:
The reloaded memory data dict.
"""
from deerflow.agents.memory.updater import reload_memory_data
return reload_memory_data(user_id=get_effective_user_id())
def clear_memory(self) -> dict:
"""Clear all persisted memory data."""
from deerflow.agents.memory.updater import clear_memory_data
return clear_memory_data(user_id=get_effective_user_id())
def create_memory_fact(self, content: str, category: str = "context", confidence: float = 0.5) -> dict:
"""Create a single fact manually."""
from deerflow.agents.memory.updater import create_memory_fact
return create_memory_fact(content=content, category=category, confidence=confidence)
def delete_memory_fact(self, fact_id: str) -> dict:
"""Delete a single fact from memory by fact id."""
from deerflow.agents.memory.updater import delete_memory_fact
return delete_memory_fact(fact_id)
def update_memory_fact(
self,
fact_id: str,
content: str | None = None,
category: str | None = None,
confidence: float | None = None,
) -> dict:
"""Update a single fact manually, preserving omitted fields."""
from deerflow.agents.memory.updater import update_memory_fact
return update_memory_fact(
fact_id=fact_id,
content=content,
category=category,
confidence=confidence,
user_id = get_effective_user_id()
config = get_effective_memory_config(user_id)
provider = BuiltinFileProvider(
memory_char_limit=config.builtin.memory_char_limit,
user_char_limit=config.builtin.user_char_limit,
deduplicate_on_load=config.builtin.deduplicate_on_load,
security=config.security,
)
provider.initialize(
user_id=user_id,
agent_id=agent or "default",
base_dir=str(get_paths().base_dir),
)
return provider
def add_memory_entry(self, target: str, content: str, agent: str | None = None) -> dict:
"""新增一条记忆条目。``target`` 为 'user'(用户画像) 或 'memory'(工作笔记)。"""
return self._build_memory_provider(agent).add(target, content)
def replace_memory_entry(self, target: str, old_text: str, content: str, agent: str | None = None) -> dict:
"""用 ``old_text`` 短串定位并替换一条记忆条目。"""
return self._build_memory_provider(agent).replace(target, old_text, content)
def remove_memory_entry(self, target: str, old_text: str, agent: str | None = None) -> dict:
"""用 ``old_text`` 短串定位并删除一条记忆条目。"""
return self._build_memory_provider(agent).remove(target, old_text)
def get_memory_config(self) -> dict:
"""Get memory system configuration.
"""读取当前用户的生效记忆配置 (含 per-user 覆盖)。"""
from deerflow.config.memory_config import get_effective_memory_config
return get_effective_memory_config(get_effective_user_id()).model_dump()
def search_memory(self, query: str, mode: str = "recall", agent: str | None = None) -> dict:
"""代理 Hindsight 检索。
Args:
query: 检索查询。
mode: ``recall`` 返回原始片段;``reflect`` 返回 LLM 综合结论。
agent: 可选 agent 名。
Returns:
Memory config dict.
``{available, results, text}``;Hindsight 不可用时 available=False。
"""
from deerflow.config.memory_config import get_memory_config
from deerflow.agents.memory.providers.hindsight import HindsightProvider
from deerflow.config.memory_config import get_effective_memory_config
config = get_memory_config()
return {
"enabled": config.enabled,
"storage_path": config.storage_path,
"debounce_seconds": config.debounce_seconds,
"max_facts": config.max_facts,
"fact_confidence_threshold": config.fact_confidence_threshold,
"injection_enabled": config.injection_enabled,
"max_injection_tokens": config.max_injection_tokens,
}
def get_memory_status(self) -> dict:
"""Get memory status: config + current data.
Returns:
Dict with "config" and "data" keys.
"""
return {
"config": self.get_memory_config(),
"data": self.get_memory(),
}
user_id = get_effective_user_id()
config = get_effective_memory_config(user_id)
if not config.enabled or config.provider != "hindsight":
return {"available": False, "results": [], "text": ""}
provider = HindsightProvider(config.hindsight)
if not provider.is_available():
return {"available": False, "results": [], "text": ""}
provider.initialize(user_id=user_id, agent_id=agent or "default", thread_id="", base_dir="")
if mode == "reflect":
return {"available": True, "results": [], "text": provider.reflect(query)}
return {"available": True, "results": provider.recall(query), "text": ""}
# ------------------------------------------------------------------
# Public API — file uploads