okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e8fb221c
main
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 { 12code :OutcomeCode ; 13tone :Tone ; 14icon :'check' | 'clock' | 'x' | 'stop' ; 15/** the reason within the outcome (busy_pc: noisy | drift | not_tried; failed: start_failed | no_memory | other) */ 16reason :string ; 17gainPct :number | null ; 18baseTokS :number | null ; 19tunedTokS :number | null ; 20noisePct :number | null ; 21minutes :number | null ; 22presetName :string | null ; 23/** the person can try again straight away (nothing is running) */ 24retry :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 { 30const v = result ?. values ?? {}; 31const facts = { 32gainPct :num ( v . gain_pct ) ?? num ( result ?. gain_pct ), 33baseTokS :num ( v . base_tok_s ) ?? num ( result ?. base ?. decode_tok_s ), 34tunedTokS :num ( v . tuned_tok_s ) ?? num ( result ?. chosen ?. decode_tok_s ), 35noisePct :num ( v . noise_pct ), minutes :num ( v . minutes ), 36presetName :v . preset_name ?? result ?. preset ?. name ?? null , 37}; 38const make = ( code :OutcomeCode , tone :Tone , icon :Outcome [ 'icon' ], reason = '' , retry = true ) :Outcome => ({ code, tone, icon, reason, retry, ...facts }); 39if ( jobState === 'cancelled' ) return make ( 'stopped' , 'neutral' , 'stop' ); 40if ( jobState === 'error' && ! result ) return make ( 'failed' , 'bad' , 'x' , 'other' ); 41const code = result ?. code ; 42if ( code === 'faster' ) return make ( 'faster' , 'ok' , 'check' ); 43if ( code === 'already_fastest' ) return make ( 'already_fastest' , 'ok' , 'check' ); 44if ( code === 'busy_pc' ) return make ( 'busy_pc' , 'warn' , 'clock' , result ?. reason_code ?? 'noisy' ); 45if ( code === 'failed' ) return make ( 'failed' , 'bad' , 'x' , result ?. reason_code ?? 'other' ); 46if ( code === 'stopped' ) return make ( 'stopped' , 'neutral' , 'stop' ); 47// answers from a check of a saved tuning 48if ( result ?. verdict === 'keep' ) return make ( 'still_fast' , 'ok' , 'check' ); 49if ( result ?. verdict === 'retune' ) return make ( 'slower' , 'warn' , 'clock' ); 50if ( result ?. verdict === 'inconclusive' ) return make ( 'unsure' , 'warn' , 'clock' ); 51// answers without a code (an older Control) 52switch ( result ?. outcome ) { 53case 'improved' :return make ( 'faster' , 'ok' , 'check' ); 54case 'default_confirmed' :return make ( 'already_fastest' , 'ok' , 'check' ); 55case 'inconclusive' :return make ( 'busy_pc' , 'warn' , 'clock' , 'noisy' ); 56case 'cancelled' :return make ( 'stopped' , 'neutral' , 'stop' ); 57case 'base_failed' :return make ( 'failed' , 'bad' , 'x' , 'other' ); 58default :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 { 70const lines :string [] = []; 71const say = ( text :unknown ) => { if ( typeof text === 'string' && text . trim () && ! lines . includes ( text . trim ())) lines . push ( text . trim ()); }; 72say ( result ?. message ); 73for ( const note of result ?. notes ?? []) say ( note ); 74if ( ! lines . length ) return null ; 75const machine :string [] = []; 76if ( typeof hardware ?. ram_gb === 'number' && hardware . ram_gb > 0 ) machine . push ( `Memory: ${ Math . round ( hardware . ram_gb )} GB in this PC` ); 77for ( const g of hardware ?. gpus ?? []) if ( g ?. name ) machine . push ( `Graphics card: ${ g . name } ${ g . vram_gb ? `, ${ Math . round ( g . vram_gb )} GB` : '' } ` ); 78return [ ...lines , ...( machine . length ?[ '' , ...machine ] :[])]. join ( '\n' ); 79}