Add admin-unlimited workspaces, AI gateway model fallback, and real streaming/thinking control

Admin-unlimited Dev Workspaces: an admin-owned workspace is now exempt from the
max-workspace-count limit, the max-tunnel-count limit, and the whole
idle-stop/idle-warn/retention-delete lifecycle. Resolved once in
quota.resolve() as Limits.unlimited (owner uid checked against
get_admin_uids()), consumed at the three enforcement points
(provision.ensure, provision.publish_tunnel,
WorkspaceService._advance_lifecycle). Also hardens
get_admin_uids()/get_primary_admin_uid() against a partially-schemaed users
table (uid/role column guard), which a fresh test/init_db() path could hit.

AI gateway per-model automatic fallback: any gateway_models route
(chat/embed/image) can now name a fallback_model, picked on /admin/gateway
from a select box of other configured public model names of the same kind
only (never an internal upstream model id). When a route fails after its own
retries are exhausted, the gateway retries once, automatically, against the
fallback's own provider/pricing/key, before any bytes reach the client
(including for a streaming response). One hop only, no chains or cycles;
self-reference and cross-kind fallbacks are rejected at write time.

AI gateway real upstream streaming and thinking-default control: stream:true
is now forwarded to the upstream and relayed to the client as real SSE
chunks (measured TTFT/inter-token latency) instead of a simulated split
response, and every chat/vision call explicitly disables model "thinking" by
default (admin-overridable via gateway_thinking), with per-dialect handling
for DeepSeek, OpenRouter, and Ollama.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TjdKTWgWpW2SMNW8SFqxz5
This commit is contained in:
2026-09-03 08:47:57 +02:00
co-authored by Claude Sonnet 5
parent 8ae3f628c7
commit a693a6f4d8
33 changed files with 1310 additions and 110 deletions
+26 -2
View File
@@ -87,13 +87,15 @@ def ensure_tables() -> None:
"price_cache_hit_per_m_tier2 REAL, price_cache_miss_per_m_tier2 REAL, "
"price_output_per_m_tier2 REAL, price_input_per_m_tier2 REAL, "
"off_peak_start_minute INTEGER, off_peak_end_minute INTEGER, "
"off_peak_discount_pct REAL DEFAULT 0, "
"off_peak_discount_pct REAL DEFAULT 0, fallback_model TEXT DEFAULT '', "
"is_active INTEGER DEFAULT 1, created_at TEXT, updated_at TEXT)"
)
models_table = get_table(MODELS_TABLE)
for column in MODEL_TIER2_COLUMNS:
if not models_table.has_column(column):
models_table.create_column_by_example(column, 0.0)
if not models_table.has_column("fallback_model"):
models_table.create_column_by_example("fallback_model", "")
try:
db.query(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_gateway_providers_name ON "
@@ -159,6 +161,7 @@ class ModelRouteIn(BaseModel):
off_peak_start_minute: Optional[int] = Field(default=None, ge=0, le=1439)
off_peak_end_minute: Optional[int] = Field(default=None, ge=0, le=1439)
off_peak_discount_pct: float = Field(default=0.0, ge=0, le=100)
fallback_model: str = Field(default="", max_length=128)
is_active: bool = True
@field_validator("source_model", "target_model")
@@ -169,7 +172,7 @@ class ModelRouteIn(BaseModel):
raise ValueError("Model name is required")
return value
@field_validator("provider", "vision_provider", "vision_model")
@field_validator("provider", "vision_provider", "vision_model", "fallback_model")
@classmethod
def _strip(cls, value: str) -> str:
return (value or "").strip()
@@ -192,6 +195,12 @@ class ModelRouteIn(BaseModel):
)
return self
@model_validator(mode="after")
def _check_fallback_is_not_self(self) -> "ModelRouteIn":
if self.fallback_model and self.fallback_model == self.source_model:
raise ValueError("A model cannot fall back to itself")
return self
@dataclass(frozen=True)
class ModelRoute:
@@ -214,6 +223,7 @@ class ModelRoute:
off_peak_start_minute: Optional[int]
off_peak_end_minute: Optional[int]
off_peak_discount_pct: float
fallback_model: str
is_active: bool
@@ -250,6 +260,7 @@ def _route_from_row(row: dict) -> ModelRoute:
off_peak_start_minute=_opt_int(row, "off_peak_start_minute"),
off_peak_end_minute=_opt_int(row, "off_peak_end_minute"),
off_peak_discount_pct=float(row.get("off_peak_discount_pct") or 0.0),
fallback_model=str(row.get("fallback_model") or ""),
is_active=_as_bool(row.get("is_active", 1)),
)
@@ -375,6 +386,7 @@ class ModelStore:
"off_peak_start_minute": payload.off_peak_start_minute,
"off_peak_end_minute": payload.off_peak_end_minute,
"off_peak_discount_pct": payload.off_peak_discount_pct,
"fallback_model": payload.fallback_model,
"is_active": 1 if payload.is_active else 0,
"updated_at": _now(),
}
@@ -402,6 +414,18 @@ class ModelStore:
provider_store = ProviderStore()
model_store = ModelStore()
def resolve_fallback(source_model: Optional[str], kind: str) -> Optional[ModelRoute]:
route = model_store.resolve(source_model, kind)
if route is None or not route.fallback_model:
return None
if route.fallback_model == route.source_model:
return None
fallback = model_store.resolve(route.fallback_model, kind)
if fallback is None or fallback.source_model == route.source_model:
return None
return fallback
DEEPSEEK_DEFAULT_ROUTES = (
{
"source_model": "deepseek-v4-flash",