"""Web dependency wiring."""

from __future__ import annotations

from typing import Any

from beaver.services.agent_service import AgentService

try:
    from fastapi import HTTPException
except ModuleNotFoundError:  # pragma: no cover - fallback for skeleton-only environments
    class HTTPException(Exception):
        """Minimal fallback exception matching FastAPI's constructor shape."""

        def __init__(self, status_code: int, detail: str) -> None:
            super().__init__(detail)
            self.status_code = status_code
            self.detail = detail


def get_agent_service(request: Any) -> AgentService:
    """从 app state 里取当前宿主层托管的 AgentService。"""

    service = getattr(request.app.state, "agent_service", None)
    if not isinstance(service, AgentService):
        raise HTTPException(status_code=503, detail="AgentService is not ready")
    return service
