fix:完善Maisaka记忆写回链路
补齐聊天摘要自动写回、发送后同步与图存储清理逻辑,对齐 visual 新配置字段并补充相关回归测试,同时忽略 algorithm_redesign 设计目录。
This commit is contained in:
@@ -802,6 +802,7 @@ class SDKMemoryKernel:
|
||||
chat_id: str,
|
||||
context_length: Optional[int] = None,
|
||||
include_personality: Optional[bool] = None,
|
||||
time_end: Optional[float] = None,
|
||||
) -> Dict[str, Any]:
|
||||
await self.initialize()
|
||||
assert self.summary_importer
|
||||
@@ -809,6 +810,7 @@ class SDKMemoryKernel:
|
||||
stream_id=str(chat_id or "").strip(),
|
||||
context_length=context_length,
|
||||
include_personality=include_personality,
|
||||
time_end=time_end,
|
||||
)
|
||||
if success:
|
||||
await self.rebuild_episodes_for_sources([self._build_source("chat_summary", chat_id, [])])
|
||||
@@ -851,6 +853,7 @@ class SDKMemoryKernel:
|
||||
chat_id=chat_id,
|
||||
context_length=self._optional_int(summary_meta.get("context_length")),
|
||||
include_personality=summary_meta.get("include_personality"),
|
||||
time_end=time_end,
|
||||
)
|
||||
result.setdefault("external_id", external_id)
|
||||
result.setdefault("chat_id", chat_id)
|
||||
|
||||
@@ -1190,11 +1190,14 @@ class GraphStore:
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 保存邻接矩阵
|
||||
matrix_path = data_dir / "graph_adjacency.npz"
|
||||
if self._adjacency is not None:
|
||||
matrix_path = data_dir / "graph_adjacency.npz"
|
||||
with atomic_write(matrix_path, "wb") as f:
|
||||
save_npz(f, self._adjacency)
|
||||
logger.debug(f"保存邻接矩阵: {matrix_path}")
|
||||
elif matrix_path.exists():
|
||||
matrix_path.unlink()
|
||||
logger.debug(f"删除陈旧邻接矩阵: {matrix_path}")
|
||||
|
||||
# 保存元数据
|
||||
metadata = {
|
||||
@@ -1288,9 +1291,20 @@ class GraphStore:
|
||||
if self._adjacency is not None:
|
||||
adj_n = self._adjacency.shape[0]
|
||||
current_n = len(self._nodes)
|
||||
if current_n > adj_n:
|
||||
if current_n == 0:
|
||||
logger.warning("检测到空图元数据但邻接矩阵仍然存在,已重置为空图。")
|
||||
self._adjacency = None
|
||||
elif current_n > adj_n:
|
||||
logger.warning(f"检测到图存储维度不匹配: 节点数={current_n}, 矩阵大小={adj_n}. 正在自动修复...")
|
||||
self._expand_adjacency_matrix(current_n - adj_n)
|
||||
elif current_n < adj_n:
|
||||
logger.warning(
|
||||
f"检测到过期邻接矩阵: 节点数={current_n}, 矩阵大小={adj_n}. 正在重置邻接矩阵..."
|
||||
)
|
||||
if self.matrix_format == "csc":
|
||||
self._adjacency = csc_matrix((current_n, current_n), dtype=np.float32)
|
||||
else:
|
||||
self._adjacency = csr_matrix((current_n, current_n), dtype=np.float32)
|
||||
|
||||
self._adjacency_dirty = True
|
||||
logger.info(
|
||||
@@ -1445,4 +1459,3 @@ class GraphStore:
|
||||
self._adjacency_dirty = True
|
||||
logger.info(f"已从 {count} 条哈希重建边哈希映射,覆盖 {len(self._edge_hash_map)} 条边")
|
||||
return count
|
||||
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
导入到 A_memorix 的存储组件中。
|
||||
"""
|
||||
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
import traceback
|
||||
from typing import List, Dict, Any, Tuple, Optional
|
||||
from pathlib import Path
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.services import llm_service as llm_api
|
||||
@@ -222,7 +223,8 @@ class SummaryImporter:
|
||||
self,
|
||||
stream_id: str,
|
||||
context_length: Optional[int] = None,
|
||||
include_personality: Optional[bool] = None
|
||||
include_personality: Optional[bool] = None,
|
||||
time_end: Optional[float] = None,
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
从指定的聊天流中提取记录并执行总结导入
|
||||
@@ -231,6 +233,7 @@ class SummaryImporter:
|
||||
stream_id: 聊天流 ID
|
||||
context_length: 总结的历史消息条数
|
||||
include_personality: 是否包含人设
|
||||
time_end: 用于截取聊天记录的时间上界(闭区间)
|
||||
|
||||
Returns:
|
||||
Tuple[bool, str]: (是否成功, 结果消息)
|
||||
@@ -248,12 +251,13 @@ class SummaryImporter:
|
||||
include_personality = self.plugin_config.get("summarization", {}).get("include_personality", True)
|
||||
|
||||
# 2. 获取历史消息
|
||||
# 获取当前时间之前的消息
|
||||
now = time.time()
|
||||
messages = message_api.get_messages_before_time_in_chat(
|
||||
query_time_end = time.time() if time_end is None else float(time_end)
|
||||
messages = message_api.get_messages_by_time_in_chat(
|
||||
chat_id=stream_id,
|
||||
timestamp=now,
|
||||
limit=context_length
|
||||
start_time=0.0,
|
||||
end_time=query_time_end,
|
||||
limit=context_length,
|
||||
limit_mode="latest",
|
||||
)
|
||||
|
||||
if not messages:
|
||||
|
||||
Reference in New Issue
Block a user