# backend/src/agent/context.py
# #region AgentChat.Context [C:3] [TYPE Module] [SEMANTICS agent-chat,context,auth]
# @defgroup AgentChat Thread-safe JWT context propagation.
# @SIDE_EFFECT Sets ContextVar before graph.invoke(), resets after.
# @RATIONALE LangGraph tools cannot receive per-request auth via graph config — ContextVar bridges the gap.

from contextvars import ContextVar

_user_jwt: ContextVar[str | None] = ContextVar("_user_jwt", default=None)
_service_jwt: ContextVar[str | None] = ContextVar("_service_jwt", default=None)


def set_user_jwt(jwt: str) -> None:
    _user_jwt.set(jwt)


def get_user_jwt() -> str | None:
    return _user_jwt.get()


def set_service_jwt(jwt: str) -> None:
    _service_jwt.set(jwt)


def get_service_jwt() -> str | None:
    return _service_jwt.get()
# #endregion AgentChat.Context
