Files
DeepTutor/deeptutor/services/model_selection/fallbacks.py
yaqi d5574b2c51 feat(chat): name the failing provider and the unused alternatives
Part of #2693, visibility only: the turn still runs the one active model, but a
failed turn no longer reads as "the provider connection was interrupted" with
no name, no attempt count, and no hint that an alternative is already
configured.

- `provider_visibility.py` builds the retry and final-failure copy from a
  pipeline's binding, model and endpoint host, plus the number of configured
  fallbacks (`services/model_selection/fallbacks.py`: read-only, bounded,
  tolerant of a hand-edited catalog).
- Non-transport provider errors (401/403/429/5xx, model-not-found) are mapped
  through the existing error taxonomy before they are raised, so the user reads
  "provider X returned a server-side error" instead of the SDK's raw
  "Error code: 502" — while the typed error, status code and retry flags that
  the loop and the forced-finish path branch on are preserved.
- The failure trace metadata gains `provider`, `attempts` and
  `fallbacks_configured`.
- zh and en chat prompt copy updated; other capabilities keep the code defaults
  until their prompt packs are updated.

No failover yet: switching, cooldowns and failback land in P1.
2026-11-24 15:27:37 +08:00

82 lines
2.7 KiB
Python

"""Read-only view of the deployment's fallback providers (issue #2693, P0).
P0 is visibility only: DeepTutor still runs the one active model. This module
answers "how many alternatives are already configured?" so a failed turn can
say so instead of a bare "please retry".
The real failover (ordered candidates, cooldown, failback) is a later phase and
does not use anything here beyond the same catalog key.
"""
from __future__ import annotations
from typing import Any
#: ``services.llm`` key holding the ordered fallback list. Bounded here too:
#: a hand-edited file must not be able to make this scan unbounded.
FALLBACKS_KEY = "fallbacks"
#: Mirrors the planned chain cap. Visibility never needs more than this.
MAX_FALLBACKS = 4
def _entry(value: Any) -> dict[str, str] | None:
"""One well-formed ``{profile_id, model_id}`` entry, or ``None``."""
if not isinstance(value, dict):
return None
profile_id = str(value.get("profile_id") or "").strip()
model_id = str(value.get("model_id") or "").strip()
if not profile_id or not model_id:
return None
return {"profile_id": profile_id, "model_id": model_id}
def configured_fallbacks(catalog: dict[str, Any] | None = None) -> list[dict[str, str]]:
"""The fallback entries the catalog declares, de-duplicated and bounded.
Tolerant on purpose: this runs on an error path, so an unreadable catalog or
a half-written entry must never turn "the provider is down" into a second
exception.
"""
if catalog is None:
try:
from deeptutor.services.config import get_model_catalog_service
catalog = get_model_catalog_service().load()
except Exception: # pragma: no cover - settings must not break an error path
return []
services = catalog.get("services") if isinstance(catalog, dict) else None
llm = services.get("llm") if isinstance(services, dict) else None
declared = llm.get(FALLBACKS_KEY) if isinstance(llm, dict) else None
if not isinstance(declared, list):
return []
entries: list[dict[str, str]] = []
seen: set[tuple[str, str]] = set()
for raw in declared:
entry = _entry(raw)
if entry is None:
continue
key = (entry["profile_id"], entry["model_id"])
if key in seen:
continue
seen.add(key)
entries.append(entry)
if len(entries) >= MAX_FALLBACKS:
break
return entries
def fallback_count(catalog: dict[str, Any] | None = None) -> int:
"""How many fallback candidates are configured (0 when the key is absent)."""
return len(configured_fallbacks(catalog))
__all__ = [
"FALLBACKS_KEY",
"MAX_FALLBACKS",
"configured_fallbacks",
"fallback_count",
]