Fix endpoint model preservation for tasks
This commit is contained in:
@@ -1402,12 +1402,18 @@ def setup_model_routes(model_discovery):
|
|||||||
return sess in variants or sess.startswith(base + "/")
|
return sess in variants or sess.startswith(base + "/")
|
||||||
|
|
||||||
def _clear_sessions_for_endpoint(db, base_url: str) -> int:
|
def _clear_sessions_for_endpoint(db, base_url: str) -> int:
|
||||||
|
"""Drop stored auth for sessions using an endpoint being deleted.
|
||||||
|
|
||||||
|
Keep the session's endpoint URL and model intact. If the admin is
|
||||||
|
replacing an endpoint with the same URL, clearing those fields leaves
|
||||||
|
the UI looking selected while chat requests arrive with an empty model.
|
||||||
|
The chat-time orphan guard still clears truly dead endpoints when no
|
||||||
|
matching enabled endpoint exists.
|
||||||
|
"""
|
||||||
cleared = 0
|
cleared = 0
|
||||||
rows = db.query(DbSession).filter(DbSession.endpoint_url.isnot(None)).all()
|
rows = db.query(DbSession).filter(DbSession.endpoint_url.isnot(None)).all()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
if _session_uses_endpoint_url(row.endpoint_url or "", base_url):
|
if _session_uses_endpoint_url(row.endpoint_url or "", base_url):
|
||||||
row.endpoint_url = ""
|
|
||||||
row.model = ""
|
|
||||||
row.headers = {}
|
row.headers = {}
|
||||||
row.updated_at = datetime.utcnow()
|
row.updated_at = datetime.utcnow()
|
||||||
cleared += 1
|
cleared += 1
|
||||||
@@ -1425,8 +1431,6 @@ def setup_model_routes(model_discovery):
|
|||||||
try:
|
try:
|
||||||
for sess in list(getattr(manager, "sessions", {}).values()):
|
for sess in list(getattr(manager, "sessions", {}).values()):
|
||||||
if _session_uses_endpoint_url(getattr(sess, "endpoint_url", "") or "", base_url):
|
if _session_uses_endpoint_url(getattr(sess, "endpoint_url", "") or "", base_url):
|
||||||
sess.endpoint_url = ""
|
|
||||||
sess.model = ""
|
|
||||||
sess.headers = {}
|
sess.headers = {}
|
||||||
cleared += 1
|
cleared += 1
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@@ -35,6 +35,18 @@ def _first_chat_model(models) -> Optional[str]:
|
|||||||
return (models[0] if models else None)
|
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
|
# Cache for Tailscale hostname → IP resolution
|
||||||
_tailscale_cache: Dict[str, Optional[str]] = {}
|
_tailscale_cache: Dict[str, Optional[str]] = {}
|
||||||
|
|
||||||
@@ -236,14 +248,9 @@ def resolve_endpoint(
|
|||||||
chat_url = build_chat_url(base)
|
chat_url = build_chat_url(base)
|
||||||
headers = build_headers(ep.api_key, base)
|
headers = build_headers(ep.api_key, base)
|
||||||
|
|
||||||
# If no model specified, try to pick the first from endpoint's cached list
|
# If no model specified, try to pick the first from endpoint's cached list.
|
||||||
if not model and hasattr(ep, 'models') and ep.models:
|
if not model:
|
||||||
try:
|
model = _first_chat_model(_endpoint_cached_models(ep)) or ""
|
||||||
models = json.loads(ep.models) if isinstance(ep.models, str) else ep.models
|
|
||||||
if models:
|
|
||||||
model = _first_chat_model(models)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return chat_url, model or fallback_model, headers
|
return chat_url, model or fallback_model, headers
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -275,13 +282,8 @@ def resolve_endpoint_by_id(
|
|||||||
chat_url = build_chat_url(base)
|
chat_url = build_chat_url(base)
|
||||||
headers = build_headers(ep.api_key, base)
|
headers = build_headers(ep.api_key, base)
|
||||||
m = (model or "").strip()
|
m = (model or "").strip()
|
||||||
if not m and getattr(ep, "models", None):
|
if not m:
|
||||||
try:
|
m = _first_chat_model(_endpoint_cached_models(ep)) or ""
|
||||||
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:
|
if not m:
|
||||||
return None
|
return None
|
||||||
return chat_url, m, headers
|
return chat_url, m, headers
|
||||||
|
|||||||
Reference in New Issue
Block a user