fix(ts-oss): isolate entity store from memory store by default (#4829)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
Chaithanya Kumar
2026-04-15 14:47:43 +05:30
committed by GitHub
co-authored by Claude Opus 4.6 kartik-mem0
parent d8d776636f
commit 9692726db4
20 changed files with 147 additions and 48 deletions
+14 -3
View File
@@ -32,15 +32,26 @@ def get_user_id():
return "anonymous_user"
def get_or_create_user_id(vector_store):
"""Store user_id in vector store and return it."""
def get_or_create_user_id(vector_store=None):
"""Store user_id in vector store and return it.
If vector_store is None, simply returns the user_id from config.
This ensures telemetry initialization never fails due to missing vector store.
"""
user_id = get_user_id()
# If no vector store provided, just return the user_id
if vector_store is None:
return user_id
# Try to get existing user_id from vector store
try:
existing = vector_store.get(vector_id=user_id)
if existing and hasattr(existing, "payload") and existing.payload and "user_id" in existing.payload:
return existing.payload["user_id"]
stored_id = existing.payload["user_id"]
# Ensure we never return None from vector store
if stored_id is not None:
return stored_id
except Exception:
pass
+47 -23
View File
@@ -88,6 +88,12 @@ class AnonymousTelemetry:
if self.posthog is None:
return
# Determine distinct_id, skip if None to prevent crashes
distinct_id = self.user_id if user_email is None else user_email
if distinct_id is None:
_logger.debug("Skipping telemetry event %r: no distinct_id available", event_name)
return
if properties is None:
properties = {}
properties = {
@@ -101,8 +107,10 @@ class AnonymousTelemetry:
"machine": platform.machine(),
**properties,
}
distinct_id = self.user_id if user_email is None else user_email
self.posthog.capture(distinct_id=distinct_id, event=event_name, properties=properties)
try:
self.posthog.capture(distinct_id=distinct_id, event=event_name, properties=properties)
except Exception as e:
_logger.debug("Failed to capture telemetry event %r: %s", event_name, e)
def close(self):
if self.posthog is not None:
@@ -158,36 +166,52 @@ atexit.register(client_telemetry.close)
def capture_event(event_name, memory_instance, additional_data=None):
"""Capture telemetry event for OSS Memory instances.
This function is designed to never raise exceptions - telemetry failures
should not affect the main application flow.
"""
if not MEM0_TELEMETRY:
return
oss_telemetry = _get_oss_telemetry()
if oss_telemetry is None:
return
try:
oss_telemetry = _get_oss_telemetry()
if oss_telemetry is None:
return
event_data = {
"collection": memory_instance.collection_name,
"vector_size": memory_instance.embedding_model.config.embedding_dims,
"history_store": "sqlite",
"vector_store": f"{memory_instance.vector_store.__class__.__module__}.{memory_instance.vector_store.__class__.__name__}",
"llm": f"{memory_instance.llm.__class__.__module__}.{memory_instance.llm.__class__.__name__}",
"embedding_model": f"{memory_instance.embedding_model.__class__.__module__}.{memory_instance.embedding_model.__class__.__name__}",
"function": f"{memory_instance.__class__.__module__}.{memory_instance.__class__.__name__}.{memory_instance.api_version}",
}
if additional_data:
event_data.update(additional_data)
event_data = {
"collection": memory_instance.collection_name,
"vector_size": memory_instance.embedding_model.config.embedding_dims,
"history_store": "sqlite",
"vector_store": f"{memory_instance.vector_store.__class__.__module__}.{memory_instance.vector_store.__class__.__name__}",
"llm": f"{memory_instance.llm.__class__.__module__}.{memory_instance.llm.__class__.__name__}",
"embedding_model": f"{memory_instance.embedding_model.__class__.__module__}.{memory_instance.embedding_model.__class__.__name__}",
"function": f"{memory_instance.__class__.__module__}.{memory_instance.__class__.__name__}.{memory_instance.api_version}",
}
if additional_data:
event_data.update(additional_data)
oss_telemetry.capture_event(event_name, event_data)
oss_telemetry.capture_event(event_name, event_data)
except Exception as e:
_logger.debug("Failed to capture OSS telemetry event %r: %s", event_name, e)
def capture_client_event(event_name, instance, additional_data=None):
"""Capture telemetry event for hosted MemoryClient instances.
This function is designed to never raise exceptions - telemetry failures
should not affect the main application flow.
"""
if not MEM0_TELEMETRY:
return
event_data = {
"function": f"{instance.__class__.__module__}.{instance.__class__.__name__}",
}
if additional_data:
event_data.update(additional_data)
try:
event_data = {
"function": f"{instance.__class__.__module__}.{instance.__class__.__name__}",
}
if additional_data:
event_data.update(additional_data)
client_telemetry.capture_event(event_name, event_data, instance.user_email)
client_telemetry.capture_event(event_name, event_data, instance.user_email)
except Exception as e:
_logger.debug("Failed to capture client telemetry event %r: %s", event_name, e)