Deploy 2026-06-05
Browse filesCo-authored-by: OpenAI Codex <codex@openai.com>
Files changed (3)
147 unmodified lines148149150151151152153154147 unmodified linesclass LLMHealthResponse(BaseModel): """LLM provider health check response."""
status: str # "ok" | "error" status: str # "ok" | "error" | "skipped" model: str error: str | None = None error_type: str | None = (50 unmodified lines515253545556572 unmodified lines6061626364656679 unmodified lines14614714814915015115215315415515691 unmodified lines24824925024425125225324725425525625725825926026126226326426526626725526826927027127227327427550 unmodified lines
from agent.core.hf_access import get_jobs_accessfrom agent.core.hf_tokens import resolve_hf_request_tokenfrom agent.core.local_models import local_model_providerfrom agent.core.llm_params import _resolve_llm_paramsfrom agent.core.model_ids import ( CLAUDE_OPUS_48_MODEL_ID,2 unmodified lines GPT_55_MODEL_ID, KIMI_K26_MODEL_ID, MINIMAX_M27_MODEL_ID, strip_huggingface_model_prefix,)
logger = logging.getLogger(__name__)79 unmodified lines return user.get(INTERNAL_HF_TOKEN_KEY)
def _model_requires_hf_router_token(model_id: str | None) -> bool: normalized = strip_huggingface_model_prefix(model_id) or model_id or "" return local_model_provider(normalized) is None
def _reject_oversize_dataset_upload(request: Request) -> None: raw_content_length = request.headers.get("content-length") if raw_content_length is None:91 unmodified lines
@router.get("/health/llm", response_model=LLMHealthResponse)async def llm_health_check() -> LLMHealthResponse:async def llm_health_check(request: Request) -> LLMHealthResponse: """Check if the LLM provider is reachable and the API key is valid.
Makes a minimal 1-token completion call. Catches common errors: Makes a minimal 1-token completion call when a token is available. For token-less HF Router requests, returns ``status="skipped"`` instead of making an unauthenticated probe. Catches common errors: - 401 → invalid API key - 402/insufficient_quota → out of credits - 429 → rate limited - timeout / network → provider unreachable """ model = session_manager.config.model_name hf_token = resolve_hf_request_token(request) if _model_requires_hf_router_token(model) and not hf_token: return LLMHealthResponse(status="skipped", model=model)
try: llm_params = _resolve_llm_params(model, reasoning_effort="high") llm_params = _resolve_llm_params( model, hf_token, reasoning_effort="high", ) await acompletion( messages=[{"role": "user", "content": "hi"}], max_tokens=1,31 unmodified lines323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510631 unmodified lines assert agent._default_model_for_user({}) == agent.DEFAULT_FREE_MODEL_ID
@pytest.mark.asyncioasync def test_llm_health_uses_request_hf_token(monkeypatch): class Request: headers = {"Authorization": "Bearer user-token"} cookies = {}
resolved = [] completions = []
def fake_resolve_llm_params( model_name, session_hf_token=None, reasoning_effort=None, strict=False, ): resolved.append((model_name, session_hf_token, reasoning_effort, strict)) return { "model": f"openai/{model_name}", "api_base": "https://router.huggingface.co/v1", "api_key": session_hf_token, }
async def fake_acompletion(**kwargs): completions.append(kwargs)
monkeypatch.setattr( agent.session_manager, "config", SimpleNamespace(model_name=agent.DEFAULT_FREE_MODEL_ID), ) monkeypatch.setattr(agent, "_resolve_llm_params", fake_resolve_llm_params) monkeypatch.setattr(agent, "acompletion", fake_acompletion)
response = await agent.llm_health_check(Request())
assert response.status == "ok" assert resolved == [(agent.DEFAULT_FREE_MODEL_ID, "user-token", "high", False)] assert completions[0]["api_key"] == "user-token"
@pytest.mark.asyncioasync def test_llm_health_skips_router_probe_without_token(monkeypatch): class Request: headers = {} cookies = {}
def fail_resolve_llm_params(*args, **kwargs): raise AssertionError( "health check should not resolve router params without token" )
async def fail_acompletion(**kwargs): raise AssertionError("health check should not call LiteLLM without token")
monkeypatch.delenv("HF_TOKEN", raising=False) monkeypatch.setattr( agent.session_manager, "config", SimpleNamespace(model_name=agent.DEFAULT_FREE_MODEL_ID), ) monkeypatch.setattr(agent, "_resolve_llm_params", fail_resolve_llm_params) monkeypatch.setattr(agent, "acompletion", fail_acompletion)
response = await agent.llm_health_check(Request())
assert response.status == "skipped" assert response.model == agent.DEFAULT_FREE_MODEL_ID
@pytest.mark.asyncioasync def test_empty_session_model_uses_plan_default(): assert (backend/models.py+1-1
backend/routes/agent.py+20-3
tests/unit/test_agent_model_gating.py+69