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:
@@ -0,0 +1,125 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
CLIENT_THINKING_KEYS = (
|
||||
"think",
|
||||
"thinking",
|
||||
"reasoning",
|
||||
"reasoning_effort",
|
||||
"enable_thinking",
|
||||
)
|
||||
|
||||
FALSE_VALUES = {False, 0, "0", "false", "no", "off", "none", "disabled"}
|
||||
TRUE_VALUES = {True, 1, "1", "true", "yes", "on", "enabled"}
|
||||
EFFORT_VALUES = {"low", "medium", "high", "max", "xhigh"}
|
||||
|
||||
|
||||
def thinking_dialect(url: str) -> str:
|
||||
text = (url or "").lower()
|
||||
if "deepseek.com" in text:
|
||||
return "deepseek"
|
||||
if "openrouter.ai" in text:
|
||||
return "openrouter"
|
||||
if "11434" in text or "ollama" in text:
|
||||
return "ollama"
|
||||
return "deepseek"
|
||||
|
||||
|
||||
def _as_text(value: Any) -> str:
|
||||
if isinstance(value, str):
|
||||
return value.strip().lower()
|
||||
return ""
|
||||
|
||||
|
||||
def _is_false(value: Any) -> bool:
|
||||
if isinstance(value, str):
|
||||
return value.strip().lower() in FALSE_VALUES
|
||||
return value in FALSE_VALUES
|
||||
|
||||
|
||||
def _is_true(value: Any) -> bool:
|
||||
if isinstance(value, str):
|
||||
text = value.strip().lower()
|
||||
return text in TRUE_VALUES or text in EFFORT_VALUES
|
||||
return value in TRUE_VALUES
|
||||
|
||||
|
||||
def client_thinking_enabled(body: Any) -> Optional[bool]:
|
||||
if not isinstance(body, dict):
|
||||
return None
|
||||
thinking = body.get("thinking")
|
||||
if isinstance(thinking, dict) and "type" in thinking:
|
||||
kind = _as_text(thinking.get("type"))
|
||||
if kind in ("disabled", "none", "off"):
|
||||
return False
|
||||
if kind in ("enabled", "on"):
|
||||
return True
|
||||
if "think" in body:
|
||||
value = body.get("think")
|
||||
if _is_false(value):
|
||||
return False
|
||||
if _is_true(value):
|
||||
return True
|
||||
return bool(value)
|
||||
reasoning = body.get("reasoning")
|
||||
if isinstance(reasoning, dict):
|
||||
if "enabled" in reasoning:
|
||||
return _flag_enabled(reasoning.get("enabled"))
|
||||
effort = _as_text(reasoning.get("effort"))
|
||||
if effort == "none":
|
||||
return False
|
||||
if effort in EFFORT_VALUES:
|
||||
return True
|
||||
if "reasoning_effort" in body:
|
||||
effort = _as_text(body.get("reasoning_effort"))
|
||||
if effort == "none":
|
||||
return False
|
||||
if effort in EFFORT_VALUES:
|
||||
return True
|
||||
if "enable_thinking" in body:
|
||||
return _flag_enabled(body.get("enable_thinking"))
|
||||
kwargs = body.get("chat_template_kwargs")
|
||||
if isinstance(kwargs, dict) and "enable_thinking" in kwargs:
|
||||
return _flag_enabled(kwargs.get("enable_thinking"))
|
||||
return None
|
||||
|
||||
|
||||
def _flag_enabled(value: Any) -> bool:
|
||||
if _is_false(value):
|
||||
return False
|
||||
if _is_true(value):
|
||||
return True
|
||||
return bool(value)
|
||||
|
||||
|
||||
def _clear_thinking_fields(payload: dict) -> None:
|
||||
for key in CLIENT_THINKING_KEYS:
|
||||
payload.pop(key, None)
|
||||
kwargs = payload.get("chat_template_kwargs")
|
||||
if isinstance(kwargs, dict):
|
||||
kwargs.pop("enable_thinking", None)
|
||||
if not kwargs:
|
||||
payload.pop("chat_template_kwargs", None)
|
||||
|
||||
|
||||
def write_thinking(payload: dict, dialect: str, enabled: bool) -> None:
|
||||
_clear_thinking_fields(payload)
|
||||
if dialect == "openrouter":
|
||||
payload["reasoning"] = {"effort": "high" if enabled else "none"}
|
||||
return
|
||||
if dialect == "ollama":
|
||||
payload["think"] = bool(enabled)
|
||||
return
|
||||
payload["thinking"] = {"type": "enabled" if enabled else "disabled"}
|
||||
|
||||
|
||||
def apply_thinking(
|
||||
payload: dict,
|
||||
upstream_url: str,
|
||||
default_enabled: bool = False,
|
||||
) -> dict:
|
||||
specified = client_thinking_enabled(payload)
|
||||
enabled = default_enabled if specified is None else specified
|
||||
write_thinking(payload, thinking_dialect(upstream_url), enabled)
|
||||
return payload
|
||||
Reference in New Issue
Block a user