feat(admin): decision model grid in router panel

This commit is contained in:
2026-11-25 13:59:39 +00:00
parent f8a38e10da
commit 5d4a9e6cad
5 changed files with 168 additions and 0 deletions
+68
View File
@@ -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> = {}): 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()
})
})
+40
View File
@@ -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 })),
}
}
+27
View File
@@ -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))
</div>
</PanelSection>
<PanelSection v-if="decision" title="Decision model">
<p class="text-muted-foreground m-0 font-mono text-xs" data-testid="decision-summary">{{ decision.summary }}</p>
<p v-if="decision.grid" class="text-muted-foreground m-0 text-xs">Rows: heuristic tier. Columns: model tier.</p>
<div v-if="decision.grid" data-wb-ignore-numeric>
<Heatmap
:values="decision.grid.values"
:row-labels="decision.grid.rows"
:col-labels="decision.grid.cols"
tone="info"
:levels="5"
fit
:min="0"
show-legend
label="Heuristic tier by model tier request counts"
:cell-title="(v: number | null, r: string, c: string) => `heuristic ${r}, model ${c}: ${formatExactInt(v ?? 0)}`"
/>
</div>
<p v-else class="text-muted-foreground m-0 text-sm">No decisions recorded yet.</p>
<DescriptionList variant="inline" size="sm" density="compact">
<DescriptionItem v-for="o in decision.outcomes" :key="o.name" :term="o.name">
<span class="font-mono tabular-nums">{{ formatExactInt(o.count) }}</span>
</DescriptionItem>
</DescriptionList>
</PanelSection>
<PanelSection title="Settings">
<DescriptionList variant="inline" size="sm" density="compact">
<DescriptionItem v-for="s in model.settings" :key="s.key" :term="s.label">
+24
View File
@@ -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
}