supermemory/packages/livekit-sdk-python/src/supermemory_livekit/identifiers.py
Dhravya Shah b36b225375
feat(livekit): add persistent memory for LiveKit Agents (#1702)
Co-authored-by: Ishaan Gupta <ishaankone@gmail.com>
2026-10-01 15:26:35 -07:00

22 lines
674 B
Python

"""Map LiveKit identities onto Supermemory container tags and custom ids."""
import hashlib
import re
_INVALID = re.compile(r"[^a-zA-Z0-9_:-]+")
_MAX_LEN = 100
def to_identifier(value: str) -> str:
"""Return a stable container tag or custom id, hashing only when sanitizing."""
raw = value.strip()
if not raw:
raise ValueError("identifier is empty")
cleaned = _INVALID.sub("_", raw).strip("_") or "id"
if cleaned == raw and len(cleaned) <= _MAX_LEN:
return cleaned
digest = hashlib.sha256(raw.encode()).hexdigest()[:8]
head = cleaned[: _MAX_LEN - len(digest) - 1].strip("_") or "id"
return f"{head}_{digest}"[:_MAX_LEN]