An upstream that rejects a key the gateway adds (e.g. thinking) now gets retried with other shapes on a parsable 400/422; the working method is stored per (provider, target model) and applied first-try from then on. Fallback attempts are re-shaped per attempt. Admin list/reset, Devii tools, audit event and ledger resolved_method included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
300 lines
20 KiB
HTML
300 lines
20 KiB
HTML
{% extends "admin_base.html" %}
|
|
{% block extra_head %}
|
|
{{ super() }}
|
|
<link rel="stylesheet" href="{{ static_url('/static/css/gateway.css') }}">
|
|
{% endblock %}
|
|
{% block admin_content %}
|
|
<div class="admin-toolbar">
|
|
<h2>Gateway routing</h2>
|
|
<span class="admin-count">{{ providers|length }} providers, {{ models|length }} routes, {{ quota_rules|length }} quota rules</span>
|
|
</div>
|
|
<p class="gw-intro">Map any requested model name onto a provider and target model, each with its own pricing economy and an optional vision model for image to text merging. Image routes use a flat per-image price. Unmapped requests fall through to the default upstream unchanged.</p>
|
|
|
|
<nav class="admin-tabs" aria-label="Gateway configuration">
|
|
<a href="/admin/gateway?tab=models" class="admin-tab {% if tab == 'models' %}active{% endif %}">Models <span class="admin-tab-count">{{ models|length }}</span></a>
|
|
<a href="/admin/gateway?tab=providers" class="admin-tab {% if tab == 'providers' %}active{% endif %}">Providers <span class="admin-tab-count">{{ providers|length }}</span></a>
|
|
<a href="/admin/gateway?tab=quota" class="admin-tab {% if tab == 'quota' %}active{% endif %}">Quota rules <span class="admin-tab-count">{{ quota_rules|length }}</span></a>
|
|
<a href="/admin/gateway?tab=stats" class="admin-tab {% if tab == 'stats' %}active{% endif %}">Stats</a>
|
|
</nav>
|
|
|
|
{% if tab == 'models' %}
|
|
<section class="gw-section">
|
|
<div class="gw-section-head">
|
|
<h3>Model routes</h3>
|
|
<form method="post" action="/admin/gateway/models/test-all" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm">Test all</button>
|
|
</form>
|
|
<a href="/admin/gateway/models/new" class="admin-btn admin-btn-primary admin-btn-sm">Add model route</a>
|
|
</div>
|
|
<p class="gw-section-hint">Each source model maps to a provider and target model with its own economy. Chat uses cache-hit, cache-miss and output prices (USD per 1M tokens); embeddings use the input price; image routes use the input price as a flat USD per image; a vision model adds input and output pricing for the image description merge. An optional fallback model is followed link by link, automatically, whenever this route fails after its own retries are exhausted; unhealthy and inactive links are skipped. Test sends one tiny real call per route (image tests generate one hedgehog-and-bear picture, shown after); a failing route is hidden from the model list until a later test passes.</p>
|
|
<div class="admin-table-wrap">
|
|
<table class="admin-table">
|
|
<caption class="sr-only">Model routes</caption>
|
|
<thead>
|
|
<tr><th scope="col">Source</th><th scope="col">Provider</th><th scope="col">Target</th><th scope="col">Kind</th><th scope="col">Vision</th><th scope="col">Economy</th><th scope="col">Health</th><th scope="col" class="gw-actions">Actions</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{% if not models %}
|
|
<tr><td colspan="8" class="admin-empty">No model routes. Requests fall through to the default upstream.</td></tr>
|
|
{% endif %}
|
|
{% for m in models %}
|
|
<tr{% if m.health_status == 'bad' %} class="gw-row-bad"{% endif %}>
|
|
<td>{{ m.source_model }}</td>
|
|
<td>{{ m.provider or "default" }}</td>
|
|
<td><code class="gw-code">{{ m.target_model }}</code></td>
|
|
<td>{{ m.kind }}</td>
|
|
<td>
|
|
{% if m.vision_model %}
|
|
<code class="gw-code">{{ m.vision_provider or m.provider or "default" }}/{{ m.vision_model }}</code>
|
|
{% else %}
|
|
<span class="gw-muted">-</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
{% if m.context_tier_threshold_tokens %}<span class="gw-badge" title="Tier-2 rates above {{ m.context_tier_threshold_tokens }} input tokens">tiered</span>{% endif %}
|
|
{% if m.off_peak_start_minute is not none %}<span class="gw-badge" title="{{ m.off_peak_discount_pct }}% off during the configured UTC window">off-peak</span>{% endif %}
|
|
{% if m.kind == 'image' and m.price_input_per_m %}<span class="gw-badge" title="Flat price per generated image">${{ m.price_input_per_m }}/img</span>{% endif %}
|
|
{% if m.fallback_model %}<span class="gw-badge" title="Falls back to {{ m.fallback_model }} on failure">fallback: {{ m.fallback_model }}</span>{% endif %}
|
|
{% if m.tools_mode == 'emulated' %}<span class="gw-badge" title="This model does not support tool calling; tools are emulated through the prompt">tools: emulated</span>{% endif %}
|
|
{% if m.learned_shape %}
|
|
<span class="gw-badge" title="Request shape learned automatically after the upstream rejected the default">shape: {{ m.learned_shape }}</span>
|
|
<form method="post" action="/admin/gateway/learned/reset-page" class="gw-inline-form">
|
|
<input type="hidden" name="provider" value="{{ m.learned_provider }}">
|
|
<input type="hidden" name="target_model" value="{{ m.target_model }}">
|
|
<button type="submit" class="admin-btn admin-btn-sm" data-confirm="Forget the learned request shape for "{{ m.target_model }}"?">Reset learned</button>
|
|
</form>
|
|
{% endif %}
|
|
{% if not m.context_tier_threshold_tokens and m.off_peak_start_minute is none and not (m.kind == 'image' and m.price_input_per_m) and not m.fallback_model and m.tools_mode != 'emulated' and not m.learned_shape %}<span class="gw-muted">-</span>{% endif %}
|
|
</td>
|
|
<td>
|
|
{% if m.health_status == 'bad' %}
|
|
<span class="gw-badge gw-badge-bad">unhealthy</span>
|
|
<div class="gw-health-error"><code class="gw-code">{{ m.health_code }}</code> {{ m.health_message }}</div>
|
|
{% elif m.health_status == 'ok' %}
|
|
<span class="gw-badge gw-badge-ok" title="Last checked {{ m.health_checked_at }}">ok</span>
|
|
{% else %}
|
|
<span class="gw-muted">-</span>
|
|
{% endif %}
|
|
</td>
|
|
<td class="gw-actions">
|
|
<form method="post" action="/admin/gateway/models/{{ m.source_model }}/test" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm">Test</button>
|
|
</form>
|
|
<a class="admin-btn admin-btn-sm" href="/admin/gateway/models/{{ m.source_model }}/edit">Edit</a>
|
|
<form method="post" action="/admin/gateway/models/{{ m.source_model }}/delete" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" data-confirm="Delete model route "{{ m.source_model }}"?">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
{% if test_image %}
|
|
<div id="gw-test-image-modal" class="modal-overlay visible">
|
|
<div class="modal">
|
|
<div class="modal-header">
|
|
<h3>Test image: {{ tested }}</h3>
|
|
<a href="/admin/gateway?tab=models" class="modal-close" aria-label="Close dialog">×</a>
|
|
</div>
|
|
<div class="modal-body">
|
|
<img src="{{ test_image }}" alt="Generated test image: a hedgehog and a bear" class="gw-test-image">
|
|
<p class="gw-section-hint">Generated by the "{{ tested }}" route: a hedgehog and a bear.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
</section>
|
|
{% endif %}
|
|
|
|
{% if tab == 'providers' %}
|
|
<section class="gw-section">
|
|
<div class="gw-section-head">
|
|
<h3>Providers</h3>
|
|
<a href="/admin/gateway/providers/new" class="admin-btn admin-btn-primary admin-btn-sm">Add provider</a>
|
|
</div>
|
|
<p class="gw-section-hint">Named upstreams reused across model routes. A model route with a blank provider uses the default below.</p>
|
|
<div class="gw-default">
|
|
<strong>default</strong> (from <a href="/admin/services">Services config</a>):
|
|
chat <code class="gw-code">{{ default_provider.model }}</code> at <code class="gw-code">{{ default_provider.base_url }}</code>,
|
|
embed <code class="gw-code">{{ default_provider.embed_model }}</code>,
|
|
image <code class="gw-code">{{ default_provider.image_model }}</code>,
|
|
vision <code class="gw-code">{{ default_provider.vision_model }}</code>
|
|
</div>
|
|
<div class="admin-table-wrap">
|
|
<table class="admin-table">
|
|
<caption class="sr-only">Providers</caption>
|
|
<thead>
|
|
<tr><th scope="col">Name</th><th scope="col">Base URL</th><th scope="col">Protocol</th><th scope="col">Active</th><th scope="col">Client profile</th><th scope="col" class="gw-actions">Actions</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{% if not providers %}
|
|
<tr><td colspan="6" class="admin-empty">No extra providers. A route with a blank provider uses the default.</td></tr>
|
|
{% endif %}
|
|
{% for p in providers %}
|
|
<tr>
|
|
<td>{{ p.name }}</td>
|
|
<td><code class="gw-code">{{ p.base_url }}</code></td>
|
|
<td>{{ p.protocol or "chat_completions" }}</td>
|
|
<td>{{ "yes" if p.is_active else "no" }}</td>
|
|
<td>{{ p.client_profile or "generic" }}</td>
|
|
<td class="gw-actions">
|
|
<a class="admin-btn admin-btn-sm" href="/admin/gateway/providers/{{ p.name }}/edit">Edit</a>
|
|
<form method="post" action="/admin/gateway/providers/{{ p.name }}/delete" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" data-confirm="Delete provider "{{ p.name }}"?">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
{% endif %}
|
|
|
|
{% if tab == 'quota' %}
|
|
<section class="gw-section">
|
|
<div class="gw-section-head">
|
|
<h3>Quota rules</h3>
|
|
<a href="/admin/gateway/quota-rules/new" class="admin-btn admin-btn-primary admin-btn-sm">Add quota rule</a>
|
|
</div>
|
|
<p class="gw-section-hint">Rolling 24h USD caps on <code class="gw-code">/openai/v1/*</code>. A rule scopes by any combination of role, specific user, and app label (the <code class="gw-code">X-App-Reference</code> header); the most specific active match wins, and a rule that omits a dimension pools spend across everyone matching it. With no matching rule, the global defaults below apply per caller. A limit of 0 means unlimited.</p>
|
|
<div class="gw-default">
|
|
<strong>global defaults</strong> (from <a href="/admin/services">Services config</a>, apply per caller with no matching rule):
|
|
member <code class="gw-code">{{ "$%.2f/24h"|format(quota_defaults.user) if quota_defaults.user else "unlimited" }}</code>,
|
|
admin <code class="gw-code">{{ "$%.2f/24h"|format(quota_defaults.admin) if quota_defaults.admin else "unlimited" }}</code>,
|
|
guest <code class="gw-code">{{ "$%.2f/24h"|format(quota_defaults.guest) if quota_defaults.guest else "unlimited" }}</code>,
|
|
internal <code class="gw-code">{{ "$%.2f/24h"|format(quota_defaults.internal) if quota_defaults.internal else "unlimited" }}</code>,
|
|
access key <code class="gw-code">{{ "$%.2f/24h"|format(quota_defaults.key) if quota_defaults.key else "unlimited" }}</code>
|
|
</div>
|
|
<div class="admin-table-wrap">
|
|
<table class="admin-table">
|
|
<caption class="sr-only">Quota rules</caption>
|
|
<thead>
|
|
<tr><th scope="col">Scope</th><th scope="col">Limit / 24h</th><th scope="col">Spent 24h</th><th scope="col">Active</th><th scope="col">Label</th><th scope="col" class="gw-actions">Actions</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{% if not quota_rules %}
|
|
<tr><td colspan="6" class="admin-empty">No quota rules. Every caller is capped by the global defaults above.</td></tr>
|
|
{% endif %}
|
|
{% for r in quota_rules %}
|
|
<tr>
|
|
<td><code class="gw-code">role={{ r.owner_kind or "any" }}, user={{ r.owner_id or "any" }}, app={{ r.app_reference or "any" }}</code></td>
|
|
<td>{{ "$%.2f"|format(r.limit_usd) if r.limit_usd else "unlimited" }}</td>
|
|
<td>${{ "%.4f"|format(r.spent_24h_usd) }}</td>
|
|
<td>{{ "yes" if r.is_active else "no" }}</td>
|
|
<td>{{ r.label }}</td>
|
|
<td class="gw-actions">
|
|
<a class="admin-btn admin-btn-sm" href="/admin/gateway/quota-rules/{{ r.uid }}/edit">Edit</a>
|
|
<form method="post" action="/admin/gateway/quota-rules/{{ r.uid }}/reset" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm" data-confirm="Reset the counted 24h spend for this rule? The usage history is kept.">Reset spend</button>
|
|
</form>
|
|
<form method="post" action="/admin/gateway/quota-rules/{{ r.uid }}/delete" class="gw-inline-form">
|
|
<button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" data-confirm="Delete this quota rule?">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
{% endif %}
|
|
|
|
{% if tab == 'stats' %}
|
|
<section class="gw-section" id="gw-stats-panel" data-stats-root>
|
|
<div class="gw-stats-head">
|
|
<p class="gw-section-hint" style="margin:0;">Request volume, latency, and per-model reliability, computed from the gateway usage ledger you already record - no separate data collection.</p>
|
|
<select id="gw-stats-range">
|
|
{% for r in stats_ranges %}
|
|
<option value="{{ r }}" {% if r == '24h' %}selected{% endif %}>{{ r }}</option>
|
|
{% endfor %}
|
|
</select>
|
|
</div>
|
|
|
|
<div class="gw-tiles">
|
|
<div class="gw-tile"><div class="gw-tile-label">Total requests</div><div class="gw-tile-value" id="gw-stat-total-requests">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Success rate</div><div class="gw-tile-value" id="gw-stat-success-rate">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Models tracked</div><div class="gw-tile-value" id="gw-stat-models-tracked">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Last updated</div><div class="gw-tile-value" id="gw-stat-generated-at">-</div></div>
|
|
</div>
|
|
|
|
<div class="gw-charts-grid">
|
|
<div class="gw-panel gw-wide"><h4>Requests over time</h4><div class="gw-chart-box"><canvas id="gw-chart-timeseries"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Success / failure</h4><div class="gw-chart-box"><canvas id="gw-chart-totals"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Requests by model</h4><div class="gw-chart-box"><canvas id="gw-chart-per-model"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Requests by endpoint</h4><div class="gw-chart-box"><canvas id="gw-chart-endpoints"></canvas></div></div>
|
|
<div class="gw-panel"><h4>HTTP status codes</h4><div class="gw-chart-box"><canvas id="gw-chart-status-codes"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Streaming vs non-streaming</h4><div class="gw-chart-box"><canvas id="gw-chart-streaming"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Top failure reasons</h4><div class="gw-chart-box"><canvas id="gw-chart-failure-reasons"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Requests by hour of day (UTC)</h4><div class="gw-chart-box"><canvas id="gw-chart-hourly"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Latency distribution</h4><div class="gw-chart-box"><canvas id="gw-chart-latency-hist"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Tokens/sec distribution</h4><div class="gw-chart-box"><canvas id="gw-chart-tps-hist"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Model reliability weight</h4><div class="gw-chart-box"><canvas id="gw-chart-weight"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Prompt vs completion tokens</h4><div class="gw-chart-box"><canvas id="gw-chart-tokens"></canvas></div></div>
|
|
</div>
|
|
|
|
<section class="gw-section">
|
|
<div class="gw-section-head">
|
|
<h3>Per-model detail</h3>
|
|
<select id="gw-model-detail-select"><option value="">Select a model...</option></select>
|
|
</div>
|
|
<div id="gw-model-detail-empty-state" class="gw-section-hint">Pick a model above to see its own timeseries and latency distribution.</div>
|
|
<div id="gw-model-detail-charts" hidden>
|
|
<div class="gw-tiles">
|
|
<div class="gw-tile"><div class="gw-tile-label">Requests</div><div class="gw-tile-value" id="gw-model-detail-requests">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Success rate</div><div class="gw-tile-value" id="gw-model-detail-success-rate">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Avg latency</div><div class="gw-tile-value" id="gw-model-detail-latency">-</div></div>
|
|
<div class="gw-tile"><div class="gw-tile-label">Avg tokens/sec</div><div class="gw-tile-value" id="gw-model-detail-tps">-</div></div>
|
|
</div>
|
|
<div class="gw-charts-grid">
|
|
<div class="gw-panel gw-wide"><h4>Requests over time</h4><div class="gw-chart-box"><canvas id="gw-chart-model-timeseries"></canvas></div></div>
|
|
<div class="gw-panel"><h4>Latency distribution</h4><div class="gw-chart-box"><canvas id="gw-chart-model-latency-hist"></canvas></div></div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="gw-section">
|
|
<h3>Model pool</h3>
|
|
<div class="admin-table-wrap gw-table-scroll">
|
|
<table class="admin-table">
|
|
<caption class="sr-only">Model pool</caption>
|
|
<thead>
|
|
<tr><th scope="col">Model</th><th scope="col">Provider</th><th scope="col">Requests</th><th scope="col">Success</th><th scope="col">Avg latency</th><th scope="col">Avg tok/s</th><th scope="col">Weight</th><th scope="col">Circuit</th><th scope="col">Prompt tok</th><th scope="col">Completion tok</th></tr>
|
|
</thead>
|
|
<tbody id="gw-stats-model-table-body">
|
|
<tr><td colspan="10" class="admin-empty">Loading...</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="gw-section">
|
|
<h3>Recent failures</h3>
|
|
<div class="admin-table-wrap gw-table-scroll">
|
|
<table class="admin-table">
|
|
<caption class="sr-only">Recent failures</caption>
|
|
<thead>
|
|
<tr><th scope="col">When</th><th scope="col">Model</th><th scope="col">Provider</th><th scope="col">Endpoint</th><th scope="col">Status</th><th scope="col">Reason</th><th scope="col">Fell back to</th></tr>
|
|
</thead>
|
|
<tbody id="gw-stats-failures-table-body">
|
|
<tr><td colspan="7" class="admin-empty">Loading...</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
</section>
|
|
{% endif %}
|
|
{% endblock %}
|
|
{% block extra_js %}
|
|
{% if tab == 'stats' %}
|
|
<script src="{{ static_url('/static/js/vendor/chart.js') }}"></script>
|
|
<script type="module">
|
|
import { GatewayStats } from "{{ static_url('/static/js/GatewayStats.js') }}";
|
|
const gatewayStats = new GatewayStats(document.querySelector("[data-stats-root]"));
|
|
gatewayStats.start();
|
|
window.app.gatewayStats = gatewayStats;
|
|
</script>
|
|
{% endif %}
|
|
{% endblock %}
|