"""Which tenant the current work is for, so a model call deep inside a service is put down to the right
organisation's AI credit pool without every caller passing the tenant along. Set where a tenant is known: the
quota check that precedes every AI path, and the request handlers."""
from contextvars import ContextVar
from typing import Optional

_asking: ContextVar[Optional[str]] = ContextVar("asking", default=None)


def note_who_is_asking(tenant_id: Optional[str]) -> None:
    _asking.set(str(tenant_id) if tenant_id else None)


def who_is_asking() -> Optional[str]:
    return _asking.get()
