Refine memory system user-key flow and search output

This commit is contained in:
2026-05-22 16:30:42 +08:00
parent 92632553ab
commit d73f59f38d
16 changed files with 1888 additions and 255 deletions

View File

@ -105,9 +105,14 @@ def _parse_env_file(path: Path) -> Dict[str, str]:
def _memory_system_config(hermes_home: str = "") -> Dict[str, str]:
candidates: List[Path] = []
explicit_file = os.environ.get("MEMORY_SYSTEM_ENV_FILE", "")
if explicit_file:
candidates.append(Path(explicit_file).expanduser())
candidates.extend(
[
Path.cwd() / ".env",
Path.cwd() / "memory_system.env",
Path.home() / ".hermes" / ".env",
Path.home() / ".hermes" / "memory_system.env",
]
)
if hermes_home:
candidates.append(Path(hermes_home).expanduser() / ".env")
candidates.append(Path(hermes_home).expanduser() / "memory_system.env")
@ -115,14 +120,9 @@ def _memory_system_config(hermes_home: str = "") -> Dict[str, str]:
if env_hermes_home:
candidates.append(Path(env_hermes_home).expanduser() / ".env")
candidates.append(Path(env_hermes_home).expanduser() / "memory_system.env")
candidates.extend(
[
Path.home() / ".hermes" / ".env",
Path.home() / ".hermes" / "memory_system.env",
Path.cwd() / "memory_system.env",
Path.cwd() / ".env",
]
)
explicit_file = os.environ.get("MEMORY_SYSTEM_ENV_FILE", "")
if explicit_file:
candidates.append(Path(explicit_file).expanduser())
config: Dict[str, str] = {}
seen: set[Path] = set()
@ -478,14 +478,10 @@ class MemorySystemMemoryProvider(MemoryProvider):
def _format_items(self, items: List[Dict[str, Any]], *, limit: int) -> str:
parts = []
for item in items[:limit]:
text = (
item.get("content")
or item.get("text")
or item.get("memory")
or item.get("summary")
or json.dumps(item, ensure_ascii=False)
)
source = item.get("source") or item.get("backend") or "memory"
text = self._memory_text(item)
if not text:
continue
source = item.get("source_backend") or item.get("source") or item.get("backend") or "memory"
score = item.get("score")
prefix = f"[{source}]"
if isinstance(score, (int, float)):
@ -493,6 +489,56 @@ class MemorySystemMemoryProvider(MemoryProvider):
parts.append(f"- {prefix} {text}")
return "\n".join(parts)
def _memory_text(self, item: Dict[str, Any]) -> str:
for key in (
"memory",
"content",
"text",
"summary",
"abstract",
"fact",
"value",
):
value = item.get(key)
if isinstance(value, str) and value.strip():
return value.strip()
for key in ("memory", "content", "text", "summary"):
value = item.get("data", {}).get(key) if isinstance(item.get("data"), dict) else None
if isinstance(value, str) and value.strip():
return value.strip()
return ""
def _compact_search_response(self, response: Dict[str, Any], *, limit: int) -> Dict[str, Any]:
compact_items = []
for item in response.get("items", [])[:limit]:
if not isinstance(item, dict):
continue
text = self._memory_text(item)
if not text:
continue
compact: Dict[str, Any] = {
"source_backend": item.get("source_backend") or item.get("source") or item.get("backend") or "memory",
"text": text[:1200],
}
memory_type = item.get("memory_type") or item.get("type") or item.get("category")
if memory_type:
compact["memory_type"] = memory_type
score = item.get("score")
if isinstance(score, (int, float)):
compact["score"] = score
uri = item.get("uri")
if isinstance(uri, str) and uri.startswith("viking://"):
compact["uri"] = uri
compact_items.append(compact)
result: Dict[str, Any] = {
"status": response.get("status", "success"),
"items": compact_items,
}
if not compact_items:
result["message"] = "No memory items found."
return result
def _should_commit_now(self) -> bool:
if self._last_commit_turn >= self._turn_count:
return False
@ -527,7 +573,8 @@ class MemorySystemMemoryProvider(MemoryProvider):
"use_llm": bool(args.get("use_llm", self._default_use_llm)),
"limit": limit,
}
return json.dumps(self._client.post("/memory-system/search", payload), ensure_ascii=False)
response = self._client.post("/memory-system/search", payload)
return json.dumps(self._compact_search_response(response, limit=limit), ensure_ascii=False)
def _tool_profile(self, args: Dict[str, Any]) -> str:
user_id = str(args.get("user_id") or self._user_id).strip()