okami.mom/strata

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

Niko1221Trial2 merge f-0291-gh2-app (0dfa2d9b1)2e8fb221c

main
4.6 KiB79 linesraw
1import type { TuneResult } from './native-tune';
2
3/**
4 * One layout for every way a tune can end: a tone (the round tile's colour and icon), a headline, one plain sentence, up to three facts, and
5 * the buttons that make sense. The host says which outcome it was (`code`, `reason_code`, `values`, see strata_control/autotune.py); older
6 * answers without a code are read from `outcome` and `verdict`. All words go through the catalog in the component, not here.
7 */
8export type OutcomeCode = 'faster' | 'already_fastest' | 'busy_pc' | 'failed' | 'stopped' | 'still_fast' | 'slower' | 'unsure';
9export type Tone = 'ok' | 'warn' | 'bad' | 'neutral';
10
11export interface Outcome {
12  code: OutcomeCode;
13  tone: Tone;
14  icon: 'check' | 'clock' | 'x' | 'stop';
15  /** the reason within the outcome (busy_pc: noisy | drift | not_tried; failed: start_failed | no_memory | other) */
16  reason: string;
17  gainPct: number | null;
18  baseTokS: number | null;
19  tunedTokS: number | null;
20  noisePct: number | null;
21  minutes: number | null;
22  presetName: string | null;
23  /** the person can try again straight away (nothing is running) */
24  retry: boolean;
25}
26
27const num = (v: unknown): number | null => (typeof v === 'number' && Number.isFinite(v) ? v : null);
28
29export function readOutcome(result: TuneResult | null | undefined, jobState: 'done' | 'error' | 'cancelled' | 'running' | undefined): Outcome {
30  const v = result?.values ?? {};
31  const facts = {
32    gainPct: num(v.gain_pct) ?? num(result?.gain_pct),
33    baseTokS: num(v.base_tok_s) ?? num(result?.base?.decode_tok_s),
34    tunedTokS: num(v.tuned_tok_s) ?? num(result?.chosen?.decode_tok_s),
35    noisePct: num(v.noise_pct), minutes: num(v.minutes),
36    presetName: v.preset_name ?? result?.preset?.name ?? null,
37  };
38  const make = (code: OutcomeCode, tone: Tone, icon: Outcome['icon'], reason = '', retry = true): Outcome => ({ code, tone, icon, reason, retry, ...facts });
39  if (jobState === 'cancelled') return make('stopped', 'neutral', 'stop');
40  if (jobState === 'error' && !result) return make('failed', 'bad', 'x', 'other');
41  const code = result?.code;
42  if (code === 'faster') return make('faster', 'ok', 'check');
43  if (code === 'already_fastest') return make('already_fastest', 'ok', 'check');
44  if (code === 'busy_pc') return make('busy_pc', 'warn', 'clock', result?.reason_code ?? 'noisy');
45  if (code === 'failed') return make('failed', 'bad', 'x', result?.reason_code ?? 'other');
46  if (code === 'stopped') return make('stopped', 'neutral', 'stop');
47  // answers from a check of a saved tuning
48  if (result?.verdict === 'keep') return make('still_fast', 'ok', 'check');
49  if (result?.verdict === 'retune') return make('slower', 'warn', 'clock');
50  if (result?.verdict === 'inconclusive') return make('unsure', 'warn', 'clock');
51  // answers without a code (an older Control)
52  switch (result?.outcome) {
53    case 'improved': return make('faster', 'ok', 'check');
54    case 'default_confirmed': return make('already_fastest', 'ok', 'check');
55    case 'inconclusive': return make('busy_pc', 'warn', 'clock', 'noisy');
56    case 'cancelled': return make('stopped', 'neutral', 'stop');
57    case 'base_failed': return make('failed', 'bad', 'x', 'other');
58    default: return make(jobState === 'error' ? 'failed' : 'unsure', jobState === 'error' ? 'bad' : 'warn', jobState === 'error' ? 'x' : 'clock', jobState === 'error' ? 'other' : '');
59  }
60}
61
62export const tokS = (n: number | null): string => (n == null ? '—' : `${(Math.round(n * 10) / 10).toFixed(1)}`);
63
64/**
65 * What the Details of a tuning that did not finish say (#4805): the tuner's own sentence for why (for a start that failed, the engine's last words;
66 * for memory, how much was free and how much it needed), its notes, and what this PC has, so a person reading the dialog sees the cause and the
67 * resources in one place. Plain text, one line each; null when there is nothing to add.
68 */
69export function failureDetails(result: TuneResult | null | undefined, hardware?: { ram_gb?: number; gpus?: { name: string; vram_gb: number }[] } | null): string | null {
70  const lines: string[] = [];
71  const say = (text: unknown) => { if (typeof text === 'string' && text.trim() && !lines.includes(text.trim())) lines.push(text.trim()); };
72  say(result?.message);
73  for (const note of result?.notes ?? []) say(note);
74  if (!lines.length) return null;
75  const machine: string[] = [];
76  if (typeof hardware?.ram_gb === 'number' && hardware.ram_gb > 0) machine.push(`Memory: ${Math.round(hardware.ram_gb)} GB in this PC`);
77  for (const g of hardware?.gpus ?? []) if (g?.name) machine.push(`Graphics card: ${g.name}${g.vram_gb ? `, ${Math.round(g.vram_gb)} GB` : ''}`);
78  return [...lines, ...(machine.length ? ['', ...machine] : [])].join('\n');
79}