Files
devplacepy/devplacepy/services/openai_gateway/thinking.py
T
retoorandClaude Sonnet 5 569f1dcc64 Add OpenCode Zen support, model health/stats dashboard, and gateway fallback fixes
AI gateway:
- Add a generic, admin-selectable `client_profile` field on gateway_providers
  (e.g. "opencode") so a provider needing special request headers (OpenCode
  Zen's client-identity spoofing) is configured like any other provider, not
  hardcoded by name.
- Track per-(provider, model) reliability/speed/latency health in memory,
  seeded from the existing gateway_usage_ledger at startup - purely
  observational, never influences routing.
- New Stats tab on /admin/gateway: request volume, latency, per-model
  breakdowns, and reliability weight, charted with a vendored Chart.js and
  devplace's own theme tokens.
- Record which model a failed request actually fell back to
  (fallback_used_route), surfaced in the Recent Failures table.
- Stop excluding context_length errors from fallback, and skip a primary
  attempt outright when its known context window is already too small for
  the estimated request size, going straight to the fallback.
- gateway_usage_ledger's provider/fallback_used_route columns and indexes
  are ensured centrally in database/schema.py's init_db(), the single point
  of truth for this table's schema.
- Non-OpenAI upstream routing and client-model passthrough; trust only the
  upstream's own X-Gateway-Model header for served-model attribution.

Devii agent:
- Fix a real lockup: plan/verify tools could be individually disabled via
  the admin tool toggles while still being required by the protocol gate,
  permanently bricking any task that needed tools. They can no longer be
  disabled, and the gate now also checks the tool is actually offered.
- Fix compaction being silently calibrated for a 1M-token model while
  running a much smaller one: context budget is now percentage-based and
  the summarizer's own request is sized to fit the real model.
- Give a specific, actionable retry message when plan()'s own arguments get
  cut off by the output limit, and tighten its schema to discourage
  overlong plans.

Other:
- Backup service: offload completed backups to a remote Hetzner Storage Box.
- Container manager: fix orphan blob leaks from sync races, add a two-phase
  plan/execute `system prune` CLI command.
- Admin gateway UI: replace the JS-rendered model/provider tables with
  server-rendered forms and pages.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DhmEkvutuwtzFVcLbTrhdo
2026-09-09 07:38:24 +02:00

175 lines
5.5 KiB
Python

# retoor <retoor@molodetz.nl>
from dataclasses import dataclass
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 _resolve_dialect(url: str, dialect: str) -> str:
effective = dialect.strip().lower()
if effective in ("", "auto"):
return thinking_dialect(url)
return effective
@dataclass(frozen=True)
class UpstreamCapabilities:
dialect: str
supports_stream_options: bool
supports_stream_usage: bool
supports_thinking_field: bool
def upstream_capabilities(
url: str, dialect: str = "", ollama_stream_usage: bool = False
) -> UpstreamCapabilities:
# Current Ollama docs list stream_options.include_usage as supported, but
# real deployed server versions vary and some reject it outright (see
# github.com/ollama/ollama issues #15288/#15293/#14820) - default off,
# gateway_ollama_stream_usage lets an operator opt in for a known-good build.
effective = _resolve_dialect(url, dialect)
if effective == "ollama":
return UpstreamCapabilities(
dialect=effective,
supports_stream_options=bool(ollama_stream_usage),
supports_stream_usage=bool(ollama_stream_usage),
supports_thinking_field=True,
)
return UpstreamCapabilities(
dialect=effective,
supports_stream_options=True,
supports_stream_usage=True,
supports_thinking_field=True,
)
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":
# Ollama's native /api/chat honors a boolean `think`, but its
# OpenAI-compatible /v1/chat/completions layer does not (confirmed
# against github.com/ollama/ollama issues #15288/#15293/#14820,
# Sep 2026): that endpoint maps `reasoning_effort`/`reasoning.effort`
# onto its internal think flag instead. Send both - `think` is
# harmless if ignored, `reasoning_effort` is the field the
# OpenAI-compat endpoint actually honors.
payload["think"] = bool(enabled)
payload["reasoning_effort"] = "high" if enabled else "none"
return
payload["thinking"] = {"type": "enabled" if enabled else "disabled"}
def apply_thinking(
payload: dict,
upstream_url: str,
default_enabled: bool = False,
dialect: str = "",
) -> dict:
effective = _resolve_dialect(upstream_url, dialect)
specified = client_thinking_enabled(payload)
enabled = default_enabled if specified is None else specified
write_thinking(payload, effective, enabled)
return payload