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 })),
}
}