Deploy Development / deploy (push) Successful in 1m0s
Test Suite / pytest-backend (push) Successful in 50s
Test Suite / lint-backend (push) Successful in 1s
Test Suite / build-frontend (push) Successful in 20s
Test Suite / k6 /health Baseline (push) Successful in 36s
Test Suite / playwright-tests (push) Failing after 7m52s
The platform default can be replaced in the admin UI when a model is no longer available, without editing OPENROUTER_MODEL. Co-authored-by: Cursor <cursoragent@cursor.com>
137 lines
4.5 KiB
Python
137 lines
4.5 KiB
Python
"""
|
|
Superadmin: OpenRouter-Modellkatalog und Plattform-Standardmodell.
|
|
|
|
# ACCESS_LAYER exempt: Plattform-KI-Konfiguration ohne Vereinsbezug;
|
|
# require_auth + is_superadmin, kein TenantContext.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from pydantic import BaseModel, Field
|
|
|
|
from auth import require_auth
|
|
from club_tenancy import is_superadmin
|
|
from db import get_cursor, get_db, r2d
|
|
from openrouter_chat import (
|
|
OpenRouterError,
|
|
env_openrouter_model_id,
|
|
fetch_openrouter_model_catalog,
|
|
normalize_openrouter_model_id,
|
|
resolve_default_openrouter_model_id,
|
|
)
|
|
|
|
router = APIRouter(tags=["admin_openrouter"])
|
|
|
|
|
|
def _require_superadmin(session: dict = Depends(require_auth)) -> dict:
|
|
role = (session.get("role") or "").strip().lower()
|
|
if not is_superadmin(role):
|
|
raise HTTPException(status_code=403, detail="Nur Superadmins")
|
|
return session
|
|
|
|
|
|
class OpenRouterSettingsUpdate(BaseModel):
|
|
openrouter_model: Optional[str] = Field(None, max_length=200)
|
|
|
|
|
|
def _iso(value: Any) -> Optional[str]:
|
|
if value is None:
|
|
return None
|
|
if hasattr(value, "isoformat"):
|
|
return value.isoformat()
|
|
text = str(value).strip()
|
|
return text or None
|
|
|
|
|
|
def _settings_payload(stored: Optional[str], updated_at: Any = None) -> dict:
|
|
effective, source = resolve_default_openrouter_model_id(stored)
|
|
return {
|
|
"openrouter_model": stored or None,
|
|
"effective_model": effective,
|
|
"env_model": env_openrouter_model_id(),
|
|
"source": source,
|
|
"updated_at": _iso(updated_at),
|
|
}
|
|
|
|
|
|
def _missing_table(exc: Exception) -> bool:
|
|
text = str(exc).lower()
|
|
return "platform_ai_settings" in text or "does not exist" in text or "undefinedtable" in text
|
|
|
|
|
|
@router.get("/api/admin/openrouter/settings")
|
|
def get_openrouter_settings(session: dict = Depends(_require_superadmin)):
|
|
try:
|
|
with get_db() as conn:
|
|
cur = get_cursor(conn)
|
|
cur.execute(
|
|
"SELECT openrouter_model, updated_at FROM platform_ai_settings WHERE id = 1"
|
|
)
|
|
row = r2d(cur.fetchone())
|
|
except Exception as exc:
|
|
if _missing_table(exc):
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail="Tabelle platform_ai_settings fehlt. Migration 097 ausführen.",
|
|
) from exc
|
|
raise
|
|
if not row:
|
|
return _settings_payload(None, None)
|
|
stored = str(row.get("openrouter_model") or "").strip() or None
|
|
return _settings_payload(stored, row.get("updated_at"))
|
|
|
|
|
|
@router.put("/api/admin/openrouter/settings")
|
|
def put_openrouter_settings(
|
|
body: OpenRouterSettingsUpdate,
|
|
session: dict = Depends(_require_superadmin),
|
|
):
|
|
try:
|
|
model = normalize_openrouter_model_id(body.openrouter_model)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
profile_id = session.get("profile_id")
|
|
try:
|
|
with get_db() as conn:
|
|
cur = get_cursor(conn)
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO platform_ai_settings (id, openrouter_model, updated_at, updated_by_profile_id)
|
|
VALUES (1, %s, NOW(), %s)
|
|
ON CONFLICT (id) DO UPDATE
|
|
SET openrouter_model = EXCLUDED.openrouter_model,
|
|
updated_at = NOW(),
|
|
updated_by_profile_id = EXCLUDED.updated_by_profile_id
|
|
RETURNING openrouter_model, updated_at
|
|
""",
|
|
(model, profile_id),
|
|
)
|
|
row = r2d(cur.fetchone())
|
|
except Exception as exc:
|
|
if _missing_table(exc):
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail="Tabelle platform_ai_settings fehlt. Migration 097 ausführen.",
|
|
) from exc
|
|
raise
|
|
if not row:
|
|
raise HTTPException(status_code=500, detail="platform_ai_settings konnte nicht gespeichert werden.")
|
|
stored = str(row.get("openrouter_model") or "").strip() or None
|
|
return _settings_payload(stored, row.get("updated_at"))
|
|
|
|
|
|
@router.get("/api/admin/openrouter/models")
|
|
def list_openrouter_models(
|
|
refresh: bool = Query(False),
|
|
session: dict = Depends(_require_superadmin),
|
|
):
|
|
try:
|
|
return fetch_openrouter_model_catalog(force=refresh)
|
|
except OpenRouterError as exc:
|
|
raise HTTPException(
|
|
status_code=502,
|
|
detail=f"OpenRouter-Modellliste nicht erreichbar: {exc}",
|
|
) from exc
|