Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions dashboard/plugin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,45 @@ def _session_lineage_root(db: Any, session_id: str) -> str:
return session_id


def _anchored_view(store: Any, session_id: str, message_id: Any) -> Dict[str, Any]:
"""The store's anchored view in the dict shape of the old SessionDB.get_anchored_view."""
view = store.messages.read_anchored_view(session_id, message_id, window=5, bookend=3)

def rows(records: Any) -> List[Dict[str, Any]]:
return [
{
"id": record.id,
"role": record.role,
"content": record.content,
"timestamp": record.timestamp,
"tool_name": record.tool_name,
"tool_calls": list(record.tool_calls),
"tool_call_id": record.tool_call_id,
}
for record in records
]

return {
"window": rows(view.window),
"messages_before": view.before,
"messages_after": view.after,
"bookend_start": rows(view.bookend_start),
"bookend_end": rows(view.bookend_end),
}


def _source_filtered_session_search(query: str, *, source: str, limit: int, sort: str) -> Dict[str, Any]:
"""Run source-aware session search without post-filtering a capped global result set."""
try:
from persistence_assembly import open_persistence_reader, profile_scope # type: ignore
except ImportError: # Hermes before the storage interface: its SessionDB serves every read below.
return _search_sessions_by_source(None, query, source=source, limit=limit, sort=sort)
with open_persistence_reader(profile_scope().profile_dir) as store:
return _search_sessions_by_source(store, query, source=source, limit=limit, sort=sort)


def _search_sessions_by_source(store: Any, query: str, *, source: str, limit: int, sort: str) -> Dict[str, Any]:
# search_messages and get_session still go through hermes_state, which Hermes v0.27.0 removes; read them from the store then.
from hermes_state import SessionDB # type: ignore

db = SessionDB()
Expand All @@ -418,7 +455,7 @@ def _source_filtered_session_search(query: str, *, source: str, limit: int, sort
seen_sessions: Dict[str, Dict[str, Any]] = {}
for row in raw_results:
raw_sid = row.get("session_id") or ""
lineage_root = _session_lineage_root(db, raw_sid)
lineage_root = _session_lineage_root(db if store is None else store, raw_sid)
if lineage_root not in seen_sessions:
item = dict(row)
item["_lineage_root"] = lineage_root
Expand All @@ -431,7 +468,7 @@ def _source_filtered_session_search(query: str, *, source: str, limit: int, sort
hit_sid = match_info.get("session_id") or lineage_root
msg_id = match_info.get("id")
try:
view = db.get_anchored_view(hit_sid, msg_id, window=5, bookend=3)
view = db.get_anchored_view(hit_sid, msg_id, window=5, bookend=3) if store is None else _anchored_view(store, hit_sid, msg_id)
except Exception:
continue
try:
Expand Down
100 changes: 100 additions & 0 deletions tests/test_plugin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def load_plugin_api(monkeypatch, tmp_path):
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setenv("USERPROFILE", str(tmp_path))
monkeypatch.setitem(sys.modules, "hindsight_embed", None)
# Session search reads through hermes_state unless a test installs the storage interface.
monkeypatch.setitem(sys.modules, "persistence_assembly", None)
fake_constants = types.ModuleType("hermes_constants")
fake_constants.get_hermes_home = lambda: str(tmp_path)
monkeypatch.setitem(sys.modules, "hermes_constants", fake_constants)
Expand Down Expand Up @@ -227,6 +229,104 @@ def search_messages(self, **kwargs):
assert db_calls[0]["source_filter"] == ["Paperclip"]


def test_session_search_payload_reads_anchored_view_from_storage_interface(monkeypatch, tmp_path):
calls = []

class FakeSessionDB:
# No get_anchored_view: the anchored view must come from the store.
def search_messages(self, **kwargs):
calls.append(("search_messages", kwargs["source_filter"]))
return [{
"id": 43,
"session_id": "s2",
"role": "user",
"source": "telegram",
"model": "test-model",
"session_started": 123,
"snippet": "telegram result",
}]

def get_session(self, session_id):
calls.append(("get_session", session_id))
return {"source": "telegram", "title": "Telegram memory UI work", "started_at": 123, "model": "test-model"}

def record(message_id, role, content, timestamp, **tool_fields):
fields = {"tool_name": None, "tool_calls": (), "tool_call_id": None, **tool_fields}
return types.SimpleNamespace(id=message_id, role=role, content=content, timestamp=timestamp, **fields)

class FakeMessages:
def read_anchored_view(self, session_id, message_id, *, window=5, bookend=3, keep_roles=("user", "assistant")):
calls.append(("read_anchored_view", session_id, message_id, window, bookend))
return types.SimpleNamespace(
window=(
record(42, "assistant", "earlier turn", 2.5, tool_calls=({"id": "call-1"},)),
record(message_id, "user", "find memory from telegram", 3.5),
),
before=7,
after=2,
bookend_start=(record(1, "user", "first turn", 1.5),),
bookend_end=(record(99, "tool", "last turn", 9.5, tool_name="lookup", tool_call_id="call-1"),),
)

store = types.SimpleNamespace(messages=FakeMessages())

class FakeReader:
def __init__(self, target):
calls.append(("open_persistence_reader", target))

def __enter__(self):
return store

def __exit__(self, *_exc_info):
calls.append(("close",))
return False

fake_hermes_state = types.ModuleType("hermes_state")
fake_hermes_state.SessionDB = FakeSessionDB
monkeypatch.setitem(sys.modules, "hermes_state", fake_hermes_state)

fake_tools = types.ModuleType("tools")
fake_session_search_tool = types.ModuleType("tools.session_search_tool")
fake_session_search_tool._resolve_to_parent = (
lambda db, session_id: calls.append(("resolve_to_parent", db is store, session_id)) or "s1"
)
fake_session_search_tool._format_timestamp = lambda value: f"ts:{value}"
monkeypatch.setitem(sys.modules, "tools", fake_tools)
monkeypatch.setitem(sys.modules, "tools.session_search_tool", fake_session_search_tool)

module = load_plugin_api(monkeypatch, tmp_path)
fake_persistence = types.ModuleType("persistence_assembly")
fake_persistence.profile_scope = lambda: types.SimpleNamespace(profile_dir=tmp_path)
fake_persistence.open_persistence_reader = FakeReader
monkeypatch.setitem(sys.modules, "persistence_assembly", fake_persistence)

payload = module._session_search_payload(query="memory", limit=3, sort="newest", source="telegram")

assert payload["error"] is None
assert payload["count"] == 1
result = payload["results"][0]
assert result["session_id"] == "s2"
assert result["parent_session_id"] == "s1"
assert result["title"] == "Telegram memory UI work"
assert result["bookend_start"] == [{"id": 1, "role": "user", "content": "first turn", "timestamp": 1.5}]
assert result["messages"] == [
{"id": 42, "role": "assistant", "content": "earlier turn", "timestamp": 2.5, "tool_calls": [{"id": "call-1"}]},
{"id": 43, "role": "user", "content": "find memory from telegram", "timestamp": 3.5, "anchor": True},
]
assert result["bookend_end"] == [
{"id": 99, "role": "tool", "content": "last turn", "timestamp": 9.5, "tool_name": "lookup", "tool_call_id": "call-1"},
]
assert (result["messages_before"], result["messages_after"]) == (7, 2)
assert calls == [
("open_persistence_reader", tmp_path),
("search_messages", ["telegram"]),
("resolve_to_parent", True, "s2"),
("read_anchored_view", "s2", 43, 5, 3),
("get_session", "s1"),
("close",),
]


def test_mem0_config_hides_api_key_and_uses_memory_client(monkeypatch, tmp_path):
(tmp_path / "config.yaml").write_text("memory:\n provider: mem0\n", encoding="utf-8")
(tmp_path / "mem0.json").write_text(
Expand Down