"""Helpers shared by the admin endpoints."""
import re
import structlog
from fastapi import HTTPException
log = structlog.get_logger()
_MCP_NAME_RE = re.compile(r"^[\w_-]+$")
def _invalidate_prompt_cache() -> None:
"""Drop the cached system prompts after a profile's shape changed.
Every other profile's prompt embeds the "Available profiles" block, so a
toggle here invalidates all of them, not just this one. Best effort: the
cache is an optimisation, and a failure must not fail the admin write.
"""
try:
from navi.api.deps import get_agent
get_agent().invalidate_prompt_cache()
except Exception:
log.warning("admin.prompt_cache_invalidate_failed", exc_info=True)
def _validate_mcp_name(name: str) -> None:
if not _MCP_NAME_RE.match(name):
raise HTTPException(
status_code=400,
detail="Invalid server name. Use only letters, digits, underscores, and hyphens.",
)