mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-27 06:15:15 +00:00
Bundles 10 community fixes for the Python wrappers, each commit kept with its original author. Ships supermemory-openai-sdk 1.0.10, supermemory-cartesia 0.1.5 and supermemory-pipecat 0.1.5. - openai-sdk-python: shared OpenAI clients no longer leak memories across tenants (#1503), memory search fails open instead of crashing the chat (#1288), the requests fallback runs off the event loop (#1660), get_profile works for users with no memories (#1759). - cartesia: the wrapped agent runs exactly once per event (#1280, #1670), plain-string and chunk-only search results are kept (#1683, #1693). - pipecat: memory text stays literal when replacing the injected block (#1647), `InputParams` is exported from the package root (#1575). - #1503 was hand-ported onto main's newer middleware. #1751 is left out: the v5 agent-framework migration (#1786) appends to the conversation document by design. Co-Authored-By: abhinav7x94 <204053250+abhinav7x94@users.noreply.github.com> Co-Authored-By: shamAnimates <145093437+shamAnimates@users.noreply.github.com> Co-Authored-By: aloktripathi1 <142301030+aloktripathi1@users.noreply.github.com> Co-Authored-By: Cintu07 <178455858+Cintu07@users.noreply.github.com> Co-Authored-By: Atharvsinh-codez <201381760+Atharvsinh-codez@users.noreply.github.com> Co-Authored-By: abhay-codes07 <182421137+abhay-codes07@users.noreply.github.com> Co-Authored-By: nkootstra <545768+nkootstra@users.noreply.github.com> Co-Authored-By: Agnik47 <140933190+Agnik47@users.noreply.github.com>
129 lines
3.8 KiB
Python
129 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
import sys
|
|
import types
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
def _install_test_stubs() -> None:
|
|
if "loguru" not in sys.modules:
|
|
loguru_module = types.ModuleType("loguru")
|
|
|
|
class _Logger:
|
|
def info(self, *_args, **_kwargs):
|
|
return None
|
|
|
|
def warning(self, *_args, **_kwargs):
|
|
return None
|
|
|
|
def error(self, *_args, **_kwargs):
|
|
return None
|
|
|
|
loguru_module.logger = _Logger()
|
|
sys.modules["loguru"] = loguru_module
|
|
|
|
if "pydantic" not in sys.modules:
|
|
pydantic_module = types.ModuleType("pydantic")
|
|
|
|
class BaseModel:
|
|
def __init__(self, **kwargs):
|
|
for key, value in kwargs.items():
|
|
setattr(self, key, value)
|
|
|
|
def Field(*, default=None, **_kwargs):
|
|
return default
|
|
|
|
pydantic_module.BaseModel = BaseModel
|
|
pydantic_module.Field = Field
|
|
sys.modules["pydantic"] = pydantic_module
|
|
|
|
|
|
_install_test_stubs()
|
|
|
|
from supermemory_cartesia.agent import SupermemoryCartesiaAgent
|
|
|
|
|
|
class _MockSupermemoryClient:
|
|
def __init__(self, response):
|
|
self.profile = AsyncMock(return_value=response)
|
|
|
|
|
|
class TestSupermemoryCartesiaNullProfile(unittest.IsolatedAsyncioTestCase):
|
|
async def test_retrieve_memories_handles_null_profile(self) -> None:
|
|
agent = SupermemoryCartesiaAgent(
|
|
agent=SimpleNamespace(),
|
|
api_key="mock_key",
|
|
container_tag="user-123",
|
|
custom_id="conversation-456",
|
|
)
|
|
|
|
response = SimpleNamespace(profile=None, search_results=None)
|
|
agent._supermemory_client = _MockSupermemoryClient(response)
|
|
|
|
result = await agent._retrieve_memories("Hello world")
|
|
|
|
self.assertEqual(
|
|
result,
|
|
{
|
|
"profile": {"static": [], "dynamic": []},
|
|
"search_results": [],
|
|
},
|
|
)
|
|
|
|
def test_query_mode_keeps_search_fact_also_present_in_profile(self) -> None:
|
|
fact = "User likes machine learning projects"
|
|
agent = SupermemoryCartesiaAgent(
|
|
agent=SimpleNamespace(),
|
|
api_key="mock_key",
|
|
container_tag="user-123",
|
|
custom_id="conversation-456",
|
|
config=SupermemoryCartesiaAgent.MemoryConfig(mode="query"),
|
|
)
|
|
|
|
context = agent._build_memory_message(
|
|
{
|
|
"profile": {"static": [fact], "dynamic": []},
|
|
"search_results": [SimpleNamespace(memory=fact)],
|
|
}
|
|
)
|
|
|
|
self.assertIsNotNone(context)
|
|
self.assertIn(fact, context)
|
|
|
|
async def test_process_does_not_retry_agent_after_output(self) -> None:
|
|
class UserTurnEnded:
|
|
pass
|
|
|
|
class FailingAfterOutputAgent:
|
|
def __init__(self) -> None:
|
|
self.calls = 0
|
|
|
|
async def process(self, _env, _event, **_kwargs):
|
|
self.calls += 1
|
|
yield "partial"
|
|
raise RuntimeError("agent failed after output")
|
|
|
|
wrapped_agent = FailingAfterOutputAgent()
|
|
agent = SupermemoryCartesiaAgent(
|
|
agent=wrapped_agent,
|
|
api_key="mock_key",
|
|
container_tag="user-123",
|
|
custom_id="conversation-456",
|
|
add_memory="never",
|
|
)
|
|
event = UserTurnEnded()
|
|
agent._enrich_event_with_memories = AsyncMock(return_value=(event, None))
|
|
|
|
outputs = []
|
|
with self.assertRaisesRegex(RuntimeError, "agent failed after output"):
|
|
async for output in agent.process(None, event):
|
|
outputs.append(output)
|
|
|
|
self.assertEqual(outputs, ["partial"])
|
|
self.assertEqual(wrapped_agent.calls, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|