Fix endpoint model preservation for tasks

This commit is contained in:
pewdiepie-archdaemon
2026-06-02 09:44:24 +09:00
parent d60ff44c1b
commit 6a78b02976
2 changed files with 25 additions and 19 deletions
+17 -15
View File
@@ -35,6 +35,18 @@ def _first_chat_model(models) -> Optional[str]:
return (models[0] if models else None)
def _endpoint_cached_models(ep) -> list:
"""Return cached model ids from the current or legacy endpoint field."""
raw = getattr(ep, "cached_models", None) or getattr(ep, "models", None)
if not raw:
return []
try:
models = json.loads(raw) if isinstance(raw, str) else raw
except Exception:
return []
return models if isinstance(models, list) else []
# Cache for Tailscale hostname → IP resolution
_tailscale_cache: Dict[str, Optional[str]] = {}
@@ -236,14 +248,9 @@ def resolve_endpoint(
chat_url = build_chat_url(base)
headers = build_headers(ep.api_key, base)
# If no model specified, try to pick the first from endpoint's cached list
if not model and hasattr(ep, 'models') and ep.models:
try:
models = json.loads(ep.models) if isinstance(ep.models, str) else ep.models
if models:
model = _first_chat_model(models)
except Exception:
pass
# If no model specified, try to pick the first from endpoint's cached list.
if not model:
model = _first_chat_model(_endpoint_cached_models(ep)) or ""
return chat_url, model or fallback_model, headers
except Exception as e:
@@ -275,13 +282,8 @@ def resolve_endpoint_by_id(
chat_url = build_chat_url(base)
headers = build_headers(ep.api_key, base)
m = (model or "").strip()
if not m and getattr(ep, "models", None):
try:
models = json.loads(ep.models) if isinstance(ep.models, str) else ep.models
if models:
m = _first_chat_model(models) or ""
except Exception:
pass
if not m:
m = _first_chat_model(_endpoint_cached_models(ep)) or ""
if not m:
return None
return chat_url, m, headers