okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
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 ) => { 6if ( kb < 1 ) 7return ` ${ Math . max ( 1 , Math . round ( kb * 1024 ))} B` ; 8if ( kb < 1024 ) 9return ` ${ kb . toFixed ( kb < 10 ? 1 : 0 )} KB` ; 10return ` ${( 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 { 19const d = new Date ( iso ); 20return d . toLocaleTimeString ( localeTag (), { hour :'2-digit' , minute :'2-digit' , hour12 :false }); 21} 22export function relTime ( iso :string | number | Date ) :string { 23const then = new Date ( iso ). getTime (); 24const diff = Date . now () - then ; 25if ( diff < 45000 ) 26return 'just now' ; 27const mins = Math . round ( diff / 60000 ); 28if ( mins < 60 ) 29return ` ${ mins } m ago` ; 30const hours = Math . round ( mins / 60 ); 31if ( hours < 24 ) 32return ` ${ hours } h ago` ; 33const days = Math . round ( hours / 24 ); 34if ( days < 7 ) 35return ` ${ days } d ago` ; 36return 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 { 46const then = new Date ( iso ). getTime (); 47const diff = Date . now () - then ; 48if ( diff < 45000 ) 49return 'now' ; 50const mins = Math . round ( diff / 60000 ); 51if ( mins < 60 ) 52return ` ${ mins } m` ; 53const hours = Math . round ( mins / 60 ); 54if ( hours < 24 ) 55return ` ${ hours } h` ; 56const days = Math . round ( hours / 24 ); 57if ( days < 7 ) 58return ` ${ days } d` ; 59return new Date ( iso ). toLocaleDateString ( localeTag (), { month :'short' , day :'numeric' }); 60} 61export function duration ( ms :number ) :string { 62if ( ms < 1000 ) 63return ` ${ Math . round ( ms )} ms` ; 64if ( ms < 60000 ) 65return ` ${( ms / 1000 ). toFixed ( 1 )} s` ; 66const s = Math . round ( ms / 1000 ); 67return ` ${ Math . floor ( s / 60 )} m ${ s % 60 } s` ; 68} 69export function uptime ( sec :number ) :string { 70if ( sec < 60 ) 71return ` ${ Math . round ( sec )} s` ; 72const m = Math . floor ( sec / 60 ); 73if ( m < 60 ) 74return ` ${ m } m ${ Math . round ( sec % 60 )} s` ; 75const h = Math . floor ( m / 60 ); 76if ( h < 24 ) 77return ` ${ h } h ${ m % 60 } m` ; 78return ` ${ Math . floor ( h / 24 )} d ${ h % 24 } h` ; 79} 80export function tokens ( n :number | null | undefined ) :string { 81if ( n == null || ! Number . isFinite ( n )) 82return '—' ; 83if ( n >= 1000000 ) 84return ` ${( n / 1000000 ). toFixed ( 1 )} M` ; 85if ( n >= 10000 ) 86return ` ${ Math . round ( n / 1000 )} k` ; 87if ( n >= 1000 ) 88return ` ${( n / 1000 ). toFixed ( 1 )} k` ; 89return ` ${ 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 :{ 103minGb :number ; 104maxGb :number ; 105}, memoryGb :number ) :Fit { 106if ( memoryGb >= ramClass . maxGb ) 107return 'good' ; 108if ( memoryGb >= ramClass . minGb ) 109return 'tight' ; 110return 'too-large' ; 111} 112export const FIT_LABEL :Record < Fit , string > = { 113good :'Likely to fit (estimate)' , 114tight :'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 { 129return Math . max ( 2 , Math . round ( 1 + gb * 1.2 )); 130} 131export function clamp ( n :number , min = 0 , max = 1 ) :number { 132return Math . min ( max , Math . max ( min , n )); 133} 134export function cx ( ...parts :( string | false | null | undefined )[]) :string { 135return parts . filter ( Boolean ). join ( ' ' ); 136} 137/** Identity comes from the host; never substitute a different real model. */ 138export function formatModelName ( model :{ 139family :string ; 140quant :string ; 141name ?:string ; 142local ?:unknown ; 143} | null | undefined ) :string { 144if ( ! model ) 145return 'No model loaded' ; 146if ( model . local ) 147return ` ${ model . name ?? model . family } · ${ model . quant } ` ; 148const 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 ; 149return ` ${ model . family } · ${ version ? ` ${ version } ( ${ model . quant } )` : model . quant } ` ; 150} 151export function userError ( message :string ) :string { 152return 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 ));