mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-08-25 00:14:08 +00:00
feat(python-sdks): SDK-level cross-source memory deduplication
Port the normalized, priority-ordered (static > dynamic > search) profile deduplication into the Python SDKs, injecting one owned memory block per request that replaces the prior block rather than accumulating. Dedup is request-local (no shared state), so it stays correct under concurrency. Covers OpenAI, Agent Framework (middleware + context provider), Cartesia, and Pipecat. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
be977489bf
commit
8de27afa2c
15 changed files with 333 additions and 57 deletions
|
|
@ -217,8 +217,8 @@ class SupermemoryContextProvider(BaseContextProvider):
|
|||
)
|
||||
|
||||
deduplicated = deduplicate_memories(
|
||||
static=static,
|
||||
dynamic=dynamic,
|
||||
static=static if self._mode != "query" else [],
|
||||
dynamic=dynamic if self._mode != "query" else [],
|
||||
search_results=search_results_raw,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ from .utils import (
|
|||
convert_profile_to_markdown,
|
||||
create_logger,
|
||||
deduplicate_memories,
|
||||
replace_memory_injection,
|
||||
strip_memory_injection,
|
||||
wrap_memory_injection,
|
||||
)
|
||||
|
||||
|
|
@ -152,8 +154,8 @@ async def _build_memories_text(
|
|||
)
|
||||
|
||||
deduplicated = deduplicate_memories(
|
||||
static=static,
|
||||
dynamic=dynamic,
|
||||
static=static if mode != "query" else [],
|
||||
dynamic=dynamic if mode != "query" else [],
|
||||
search_results=search_results_raw,
|
||||
)
|
||||
|
||||
|
|
@ -393,10 +395,11 @@ def _inject_memories(context: Any, memories: str) -> None:
|
|||
different Agent Framework providers.
|
||||
"""
|
||||
messages = context.messages
|
||||
memory_text = f"\n\n{wrap_memory_injection(memories)}"
|
||||
memory_text = wrap_memory_injection(memories)
|
||||
|
||||
# Try to find and augment existing system message
|
||||
for i, msg in enumerate(messages):
|
||||
# Replace prior SDK blocks in every system message and inject once.
|
||||
injected = False
|
||||
for msg in messages:
|
||||
role = None
|
||||
if hasattr(msg, "role"):
|
||||
role = msg.role
|
||||
|
|
@ -405,17 +408,35 @@ def _inject_memories(context: Any, memories: str) -> None:
|
|||
|
||||
if role == "system":
|
||||
if hasattr(msg, "text"):
|
||||
msg.text = (msg.text or "") + memory_text
|
||||
existing = msg.text or ""
|
||||
msg.text = (
|
||||
replace_memory_injection(existing, memories)
|
||||
if not injected
|
||||
else strip_memory_injection(existing)
|
||||
)
|
||||
elif hasattr(msg, "content"):
|
||||
msg.content = (msg.content or "") + memory_text
|
||||
existing = msg.content or ""
|
||||
msg.content = (
|
||||
replace_memory_injection(existing, memories)
|
||||
if not injected
|
||||
else strip_memory_injection(existing)
|
||||
)
|
||||
elif isinstance(msg, dict):
|
||||
msg["content"] = (msg.get("content", "") or "") + memory_text
|
||||
return
|
||||
existing = msg.get("content", "") or ""
|
||||
msg["content"] = (
|
||||
replace_memory_injection(existing, memories)
|
||||
if not injected
|
||||
else strip_memory_injection(existing)
|
||||
)
|
||||
injected = True
|
||||
|
||||
if injected:
|
||||
return
|
||||
|
||||
# No system message found - prepend one
|
||||
try:
|
||||
if isinstance(messages, list):
|
||||
messages.insert(0, Message("system", [memories]))
|
||||
messages.insert(0, Message("system", [memory_text]))
|
||||
except Exception:
|
||||
# If messages is immutable, log a warning
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ import re
|
|||
from typing import Any, Optional, Protocol
|
||||
|
||||
DEFAULT_CONTEXT_PROMPT = "The following are retrieved memories about the user."
|
||||
MEMORY_CONTEXT_PATTERN = re.compile(
|
||||
r'[ \t]*<supermemory context="user-memories" readonly>.*?</supermemory>[ \t]*',
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
def wrap_memory_injection(memories: str, context_prompt: str = "") -> str:
|
||||
|
|
@ -19,6 +23,21 @@ def wrap_memory_injection(memories: str, context_prompt: str = "") -> str:
|
|||
)
|
||||
|
||||
|
||||
def strip_memory_injection(content: str) -> str:
|
||||
"""Remove every context block previously owned by this middleware."""
|
||||
stripped = MEMORY_CONTEXT_PATTERN.sub("", content)
|
||||
return re.sub(r"\n{3,}", "\n\n", stripped).strip()
|
||||
|
||||
|
||||
def replace_memory_injection(content: str, memories: str) -> str:
|
||||
"""Replace middleware-owned context while preserving caller instructions."""
|
||||
preserved = strip_memory_injection(content)
|
||||
memory_context = wrap_memory_injection(memories) if memories.strip() else ""
|
||||
if not memory_context:
|
||||
return preserved
|
||||
return f"{preserved}\n\n{memory_context}" if preserved else memory_context
|
||||
|
||||
|
||||
class Logger(Protocol):
|
||||
"""Logger protocol for type safety."""
|
||||
|
||||
|
|
@ -110,36 +129,40 @@ def deduplicate_memories(
|
|||
return None
|
||||
|
||||
def comparison_key(memory: str) -> str:
|
||||
"""Remove Mono's dynamic-profile date decoration for comparison only."""
|
||||
return re.sub(
|
||||
"""Normalize display-only profile decoration for duplicate comparison."""
|
||||
without_prefix = re.sub(
|
||||
r"^(?:\[Recent\]\s*)?\[\d{4}-\d{2}-\d{2}\]\s*",
|
||||
"",
|
||||
memory,
|
||||
count=1,
|
||||
).strip()
|
||||
)
|
||||
return " ".join(without_prefix.strip().split()).casefold()
|
||||
|
||||
static_memories: list[str] = []
|
||||
seen_memories: set[str] = set()
|
||||
|
||||
for item in static_items:
|
||||
memory = extract_memory_text(item)
|
||||
if memory is not None:
|
||||
key = comparison_key(memory) if memory is not None else None
|
||||
if memory is not None and key is not None and key not in seen_memories:
|
||||
static_memories.append(memory)
|
||||
seen_memories.add(comparison_key(memory))
|
||||
seen_memories.add(key)
|
||||
|
||||
dynamic_memories: list[str] = []
|
||||
for item in dynamic_items:
|
||||
memory = extract_memory_text(item)
|
||||
if memory is not None and comparison_key(memory) not in seen_memories:
|
||||
key = comparison_key(memory) if memory is not None else None
|
||||
if memory is not None and key is not None and key not in seen_memories:
|
||||
dynamic_memories.append(memory)
|
||||
seen_memories.add(comparison_key(memory))
|
||||
seen_memories.add(key)
|
||||
|
||||
search_memories: list[str] = []
|
||||
for item in search_items:
|
||||
memory = extract_memory_text(item)
|
||||
if memory is not None and comparison_key(memory) not in seen_memories:
|
||||
key = comparison_key(memory) if memory is not None else None
|
||||
if memory is not None and key is not None and key not in seen_memories:
|
||||
search_memories.append(memory)
|
||||
seen_memories.add(comparison_key(memory))
|
||||
seen_memories.add(key)
|
||||
|
||||
return DeduplicatedMemories(
|
||||
static=static_memories,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
"""Tests for Supermemory context provider."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from supermemory_agent_framework import AgentSupermemory, SupermemoryContextProvider
|
||||
|
|
@ -123,3 +126,23 @@ class TestExtractConversation:
|
|||
result = provider._extract_conversation_from_context(MockContext())
|
||||
assert "User: Hello!" in result
|
||||
assert "Assistant: Hi there!" in result
|
||||
|
||||
|
||||
class TestMemoryRetrieval:
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_mode_keeps_search_fact_also_present_in_profile(self) -> None:
|
||||
fact = "User likes machine learning projects"
|
||||
conn = _make_conn()
|
||||
conn.client.profile = AsyncMock(
|
||||
return_value=SimpleNamespace(
|
||||
profile=SimpleNamespace(static=[fact], dynamic=[]),
|
||||
search_results=SimpleNamespace(
|
||||
results=[SimpleNamespace(memory=fact)]
|
||||
),
|
||||
)
|
||||
)
|
||||
provider = SupermemoryContextProvider(conn, mode="query")
|
||||
|
||||
memories = await provider._fetch_memories("machine learning")
|
||||
|
||||
assert fact in memories
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
"""Tests for Supermemory middleware."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from supermemory_agent_framework import (
|
||||
|
|
@ -10,6 +13,8 @@ from supermemory_agent_framework import (
|
|||
from supermemory_agent_framework.middleware import (
|
||||
_get_last_user_message,
|
||||
_get_conversation_content,
|
||||
_build_memories_text,
|
||||
_inject_memories,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -111,3 +116,52 @@ class TestMiddlewareConfiguration:
|
|||
conn = _make_conn(entity_context="User is a Python developer")
|
||||
middleware = SupermemoryChatMiddleware(conn)
|
||||
assert middleware._connection.entity_context == "User is a Python developer"
|
||||
|
||||
|
||||
class TestMemoryInjection:
|
||||
def test_replaces_prior_sdk_context(self) -> None:
|
||||
context = SimpleNamespace(
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": (
|
||||
"Be helpful.\n\n"
|
||||
'<supermemory context="user-memories" readonly>\n'
|
||||
"Stale profile fact\n"
|
||||
"</supermemory>"
|
||||
),
|
||||
},
|
||||
{"role": "user", "content": "What do you remember?"},
|
||||
]
|
||||
)
|
||||
|
||||
_inject_memories(context, "Fresh profile fact")
|
||||
|
||||
content = context.messages[0]["content"]
|
||||
assert "Be helpful." in content
|
||||
assert "Fresh profile fact" in content
|
||||
assert "Stale profile fact" not in content
|
||||
assert content.count(
|
||||
'<supermemory context="user-memories" readonly>'
|
||||
) == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_mode_keeps_search_fact_also_present_in_profile(self) -> None:
|
||||
fact = "User likes machine learning projects"
|
||||
client = SimpleNamespace(
|
||||
profile=AsyncMock(
|
||||
return_value=SimpleNamespace(
|
||||
profile=SimpleNamespace(static=[fact], dynamic=[]),
|
||||
search_results=SimpleNamespace(
|
||||
results=[SimpleNamespace(memory=fact)]
|
||||
),
|
||||
)
|
||||
)
|
||||
)
|
||||
logger = Mock()
|
||||
|
||||
memories = await _build_memories_text(
|
||||
"user-123", logger, "query", client, "machine learning"
|
||||
)
|
||||
|
||||
assert fact in memories
|
||||
|
|
|
|||
|
|
@ -56,6 +56,14 @@ class TestDeduplicateMemories:
|
|||
)
|
||||
assert result.static == ["valid"]
|
||||
|
||||
def test_normalized_fact_variants(self) -> None:
|
||||
result = deduplicate_memories(
|
||||
static=["User likes Python", " user likes python "],
|
||||
dynamic=["[2026-08-10] USER LIKES PYTHON"],
|
||||
)
|
||||
assert result.static == ["User likes Python"]
|
||||
assert result.dynamic == []
|
||||
|
||||
|
||||
class TestConvertProfileToMarkdown:
|
||||
def test_empty_profile(self) -> None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue