okami.mom/strata

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

Niko1221rc1 merge tui-update (3165c2e8a)a1cc285fa

main
7.4 KiB97 linesraw
1import { tr } from '../lib/i18n';
2import { Icon, type IconName } from './ui';
3import type { TuneProgress } from '../lib/native-tune';
4import type { Outcome } from '../lib/tune-outcome';
5import { tokS } from '../lib/tune-outcome';
6import { tunePhase, tuneSpeeds } from '../lib/tune-phase';
7
8const STEPS: { icon: IconName; title: string; running: string }[] = [
9  { icon: 'cpu', title: 'Checking your PC', running: 'Looking at your graphics card and memory' },
10  { icon: 'gauge', title: 'Measuring the base speed', running: 'Timing your current settings' },
11  { icon: 'sliders', title: 'Trying settings', running: 'Trying other settings, one at a time' },
12  { icon: 'thinking', title: 'Confirming the best one', running: 'Repeating the test to be sure' },
13  { icon: 'disk', title: 'Saving', running: 'Keeping it for this model; you can undo it' },
14];
15
16const fmt = (n: number) => (Math.round(n * 10) / 10).toFixed(1);
17
18function Spark({ values }: { values: number[] }) {
19  if (values.length < 2) return <div className="tune-spark tune-spark--empty" aria-hidden="true"/>;
20  const lo = Math.min(...values), hi = Math.max(...values), span = Math.max(0.0001, hi - lo);
21  const pts = values.map((v, i) => [(i / (values.length - 1)) * 220, 56 - ((v - lo) / span) * 46] as const);
22  const line = pts.map(([x, y], i) => `${i ? 'L' : 'M'}${x.toFixed(1)} ${y.toFixed(1)}`).join(' ');
23  const [lx, ly] = pts[pts.length - 1];
24  return <svg className="tune-spark" viewBox="0 0 220 64" preserveAspectRatio="none" aria-hidden="true"><path d={`${line} L220 64 L0 64Z`} className="tune-spark__fill"/><path d={line} className="tune-spark__line"/><circle cx={lx} cy={ly} r="4"/></svg>;
25}
26
27/** The tuner at work: five steps, each with its own icon and state, and the speed it has measured so far. */
28export function TuneRunView({ progress, ratio, saving }: { progress: TuneProgress | undefined; ratio: number; saving: boolean }) {
29  const at = saving ? 4 : tunePhase(progress?.stage);
30  const speeds = tuneSpeeds(progress);
31  const base = speeds[0]?.tok_s ?? null;
32  const now = speeds.length ? speeds[speeds.length - 1].tok_s : null;
33  const left = progress?.minutesLeft ?? null;
34  const sub = (i: number): string => {
35    if (i < at) return i === 1 && base != null ? tr("Starting point: {0} tokens per second", [fmt(base)]) : '';
36    if (i === at && progress?.step) return progress.step;
37    return i === at ? tr(STEPS[i].running) : '';
38  };
39  return <div className="tune-run">
40    <ol className="tune-steps2" aria-label={tr("Tuning steps")}>
41      {STEPS.map((s, i) => {
42        const state = i < at ? 'done' : i === at ? 'run' : 'pending';
43        return <li key={s.title} className="tune-step" data-s={state} data-step={s.icon} aria-current={state === 'run' ? 'step' : undefined}>
44          <span className="tune-step__badge"><Icon name={state === 'done' ? 'check' : s.icon} size={22}/></span>
45          <span className="tune-step__text"><strong>{tr(s.title)}</strong><small>{sub(i)}</small></span>
46          <span className="tune-step__state">{tr(state === 'done' ? 'Done' : state === 'run' ? 'Running' : 'Waiting')}</span>
47        </li>;
48      })}
49    </ol>
50    <aside className="tune-live" aria-live="polite">
51      <span className="label">{tr("Speed now")}</span>
52      <div className="tune-live__big">{now != null ? fmt(now) : '—'}<small>{tr("tokens per second")}</small></div>
53      <Spark values={speeds.map(x => x.tok_s)}/>
54      <div className="tune-live__bar" role="progressbar" aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(ratio * 100)}><i style={{ transform: `scaleX(${Math.min(1, Math.max(.03, ratio))})` }}/></div>
55      <p className="small muted">{left != null ? tr("about {0} min left", [left]) : tr("This takes about 20 minutes.")}</p>
56      <ul className="tune-live__list">{speeds.slice(-6).map((x, i) => <li key={`${x.label}-${i}`}><span>{x.label === 'base' ? tr("Your settings") : x.label}</span><b data-up={base != null && x.label !== 'base' && x.tok_s > base ? 'true' : undefined}>{fmt(x.tok_s)}</b></li>)}</ul>
57    </aside>
58  </div>;
59}
60
61/**
62 * The result of a tune, in one layout whatever happened: a round tile (green, amber, red or quiet), a headline, one sentence, up to three facts.
63 * The buttons are the dialog's footer.
64 */
65export function TuneOutcomeCard({ outcome, details }: { outcome: Outcome; details?: string | null }) {
66  const o = outcome;
67  const headline = ({
68    faster: o.gainPct != null ? tr('{0}% faster', [Math.round(o.gainPct)]) : tr('Faster answers'),
69    already_fastest: tr('Already the fastest'), still_fast: tr('Still as fast as before'), slower: tr('Slower than before'),
70    busy_pc: tr("Couldn't measure reliably"), unsure: tr("Couldn't tell right now"), failed: tr("Tuning didn't finish"), stopped: tr('Stopped'),
71  } as Record<string, string>)[o.code];
72  const lead = ({
73    faster: tr('Your answers now arrive sooner. Quality is the same.'),
74    already_fastest: tr('Nothing we tried beat your current settings, so nothing was changed.'),
75    still_fast: tr('Your saved tuning is still working as well as when it was saved.'),
76    slower: tr('It is slower than before. You can tune again.'),
77    unsure: tr('Your tuning was kept. Try the check again later.'),
78    busy_pc: o.reason === 'not_tried' ? tr('Your PC was too busy to try other settings, so nothing was changed. Try again when other programs are closed.') : tr("Your PC was busy during the test, so we couldn't measure reliably. Nothing was changed. Try again when other programs are closed."),
79    failed: o.reason === 'no_memory' ? tr("There wasn't enough free memory to test. Nothing was changed. Close other programs and try again.") : o.reason === 'start_failed' ? tr('The engine could not start with these settings. Nothing was changed.') : tr('The engine stopped while we were measuring. Nothing was changed.'),
80    stopped: tr('Nothing was changed.'),
81  } as Record<string, string>)[o.code];
82  const facts: [string, string][] = [];
83  if (o.code === 'faster') { facts.push([tr('Before'), `${tokS(o.baseTokS)} ${tr('tok/s')}`], [tr('After'), `${tokS(o.tunedTokS)} ${tr('tok/s')}`]); if (o.presetName) facts.push([tr('Saved as'), o.presetName]); }
84  else if (o.code === 'already_fastest' || o.code === 'still_fast') { facts.push([tr('Speed'), `${tokS(o.baseTokS)} ${tr('tok/s')}`]); if (o.minutes != null) facts.push([tr('Took'), tr('{0} min', [Math.max(1, Math.round(o.minutes))])]); if (o.presetName) facts.push([tr('Saved as'), o.presetName]); }
85  else if (o.code === 'busy_pc') { facts.push([tr('Speed'), o.baseTokS != null ? `${tokS(o.baseTokS)} ${tr('tok/s')}` : '—']); if (o.noisePct != null) facts.push([tr('Variation'), `${Math.round(o.noisePct)}%`]); facts.push([tr('Changed'), tr('Nothing')]); }
86  else if (o.code === 'failed' || o.code === 'stopped' || o.code === 'unsure' || o.code === 'slower') facts.push([tr('Changed'), tr('Nothing')]);
87  return <div className="tune-outcome" data-tone={o.tone} data-code={o.code}>
88    <div className="tune-outcome__tile" aria-hidden="true"><Icon name={o.icon} size={34}/></div>
89    <div className="tune-outcome__body" role="status">
90      <h3>{headline}</h3>
91      <p>{lead}</p>
92      {details && (o.code === 'failed' || o.code === 'busy_pc') ? <p className="small muted">{tr("Open Details to see why.")}</p> : null}
93      {facts.length ? <dl className="tune-outcome__facts">{facts.map(([k, v]) => <div key={k}><dt>{k}</dt><dd>{v}</dd></div>)}</dl> : null}
94      {details ? <details className="tune-outcome__details"><summary>{tr('Details')}</summary><pre>{details}</pre></details> : null}
95    </div>
96  </div>;
97}