feat(webui): show the full token-usage picture in settings (#6638)
This commit is contained in:
1 parent
b49218b6a3
commit
e6b23c7356
42 files changed
+1116
-342
No files matched your search
+26
-14
@@ -842,28 +842,40 @@ export async function deleteSession(
|
||||
);
|
||||
}
|
||||
|
||||
export async function fetchSettings(
|
||||
const settingsReads = new WeakMap<WebUIMutationTransport, Map<string, Promise<SettingsPayload>>>();
|
||||
|
||||
export function fetchSettings(
|
||||
token: string,
|
||||
base: string = "",
|
||||
scope?: WebUIMutationTransport,
|
||||
): Promise<SettingsPayload> {
|
||||
return request<SettingsPayload>(
|
||||
`${base}/api/settings`,
|
||||
token,
|
||||
undefined,
|
||||
API_READ_TIMEOUT_MS,
|
||||
);
|
||||
const load = () => request<SettingsPayload>(`${base}/api/settings`, token, undefined, API_READ_TIMEOUT_MS);
|
||||
if (!scope) return load();
|
||||
let reads = settingsReads.get(scope);
|
||||
if (!reads) {
|
||||
reads = new Map();
|
||||
settingsReads.set(scope, reads);
|
||||
}
|
||||
const key = JSON.stringify([base, token]);
|
||||
const pending = reads.get(key);
|
||||
if (pending) return pending;
|
||||
const read = load().finally(() => reads.delete(key));
|
||||
reads.set(key, read);
|
||||
return read;
|
||||
}
|
||||
|
||||
export async function fetchSettingsUsage(
|
||||
token: string,
|
||||
base: string = "",
|
||||
options: { details?: boolean; cached?: NonNullable<SettingsPayload["usage"]>; signal?: AbortSignal } = {},
|
||||
): Promise<NonNullable<SettingsPayload["usage"]>> {
|
||||
return request<NonNullable<SettingsPayload["usage"]>>(
|
||||
`${base}/api/settings/usage`,
|
||||
token,
|
||||
undefined,
|
||||
API_READ_TIMEOUT_MS,
|
||||
);
|
||||
const headers: Record<string, string> = { Authorization: `Bearer ${token}` };
|
||||
if (options.cached?.revision) headers["If-None-Match"] = `W/"${options.cached.revision}"`;
|
||||
const response = await fetchWithTimeout(`/api/settings/usage${options.details ? "?details=true" : ""}`, {
|
||||
headers, credentials: "same-origin", cache: "no-store", signal: options.signal,
|
||||
}, API_READ_TIMEOUT_MS);
|
||||
if (response.status === 304 && options.cached) return options.cached;
|
||||
if (!response.ok) throw new ApiError(response.status, `HTTP ${response.status}`);
|
||||
return await response.json() as NonNullable<SettingsPayload["usage"]>;
|
||||
}
|
||||
|
||||
export type UpdateChannel = "stable" | "preview";
|
||||
|
||||
+23
-8
@@ -918,6 +918,8 @@ export interface SettingsPayload {
|
||||
cache_write_tokens: number;
|
||||
cache_read_observed_input_tokens: number;
|
||||
cache_write_observed_input_tokens: number;
|
||||
cache_miss_tokens?: number;
|
||||
cache_unknown_tokens?: number;
|
||||
total_tokens: number;
|
||||
reported_tokens?: number;
|
||||
estimated_tokens?: number;
|
||||
@@ -940,6 +942,8 @@ export interface SettingsPayload {
|
||||
cache_write_tokens: number;
|
||||
cache_read_observed_input_tokens: number;
|
||||
cache_write_observed_input_tokens: number;
|
||||
cache_miss_tokens?: number;
|
||||
cache_unknown_tokens?: number;
|
||||
total_tokens: number;
|
||||
reported_tokens?: number;
|
||||
estimated_tokens?: number;
|
||||
@@ -956,14 +960,17 @@ export interface SettingsPayload {
|
||||
}
|
||||
>;
|
||||
}>;
|
||||
total_tokens: number;
|
||||
total_tokens_30d: number;
|
||||
total_tokens_365d: number;
|
||||
peak_day_tokens: number;
|
||||
current_streak_days: number;
|
||||
longest_streak_days: number;
|
||||
active_days_30d: number;
|
||||
requests_30d: number;
|
||||
revision?: string;
|
||||
range_start?: string;
|
||||
range_end?: string;
|
||||
total_tokens?: number;
|
||||
total_tokens_30d?: number;
|
||||
total_tokens_365d?: number;
|
||||
peak_day_tokens?: number;
|
||||
current_streak_days?: number;
|
||||
longest_streak_days?: number;
|
||||
active_days_30d?: number;
|
||||
requests_30d?: number;
|
||||
failed_requests_30d?: number;
|
||||
reported_tokens_30d?: number;
|
||||
estimated_tokens_30d?: number;
|
||||
@@ -979,6 +986,8 @@ export interface SettingsPayload {
|
||||
cache_write_tokens: number;
|
||||
cache_read_observed_input_tokens: number;
|
||||
cache_write_observed_input_tokens: number;
|
||||
cache_miss_tokens?: number;
|
||||
cache_unknown_tokens?: number;
|
||||
total_tokens: number;
|
||||
reported_tokens: number;
|
||||
estimated_tokens: number;
|
||||
@@ -998,6 +1007,12 @@ export interface SettingsPayload {
|
||||
provider: string;
|
||||
model: string;
|
||||
total_tokens: number;
|
||||
measured_output_tokens?: number;
|
||||
generation_ms?: number;
|
||||
ttft_ms?: number;
|
||||
timed_requests?: number;
|
||||
cache_read_tokens?: number;
|
||||
cache_read_observed_input_tokens?: number;
|
||||
}>;
|
||||
updated_at?: string | null;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Providers report the same model under different ids: the Codex backend
|
||||
* answers with `openai-codex/gpt-6.1-sol` while other calls record the bare
|
||||
* `gpt-6-astra`. Grouping stays per provider, but the model name has to be
|
||||
* normalized first, otherwise one model shows up as several cards.
|
||||
*/
|
||||
const SEPARATORS = /[^a-z0-9]/g;
|
||||
|
||||
function identity(value: string): string {
|
||||
return value.toLowerCase().replace(SEPARATORS, "");
|
||||
}
|
||||
|
||||
export function normalizeUsageModel(provider: string, model: string): string {
|
||||
const separator = model.indexOf("/");
|
||||
if (separator <= 0) return model;
|
||||
const prefix = identity(model.slice(0, separator));
|
||||
const owner = identity(provider);
|
||||
// Only strip a prefix that names the provider itself; `qwen/…` under a
|
||||
// custom provider is part of the model name.
|
||||
return prefix && owner.startsWith(prefix) ? model.slice(separator + 1) : model;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Usage counters are routinely eight to eleven digits. Reading them requires
|
||||
* locale units (`100.5亿`, `10B`), but the exact value still has to stay
|
||||
* available for titles, accessible labels, and diagnostics.
|
||||
*/
|
||||
const COMPACT_THRESHOLD = 10_000;
|
||||
|
||||
export interface UsageNumberFormats {
|
||||
/** Locale units for five-digit and larger values, exact digits below that. */
|
||||
display: (value: number) => string;
|
||||
/** Grouped exact digits. */
|
||||
exact: (value: number) => string;
|
||||
}
|
||||
|
||||
export interface UsageFormats extends UsageNumberFormats {
|
||||
/** Locale units regardless of magnitude. */
|
||||
compact: (value: number) => string;
|
||||
/** Percentage with one fraction digit. */
|
||||
percent: (value: number) => string;
|
||||
/** Milliseconds as seconds with one fraction digit. */
|
||||
seconds: (value: number) => string;
|
||||
/** Short month and day for a `YYYY-MM-DD` key, in UTC. */
|
||||
date: (date: string) => string;
|
||||
}
|
||||
|
||||
const formatsByLocale = new Map<string, UsageFormats>();
|
||||
|
||||
/**
|
||||
* `Intl` constructors are comparatively expensive and the usage panels
|
||||
* re-render on every hover, so each locale's formatters are built once.
|
||||
*/
|
||||
export function usageFormats(locale: string): UsageFormats {
|
||||
const cached = formatsByLocale.get(locale);
|
||||
if (cached) return cached;
|
||||
const compact = new Intl.NumberFormat(locale, { notation: "compact", maximumFractionDigits: 1 });
|
||||
const exact = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 });
|
||||
const percent = new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 1 });
|
||||
const seconds = new Intl.NumberFormat(locale, { minimumFractionDigits: 1, maximumFractionDigits: 1 });
|
||||
const date = new Intl.DateTimeFormat(locale, { month: "short", day: "numeric", timeZone: "UTC" });
|
||||
const formats: UsageFormats = {
|
||||
display: (value) => (Math.abs(value) >= COMPACT_THRESHOLD ? compact.format(value) : exact.format(value)),
|
||||
exact: (value) => exact.format(value),
|
||||
compact: (value) => compact.format(value),
|
||||
percent: (value) => percent.format(value),
|
||||
seconds: (value) => seconds.format(value / 1000),
|
||||
date: (value) => date.format(new Date(`${value}T00:00:00Z`)),
|
||||
};
|
||||
formatsByLocale.set(locale, formats);
|
||||
return formats;
|
||||
}
|
||||
|
||||
/** Output tokens per second, or `null` when no generation time was measured. */
|
||||
export function usageSpeed(measuredOutputTokens: number, generationMs: number): number | null {
|
||||
return generationMs > 0 ? measuredOutputTokens / (generationMs / 1000) : null;
|
||||
}
|
||||
Reference in new issue
Block a user