From ca22e1dd74cbb097f83355ae45202ebcbd3b91cd Mon Sep 17 00:00:00 2001 From: Daoji Wang <627665797@qq.com> Date: Tue, 29 Sep 2026 10:49:40 +0800 Subject: [PATCH] fix: recall playground memories from every searched cube Playground only kept the first cube in text_mem, so personal memories were dropped when a knowledge-base cube came first. Co-authored-by: Cursor --- src/memos/api/handlers/chat_handler.py | 24 ++++++++++++------------ tests/api/test_playground_cube_recall.py | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 tests/api/test_playground_cube_recall.py diff --git a/src/memos/api/handlers/chat_handler.py b/src/memos/api/handlers/chat_handler.py index 58a96cd75..31acec6d0 100644 --- a/src/memos/api/handlers/chat_handler.py +++ b/src/memos/api/handlers/chat_handler.py @@ -482,12 +482,8 @@ def generate_chat_response() -> Generator[str, None, None]: yield f"data: {json.dumps({'type': 'status', 'data': '1'})}\n\n" - # Extract memories from search results (first search) - memories_list = [] - if search_response.data and search_response.data.get("text_mem"): - text_mem_results = search_response.data["text_mem"] - if text_mem_results and text_mem_results[0].get("memories"): - memories_list = text_mem_results[0]["memories"] + # Extract memories from every searched cube (first search) + memories_list = self._memories_from_cube_results(search_response.data) # Filter memories by threshold filtered_memories = self._filter_memories_by_threshold(memories_list)[:5] @@ -577,12 +573,8 @@ def generate_chat_response() -> Generator[str, None, None]: project_id=chat_req.project_id, ) - # Extract memories from search results (second search) - memories_list = [] - if search_response.data and search_response.data.get("text_mem"): - text_mem_results = search_response.data["text_mem"] - if text_mem_results and text_mem_results[0].get("memories"): - memories_list = text_mem_results[0]["memories"] + # Extract memories from every searched cube (second search) + memories_list = self._memories_from_cube_results(search_response.data) # Filter memories by threshold, min_num is the min number of memories for playground second_filtered_memories = self._filter_memories_by_threshold( @@ -1161,6 +1153,14 @@ def _format_mem_block( return "\n".join(lines_o), "\n".join(lines_p) + @staticmethod + def _memories_from_cube_results(search_data: dict[str, Any] | None) -> list[Any]: + """Collect textual memories from every cube in a search response.""" + memories_list: list[Any] = [] + for cube_result in (search_data or {}).get("text_mem") or []: + memories_list.extend(cube_result.get("memories") or []) + return memories_list + def _filter_memories_by_threshold( self, memories: list, diff --git a/tests/api/test_playground_cube_recall.py b/tests/api/test_playground_cube_recall.py new file mode 100644 index 000000000..4b1d8fd40 --- /dev/null +++ b/tests/api/test_playground_cube_recall.py @@ -0,0 +1,24 @@ +"""Playground recall must keep memories from every searched cube.""" + +from memos.api.handlers.chat_handler import ChatHandler + + +def test_playground_recall_keeps_memories_from_later_cubes(): + search_data = { + "text_mem": [ + {"cube_id": "knowledge-base", "memories": []}, + { + "cube_id": "personal", + "memories": [{"id": "mem-1", "memory": "use event_id as the idempotency key"}], + }, + ] + } + + memories = ChatHandler._memories_from_cube_results(search_data) + + assert [item["id"] for item in memories] == ["mem-1"] + + +def test_playground_recall_returns_empty_when_search_data_missing(): + assert ChatHandler._memories_from_cube_results(None) == [] + assert ChatHandler._memories_from_cube_results({}) == []