Send per-app provider attribution titles from one gateway app registry

Add openai_gateway/apps.py as the single list of internal gateway apps and
send X-OpenRouter-Title (plus legacy X-Title and HTTP-Referer) per app to
every upstream. Label the Devii agent, bots, modifier, quiz grading, SEO
metadata, community and sync callers. Shorten two app references that
exceeded the 30 character limit and were recorded as default.
Also includes the pending Muse Code, route health and Responses work.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 18:45:09 +01:00
co-authored by Claude Sonnet 5.5
parent ec30ed408c
commit bc11475f29
31 changed files with 2198 additions and 208 deletions
+106 -5
View File
@@ -15,7 +15,7 @@ from devplacepy.database import (
get_table,
sync_local_cache,
)
from devplacepy.services.openai_gateway import opencode_zen
from devplacepy.services.openai_gateway import muse_code, opencode_zen
logger = logging.getLogger(__name__)
@@ -29,11 +29,16 @@ KINDS = ("chat", "embed", "image")
# 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.
# and returns 429/400 to anything else, and api.meta.ai only serves
# subscription-billed keys to the real muse CLI (spoofed via
# muse_code.muse_impersonation_headers) since the 2026-10-29 rollout,
# returning 403 client_attestation_denied 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,
"muse": muse_code.muse_impersonation_headers,
}
# Upstream wire protocol a provider speaks. `chat_completions` is the
@@ -143,7 +148,13 @@ async def fetch_provider_models(
from devplacepy import stealth
headers = {"authorization": f"Bearer {api_key}"} if api_key else {}
headers: dict = {}
if provider_name:
header_hook = _header_hook_for(provider_name)
if header_hook is not None:
headers.update(header_hook())
if api_key:
headers["authorization"] = f"Bearer {api_key}"
for models_url in models_urls:
try:
async with stealth.stealth_async_client(
@@ -187,6 +198,16 @@ MODEL_TIER2_COLUMNS = (
"off_peak_discount_pct",
)
MODEL_HEALTH_COLUMNS = (
"health_status",
"health_code",
"health_message",
"health_checked_at",
)
HEALTH_OK = "ok"
HEALTH_BAD = "bad"
def ensure_tables() -> None:
db.query(
@@ -217,7 +238,10 @@ def ensure_tables() -> None:
"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, fallback_model TEXT DEFAULT '', "
"is_active INTEGER DEFAULT 1, created_at TEXT, updated_at TEXT)"
"is_active INTEGER DEFAULT 1, health_status TEXT DEFAULT '', "
"health_code TEXT DEFAULT '', health_message TEXT DEFAULT '', "
"health_checked_at TEXT DEFAULT '', "
"created_at TEXT, updated_at TEXT)"
)
models_table = get_table(MODELS_TABLE)
for column in MODEL_TIER2_COLUMNS:
@@ -225,6 +249,9 @@ def ensure_tables() -> None:
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", "")
for column in MODEL_HEALTH_COLUMNS:
if not models_table.has_column(column):
models_table.create_column_by_example(column, "")
try:
db.query(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_gateway_providers_name ON "
@@ -376,6 +403,10 @@ class ModelRoute:
off_peak_discount_pct: float
fallback_model: str
is_active: bool
health_status: str
health_code: str
health_message: str
health_checked_at: str
def _opt_float(row: dict, key: str) -> Optional[float]:
@@ -413,6 +444,10 @@ def _route_from_row(row: dict) -> ModelRoute:
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)),
health_status=str(row.get("health_status") or ""),
health_code=str(row.get("health_code") or ""),
health_message=str(row.get("health_message") or ""),
health_checked_at=str(row.get("health_checked_at") or ""),
)
@@ -583,6 +618,66 @@ def resolve_fallback(source_model: Optional[str], kind: str) -> Optional[ModelRo
return None
return fallback
MAX_FALLBACK_HOPS = 5
def is_route_healthy(route: Optional[ModelRoute]) -> bool:
return route is not None and (route.health_status or "") != HEALTH_BAD
def resolve_fallback_chain(
source_model: Optional[str], kind: str, max_hops: int = MAX_FALLBACK_HOPS
) -> list:
"""Attempt line after `source_model`: usable routes only, in order.
Inactive and unhealthy links are skipped transparently (their own
fallback is followed instead), so a bad link never breaks the line.
The visited set makes cycles impossible; max_hops caps the attempts.
"""
line = []
seen = set()
source = (source_model or "").strip()
current = source
while current and current not in seen and len(line) < max_hops:
seen.add(current)
route = model_store.get(current)
if route is None or route.kind != kind:
break
nxt = (route.fallback_model or "").strip()
if not nxt or nxt == current:
break
current = nxt
target = model_store.get(current)
if target is None or target.kind != kind:
break
if (
current != source
and target.is_active
and is_route_healthy(target)
):
line.append(target)
return line
def resolve_entry_route(
source_model: Optional[str], kind: str
) -> Optional[ModelRoute]:
"""Route a direct request starts on: healthy-next when bad, else itself.
An unhealthy route with a usable fallback line starts on the line's
first member (bad models are skipped without attempting); when the
line is empty the original route is still tried - health is advisory,
and the model may have recovered since its last test.
"""
route = model_store.resolve(source_model, kind)
if route is None or is_route_healthy(route):
return route
chain = resolve_fallback_chain(route.source_model, kind)
if chain:
return chain[0]
return route
DEEPSEEK_DEFAULT_ROUTES = (
{
"source_model": "deepseek-v4-flash",
@@ -707,6 +802,8 @@ def chat_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[dic
overlay["gateway_upstream_protocol"] = (
provider.get("protocol") or PROTOCOL_DEFAULT
)
if provider.get("client_profile"):
overlay["gateway_client_profile"] = provider["client_profile"]
if route.context_window:
from devplacepy.services.openai_gateway.usage import parse_context_map
@@ -756,6 +853,8 @@ def embed_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
header_hook = _header_hook_for(route.provider)
if header_hook is not None:
overlay["gateway_extra_request_headers"] = header_hook()
if provider.get("client_profile"):
overlay["gateway_client_profile"] = provider["client_profile"]
return overlay
@@ -781,6 +880,8 @@ def image_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di
header_hook = _header_hook_for(route.provider)
if header_hook is not None:
overlay["gateway_extra_request_headers"] = header_hook()
if provider.get("client_profile"):
overlay["gateway_client_profile"] = provider["client_profile"]
return overlay