mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(webui): models page
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { groupByProvider, priceStatus, selectModels } from '@/lib/models-view'
|
||||
import type { ModelCatalogRow } from '@/lib/model-table-columns'
|
||||
|
||||
function row(id: string, over: Partial<ModelCatalogRow> = {}): ModelCatalogRow {
|
||||
const slash = id.indexOf('/')
|
||||
return {
|
||||
id,
|
||||
model: id.slice(slash + 1),
|
||||
providerName: id.slice(0, slash),
|
||||
kind: '',
|
||||
endpoint: false,
|
||||
priceSource: 'litellm',
|
||||
aliases: [],
|
||||
requests: 0,
|
||||
tokensIn: 0,
|
||||
tokensOut: 0,
|
||||
costMicroUsd: 0,
|
||||
attempts: 0,
|
||||
failures: 0,
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
describe('priceStatus', () => {
|
||||
it('maps price sources to the three filter values', () => {
|
||||
expect(priceStatus(row('a/x', { priceSource: 'override' }))).toBe('priced')
|
||||
expect(priceStatus(row('a/x', { priceSource: 'unpriced' }))).toBe('unpriced')
|
||||
expect(priceStatus(row('a/x', { priceSource: 'free' }))).toBe('free')
|
||||
})
|
||||
it('gives endpoint pseudo-models no price status', () => {
|
||||
expect(priceStatus(row('a/x', { endpoint: true, priceSource: 'unpriced' }))).toBe('none')
|
||||
})
|
||||
})
|
||||
|
||||
describe('selectModels', () => {
|
||||
const rows = [
|
||||
row('a/one', { kind: 'embedding', priceSource: 'free' }),
|
||||
row('a/two', { priceSource: 'unpriced', aliases: ['fast'] }),
|
||||
row('b/three'),
|
||||
]
|
||||
it('returns every row for an empty selection', () => {
|
||||
expect(selectModels(rows, {})).toHaveLength(3)
|
||||
})
|
||||
it('combines provider, kind, price and text with AND', () => {
|
||||
expect(selectModels(rows, { provider: 'a' }).map((r) => r.id)).toEqual(['a/one', 'a/two'])
|
||||
expect(selectModels(rows, { kind: 'embedding' }).map((r) => r.id)).toEqual(['a/one'])
|
||||
expect(selectModels(rows, { price: 'unpriced' }).map((r) => r.id)).toEqual(['a/two'])
|
||||
expect(selectModels(rows, { q: 'FAST' }).map((r) => r.id)).toEqual(['a/two'])
|
||||
expect(selectModels(rows, { provider: 'b', price: 'unpriced' })).toEqual([])
|
||||
})
|
||||
it('treats kind "none" as the unlabelled models', () => {
|
||||
expect(selectModels(rows, { kind: 'none' }).map((r) => r.id)).toEqual(['a/two', 'b/three'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('groupByProvider', () => {
|
||||
it('groups in first-seen order with counts and request totals', () => {
|
||||
const g = groupByProvider([row('b/x', { requests: 2 }), row('a/y', { requests: 5 }), row('b/z', { requests: 3 })])
|
||||
expect(g.map((x) => [x.provider, x.rows.length, x.requests])).toEqual([
|
||||
['b', 2, 5],
|
||||
['a', 1, 5],
|
||||
])
|
||||
})
|
||||
it('sorts rows inside a group by requests then id', () => {
|
||||
const [g] = groupByProvider([row('a/b'), row('a/a'), row('a/c', { requests: 9 })])
|
||||
expect(g!.rows.map((r) => r.id)).toEqual(['a/c', 'a/a', 'a/b'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,52 @@
|
||||
import { filterModelRows } from '@/lib/model-filter'
|
||||
import type { ModelCatalogRow } from '@/lib/model-table-columns'
|
||||
|
||||
export type PriceStatus = 'priced' | 'unpriced' | 'free' | 'none'
|
||||
|
||||
/** Filter value for models with no kind label. */
|
||||
export const NO_KIND = 'none'
|
||||
|
||||
export interface ModelSelection {
|
||||
q?: string
|
||||
provider?: string
|
||||
kind?: string
|
||||
price?: string
|
||||
}
|
||||
|
||||
export interface ProviderGroup {
|
||||
provider: string
|
||||
rows: ModelCatalogRow[]
|
||||
requests: number
|
||||
}
|
||||
|
||||
/** Endpoint pseudo-models carry no price, so they are neither priced nor unpriced. */
|
||||
export function priceStatus(r: ModelCatalogRow): PriceStatus {
|
||||
if (r.endpoint) return 'none'
|
||||
if (r.priceSource === 'free') return 'free'
|
||||
return r.priceSource === 'unpriced' ? 'unpriced' : 'priced'
|
||||
}
|
||||
|
||||
export function selectModels(rows: ModelCatalogRow[], sel: ModelSelection): ModelCatalogRow[] {
|
||||
const byText = filterModelRows(rows, sel.q ?? '')
|
||||
return byText.filter(
|
||||
(r) =>
|
||||
(!sel.provider || r.providerName === sel.provider) &&
|
||||
(!sel.kind || (sel.kind === NO_KIND ? r.kind === '' : r.kind === sel.kind)) &&
|
||||
(!sel.price || priceStatus(r) === sel.price),
|
||||
)
|
||||
}
|
||||
|
||||
const byTraffic = (a: ModelCatalogRow, b: ModelCatalogRow): number => b.requests - a.requests || a.id.localeCompare(b.id)
|
||||
|
||||
/** One group per provider in first-seen order; rows busiest first. */
|
||||
export function groupByProvider(rows: ModelCatalogRow[]): ProviderGroup[] {
|
||||
const groups = new Map<string, ProviderGroup>()
|
||||
for (const r of rows) {
|
||||
let g = groups.get(r.providerName)
|
||||
if (!g) groups.set(r.providerName, (g = { provider: r.providerName, rows: [], requests: 0 }))
|
||||
g.rows.push(r)
|
||||
g.requests += r.requests
|
||||
}
|
||||
for (const g of groups.values()) g.rows.sort(byTraffic)
|
||||
return [...groups.values()]
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { routersByModel } from '@/lib/router-index'
|
||||
import type { AdminRouterView } from '@/types/api'
|
||||
|
||||
function router(name: string, ...tiers: string[][]): AdminRouterView {
|
||||
return { name, tiers: tiers.map((models, i) => ({ name: `t${i}`, models, minScore: i })) } as unknown as AdminRouterView
|
||||
}
|
||||
|
||||
describe('routersByModel', () => {
|
||||
it('bare and canonical ids both indexed', () => {
|
||||
const idx = routersByModel([router('smartrouter/gx10', ['gx10/Qwen3.8-27B'], ['Inkling-2-Mini'])])
|
||||
expect(idx.get('gx10/Qwen3.8-27B')).toEqual(['smartrouter/gx10'])
|
||||
expect(idx.get('Qwen3.8-27B')).toEqual(['smartrouter/gx10'])
|
||||
expect(idx.get('Inkling-2-Mini')).toEqual(['smartrouter/gx10'])
|
||||
})
|
||||
|
||||
it('lists each router once per model, sorted by name', () => {
|
||||
const idx = routersByModel([router('b', ['p/m'], ['p/m']), router('a', ['p/m'])])
|
||||
expect(idx.get('p/m')).toEqual(['a', 'b'])
|
||||
})
|
||||
|
||||
it('keeps later slashes in the bare id', () => {
|
||||
const idx = routersByModel([router('r', ['openrouter/meta/llama'])])
|
||||
expect(idx.get('meta/llama')).toEqual(['r'])
|
||||
})
|
||||
|
||||
it('is empty for no routers or routers without tiers', () => {
|
||||
expect(routersByModel([]).size).toBe(0)
|
||||
expect(routersByModel(undefined).size).toBe(0)
|
||||
expect(routersByModel([{ name: 'x' } as unknown as AdminRouterView]).size).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
import { splitModelId } from '@/lib/model-id'
|
||||
import type { AdminRouterView } from '@/types/api'
|
||||
|
||||
/** Router names per model id. A tier target is indexed as written and, when canonical, by its bare model id too, because configs list both forms. */
|
||||
export function routersByModel(routers: AdminRouterView[] | undefined): Map<string, string[]> {
|
||||
const index = new Map<string, Set<string>>()
|
||||
const add = (key: string, router: string): void => {
|
||||
let set = index.get(key)
|
||||
if (!set) index.set(key, (set = new Set()))
|
||||
set.add(router)
|
||||
}
|
||||
for (const r of routers ?? []) {
|
||||
for (const tier of r.tiers ?? []) {
|
||||
for (const target of tier.models) {
|
||||
add(target, r.name)
|
||||
const { provider, model } = splitModelId(target)
|
||||
if (provider) add(model, r.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Map([...index].map(([k, v]) => [k, [...v].sort()]))
|
||||
}
|
||||
Reference in New Issue
Block a user