okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge tui-update (3165c2e8a)a1cc285fa

main
6.3 KiB156 linesraw
1import {localeTag} from './i18n';
2/** Small formatting helpers used across screens. */
3export const gb = (n: number, digits = 1) => `${n.toLocaleString(localeTag(), { minimumFractionDigits: digits, maximumFractionDigits: digits })} GB`;
4/** Human file size from a KB value. */
5export const fileSize = (kb: number) => {
6    if (kb < 1)
7        return `${Math.max(1, Math.round(kb * 1024))} B`;
8    if (kb < 1024)
9        return `${kb.toFixed(kb < 10 ? 1 : 0)} KB`;
10    return `${(kb / 1024).toFixed(1)} MB`;
11};
12/** Human size for model files: keeps one decimal below 1 GB. */
13export const sizeGb = (n: number) => (n < 1 ? `${n.toFixed(1)} GB` : `${Math.round(n)} GB`);
14export const gbShort = (n: number) => n >= 100 ? `${Math.round(n)}` : `${n.toFixed(n < 10 ? 1 : 0)}`;
15export const pct = (n: number, digits = 0) => `${n.toFixed(digits)}%`;
16export const num = (n: number) => n.toLocaleString(localeTag());
17export const tps = (n: number) => n >= 1000 ? `${Math.round(n).toLocaleString(localeTag())}` : n.toFixed(1);
18export function clockTime(iso: string | number | Date): string {
19    const d = new Date(iso);
20    return d.toLocaleTimeString(localeTag(), { hour: '2-digit', minute: '2-digit', hour12: false });
21}
22export function relTime(iso: string | number | Date): string {
23    const then = new Date(iso).getTime();
24    const diff = Date.now() - then;
25    if (diff < 45000)
26        return 'just now';
27    const mins = Math.round(diff / 60000);
28    if (mins < 60)
29        return `${mins}m ago`;
30    const hours = Math.round(mins / 60);
31    if (hours < 24)
32        return `${hours}h ago`;
33    const days = Math.round(hours / 24);
34    if (days < 7)
35        return `${days}d ago`;
36    return new Date(iso).toLocaleDateString(localeTag(), { month: 'short', day: 'numeric' });
37}
38/**
39 * The same age without the words: "9m", "3h", "5d".
40 *
41 * A sidebar row has no width to spare, and "26m ago" cost a third of the space a
42 * chat title needed (the title was the thing being cut). The unit carries the
43 * meaning on its own; the sentence form stays everywhere there is room for it.
44 */
45export function relTimeShort(iso: string | number | Date): string {
46    const then = new Date(iso).getTime();
47    const diff = Date.now() - then;
48    if (diff < 45000)
49        return 'now';
50    const mins = Math.round(diff / 60000);
51    if (mins < 60)
52        return `${mins}m`;
53    const hours = Math.round(mins / 60);
54    if (hours < 24)
55        return `${hours}h`;
56    const days = Math.round(hours / 24);
57    if (days < 7)
58        return `${days}d`;
59    return new Date(iso).toLocaleDateString(localeTag(), { month: 'short', day: 'numeric' });
60}
61export function duration(ms: number): string {
62    if (ms < 1000)
63        return `${Math.round(ms)} ms`;
64    if (ms < 60000)
65        return `${(ms / 1000).toFixed(1)} s`;
66    const s = Math.round(ms / 1000);
67    return `${Math.floor(s / 60)}m ${s % 60}s`;
68}
69export function uptime(sec: number): string {
70    if (sec < 60)
71        return `${Math.round(sec)}s`;
72    const m = Math.floor(sec / 60);
73    if (m < 60)
74        return `${m}m ${Math.round(sec % 60)}s`;
75    const h = Math.floor(m / 60);
76    if (h < 24)
77        return `${h}h ${m % 60}m`;
78    return `${Math.floor(h / 24)}d ${h % 24}h`;
79}
80export function tokens(n: number | null | undefined): string {
81    if (n == null || !Number.isFinite(n))
82        return '—';
83    if (n >= 1000000)
84        return `${(n / 1000000).toFixed(1)}M`;
85    if (n >= 10000)
86        return `${Math.round(n / 1000)}k`;
87    if (n >= 1000)
88        return `${(n / 1000).toFixed(1)}k`;
89    return `${Math.round(n)}`;
90}
91export const contextLabel = (max: number) => (max >= 1024 ? `${Math.round(max / 1024)}K` : `${max}`);
92/**
93 * Whether a model can actually run on this computer.
94 *
95 * A variant carries its requirement as a RAM class range; the machine has one
96 * figure. Turning that into three plain words is the whole job of the Models
97 * screen — "48 GB RAM class" asks the reader to know their own memory and do the
98 * comparison themselves, which is precisely what someone who finds computers
99 * intimidating cannot do.
100 */
101export type Fit = 'good' | 'tight' | 'too-large';
102export function fitFor(ramClass: {
103    minGb: number;
104    maxGb: number;
105}, memoryGb: number): Fit {
106    if (memoryGb >= ramClass.maxGb)
107        return 'good';
108    if (memoryGb >= ramClass.minGb)
109        return 'tight';
110    return 'too-large';
111}
112export const FIT_LABEL: Record<Fit, string> = {
113    good: 'Likely to fit (estimate)',
114    tight: 'Memory may be tight (estimate)',
115    'too-large': 'Exceeds estimated memory budget',
116};
117/**
118 * A rough wall-clock estimate for fetching a model.
119 *
120 * Downloads dominate the time the setup wizard and the Models screen are asking
121 * about, and both need to give the same answer — a figure that differs between
122 * two screens describing the same download is worse than no figure. Kept here so
123 * there is one formula rather than two.
124 *
125 * Deliberately coarse: it is rounded to whole minutes and floored at 2, because
126 * a precise-looking number from an unknown connection is a false promise.
127 */
128export function downloadMinutes(gb: number): number {
129    return Math.max(2, Math.round(1 + gb * 1.2));
130}
131export function clamp(n: number, min = 0, max = 1): number {
132    return Math.min(max, Math.max(min, n));
133}
134export function cx(...parts: (string | false | null | undefined)[]): string {
135    return parts.filter(Boolean).join(' ');
136}
137/** Identity comes from the host; never substitute a different real model. */
138export function formatModelName(model: {
139    family: string;
140    quant: string;
141    name?: string;
142    local?: unknown;
143} | null | undefined): string {
144    if (!model)
145        return 'No model loaded';
146    if (model.local)
147        return `${model.name ?? model.family} · ${model.quant}`;
148    const version = ({ Q2_K: 'Smallest', Q3_K_M: 'Compact', Q4_K_M: 'Balanced', Q5_K_M: 'High quality', Q6_K: 'Very high', Q8_0: 'Near-original', BF16: 'Original' } as Record<string, string>)[model.quant] ?? model.name;
149    return `${model.family} · ${version ? `${version} (${model.quant})` : model.quant}`;
150}
151export function userError(message: string): string {
152    return message.replace(/^\s*Fixture:\s*/i, '').replace(/^./, c => c.toUpperCase());
153}
154
155/** Decimal megabytes (1 MB = 1,000,000 bytes, at least 1): the same figure as the Terminal and the release page. The only size helper in MB; no binary units. */
156export const megabytes = (bytes: number) => Math.max(1, Math.round(bytes / 1_000_000));