Files
devplacepy/tests/unit/services/openai_gateway/thinking.py
T
retoorandClaude Sonnet 5 a693a6f4d8 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
2026-09-03 08:47:57 +02:00

104 lines
3.7 KiB
Python

# retoor <retoor@molodetz.nl>
from devplacepy.services.openai_gateway.thinking import (
apply_thinking,
client_thinking_enabled,
thinking_dialect,
)
def test_dialect_from_upstream_url():
assert thinking_dialect("https://api.deepseek.com/chat/completions") == "deepseek"
assert thinking_dialect("https://openrouter.ai/api/v1/chat/completions") == "openrouter"
assert thinking_dialect("http://127.0.0.1:11434/api/chat") == "ollama"
assert thinking_dialect("http://ollama.local/v1/chat/completions") == "ollama"
assert thinking_dialect("https://unknown.example/v1/chat/completions") == "deepseek"
def test_client_intent_unspecified():
assert client_thinking_enabled({}) is None
assert client_thinking_enabled({"messages": []}) is None
assert client_thinking_enabled(None) is None
def test_client_intent_disable():
assert client_thinking_enabled({"think": False}) is False
assert client_thinking_enabled({"think": "false"}) is False
assert client_thinking_enabled({"thinking": {"type": "disabled"}}) is False
assert client_thinking_enabled({"reasoning": {"effort": "none"}}) is False
assert client_thinking_enabled({"reasoning": {"enabled": False}}) is False
assert client_thinking_enabled({"enable_thinking": False}) is False
assert client_thinking_enabled(
{"chat_template_kwargs": {"enable_thinking": False}}
) is False
def test_client_intent_enable():
assert client_thinking_enabled({"think": True}) is True
assert client_thinking_enabled({"think": "high"}) is True
assert client_thinking_enabled({"thinking": {"type": "enabled"}}) is True
assert client_thinking_enabled({"reasoning": {"effort": "low"}}) is True
assert client_thinking_enabled({"reasoning_effort": "max"}) is True
def test_default_disables_deepseek_thinking():
payload = apply_thinking(
{"messages": [{"role": "user", "content": "hi"}]},
"https://api.deepseek.com/chat/completions",
)
assert payload["thinking"] == {"type": "disabled"}
assert "think" not in payload
assert "reasoning" not in payload
assert "reasoning_effort" not in payload
def test_default_disables_openrouter_and_ollama():
openrouter = apply_thinking({}, "https://openrouter.ai/api/v1/chat/completions")
assert openrouter["reasoning"] == {"effort": "none"}
assert "thinking" not in openrouter
ollama = apply_thinking({}, "http://127.0.0.1:11434/api/chat")
assert ollama["think"] is False
assert "thinking" not in ollama
def test_client_can_enable_thinking():
payload = apply_thinking(
{"think": True, "messages": []},
"https://api.deepseek.com/chat/completions",
)
assert payload["thinking"] == {"type": "enabled"}
assert "think" not in payload
def test_client_disable_beats_admin_default_on():
payload = apply_thinking(
{"thinking": {"type": "disabled"}},
"https://api.deepseek.com/chat/completions",
default_enabled=True,
)
assert payload["thinking"] == {"type": "disabled"}
def test_admin_default_on_when_client_silent():
payload = apply_thinking(
{"messages": []},
"https://api.deepseek.com/chat/completions",
default_enabled=True,
)
assert payload["thinking"] == {"type": "enabled"}
def test_strips_conflicting_client_fields():
payload = apply_thinking(
{
"think": False,
"reasoning_effort": "high",
"chat_template_kwargs": {"enable_thinking": True, "other": 1},
},
"https://api.deepseek.com/chat/completions",
)
assert payload["thinking"] == {"type": "disabled"}
assert "think" not in payload
assert "reasoning_effort" not in payload
assert payload["chat_template_kwargs"] == {"other": 1}