import type { HistoryWindow, LimitSources, LimitsConfig } from '@/types/api' /** ZERO_TIME is Go's zero time.Time value in its default JSON (RFC 3339) encoding — the unset-timestamp sentinel every admin API "never happened yet" field marshals as, and every formatter/test below treats as absent. */ export const ZERO_TIME = '0001-01-01T00:00:00Z' /** * routableModelId joins a provider name and one of its own model ids into * the id form the gateway actually routes on (routes_unified.go's * "provider/model" prefix rule) — always `${providerName}/${modelId}`, * even when modelId itself already contains a slash (a discovered id * like "uni/deepseek-v4-flash-0731" on provider "real" becomes * "real/uni/deepseek-v4-flash-0731": the FIRST path segment names the * provider, everything after it is the model id verbatim, so this stays * copy-paste-valid regardless of how many slashes the model id itself * carries). */ export function routableModelId(providerName: string, modelId: string): string { return `${providerName}/${modelId}` } /** * MICROS_PER_USD converts a plain USD amount (e.g. a LimitsConfig cost * limit, `costPerDayUSD: 5`) into the micro-USD unit AdminUsageEntryView's * own cost fields already use — matching metrics.go's usdToMicros(round) * convention (ground truth, dashboard-plan.md section 0). * * reuse-audit.md F8: the ONE definition (moved here from lib/usage-bars.ts, * which re-exports it for every existing import site) — usdToMicros/ * microsToUsd below are the ONE place a USD<->micro-USD conversion happens, * replacing the hand-rolled `Math.round(usd * 1_000_000)` / `micros / * 1_000_000` copies that used to bypass this constant. */ export const MICROS_PER_USD = 1_000_000 /** usdToMicros converts a plain USD amount to micro-USD, rounded to the nearest whole micro-USD (metrics.go's own usdToMicros(round) convention). */ export function usdToMicros(usd: number): number { return Math.round(usd * MICROS_PER_USD) } /** microsToUsd converts a micro-USD integer back to a plain USD float — the exact inverse of usdToMicros, modulo usdToMicros' own rounding. */ export function microsToUsd(micros: number): number { return micros / MICROS_PER_USD } /** formatCost renders a micro-USD integer (adminUsageEntryView's convention) as "$1.2345". */ export function formatCost(micros: number): string { return `$${microsToUsd(micros).toFixed(4)}` } /** Thresholds in micro-USD: 4 decimals under $1 (per-request costs are tiny), cents under $1000, whole dollars above. */ const COST_DISPLAY_CENTS_FROM_MICROS = 1_000_000 const COST_DISPLAY_WHOLE_FROM_MICROS = 1_000_000_000 function costFractionDigits(absMicros: number): number { if (absMicros < COST_DISPLAY_CENTS_FROM_MICROS) return 4 return absMicros < COST_DISPLAY_WHOLE_FROM_MICROS ? 2 : 0 } function formatUsdAmount(micros: number, minDigits: (max: number) => number): string { if (!Number.isFinite(micros)) return '—' const abs = Math.abs(micros) const max = costFractionDigits(abs) const text = microsToUsd(abs).toLocaleString('en-US', { minimumFractionDigits: minDigits(max), maximumFractionDigits: max }) return `${micros < 0 ? '-' : ''}$${text}` } /** * formatCostDisplay renders micro-USD for cards, headings and tooltips with thousands separators and magnitude-scaled * decimals. Tables and CSV keep formatCost's fixed 4 decimals. Rounding error is at most 0.5% from $1 up * (formal/Gateway/Ui/Cost.lean rel_err_ge1). */ export function formatCostDisplay(micros: number): string { return formatUsdAmount(micros, (max) => max) } /** formatCostAxis is formatCostDisplay without trailing zeros ("$0", "$0.5", "$1,600"), for chart ticks. */ export function formatCostAxis(micros: number): string { return formatUsdAmount(micros, () => 0) } /** * formatShortDate renders a Date as a compact "Oct 15" label (month/day * only, no time-of-day) — reuse-audit.md F13: the ONE run-out-date format, * replacing BurnDownChart.vue's and UserDetail.vue's two disagreeing * formats (a short date vs. a full formatTimestamp). A PROJECTED date (a * run-out estimate) should never imply false time-of-day precision, so * this intentionally omits it. */ export function formatShortDate(d: Date): string { return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) } /** formatExactInt renders a raw integer count with thousands separators ("1,234,567") — the precise value CompactNumber.vue and the usage chart's tooltip carry alongside a compact rendering, never lost. */ export function formatExactInt(n: number): string { return n.toLocaleString('en-US') } /** * formatCompactCount renders a raw token/request count as a compact, * SI-style label: below 10,000 the exact integer with thousands * separators ("9,999"); at or above that, three significant figures * with a k/M/B suffix ("12.3k", "456k", "1.23M", "45.6M", "1.23B"), * trailing zeros trimmed. Every digit beyond the third significant one * is FLOORED, never rounded — the same honesty rule formatRatePercent * already applies to the success-rate badge (lib/provider-rate.ts): a * count of 999,950 must never display as "1,000k", which would imply a * boundary the raw value has not actually crossed. This is DISPLAY * only — every caller pairs it with the exact value via a title/ * aria-label (CompactNumber.vue's own doc comment), and no accessor/sort * key ever calls this; DataTable columns sort on the raw number. */ export function formatCompactCount(n: number): string { const abs = Math.abs(n) if (abs < 10_000) return formatExactInt(n) const sign = n < 0 ? '-' : '' const [divisor, suffix] = abs < 1_000_000 ? [1_000, 'k'] : abs < 1_000_000_000 ? [1_000_000, 'M'] : [1_000_000_000, 'B'] // Three significant figures total: the floored integer part's own // digit count (1, 2, or 3) decides how many decimal places are left. // Uses `abs / divisor` ONLY to pick the digit-count tier, never as the // value that gets floored — see the review fix below for why. const roughScaled = abs / divisor const intDigits = Math.floor(roughScaled) >= 100 ? 3 : Math.floor(roughScaled) >= 10 ? 2 : 1 const decimals = 3 - intDigits const factor = 10 ** decimals // Review fix (IEEE-754 precision bug): computing `scaled = abs / // divisor` first, THEN `Math.floor(scaled * factor)`, under-reports by // one unit whenever the intermediate `scaled` is not exactly // representable in binary floating point — 8_700_000 / 1_000_000 // rounds to a double a hair BELOW 8.7 (≈8.699999999999999), so // flooring `that * 100` lands on 869, not 870, and displays "8.69M" // instead of "8.7M". Multiplying the ORIGINAL INTEGER abs by factor // FIRST keeps every intermediate value exact for any realistic count // (abs*factor stays within Number.MAX_SAFE_INTEGER, 2^53, for abs up // to roughly 9×10^13 — far beyond any token/request count this // function will ever see) — only the final division by divisor can // still land a hair off after toFixed's own rounding, which // toFixed(decimals) then resolves correctly. const floored = Math.floor((abs * factor) / divisor) / factor let text = floored.toFixed(decimals) if (decimals > 0) { text = text.replace(/0+$/, '').replace(/\.$/, '') } return `${sign}${text}${suffix}` } /** * isPowerOfTwo reports whether n is an exact power of two (1, 2, 4, 8, * ..., 262144, ...) — the classic bit trick: a power of two has exactly * one set bit, so `n & (n - 1)` clears it and leaves zero. Requires n > * 0 (0 and negative inputs are not powers of two here, by definition of * a token count). */ function isPowerOfTwo(n: number): boolean { return n > 0 && (n & (n - 1)) === 0 } /** * formatContextWindow renders a token count as a compact "128k"/"256k"- * style label (feature v0.23's context_window/AdminModelMetaView. * contextTokens unit — plain token count, not bytes). * * Review fix: the original version always divided by 1024 (binary-K), * which is right for LM Studio's own genuinely binary context sizes * (262144 -> "256k") but WRONG for a decimal, marketing-style count a * model card advertises in round thousands (128000 -> "125k" under * binary division, which reads as an odd, unexplained number — the * model is advertised as "128K", not "125K"). A plain "tokens % 1024 == * 0" check cannot tell these apart either: 128000 is ALSO an exact * multiple of 1024 (128000 = 125 × 1024), yet the two cases must render * differently. The correct discriminator is POWER OF TWO, not "multiple * of 1024": 262144 = 2^18 (a genuine binary size), 128000 factors as * 2^10 × 5^3 (not a power of two, despite dividing evenly by 1024). So: * binary-K/M (÷1024, ÷1024²) only when tokens is an exact power of two; * decimal-K/M (÷1000, ÷1,000,000) otherwise. Verified against every * worked example: 128000 -> "128k" (decimal), 1_000_000 -> "1M" * (decimal), 2_000_000 -> "2M" (decimal), 262144 -> "256k" (binary, * unchanged), 32768 -> "32k" (binary, unchanged). * * Plain digits, no suffix, for anything under 1024 either way: a * context window that small is rare (LM Studio's smallest observed * embedding models run 512-8192) but must still render as a real * number, not "0k". */ export function formatContextWindow(tokens: number): string { if (tokens < 1024) return `${tokens}` if (isPowerOfTwo(tokens)) { if (tokens < 1024 * 1024) return `${Math.round(tokens / 1024)}k` return `${Math.round(tokens / (1024 * 1024))}M` } // 999_500+ would round to "1000k"; the M branch renders it as "1M". if (tokens < 999_500) return `${roundScaled(tokens / 1_000)}k` return `${roundScaled(tokens / 1_000_000)}M` } /** roundScaled keeps one decimal below 10 ("1.5") so 1500 does not read as 2k, and rounds to whole units from 10 up. */ function roundScaled(value: number): string { return value < 10 ? String(Math.round(value * 10) / 10) : String(Math.round(value)) } /** formatUsdPerMTok renders a USD-per-million-tokens float as "$0.19" (2 decimals) — the shared building block for both the visible cost chip and ModelChip's hover detail. */ function formatUsdPerMTok(usd: number): string { return `$${usd.toFixed(2)}` } /** * formatModelCostHover renders ModelChip's hover-detail cost phrase * (feature v0.23, hover-detail refinement): "in $0.19 / out $0.51 per * MTok" for a priced model, or "free" when both sides are exactly 0 (a * KNOWN zero cost, ModelMetaConfig.Free — distinct from the caller never * passing a cost at all, which ModelChip's own metaDetail computed * handles by omitting the phrase entirely rather than calling this). */ export function formatModelCostHover(inputUsd: number, outputUsd: number): string { if (inputUsd === 0 && outputUsd === 0) return 'free' return `in ${formatUsdPerMTok(inputUsd)} / out ${formatUsdPerMTok(outputUsd)} per MTok` } /** formatPercentAxis labels a 0-1 rate axis tick with up to one decimal, so half-percent ticks stay distinct ("0.5%" not "1%"). */ export function formatPercentAxis(rate: number): string { return `${Math.round(rate * 1000) / 10}%` } /** formatModelPriceShort is the table-cell form of formatModelCostHover: "$1.00 / $2.00" (input / output per MTok). */ export function formatModelPriceShort(inputUsd: number, outputUsd: number): string { if (inputUsd === 0 && outputUsd === 0) return 'free' return `${formatUsdPerMTok(inputUsd)} / ${formatUsdPerMTok(outputUsd)}` } /** * formatLatencyMs renders a millisecond average (admin.go's * adminLatencyView.avgTtfbMs/avgDurationMs — feat: latency panel) as a * compact duration: plain milliseconds below 1000 ("120ms"), one-decimal * seconds at or above it ("1.2s"). These values are already averages * (sum/count from the in-process latency accumulator), so rounding here * carries none of formatCompactCount's boundary-crossing honesty concern * above — there is no discrete threshold a caller could misread as * crossed, just a plain rounded reading. */ export function formatLatencyMs(ms: number): string { if (!Number.isFinite(ms)) return '—' const rounded = Math.round(ms) if (rounded < 1000) return `${rounded}ms` return `${(ms / 1000).toFixed(1)}s` } /** * elapsedSecondsSince computes whole seconds between `iso` and `now`, or * null for the unset zero-time sentinel or an unparseable value — the * shared guard formatAgo and formatElapsedAgo below both build on, so * neither duplicates the sentinel/NaN check. */ function elapsedSecondsSince(iso: string | undefined, now: number): number | null { if (!iso || iso === ZERO_TIME) return null const then = new Date(iso).getTime() if (Number.isNaN(then)) return null return Math.max(0, Math.round((now - then) / 1000)) } /** formatAgo renders an ISO timestamp as "(Ns ago)", or "" for the unset zero-time sentinel. */ export function formatAgo(iso: string | undefined): string { const secs = elapsedSecondsSince(iso, Date.now()) return secs === null ? '' : ` (${secs}s ago)` } /** * formatElapsedAgo renders elapsed time since an ISO timestamp as a bare * "Ns ago" string — no surrounding parentheses, unlike formatAgo above * (every existing formatAgo caller depends on its own " (Ns ago)" shape * unchanged, so that convention stays untouched here). Target health * badges (AdminTargetHealthView, TargetsView's health column — feat/ * target-health) are the only caller today: a target's `lastCheck` needs * this bare form both standalone ("unhealthy (Ns ago)", parenthesized by * the caller) and inline ("Ns ago via probe"). The optional `now` * parameter (default Date.now()) makes the elapsed-time math * deterministic under test. "" for the unset zero-time sentinel or an * unparseable timestamp, mirroring formatAgo's own "nothing to show" * convention. */ export function formatElapsedAgo(iso: string | undefined, now = Date.now()): string { const secs = elapsedSecondsSince(iso, now) return secs === null ? '' : `${secs}s ago` } /** * formatUntil renders a FUTURE ISO timestamp as "in Ns" — the discovery * circuit breaker's openUntil (feat/provider-health, admin.go's * adminProviderView.OpenUntil) is the only caller — or "" for the unset * zero-time sentinel, an unparseable value, or a time already in the * past (a stale poll reading an openUntil the breaker has since moved * past; the next 5s refresh corrects it, so this just omits the detail * rather than showing a negative/nonsensical countdown). */ export function formatUntil(iso: string | undefined): string { if (!iso || iso === ZERO_TIME) return '' const then = new Date(iso).getTime() if (Number.isNaN(then)) return '' const secs = Math.round((then - Date.now()) / 1000) if (secs <= 0) return '' return `in ${secs}s` } /** formatTimestamp renders an ISO timestamp for display, or "never" for the unset zero-time sentinel. */ export function formatTimestamp(iso: string | undefined): string { if (!iso || iso === ZERO_TIME) return 'never' const d = new Date(iso) if (Number.isNaN(d.getTime())) return iso return d.toLocaleString() } /** * refreshState is the Providers tab's three-state refresh classification * (Feature B, v0.22 — last-refresh label honesty), shared by refreshLabel * (the accordion trigger's compact text) and refreshDetailLabel (the * accordion content's "Last refresh" detail row — folded review minor, * v0.22 review round: that row still read raw formatTimestamp before this * existed, so a discovery-off provider's expanded detail said "never" * right next to a trigger that correctly said "discovery off"): * discovery disabled reads 'off' regardless of lastRefresh (a disabled * provider's lastRefresh is always the unset sentinel, registry.go's own * maybeRefresh/warmFill never call finishRefresh for it — but this checks * discoveryEnabled first anyway, not lastRefresh's value, so it stays * correct even if that invariant ever changes); discovery enabled but * never yet refreshed reads 'pending'; otherwise 'refreshed'. */ function refreshState(discoveryEnabled: boolean, lastRefresh: string | undefined): 'off' | 'pending' | 'refreshed' { if (!discoveryEnabled) return 'off' return formatAgo(lastRefresh) ? 'refreshed' : 'pending' } /** * refreshLabel renders refreshState as the Providers tab's compact * trigger-row text: "discovery off", "pending", or the existing * "refreshed (Ns ago)" relative-time text. */ export function refreshLabel(discoveryEnabled: boolean, lastRefresh: string | undefined): string { switch (refreshState(discoveryEnabled, lastRefresh)) { case 'off': return 'discovery off' case 'pending': return 'pending' case 'refreshed': return `refreshed${formatAgo(lastRefresh)}` } } /** * refreshDetailLabel renders refreshState as the Providers tab's * accordion-content "Last refresh" detail row: "discovery off", "pending", * or the full formatTimestamp date/time (this row's own pre-existing * convention — refreshLabel's relative "(Ns ago)" belongs on the compact * trigger row only). */ export function refreshDetailLabel(discoveryEnabled: boolean, lastRefresh: string | undefined): string { switch (refreshState(discoveryEnabled, lastRefresh)) { case 'off': return 'discovery off' case 'pending': return 'pending' case 'refreshed': return formatTimestamp(lastRefresh) } } /** formatLimits renders a LimitsConfig as a short comma-joined summary, or "none" when unset. */ export function formatLimits(limits: LimitsConfig | undefined, sources?: LimitSources): string { if (!limits) return 'none' const parts: string[] = [] const add = (key: keyof LimitsConfig, text: string) => { if (!limits[key]) return const source = sources?.[key] parts.push(source ? `${text} (${source})` : text) } add('requestsPerMinute', `req/min ${limits.requestsPerMinute}`) add('requestsPerDay', `req/day ${limits.requestsPerDay}`) add('tokensPerMinute', `tok/min ${limits.tokensPerMinute}`) add('tokensPerDay', `tok/day ${limits.tokensPerDay}`) add('tokensPerMonth', `tok/month ${limits.tokensPerMonth}`) add('costPerDayUSD', `cost/day $${limits.costPerDayUSD}`) add('costPerMonthUSD', `cost/month $${limits.costPerMonthUSD}`) if (limits.pacing) add('pacing', `pace from ${limits.pacing.startPct}% up to ${limits.pacing.maxDelay}`) return parts.length ? parts.join(', ') : 'none' } /** * historyBucketFormat maps each history window to the fixed-width bucket * key format serveAdminUsageHistory echoes back (limits.go's windowKey: * hour="2006010215", day="20060102", month="200601" — Go reference-time * layouts, all digits, no separators). */ export function formatBucketLabel(bucket: string, window: HistoryWindow): string { switch (window) { case 'hour': { // bucketFor (limits.go) always buckets in UTC (t.UTC() before // formatting) — labeling the bare hour digits with no timezone reads // as local time to an operator outside UTC (14:xx local shows as // "12:00" for a UTC+2 reader). Explicit "UTC" suffix, not a // local-time conversion: day/month buckets are calendar dates with // no such ambiguity, so only the hour case needs this. const hour = bucket.slice(8, 10) return `${hour}:00 UTC` } case 'day': { const year = Number(bucket.slice(0, 4)) const month = Number(bucket.slice(4, 6)) const day = Number(bucket.slice(6, 8)) return new Date(year, month - 1, day).toLocaleDateString(undefined, { month: 'short', day: 'numeric', }) } case 'month': { const year = Number(bucket.slice(0, 4)) const month = Number(bucket.slice(4, 6)) return new Date(year, month - 1, 1).toLocaleDateString(undefined, { month: 'short', year: 'numeric', }) } } }