media-skill/media_skill_deepagents/session.py

21 lines
495 B
Python

from __future__ import annotations
import re
from uuid import uuid4
_UNSAFE_ID_CHARS = re.compile(r"[^A-Za-z0-9._:-]+")
def _normalize_id(value: str) -> str:
normalized = _UNSAFE_ID_CHARS.sub("_", value.strip())
return normalized.strip("_") or "default"
def resolve_thread_id(
*,
thread_id: str | None = None,
) -> str:
"""Resolve a persistent thread id."""
if thread_id and thread_id.strip():
return _normalize_id(thread_id)
return f"thread:{uuid4()}"