fix(webui): hide provider models past OpenAI shutdown_date
The settings picker mirrored GET /v1/models without reading shutdown_date, so retired chat-latest snapshots stayed selectable until a turn failed. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
559107172a
commit
bd779e9f54
2 files changed
+98
No files matched your search
@@ -19,6 +19,7 @@ import secrets
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Awaitable, Callable, TypedDict, cast
|
||||
|
||||
@@ -468,6 +469,28 @@ def _model_context_window(row: Any) -> int | None:
|
||||
return None
|
||||
|
||||
|
||||
def _model_shutdown_date(row: Any) -> date | None:
|
||||
if not isinstance(row, dict):
|
||||
return None
|
||||
row_mapping = cast(dict[str, Any], row)
|
||||
raw = row_mapping.get("shutdown_date")
|
||||
if not isinstance(raw, str) or not raw.strip():
|
||||
return None
|
||||
try:
|
||||
return date.fromisoformat(raw.strip())
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _model_catalog_row_active(row: Any, *, on: date | None = None) -> bool:
|
||||
"""True when a provider catalog row should stay in the advisory model list."""
|
||||
shutdown = _model_shutdown_date(row)
|
||||
if shutdown is None:
|
||||
return True
|
||||
reference = on if on is not None else datetime.now(timezone.utc).date()
|
||||
return shutdown > reference
|
||||
|
||||
|
||||
def _model_row_payload(row: Any) -> dict[str, Any] | None:
|
||||
model_id = _model_id_from_row(row)
|
||||
if not model_id:
|
||||
@@ -504,6 +527,8 @@ def _extract_model_rows(body: Any) -> list[dict[str, Any]]:
|
||||
rows: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for raw_row in cast(list[object], raw_rows):
|
||||
if not _model_catalog_row_active(raw_row):
|
||||
continue
|
||||
row = _model_row_payload(raw_row)
|
||||
if row is None or row["id"] in seen:
|
||||
continue
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import builtins
|
||||
import json
|
||||
import time
|
||||
from datetime import date
|
||||
from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
@@ -42,6 +43,10 @@ from nanobot.webui.settings_api import (
|
||||
update_transcription_settings,
|
||||
update_web_search_settings,
|
||||
)
|
||||
from nanobot.webui.settings_models import (
|
||||
_extract_model_rows,
|
||||
_model_catalog_row_active,
|
||||
)
|
||||
from nanobot.webui.settings_services import WebUIOAuthFlowRegistry
|
||||
|
||||
DYNAMIC_PROVIDER_NAME = "my-company-api"
|
||||
@@ -2182,6 +2187,74 @@ def test_provider_models_payload_fetches_openai_compatible_models(
|
||||
assert payload["models"][1]["context_window"] == 65536
|
||||
|
||||
|
||||
def test_model_catalog_row_active_respects_openai_shutdown_date() -> None:
|
||||
ref = date(2026, 9, 29)
|
||||
live = {"id": "gpt-5-nano", "shutdown_date": None}
|
||||
retired = {"id": "gpt-5-chat-latest", "shutdown_date": "2026-07-23"}
|
||||
scheduled = {"id": "gpt-5.9-chat-latest", "shutdown_date": "2026-12-01"}
|
||||
|
||||
assert _model_catalog_row_active(live, on=ref)
|
||||
assert not _model_catalog_row_active(retired, on=ref)
|
||||
assert _model_catalog_row_active(scheduled, on=ref)
|
||||
assert not _model_catalog_row_active(retired, on=date(2026, 7, 23))
|
||||
assert _model_catalog_row_active(retired, on=date(2026, 7, 22))
|
||||
|
||||
|
||||
def test_extract_model_rows_skips_shut_down_openai_models(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_models._model_catalog_row_active",
|
||||
lambda row, *, on=None: _model_catalog_row_active(row, on=date(2026, 9, 29)),
|
||||
)
|
||||
body = {
|
||||
"data": [
|
||||
{"id": "gpt-5-nano", "shutdown_date": None},
|
||||
{"id": "gpt-5-chat-latest", "shutdown_date": "2026-07-23"},
|
||||
{"id": "chat-latest", "shutdown_date": None},
|
||||
]
|
||||
}
|
||||
rows = _extract_model_rows(body)
|
||||
ids = [row["id"] for row in rows]
|
||||
assert ids == ["gpt-5-nano", "chat-latest"]
|
||||
|
||||
|
||||
def test_provider_models_payload_omits_openai_models_past_shutdown_date(
|
||||
tmp_path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
config_path = tmp_path / "config.json"
|
||||
config = Config()
|
||||
config.providers.openai.api_key = "sk-test"
|
||||
save_config(config, config_path)
|
||||
monkeypatch.setattr("nanobot.config.loader._current_config_path", config_path)
|
||||
|
||||
def fake_get(url: str, **kwargs):
|
||||
assert url == "https://api.openai.com/v1/models"
|
||||
return httpx.Response(
|
||||
200,
|
||||
json={
|
||||
"data": [
|
||||
{"id": "gpt-5-nano", "owned_by": "system", "shutdown_date": None},
|
||||
{"id": "gpt-5-chat-latest", "owned_by": "system", "shutdown_date": "2026-07-23"},
|
||||
]
|
||||
},
|
||||
request=httpx.Request("GET", url),
|
||||
)
|
||||
|
||||
monkeypatch.setattr("nanobot.webui.settings_api.httpx.get", fake_get)
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_models._model_catalog_row_active",
|
||||
lambda row, *, on=None: _model_catalog_row_active(row, on=date(2026, 9, 29)),
|
||||
)
|
||||
|
||||
payload = provider_models_payload({"provider": ["openai"]})
|
||||
|
||||
assert payload["status"] == "available"
|
||||
assert payload["model_count"] == 1
|
||||
assert payload["models"][0]["id"] == "gpt-5-nano"
|
||||
|
||||
|
||||
def test_provider_models_payload_returns_online_openai_codex_models(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
|
||||
Reference in new issue
Block a user