mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-25 10:42:45 +00:00
* fix(usage): retain last successful data when polls fail Keep the latest poll outcome separate from its successful snapshot so errors remain visible without discarding known usage or advancing its freshness. Preserve attempt timestamps for response ordering and isolate retained values by scoped login set. * fix(usage): preserve history through discovery failures A failed account-store read does not confirm that an account was removed. Retain its existing host mapping until successful discovery replaces it, without carrying mappings across agent launch environments.
49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
import { formatTokenCount } from "@/components/context-window-meter.utils";
|
|
import type { UsageDisplayAs } from "./preferences";
|
|
import type { UsageBalanceUnit } from "./types";
|
|
|
|
export function clampPct(value: number): number {
|
|
return Math.max(0, Math.min(100, value));
|
|
}
|
|
|
|
export function formatPct(value: number): string {
|
|
return `${Math.round(clampPct(value))}%`;
|
|
}
|
|
|
|
/** "31%" of the window used, or "69% left" of it. */
|
|
export function formatDisplayPct(value: number, displayAs: UsageDisplayAs): string {
|
|
return displayAs === "used" ? formatPct(value) : `${formatPct(value)} left`;
|
|
}
|
|
|
|
function relativeDuration(iso: string): string | null {
|
|
const diffMs = new Date(iso).getTime() - Date.now();
|
|
if (!Number.isFinite(diffMs)) return null;
|
|
if (diffMs <= 0) return "now";
|
|
const diffMinutes = Math.floor(diffMs / 60_000);
|
|
const diffHours = Math.floor(diffMinutes / 60);
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
if (diffDays > 0) return `${diffDays}d`;
|
|
if (diffHours > 0) return `${diffHours}h`;
|
|
return `${diffMinutes}m`;
|
|
}
|
|
|
|
export function formatResetLabel(iso: string | null | undefined, retained = false): string | null {
|
|
if (!iso) return null;
|
|
const rel = relativeDuration(iso);
|
|
if (!rel) return null;
|
|
if (rel === "now" && retained) return "Reset time passed";
|
|
return rel === "now" ? "resetting now" : `resets ${rel}`;
|
|
}
|
|
|
|
/** A balance amount as the app's language writes it: "$1,234.50", "12,345". */
|
|
export function formatAmount(value: number, unit: UsageBalanceUnit, locale: string): string {
|
|
switch (unit) {
|
|
case "usd":
|
|
return new Intl.NumberFormat(locale, { style: "currency", currency: "USD" }).format(value);
|
|
case "tokens":
|
|
return formatTokenCount(value);
|
|
default:
|
|
return new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }).format(value);
|
|
}
|
|
}
|