"""Per-request cache helpers for coordinator middleware and dependencies."""

from __future__ import annotations

from typing import Any

from fastapi import Request

_REQUEST_CACHE_STATE_ATTR = "_hive_request_cache"


def get_request_cache(request: Request) -> dict[tuple[Any, ...], Any]:
    cache = getattr(request.state, _REQUEST_CACHE_STATE_ATTR, None)
    if cache is None:
        cache = {}
        setattr(request.state, _REQUEST_CACHE_STATE_ATTR, cache)
    return cache
