okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import type { ConfigDocument } from './config' ; 2import type { EngineConfig , Hardware , ImageModelChoice } from './types' ; 3 4export type ComputerUse = 'shared' | 'full' ; 5export interface RunProfile { 6id :string ; 7name :string ; 8variantId :string ; 9hardware :string ; 10engineVersion :string ; 11config :ConfigDocument ; 12imageModel ?:ImageModelChoice ; 13updatedAt :string ; 14} 15export interface RunProfiles { 16schema :1 ; 17profiles :RunProfile []; 18defaults :Record < string , string >; 19} 20export const RUN_PROFILES_KEY = 'strata.run-profiles.v1' ; 21export const emptyProfiles = () :RunProfiles => ({ schema :1 , profiles :[], defaults :{} }); 22 23export function computerIdentity ( hardware :Hardware ) { 24return JSON . stringify ({ 25host :hardware . host . name , platform :hardware . host . platform , arch :hardware . host . arch , 26cpu :hardware . cpu . model , cores :hardware . cpu . cores , ram :hardware . memory . totalGb , 27processors :hardware . processors ?. map ( p => [ p . socket , p . model , p . cores ]), 28gpus :hardware . gpus . map ( g => [ g . id ?? g . index , g . name , g . vramGb , g . backend ]), 29backend :hardware . accelerator . backend , 30}); 31} 32export function defaultProfileKey ( hardware :string , variantId :string ) { return ` ${ hardware } \n ${ variantId } ` ; } 33export function configIdentity ( config :ConfigDocument ) :string { 34const canonical = ( v :unknown ) :unknown => Array . isArray ( v ) ?v . map ( canonical ) :v && typeof v === 'object' 35 ?Object . fromEntries ( Object . entries ( v ). sort (([ a ], [ b ]) => a . localeCompare ( b )). map (([ k , item ]) => [ k , canonical ( item )])) :v ; 36return JSON . stringify ( canonical ( config )); 37} 38export function readProfiles ( text :string | null ) :RunProfiles { 39if ( ! text ) return emptyProfiles (); 40const parsed = JSON . parse ( text ); 41if ( parsed ?. schema !== 1 || ! Array . isArray ( parsed . profiles ) || ! parsed . defaults || typeof parsed . defaults !== 'object' || Array . isArray ( parsed . defaults )) throw new Error ( 'Saved run profiles could not be read.' ); 42const profiles = parsed . profiles . filter (( p :RunProfile ) => p && typeof p . id === 'string' && typeof p . name === 'string' && typeof p . variantId === 'string' && typeof p . hardware === 'string' && typeof p . engineVersion === 'string' && p . config ?. schema === 'strata.engine/1' && p . config . beginner && p . config . expert && ! Array . isArray ( p . config . expert )). map (( p :RunProfile ) => ({ ...p , imageModel :[ 'off' , 'q4' , 'q5' , 'q6' , 'q8' ]. includes ( p . imageModel ?? '' ) ?p . imageModel :'off' })); 43const defaults = Object . fromEntries ( Object . entries ( parsed . defaults ). filter (([, id ]) => typeof id === 'string' && profiles . some (( p :RunProfile ) => p . id === id ))) as Record < string , string >; 44return { schema :1 , profiles, defaults}; 45} 46export function saveProfile ( store :RunProfiles , profile :RunProfile , makeDefault :boolean ) :RunProfiles { 47const snapshot = structuredClone ( profile ); 48const next = { ...store , profiles :[ ...store . profiles . filter ( p => p . id !== profile . id ), snapshot ], defaults :{ ...store . defaults } }; 49const key = defaultProfileKey ( profile . hardware , profile . variantId ); 50if ( makeDefault ) next . defaults [ key ] = profile . id ; 51else if ( next . defaults [ key ] === profile . id ) delete next . defaults [ key ]; 52return next ; 53} 54export function resourceConfig ( config :ConfigDocument , use :ComputerUse , hardware :Hardware ) :ConfigDocument { 55const cores = Math . max ( 1 , hardware . cpu . cores ); 56return { 57 ...config , headroom :use , 58expert :{ ...config . expert , 'perf.threads' :use === 'full' ?cores :Math . max ( 1 , cores - Math . max ( 2 , Math . ceil ( cores * .2 ))) }, 59}; 60} 61export function recommendedConfig ( engine :EngineConfig , hardware :Hardware , context = 'recommended' , use :ComputerUse = 'shared' ) :ConfigDocument { 62return resourceConfig ({ 63schema :'strata.engine/1' , batchAutomatic :true , 64beginner :{ priority :'balanced' , contextLength :context as ConfigDocument [ 'beginner' ][ 'contextLength' ], readImages :false , gpuIdle :'never' }, 65expert :{ ...Object . fromEntries ( engine . expert . map ( p => [ p . id , p . recommended ])), 'ctx.length' :context === 'recommended' ?'128K' :context . toUpperCase () }, 66}, use , hardware ); 67}