mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(admin): attach model aliases to the model their target resolves to
The catalog grouped aliases by the raw target string and looked them up by canonical provider/model id. A bare target never matched, so no model card listed its aliases. Each alias now keys on the canonical id that the registry resolves its target to. The alias view gains an additive resolvedTarget field. Model-id cells in the overview, usage and panels now show an alias badge with the target, using the catalog alias list.
This commit is contained in:
@@ -649,6 +649,9 @@ type adminAliasView struct {
|
||||
ModelMeta adminModelMetaView `json:"modelMeta"`
|
||||
Alias string `json:"alias"`
|
||||
Target string `json:"target"`
|
||||
// ResolvedTarget is Target's canonical "provider/model" id; omitted
|
||||
// while Target resolves to no known model.
|
||||
ResolvedTarget string `json:"resolvedTarget,omitempty"`
|
||||
}
|
||||
|
||||
// adminOverviewResponse is the full body of GET /admin/api/overview (spec
|
||||
@@ -908,10 +911,12 @@ func (g *Gateway) buildAdminAliasViews() []adminAliasView {
|
||||
aliasSnaps := g.registry.aliasSnapshot()
|
||||
aliases := make([]adminAliasView, len(aliasSnaps))
|
||||
for i, a := range aliasSnaps {
|
||||
resolved, _ := g.registry.aliasTargetCanonical(a.Target)
|
||||
aliases[i] = adminAliasView{
|
||||
Alias: a.Alias,
|
||||
Target: a.Target,
|
||||
ModelMeta: buildAdminModelMetaView(g.registry.resolveMetaForAliasName(a.Alias)),
|
||||
Alias: a.Alias,
|
||||
Target: a.Target,
|
||||
ResolvedTarget: resolved,
|
||||
ModelMeta: buildAdminModelMetaView(g.registry.resolveMetaForAliasName(a.Alias)),
|
||||
}
|
||||
}
|
||||
return aliases
|
||||
|
||||
+10
-8
@@ -97,14 +97,16 @@ type adminCatalogResponse struct {
|
||||
func (g *Gateway) buildAdminCatalog() adminCatalogResponse {
|
||||
snaps := g.registry.snapshot()
|
||||
|
||||
// aliasesByTarget groups every configured alias by its configured
|
||||
// Target string (ModelAliases' own doc comment, llmgateway.go: a
|
||||
// target names a canonical "provider/model" id), so each model's own
|
||||
// Aliases field below is a single map lookup rather than a full scan
|
||||
// of the alias list per model.
|
||||
// aliasesByTarget groups every alias by the canonical id its target
|
||||
// resolves to (a target may be bare or provider-prefixed), so each
|
||||
// model's own Aliases field below is a single map lookup. An alias
|
||||
// whose target resolves to no known model attaches to no model.
|
||||
aliasViews := g.buildAdminAliasViews()
|
||||
aliasesByTarget := make(map[string][]string)
|
||||
for _, a := range g.registry.aliasSnapshot() {
|
||||
aliasesByTarget[a.Target] = append(aliasesByTarget[a.Target], a.Alias)
|
||||
for _, a := range aliasViews {
|
||||
if a.ResolvedTarget != "" {
|
||||
aliasesByTarget[a.ResolvedTarget] = append(aliasesByTarget[a.ResolvedTarget], a.Alias)
|
||||
}
|
||||
}
|
||||
|
||||
routers := g.buildAdminRouterViews()
|
||||
@@ -181,7 +183,7 @@ func (g *Gateway) buildAdminCatalog() adminCatalogResponse {
|
||||
}
|
||||
}
|
||||
|
||||
return adminCatalogResponse{Providers: providers, Aliases: g.buildAdminAliasViews(), Routers: routers}
|
||||
return adminCatalogResponse{Providers: providers, Aliases: aliasViews, Routers: routers}
|
||||
}
|
||||
|
||||
// sortedKeys returns m's keys in ascending order, nil for an empty map.
|
||||
|
||||
@@ -169,6 +169,55 @@ func TestAdminCatalog_AliasesGroupedByTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminCatalog_AliasTargetForms pins that a bare target, a
|
||||
// provider-prefixed target and an unresolvable target behave as the
|
||||
// registry's own resolution does: the first two attach to the canonical
|
||||
// model, the last attaches nowhere but still lists in catalog.aliases.
|
||||
func TestAdminCatalog_AliasTargetForms(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := newAdminTestConfig()
|
||||
cfg.ModelAliases = map[string]string{
|
||||
"fast-bare": "z-model",
|
||||
"fast-prefixed": "zeta/z-model",
|
||||
"fast-missing": "no-such-model",
|
||||
}
|
||||
h, _ := newAdminGatewayHandle(t, cfg)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminCatalogPath, "sk-admin1"))
|
||||
var got adminCatalogResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
attached := map[string][]string{}
|
||||
for _, p := range got.Providers {
|
||||
for _, m := range p.Models {
|
||||
attached[m.ID] = m.Aliases
|
||||
}
|
||||
}
|
||||
want := []string{"fast-bare", "fast-prefixed"}
|
||||
if g := attached["zeta/z-model"]; len(g) != 2 || g[0] != want[0] || g[1] != want[1] {
|
||||
t.Errorf("zeta/z-model Aliases = %v, want %v", g, want)
|
||||
}
|
||||
for id, names := range attached {
|
||||
for _, n := range names {
|
||||
if n == "fast-missing" {
|
||||
t.Errorf("unresolvable entry attached to %s", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
resolved := map[string]string{}
|
||||
for _, a := range got.Aliases {
|
||||
resolved[a.Alias] = a.ResolvedTarget
|
||||
}
|
||||
if len(resolved) != 3 {
|
||||
t.Fatalf("top-level Aliases = %v, want 3 entries", resolved)
|
||||
}
|
||||
if resolved["fast-bare"] != "zeta/z-model" || resolved["fast-prefixed"] != "zeta/z-model" || resolved["fast-missing"] != "" {
|
||||
t.Errorf("ResolvedTarget = %v", resolved)
|
||||
}
|
||||
}
|
||||
|
||||
// --- GET /admin/api/consumers ---
|
||||
|
||||
func TestAdminConsumers_GateMatrix(t *testing.T) {
|
||||
|
||||
+2
-2
@@ -325,8 +325,8 @@ func TestAdminOverview_SortedShapeAndVersion(t *testing.T) {
|
||||
}
|
||||
|
||||
wantAliases := []adminAliasView{
|
||||
{Alias: "aaliased/y", Target: "alpha/a-model-1"},
|
||||
{Alias: "zaliased/x", Target: "z-model"},
|
||||
{Alias: "aaliased/y", Target: "alpha/a-model-1", ResolvedTarget: "alpha/a-model-1"},
|
||||
{Alias: "zaliased/x", Target: "z-model", ResolvedTarget: "zeta/z-model"},
|
||||
}
|
||||
if len(got.Aliases) != len(wantAliases) {
|
||||
t.Fatalf("aliases = %+v, want exactly %+v", got.Aliases, wantAliases)
|
||||
|
||||
+23
@@ -2414,6 +2414,29 @@ func (m *modelRegistry) aliasSnapshot() []aliasSnapshotEntry {
|
||||
return out
|
||||
}
|
||||
|
||||
// aliasTargetCanonical resolves an alias target to the canonical
|
||||
// "provider/model" id resolveAliasTarget would pick, ignoring group
|
||||
// authorization: a provider-prefixed target resolves on that provider,
|
||||
// a bare one on the sorted-first provider that knows it. ok is false for
|
||||
// a target no provider knows yet.
|
||||
func (m *modelRegistry) aliasTargetCanonical(target string) (string, bool) {
|
||||
providerName, model, prefixed := m.splitConfiguredProvider(target)
|
||||
if !prefixed {
|
||||
model = target
|
||||
for _, name := range m.providerNames {
|
||||
if m.states[name].hasModel(model) {
|
||||
providerName = name
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if providerName == "" || !m.states[providerName].hasModel(model) {
|
||||
return "", false
|
||||
}
|
||||
canonical := providerModelScopeID(providerName, model)
|
||||
return canonical, true
|
||||
}
|
||||
|
||||
// snapshot returns every configured provider's read-only view, sorted by
|
||||
// name (m.providerNames is already sorted at construction) — the admin
|
||||
// dashboard's provider table (spec §4, v0.2).
|
||||
|
||||
@@ -10,12 +10,14 @@ import { useHashState } from '@/composables/useHashState'
|
||||
import AppLayout from '@/layout/AppLayout.vue'
|
||||
import type { PageId } from '@/lib/pages'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { useCatalogStore } from '@/stores/catalog'
|
||||
import { useConfigStore } from '@/stores/config'
|
||||
import { useDashboardStore } from '@/stores/dashboard'
|
||||
import { useNavStore } from '@/stores/nav'
|
||||
import { resetSessionData } from '@/stores/session'
|
||||
|
||||
const auth = useAuthStore()
|
||||
const catalog = useCatalogStore()
|
||||
const config = useConfigStore()
|
||||
const dashboard = useDashboardStore()
|
||||
const nav = useNavStore()
|
||||
@@ -65,6 +67,15 @@ watch(
|
||||
},
|
||||
)
|
||||
|
||||
// Every model-id cell marks an alias from the catalog's alias list, so load it once per key.
|
||||
watch(
|
||||
() => auth.apiKey,
|
||||
() => {
|
||||
if (auth.isAuthenticated) void catalog.ensureLoaded()
|
||||
},
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
dashboard.startPolling()
|
||||
})
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { aliasTargetMap } from '@/lib/alias-display'
|
||||
import { useCatalogStore } from '@/stores/catalog'
|
||||
import type { AdminAliasView, AdminCatalogResponse } from '@/types/api'
|
||||
|
||||
const meta = {} as AdminAliasView['modelMeta']
|
||||
|
||||
describe('aliasTargetMap', () => {
|
||||
it('prefers the resolved canonical target', () => {
|
||||
const m = aliasTargetMap([{ alias: 'model-fast', target: 'deepseek-v4.1-flash-1017', resolvedTarget: 'uni/deepseek-v4.1-flash-1017', modelMeta: meta }])
|
||||
expect(m.get('model-fast')).toBe('uni/deepseek-v4.1-flash-1017')
|
||||
})
|
||||
|
||||
it('falls back to the configured target when it does not resolve', () => {
|
||||
const m = aliasTargetMap([{ alias: 'ghost', target: 'no-such-model', modelMeta: meta }])
|
||||
expect(m.get('ghost')).toBe('no-such-model')
|
||||
})
|
||||
|
||||
it('is empty for no aliases and ignores non-alias ids', () => {
|
||||
expect(aliasTargetMap(undefined).size).toBe(0)
|
||||
expect(aliasTargetMap([]).get('uni/x')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('useCatalogStore.aliasTargets', () => {
|
||||
it('indexes the fetched catalog aliases', () => {
|
||||
setActivePinia(createPinia())
|
||||
const catalog = useCatalogStore()
|
||||
expect(catalog.aliasTargets.size).toBe(0)
|
||||
catalog.data = {
|
||||
providers: [],
|
||||
aliases: [{ alias: 'model-fast', target: 'x', resolvedTarget: 'uni/x', modelMeta: meta }],
|
||||
} as AdminCatalogResponse
|
||||
expect(catalog.aliasTargets.get('model-fast')).toBe('uni/x')
|
||||
expect(catalog.aliasTargets.get('uni/x')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { AdminAliasView } from '@/types/api'
|
||||
|
||||
/** Maps each alias id to the target a reader should see: the canonical `provider/model` when the gateway resolved it, else the configured target. */
|
||||
export function aliasTargetMap(aliases: readonly AdminAliasView[] | undefined): Map<string, string> {
|
||||
const map = new Map<string, string>()
|
||||
for (const a of aliases ?? []) map.set(a.alias, a.resolvedTarget || a.target)
|
||||
return map
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import { aliasTargetMap } from '@/lib/alias-display'
|
||||
import { adminFetch, isAuthRejection, messageOf } from '@/lib/api'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import type { AdminCatalogModel, AdminCatalogResponse } from '@/types/api'
|
||||
@@ -58,6 +59,10 @@ export const useCatalogStore = defineStore('catalog', {
|
||||
}
|
||||
return map
|
||||
},
|
||||
/** aliasTargets maps each configured alias id to the model it points at, so any view listing a requested model id can mark an alias. */
|
||||
aliasTargets(state): Map<string, string> {
|
||||
return aliasTargetMap(state.data?.aliases)
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
/**
|
||||
|
||||
@@ -365,6 +365,8 @@ export interface AdminRouterView {
|
||||
export interface AdminAliasView {
|
||||
alias: string
|
||||
target: string
|
||||
/** Canonical `provider/model` id the target resolves to; absent while it resolves to no known model. */
|
||||
resolvedTarget?: string
|
||||
/**
|
||||
* The alias's own resolved metadata (admin.go: adminAliasView.
|
||||
* ModelMeta — feature v0.23): INHERITED from its target unless the
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
<script setup lang="ts">
|
||||
import { Badge } from '@perfect-design/ui'
|
||||
import { computed } from 'vue'
|
||||
|
||||
import { splitModelId } from '@/lib/model-id'
|
||||
import { useCatalogStore } from '@/stores/catalog'
|
||||
|
||||
const props = defineProps<{ id: string; bare?: boolean }>()
|
||||
const parts = computed(() => splitModelId(props.id))
|
||||
const catalog = useCatalogStore()
|
||||
const target = computed(() => catalog.aliasTargets.get(props.id) ?? null)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span class="wb-model" :title="id">
|
||||
<span class="wb-model" :title="target ? `${id} → ${target}` : id">
|
||||
<span v-if="parts.provider && !bare" class="wb-model-provider">{{ parts.provider }}</span>
|
||||
<span class="wb-model-name">{{ parts.model }}</span>
|
||||
<Badge v-if="target" tone="neutral" variant="outline" size="sm" class="wb-model-alias ml-1 min-w-0 truncate font-mono">alias → {{ target }}</Badge>
|
||||
</span>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user