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
This commit is contained in:
@@ -15,6 +15,7 @@ from devplacepy.database import (
|
||||
get_table,
|
||||
sync_local_cache,
|
||||
)
|
||||
from devplacepy.services.openai_gateway import opencode_zen
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -23,6 +24,18 @@ MODELS_TABLE = "gateway_models"
|
||||
CACHE_NAME = "gateway_routing"
|
||||
KINDS = ("chat", "embed", "image")
|
||||
|
||||
# Admin-selectable on a gateway_providers row's `client_profile` field. A
|
||||
# generic provider (the default, empty profile) sends no extra headers -
|
||||
# this exists only for upstreams that reject requests unless they see a
|
||||
# specific client's headers, e.g. OpenCode Zen's free tier only serves the
|
||||
# real opencode CLI (spoofed via opencode_zen.opencode_impersonation_headers)
|
||||
# and returns 429/400 to anything else. Adding a new profile is one entry
|
||||
# here plus one option in the provider form - no other special-casing.
|
||||
CLIENT_PROFILES: dict = {
|
||||
"": None,
|
||||
"opencode": opencode_zen.opencode_impersonation_headers,
|
||||
}
|
||||
|
||||
_ROUTING_CACHE: dict = {}
|
||||
|
||||
|
||||
@@ -56,6 +69,63 @@ def _image_url_from_base(base_url: str) -> str:
|
||||
return base_url
|
||||
|
||||
|
||||
def _models_url_from_base(base_url: str) -> str:
|
||||
base_url = (base_url or "").strip()
|
||||
if not base_url:
|
||||
return ""
|
||||
if base_url.endswith("/chat/completions"):
|
||||
return base_url[: -len("/chat/completions")] + "/models"
|
||||
return base_url.rstrip("/") + "/models"
|
||||
|
||||
|
||||
PROVIDER_MODELS_PROBE_TIMEOUT = 8.0
|
||||
|
||||
|
||||
def _default_provider_credentials() -> tuple[str, str]:
|
||||
from devplacepy.services.manager import service_manager
|
||||
|
||||
svc = service_manager.get_service("openai")
|
||||
cfg = svc.get_config() if svc is not None else {}
|
||||
return cfg.get("gateway_upstream_url", ""), cfg.get("gateway_api_key", "")
|
||||
|
||||
|
||||
async def fetch_provider_models(
|
||||
provider_name: str, timeout: float = PROVIDER_MODELS_PROBE_TIMEOUT
|
||||
) -> Optional[list[str]]:
|
||||
provider_name = (provider_name or "").strip()
|
||||
if provider_name:
|
||||
provider = provider_store.get(provider_name)
|
||||
if provider is None:
|
||||
return None
|
||||
base_url, api_key = provider.get("base_url", ""), provider.get("api_key", "")
|
||||
else:
|
||||
base_url, api_key = _default_provider_credentials()
|
||||
models_url = _models_url_from_base(base_url)
|
||||
if not models_url:
|
||||
return None
|
||||
|
||||
from devplacepy import stealth
|
||||
|
||||
headers = {"authorization": f"Bearer {api_key}"} if api_key else {}
|
||||
try:
|
||||
async with stealth.stealth_async_client(timeout=timeout, headers=headers) as client:
|
||||
response = await client.get(models_url)
|
||||
except Exception as exc:
|
||||
logger.info("gateway provider model listing failed for %s: %s", models_url, exc)
|
||||
return None
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
try:
|
||||
payload = response.json()
|
||||
except Exception:
|
||||
return None
|
||||
rows = payload.get("data") if isinstance(payload, dict) else None
|
||||
if not isinstance(rows, list):
|
||||
return None
|
||||
ids = [str(row["id"]) for row in rows if isinstance(row, dict) and row.get("id")]
|
||||
return ids or None
|
||||
|
||||
|
||||
MODEL_TIER2_COLUMNS = (
|
||||
"context_tier_threshold_tokens",
|
||||
"price_cache_hit_per_m_tier2",
|
||||
@@ -73,8 +143,12 @@ def ensure_tables() -> None:
|
||||
"CREATE TABLE IF NOT EXISTS "
|
||||
+ PROVIDERS_TABLE
|
||||
+ " (id INTEGER PRIMARY KEY, name TEXT, base_url TEXT, api_key TEXT, "
|
||||
"is_active INTEGER DEFAULT 1, created_at TEXT, updated_at TEXT)"
|
||||
"is_active INTEGER DEFAULT 1, client_profile TEXT DEFAULT '', "
|
||||
"created_at TEXT, updated_at TEXT)"
|
||||
)
|
||||
providers_table = get_table(PROVIDERS_TABLE)
|
||||
if not providers_table.has_column("client_profile"):
|
||||
providers_table.create_column_by_example("client_profile", "")
|
||||
db.query(
|
||||
"CREATE TABLE IF NOT EXISTS "
|
||||
+ MODELS_TABLE
|
||||
@@ -121,6 +195,7 @@ class ProviderIn(BaseModel):
|
||||
base_url: str = Field(default="", max_length=500)
|
||||
api_key: str = Field(default="", max_length=400)
|
||||
is_active: bool = True
|
||||
client_profile: str = Field(default="", max_length=32)
|
||||
|
||||
@field_validator("name")
|
||||
@classmethod
|
||||
@@ -140,6 +215,16 @@ class ProviderIn(BaseModel):
|
||||
raise ValueError("Base URL must be a http(s) URL")
|
||||
return value
|
||||
|
||||
@field_validator("client_profile")
|
||||
@classmethod
|
||||
def _clean_client_profile(cls, value: str) -> str:
|
||||
value = (value or "").strip().lower()
|
||||
if value not in CLIENT_PROFILES:
|
||||
raise ValueError(
|
||||
f"client_profile must be one of: {', '.join(sorted(CLIENT_PROFILES)) or '(none)'}"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
class ModelRouteIn(BaseModel):
|
||||
source_model: str = Field(min_length=1, max_length=128)
|
||||
@@ -280,6 +365,7 @@ def _load() -> dict:
|
||||
"base_url": str(row.get("base_url") or ""),
|
||||
"api_key": str(row.get("api_key") or ""),
|
||||
"is_active": _as_bool(row.get("is_active", 1)),
|
||||
"client_profile": str(row.get("client_profile") or ""),
|
||||
}
|
||||
if MODELS_TABLE in db.tables:
|
||||
for row in get_table(MODELS_TABLE).all():
|
||||
@@ -317,6 +403,7 @@ class ProviderStore:
|
||||
"base_url": payload.base_url,
|
||||
"api_key": payload.api_key,
|
||||
"is_active": 1 if payload.is_active else 0,
|
||||
"client_profile": payload.client_profile,
|
||||
"updated_at": _now(),
|
||||
}
|
||||
if existing:
|
||||
@@ -500,6 +587,15 @@ def seed_default_deepseek_routes() -> None:
|
||||
_ROUTING_CACHE.clear()
|
||||
|
||||
|
||||
def _header_hook_for(provider_name: str):
|
||||
if not provider_name:
|
||||
return None
|
||||
provider = provider_store.get(provider_name)
|
||||
if provider is None:
|
||||
return None
|
||||
return CLIENT_PROFILES.get(provider.get("client_profile") or "")
|
||||
|
||||
|
||||
def _provider_overlay(name: str, base_key: str, url_key: str, overlay: dict) -> None:
|
||||
provider = provider_store.get(name)
|
||||
if provider is None:
|
||||
@@ -527,11 +623,15 @@ def chat_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[dic
|
||||
"gateway_off_peak_start_minute": route.off_peak_start_minute,
|
||||
"gateway_off_peak_end_minute": route.off_peak_end_minute,
|
||||
"gateway_off_peak_discount_pct": route.off_peak_discount_pct,
|
||||
"gateway_provider_name": route.provider or "default",
|
||||
}
|
||||
if route.provider:
|
||||
_provider_overlay(
|
||||
route.provider, "gateway_api_key", "gateway_upstream_url", overlay
|
||||
)
|
||||
header_hook = _header_hook_for(route.provider)
|
||||
if header_hook is not None:
|
||||
overlay["gateway_extra_request_headers"] = header_hook()
|
||||
if route.context_window:
|
||||
from devplacepy.services.openai_gateway.usage import parse_context_map
|
||||
|
||||
@@ -570,6 +670,7 @@ def embed_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
|
||||
"gateway_off_peak_start_minute": route.off_peak_start_minute,
|
||||
"gateway_off_peak_end_minute": route.off_peak_end_minute,
|
||||
"gateway_off_peak_discount_pct": route.off_peak_discount_pct,
|
||||
"gateway_provider_name": route.provider or "default",
|
||||
}
|
||||
provider = provider_store.get(route.provider) if route.provider else None
|
||||
if provider:
|
||||
@@ -577,6 +678,9 @@ def embed_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
|
||||
overlay["gateway_embed_url"] = _embed_url_from_base(provider["base_url"])
|
||||
if provider.get("api_key"):
|
||||
overlay["gateway_embed_key"] = provider["api_key"]
|
||||
header_hook = _header_hook_for(route.provider)
|
||||
if header_hook is not None:
|
||||
overlay["gateway_extra_request_headers"] = header_hook()
|
||||
return overlay
|
||||
|
||||
|
||||
@@ -591,6 +695,7 @@ def image_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
|
||||
"gateway_off_peak_start_minute": route.off_peak_start_minute,
|
||||
"gateway_off_peak_end_minute": route.off_peak_end_minute,
|
||||
"gateway_off_peak_discount_pct": route.off_peak_discount_pct,
|
||||
"gateway_provider_name": route.provider or "default",
|
||||
}
|
||||
provider = provider_store.get(route.provider) if route.provider else None
|
||||
if provider:
|
||||
@@ -598,6 +703,9 @@ def image_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
|
||||
overlay["gateway_image_url"] = _image_url_from_base(provider["base_url"])
|
||||
if provider.get("api_key"):
|
||||
overlay["gateway_image_key"] = provider["api_key"]
|
||||
header_hook = _header_hook_for(route.provider)
|
||||
if header_hook is not None:
|
||||
overlay["gateway_extra_request_headers"] = header_hook()
|
||||
return overlay
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user