feat: 添加记忆自动化钩子与回写

在接收和发送消息时注册记忆自动化,并重构人物记忆回写逻辑以使用 memory_service.ingest_text。主要改动如下:

在接收消息时调用 memory_automation_service.on_incoming_message(bot 侧),在发送消息时调用 on_message_sent(send_service 侧),并加入安全的错误处理。
在 person_info 中,用 memory_service.ingest_text 替换手动操作 person.memory_points 的方式;新增 resolve_person_id_for_memory 辅助方法,并为回写计算一个 external_id 指纹。
扩展插件运行时的记忆搜索能力,使其支持 mode、chat_id、person_id、user_id、group_id、时间范围以及 respect_filter 选项。
改进 find_messages 的数据库会话处理,改为使用单一 session,并修复排序和过滤逻辑。
从 KnowledgeFetcher 中移除未使用的 LLMRequest 导入和初始化。
更新术语解释器(jargon explainer)的导入路径,使用新的模块位置。
更新 .gitignore 例外规则,允许特定的 pytest 数据文件被纳入版本控制。
文档小调整:明确人物事实提取规则(将直接使用的 “you” 改写为第三人称)。
This commit is contained in:
A-Dawn
2026-03-31 15:49:49 +08:00
parent ea9a1b5802
commit a004c59e1e
9 changed files with 170 additions and 88 deletions

View File

@@ -178,6 +178,7 @@ class PersonFactWritebackService:
1. 明确是关于目标人物本人的信息。
2. 具有相对稳定性,可以作为长期记忆保存。
3. 用简洁中文陈述句表达。
4. 如果回复是在直接对目标人物说话,出现“你/你的/你自己”时,默认都指目标人物,请先改写成关于目标人物的第三人称事实再输出。
不要提取:
- 机器人的情绪、计划、临时动作、客套话

View File

@@ -434,6 +434,21 @@ def _store_sent_message(message: SessionMessage) -> None:
MessageUtils.store_message_to_db(message)
async def _notify_memory_automation_on_message_sent(message: SessionMessage) -> None:
"""在发送成功后通知长期记忆自动化服务。
Args:
message: 已成功发送的内部消息对象。
"""
try:
from src.services.memory_flow_service import memory_automation_service
await memory_automation_service.on_message_sent(message)
except Exception as exc:
session_id = message.session_id or "unknown-session"
logger.warning(f"[{session_id}] 长期记忆人物事实写回注册失败: {exc}")
def _log_platform_io_failures(delivery_batch: DeliveryBatch) -> None:
"""输出 Platform IO 批量发送失败详情。
@@ -503,6 +518,7 @@ async def _send_via_platform_io(
if delivery_batch.has_success:
if storage_message:
_store_sent_message(message)
await _notify_memory_automation_on_message_sent(message)
if show_log:
successful_driver_ids = [
receipt.driver_id or "unknown"