okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import type { NativeState } from './native' ; 2import { nativeCatalogModels } from './native-catalog' ; 3 4/** The parts of Settings > Engine that need no React: the list's shape, sizes, makes, version order, and which models need a newer engine. */ 5/** One engine package installed on this computer, as Control lists it (`strata components engines`, strata_control/engine_library.py). */ 6/** qwen, or the engine of a Qwen4 model shape: shape:<shape> (strata_control/engine_library.py SHAPE_PREFIX; an id, never shown). */ 7export type EngineFamily = 'qwen' | `shape:${string } `; 8export interface InstalledEngine { 9family :EngineFamily ; 10folder :string ; 11version :string ; 12backend :string ; 13platform :string ; 14variant :string ; 15bytes :number ; 16/** In use: the one a model starts on. */ 17active :boolean ; 18/** The newest one that can run on this computer. */ 19newest :boolean ; 20fits :boolean ; 21/** Why this computer cannot run it: a code the page words, and its values. */ 22why_code ?:string | null ; 23why_args ?:string []; 24can_use :boolean ; 25can_delete :boolean ; 26blocked_code ?:'active' | 'running' | null ; 27build_id ?:string ; 28path ?:string ; 29root ?:string ; 30} 31export interface EngineGroup { family :EngineFamily ; /** a model shape's engine: its shape (q4_lite) */ shape ?:string ; /** null for a shape: the page names it (engineGroupTitle) */ title :string | null ; engines :InstalledEngine [] } 32 33/** The model family of the feed that runs on a shape's engine (strata_control/shape_engine.py family_of). */ 34const SHAPE_FAMILY_PREFIX = 'qwen4exp_' ; 35/** 36* The heading of a group in Settings > Engine: Control's title for the Qwen engine; for a model shape's engine the model's own title from the 37* catalog, the shape's name when the catalog does not have it. No new words; the family id is never shown. 38*/ 39export function engineGroupTitle ( group :EngineGroup , catalogFamilies :readonly { family :string ; title ?:string | null }[] | null | undefined ) :string { 40if ( group . title ) return group . title ; 41const shape = group . shape ?? ( group . family . startsWith ( 'shape:' ) ?group . family . slice ( 6 ) :'' ); 42const title = ( catalogFamilies ?? []). find ( f => f . family === SHAPE_FAMILY_PREFIX + shape )?. title ; 43return title ?. trim () || shape ; 44} 45export interface EngineList { available :boolean ; families :EngineGroup []; total_bytes :number } 46 47/** The make of graphics card an engine is built for, in the words a person knows. */ 48export const engineMake = ( backend :string ) :string => ({ cuda :'NVIDIA' , hip :'AMD' , sycl :'Intel' , metal :'Apple' , cpu :'CPU' } as Record < string , string >)[ backend ] ?? backend ; 49 50/** "1.0 GB", or "640 MB" below a gigabyte: decimal units, like the Terminal and the release page. */ 51export function engineSize ( bytes :number , locale ?:string ) :string { 52if ( bytes >= 1e9 ) return ` ${( bytes / 1e9 ). toLocaleString ( locale , { minimumFractionDigits : 1 , maximumFractionDigits : 1 })} GB` ; 53return ` ${ Math . max ( 1 , Math . round ( bytes / 1e6 )). toLocaleString ( locale )} MB` ; 54} 55 56const parts = ( v :string ) => v . split ( / [.\-+] / ). map ( p => ( / ^\d+$ / . test ( p ) ?Number ( p ) :0 )); 57/** Is version a older than version b? ("0.1.9" < "0.1.10", "0.1.49" < "0.1.49.1"). */ 58export function versionLess ( a :string , b :string ) :boolean { 59const x = parts ( a ), y = parts ( b ); 60for ( let i = 0 ; i < Math . max ( x . length , y . length ); i ++ ) if (( x [ i ] ?? 0 ) !== ( y [ i ] ?? 0 )) return ( x [ i ] ?? 0 ) < ( y [ i ] ?? 0 ); 61return false ; 62} 63 64/** 65* The installed models that need a newer engine than `version` (the catalog's `engine_min`). A switch to an older engine warns about these and goes on: 66* Strata recommends, it does not force. Only the Qwen family has catalog minimums. 67*/ 68export function modelsNeedingNewer ( native :NativeState | null | undefined , engine :InstalledEngine ) :string [] { 69if ( ! native ?. installed ?. models . length || ! native . catalog || engine . family !== 'qwen' ) return []; 70const sizes = nativeCatalogModels ( native . catalog . families ). flatMap ( f => f . sizes ) as unknown as { id :string ; model :string ; engine_min ?:string | null }[]; 71const names :string [] = []; 72for ( const installed of native . installed . models ) { 73const size = sizes . find ( s => s . id === installed . model ); 74if ( size ?. engine_min && versionLess ( engine . version , size . engine_min )) names . push ( installed . model_name || size . model || installed . model ); 75} 76return names ; 77} 78