diff --git a/webui/mock/scenarios.ts b/webui/mock/scenarios.ts index 6f90cc3..e935083 100644 --- a/webui/mock/scenarios.ts +++ b/webui/mock/scenarios.ts @@ -138,6 +138,8 @@ function configWarnings(path: string, body: Obj) { const ROUTER_MIN_REQUESTS = 120 const SHADOW_CLASSIFIER = 'v3' +const DECISION_PROVIDER = 'gx10' +const DECISION_OUTCOMES = ['agree', 'override', 'lowconf', 'timeout', 'error', 'breaker', 'cache'] /** Scales counts by `factor`; with `total`, the rounding remainder lands on the largest row so the rows still sum to it. */ function scaleCounts(rows: Obj[], factor: number, total?: number) { @@ -149,6 +151,7 @@ function scaleCounts(rows: Obj[], factor: number, total?: number) { function routerEscalations(path: string, body: Obj) { if (path !== '/admin/api/overview' && path !== '/admin/api/catalog') return + let decided = false eachRouter(body, (r) => { const tiers: string[] = (r.tiers ?? []).map((t: Obj) => t.name) if (tiers.length < 2) return @@ -161,6 +164,12 @@ function routerEscalations(path: string, body: Obj) { stats.requests = target for (const list of [stats.tiers, stats.reasons]) scaleCounts(list ?? [], factor, target) scaleCounts(stats.outcomes ?? [], factor) + if (!decided) { + decided = true + r.decision = { provider: DECISION_PROVIDER, path: '/v1/decide', mode: 'live', timeout: '500ms', minProb: 700, band: 100 } + stats.decision = tiers.flatMap((heuristic, i) => tiers.map((model, j) => ({ heuristic, model, count: i === j ? 30 - i * 6 : Math.abs(i - j) === 1 ? 12 : 0 }))).filter((c) => c.count > 0) + stats.decisionOutcomes = DECISION_OUTCOMES.map((name, i) => ({ name, count: 60 - i * 9 })) + } r.shadowEnabled = true r.shadowClassifier = SHADOW_CLASSIFIER r.fellBack = 7 diff --git a/webui/src/lib/decision-view.spec.ts b/webui/src/lib/decision-view.spec.ts new file mode 100644 index 0000000..e45fa4e --- /dev/null +++ b/webui/src/lib/decision-view.spec.ts @@ -0,0 +1,68 @@ +import { describe, expect, it } from 'vitest' + +import { decisionView, hasDecision } from '@/lib/decision-view' +import type { AdminRouterView } from '@/types/api' + +function view(over: Partial = {}): AdminRouterView { + return { + name: 'smartrouter/uni', + defaultTier: 'low', + shadowEnabled: false, + classifier: 'v2', + riskZ: 128, + selection: 'ordered', + calibration: { mode: 'off', delta: [], maxLower: 0, maxRaise: 0, freeze: false }, + saturationDowngrade: false, + downgradeBand: 100, + downgraded: 0, + timeoutFallback: false, + fellBack: 0, + tiers: [ + { name: 'low', models: ['a'], minScore: 0 }, + { name: 'mid', models: ['b'], minScore: 150 }, + { name: 'top', models: ['c'], minScore: 350 }, + ], + keywordRules: [], + maxAttempts: 2, + contextEscalation: false, + decision: { provider: 'gx10', path: '/v1/decide', mode: 'live', timeout: '500ms', minProb: 700, band: 100 }, + stats: { + scope: 'shared', + requests: 10, + tiers: [], + reasons: [], + escalations: [], + shadow: [], + outcomes: [], + decision: [{ heuristic: 'mid', model: 'top', count: 40 }], + decisionOutcomes: [ + { name: 'agree', count: 5 }, + { name: 'override', count: 40 }, + ], + }, + ...over, + } +} + +describe('decisionView', () => { + it('puts mid to top count in the grid by tier order', () => { + const grid = decisionView(view())!.grid! + expect(grid.rows).toEqual(['low', 'mid', 'top']) + expect(grid.values[grid.rows.indexOf('mid')][grid.cols.indexOf('top')]).toBe(40) + }) + + it('lists outcomes by name', () => { + const d = decisionView(view())! + expect(d.outcomes).toContainEqual({ name: 'override', count: 40 }) + }) + + it('summarises the config', () => { + expect(decisionView(view())!.summary).toBe('gx10 · live · 500ms · ≥70%') + }) + + it('hides the section without a decision block', () => { + const v = view({ decision: undefined }) + expect(hasDecision(v)).toBe(false) + expect(decisionView(v)).toBeNull() + }) +}) diff --git a/webui/src/lib/decision-view.ts b/webui/src/lib/decision-view.ts new file mode 100644 index 0000000..08ae749 --- /dev/null +++ b/webui/src/lib/decision-view.ts @@ -0,0 +1,40 @@ +import type { AdminRouterView } from '@/types/api' + +const PER_MILLE_PER_PCT = 10 + +export interface DecisionGrid { + rows: string[] + cols: string[] + values: number[][] +} +export interface DecisionView { + summary: string + grid: DecisionGrid | null + outcomes: Array<{ name: string; count: number }> +} + +export const hasDecision = (v: AdminRouterView): boolean => v.decision !== undefined + +/** Decision-model section of the router panel; null hides it. Grid rows are heuristic tiers, columns model tiers. */ +export function decisionView(v: AdminRouterView): DecisionView | null { + const d = v.decision + if (!d) return null + const cells = v.stats?.decision ?? [] + let grid: DecisionGrid | null = null + if (cells.length > 0) { + const names = new Set(v.tiers.map((t) => t.name)) + for (const c of cells) { + names.add(c.heuristic) + names.add(c.model) + } + const order = [...names] + const values = order.map(() => order.map(() => 0)) + for (const c of cells) values[order.indexOf(c.heuristic)][order.indexOf(c.model)] += c.count + grid = { rows: order, cols: order, values } + } + return { + summary: [d.provider, d.mode, d.timeout, `≥${Math.round(d.minProb / PER_MILLE_PER_PCT)}%`].join(' · '), + grid, + outcomes: (v.stats?.decisionOutcomes ?? []).map((o) => ({ name: o.name, count: o.count })), + } +} diff --git a/webui/src/panels/RouterPanel.vue b/webui/src/panels/RouterPanel.vue index f59eaca..9032c96 100644 --- a/webui/src/panels/RouterPanel.vue +++ b/webui/src/panels/RouterPanel.vue @@ -3,6 +3,7 @@ import { Badge, Button, DescriptionItem, DescriptionList, Heatmap, StatBar } fro import { computed, ref, watch } from 'vue' import RouterForm from '@/components/forms/RouterForm.vue' +import { decisionView } from '@/lib/decision-view' import { formatCompactCount, formatExactInt } from '@/lib/format' import { formatSel } from '@/lib/panel-selection' import { routerPanelModel } from '@/lib/router-panel' @@ -22,6 +23,7 @@ const nav = useNavStore() const view = computed(() => models.routers.find((r) => r.name === props.id)) const model = computed(() => (view.value ? routerPanelModel(view.value) : null)) +const decision = computed(() => (view.value ? decisionView(view.value) : null)) const known = computed(() => new Set(models.rows.map((r) => r.id))) const requests = computed(() => view.value?.stats?.requests ?? 0) const scope = computed(() => (view.value?.stats?.scope === 'shared' ? 'all replicas, this month (UTC)' : 'this gateway instance, since start')) @@ -146,6 +148,31 @@ watch(() => props.id, () => (snippetOpen.value = false)) + +

{{ decision.summary }}

+

Rows: heuristic tier. Columns: model tier.

+
+ +
+

No decisions recorded yet.

+ + + {{ formatExactInt(o.count) }} + + +
+ diff --git a/webui/src/types/api.ts b/webui/src/types/api.ts index d6361d2..2119615 100644 --- a/webui/src/types/api.ts +++ b/webui/src/types/api.ts @@ -267,6 +267,25 @@ export interface AdminRouterOutcomeView { count: number } +/** One heuristic-tier by model-tier decision cell (admin_routers.go: adminRouterDecisionTierView). */ +export interface AdminRouterDecisionTierView { + heuristic: string + model: string + count: number +} + +/** Decision-model settings of a router (admin_routers.go: adminRouterDecisionView); absent when not configured. */ +export interface AdminRouterDecisionView { + provider: string + path: string + mode: 'live' | 'shadow' + timeout: string + /** Minimum model probability in per-mille. */ + minProb: number + /** Score distance from a tier boundary in per-mille that triggers a call. */ + band: number +} + /** Router counters (admin_routers.go: adminRouterStatsView); scope "shared" sums all gateway instances this month, "instance" is this process since start. */ export interface AdminRouterStatsView { scope: 'shared' | 'instance' @@ -277,6 +296,10 @@ export interface AdminRouterStatsView { /** Non-zero cells of the live-by-shadow tier matrix; empty without shadowWeights. */ shadow: AdminRouterShadowView[] outcomes: AdminRouterOutcomeView[] + /** Non-zero heuristic-by-model cells; empty without a decision block. */ + decision?: AdminRouterDecisionTierView[] + /** One row per decision outcome, zeros included; empty without a decision block. */ + decisionOutcomes?: AdminRouterCountView[] } /** One tier's calibration offset: points its threshold sits below the configured minScore. */ @@ -335,6 +358,7 @@ export interface AdminRouterView { contextEscalation: boolean /** Advertised context window in tokens; absent when no target declares one. */ contextTokens?: number + decision?: AdminRouterDecisionView stats?: AdminRouterStatsView }