Files
traefik-llmgateway/webui/src/components/ModelChip.vue
T
lukaszraczylo 758d7c7020 feat(admin-ui): alias labels, active-provider legends, denser tables and readable money
Label configured model aliases with an alias badge wherever a model id shows.
List only providers with traffic in the window in provider chart legends.
Page long tables, show a scroll shadow on wide ones and drop the duplicate model link.
Show costs with thousands separators and magnitude-scaled decimals on cards and chart axes.
Use whole-number ticks for count charts and replace all-zero charts with a message.
Back off polling after failures.
Keep the CSV status region from widening the page on phones.
Regenerate admin_assets_gen.go.
2026-11-08 02:22:22 +00:00

135 lines
5.9 KiB
Vue

<script setup lang="ts">
import { faCheck, faCopy, faTriangleExclamation } from '@fortawesome/free-solid-svg-icons'
import { computed, useTemplateRef } from 'vue'
import AliasBadge from '@/components/AliasBadge.vue'
import { Badge } from '@/components/ui/badge'
import { useAliasNames } from '@/composables/useAliasNames'
import { useCopyFeedback } from '@/composables/useCopyFeedback'
import { copyText } from '@/lib/clipboard'
import { formatContextWindow, formatModelCostHover } from '@/lib/format'
import { useToastsStore } from '@/stores/toasts'
/**
* ModelChip is one clickable, copyable model-id chip, used anywhere a
* resolvable model or alias id renders: a provider's expanded model list
* and the model-alias table (both ProviderHealthPanel.vue's accordion),
* the Models page's own catalog table (lib/model-table-columns.ts, WITH
* metadata props — see contextTokens/inputPerMTokUsd/outputPerMTokUsd
* below), and a group's own allowed-models list (ConsumerDirectory.vue —
* passed with no metadata props there for a raw glob entry, since it has
* no resolvable provider/model pair to look metadata up against). id is
* the full routable id (lib/format.ts's routableModelId) or an alias
* name — what actually gets copied, so a pasted value works straight
* into a `model: "..."` request field.
*
* contextTokens/inputPerMTokUsd/outputPerMTokUsd are this model's
* resolved metadata (feature v0.23, hover-detail refinement:
* admin.go's adminModelMetaView / adminAliasView.ModelMeta) — all
* optional and independently omittable, exactly mirroring
* AdminModelMetaView's own "undefined means unknown" convention. When
* known, they render as an appended hover-detail phrase (metaDetail
* below) on top of whichever copy-state message title already carries;
* when nothing is known at all, the hover is unchanged from before this
* feature existed. A caller resolving an ALIAS's chip passes the
* alias's own INHERITED metadata (its target's resolved values, unless
* the alias itself has a modelMeta override) — the same values GET
* /v1/models and the admin API already resolve that way.
*
* The label span (labelRef below) is copyText's select-text fallback
* target — not a rare edge case here: navigator.clipboard is undefined
* outright on any plain-HTTP admin deployment (the Clipboard API
* requires a secure context — https, or localhost), so the fallback is
* the ONLY path a click ever takes there, not a backstop for an unlikely
* failure. labelRef is a template ref on an element this component
* always renders (below), so it is populated by the time onClick can
* ever run — a click handler cannot fire before mount.
*/
const props = defineProps<{
id: string
contextTokens?: number
inputPerMTokUsd?: number
outputPerMTokUsd?: number
}>()
const { isAlias } = useAliasNames()
const labelRef = useTemplateRef<HTMLElement>('label')
const { state, set: setCopyFeedback } = useCopyFeedback()
const toasts = useToastsStore()
async function onClick(): Promise<void> {
const result = await copyText(props.id, labelRef.value ?? undefined)
// 'failed' (neither the Clipboard API nor the selection fallback
// worked) gets its own visible state rather than a silent no-op — a
// click that visibly does nothing reads as a broken button, not as
// "nothing to report". Only a FAILURE also gets a toast (states-plan.md
// item 3) — 'copied'/'selected' already have clear, accessible inline
// feedback right at the click target (the icon swap plus this same
// title/aria-label text), so a success toast on top would double-notify
// for something the reader is already looking straight at.
setCopyFeedback(result)
if (result === 'failed') toasts.push({ kind: 'error', message: `Could not copy ${props.id}.` })
}
const icon = {
idle: faCopy,
copied: faCheck,
selected: faCheck,
failed: faTriangleExclamation,
}
const baseTitle = computed<Record<typeof state.value, string>>(() => ({
idle: `Copy ${props.id}`,
copied: 'Copied',
selected: 'Selected — press Ctrl/Cmd+C',
failed: 'Copy failed',
}))
/**
* metaDetail is the resolved-metadata hover phrase (feature v0.23):
* cost first ("in $0.19 / out $0.51 per MTok", or "free"), then context
* ("256k context"), joined with " · " — either half is independently
* omitted when its props are undefined, and the whole phrase is empty
* (never appended to title below) when nothing at all is known, per
* this feature's "fully-unknown models get no metadata hover" rule.
*/
const metaDetail = computed(() => {
const parts: string[] = []
if (props.inputPerMTokUsd !== undefined && props.outputPerMTokUsd !== undefined) {
parts.push(formatModelCostHover(props.inputPerMTokUsd, props.outputPerMTokUsd))
}
if (props.contextTokens !== undefined) {
parts.push(`${formatContextWindow(props.contextTokens)} context`)
}
return parts.join(' · ')
})
/**
* title carries BOTH the copy-state message (unchanged behavior) and,
* when known, the resolved-metadata detail appended after " · " — bound
* to both the mouse-only `title` attribute and `aria-label` (same
* mouse-vs-keyboard/screen-reader parity fix ProviderRateBadge.vue's
* own `detail` computed already applies).
*/
const title = computed(() => {
const base = baseTitle.value[state.value]
return metaDetail.value ? `${base} · ${metaDetail.value}` : base
})
</script>
<template>
<Badge
as="button"
type="button"
variant="secondary"
class="max-w-full cursor-pointer gap-1 truncate font-mono text-xs font-normal transition-colors hover:bg-accent hover:text-accent-foreground"
:class="state === 'failed' ? 'text-destructive' : ''"
:title="title"
:aria-label="title"
@click="onClick"
>
<span ref="label" class="min-w-0 truncate select-text">{{ id }}</span>
<AliasBadge v-if="isAlias(id)" />
<FontAwesomeIcon :icon="icon[state]" class="size-2.5 shrink-0" aria-hidden="true" />
</Badge>
</template>