Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/memos/mem_os/utils/reference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def split_continuous_references(text: str) -> str:
# Check if there's a comma between brackets
if "," not in content_between_brackets:
return text
# Every item must look like a reference tag "int:id"; plain bracketed
# prose such as "[apple, banana]" must pass through untouched
import re

if not re.fullmatch(
r"\s*\d+:[^,\s]+(?:,\s*\d+:[^,\s]+)*\s*", content_between_brackets
):
return text
text = text.replace(content_between_brackets, content_between_brackets.replace(", ", "]["))
text = text.replace(content_between_brackets, content_between_brackets.replace(",", "]["))

Expand Down
29 changes: 29 additions & 0 deletions tests/mem_os/test_reference_split_shape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from memos.mem_os.utils.reference_utils import split_continuous_references


@pytest.mark.parametrize(
("text", "expected"),
[
("[1:92ff35fb, 4:bfe6f044]", "[1:92ff35fb][4:bfe6f044]"),
("[1:92ff35fb,4:bfe6f044]", "[1:92ff35fb][4:bfe6f044]"),
("See [1:aa, 2:bb] now", "See [1:aa][2:bb] now"),
],
)
def test_reference_lists_are_split(text, expected):
assert split_continuous_references(text) == expected


@pytest.mark.parametrize(
"text",
[
"The set is [apple, banana] here",
"[1, 2, 3]",
"[some note, another note]",
"no brackets at all",
],
)
def test_non_reference_text_passes_through(text):
# plain bracketed prose used to be corrupted into [apple][banana]
assert split_continuous_references(text) == text
Loading