11import logging
2+ import re
23from typing import Dict , Any
34from urllib .parse import urlparse
45
910logger = logging .getLogger ("memory_utils" )
1011
1112
13+ def _sanitize_index_component (value : str ) -> str :
14+ """Convert arbitrary text into an Elasticsearch-safe index component."""
15+ return re .sub (r"[^a-z0-9_.-]" , "_" , value .lower ())
16+
17+
1218def build_memory_config (tenant_id : str ) -> Dict [str , Any ]:
1319 """Return a fully-validated configuration dictionary for *mem0* ``Memory``.
1420 """
@@ -30,9 +36,8 @@ def build_memory_config(tenant_id: str) -> Dict[str, Any]:
3036 es_host = f"{ parsed .scheme } ://{ parsed .hostname } "
3137 es_port = parsed .port
3238 # Normalize repo/name to avoid problematic characters in index names
33- safe_repo = embed_raw ["model_repo" ].lower ().replace (
34- "/" , "_" ) if embed_raw ["model_repo" ] else ""
35- safe_name = embed_raw ["model_name" ].lower ().replace ("/" , "_" )
39+ safe_repo = _sanitize_index_component (embed_raw ["model_repo" ]) if embed_raw ["model_repo" ] else ""
40+ safe_name = _sanitize_index_component (embed_raw ["model_name" ])
3641 index_name = (
3742 f"mem0_{ safe_repo } _{ safe_name } _{ embed_raw ['max_tokens' ]} "
3843 if embed_raw ["model_repo" ]
@@ -73,4 +78,4 @@ def build_memory_config(tenant_id: str) -> Dict[str, Any]:
7378 },
7479 "telemetry" : {"enabled" : False },
7580 }
76- return memory_config
81+ return memory_config
0 commit comments