okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import { useEffect , useSyncExternalStore } from 'react' ; 2import { api } from './client' ; 3import type { Hardware , RuntimeModels } from './types' ; 4export interface GpuChoiceOption { key :string ; cards :number []; label :string ; verdict :'fits' | 'tight' | 'spills' | 'unknown' ; words :string ; notes :string []; recommended :boolean } 5export interface GpuChoice { options :GpuChoiceOption []; recommended :string | null ; ignored :{ name ?:string ; why_ignored :string }[]; cards :number } 6export interface NativeSize { /** The first engine that reads this size's files (the catalog), and whether the installed engine is older: the screen then says "needs a newer engine" and offers the update. */ engine_min ?:string | null ; needsNewerEngine ?:boolean ; id :string ; model :string ; about :string ; download_gb :number ; ram_needed_gb :number ; experts_gb :number ; on_this_pc :string | null ; /** The files without the lookup table (the download minus the n-gram / PLE / Engram file): what the fit label and the "Needs" figure use. */ fit_gb ?:number | null ; /** Qwen3.6: its own context rule (strata_control/own_path_fit.py via the catalog, #5715). */ context_fit ?:import ( './native-fit' ). ContextFit | null ; installed :boolean ; gpu_options ?:GpuChoice | null ; images ?:boolean ; imagesCapable ?:boolean ;} 7export interface NativeFamily { family :string ; title :string ; about :string ; experimental :boolean ; images :boolean ; sizes :NativeSize []; /** A model of the signed update feed (shown after the built-in ones); `available:false` = this engine does not run it, `unavailable_reason` says so in plain words. */ feed ?:boolean ; available ?:boolean ; unavailable_reason ?:string } 8export interface NativeField { id :string ; label :string ; group :string ; type :'number' | 'boolean' | 'string' | 'enum' | 'array' ; min ?:number ; max ?:number ; choices ?:string []; hint ?:string ; supported ?:boolean } 9export interface NativeJob { id :string ; kind :string ; guided ?:boolean ; model ?:string ; family ?:string ; state :'running' | 'done' | 'error' | 'cancelled' ; startedAt :string ; summary :string ; error :string | null ; details ?:string ; result :Record < string , any > | null ; progress ?:Record < string , any > | null ; /** An engine update beside a loaded model: which step it is on and the models it concerns (the app words these itself). */ step ?:'getting-ready' | 'stopping' | 'stopped' ; models ?:string []} 10export interface NativeInstance { id :string ; apiModelId :string ; model :string ; label :string ; aliases ?:string []; vision ?:boolean ; resourceProfile ?:'work' | 'performance' | null ; context ?:number ; port :number ; gpus :number []; status :'loading' | 'ready' | 'unloading' | 'error' ; phase :string ; loadedAt :string ; error :string | null ; /** One short line when the model was loaded without picture reading although it was on (no internet for the picture files, no room on the disk). */ picturesNote ?:string ; metrics :Record < string , any > | null } 11export interface NativeState { 12/** The host has not had an answer from Control yet (it is still starting): not "nothing installed". */ 13checking ?:boolean ; 14/** The picture reader (strata-vision) is installed with the engine, so models marked for pictures can read them here. */ 15/** Strata Control's view of the engine for this computer (the app): usable false = none found or recognised; `notice` says why in plain words. */ 16engine ?:{ usable :boolean ; notice ?:string | null ; wrongMake ?:boolean ; version ?:string | null ; /** What the last update check found newer (kept on this PC, read without the network): the engine, the app. */ updateAvailable ?:{ engine ?:string | null ; app ?:string | null } | null }; 17visionReader ?:boolean ; /** The installed app (Strata Control is bundled): picture files, engines and updates go through it. */ packaged ?:boolean ; available :boolean ; repo :string ; downloadDirectory ?:string ; /** Free bytes on the drive that holds the models folder. */ downloadFreeBytes ?:number | null ; downloadTotalBytes ?:number | null ; version ?:string | null ; summary :string ; error :string | null ; catalog :{ families :NativeFamily []; contexts :number []; recommendation :{ family :string | null ; model :string | null ; context ?:number ; why ?:string }; context_note :string } | null ; installed :{ models :{ model :string ; config :string ; model_name :string ; ready :boolean ; /** A DeepSeek model of an earlier Strata (Control's models list: no longer run): its one line and Control's refusal. */ retired ?:{ line :string ; refusal :string }; /** The picture-reading files are on this computer (notes-split-agent8.md, item 8). */ visionPrepared ?:boolean ; /** The picture choice saved was made with a reader present (ui_vision_chosen): an off holds even without the picture files. */ visionChosen ?:boolean ; /** The data folder the model's own run config keeps its pack and draft layer in (not the app's models folder). */ runDataFolder ?:string ; context ?:number ; kv ?:string ; images ?:string ; visionEnabled ?:boolean ; visionDevice ?:string ; lowRamMode ?:string ; resourceProfile ?:'work' | 'performance' ; /** The Drafting choice saved with the model: auto | mtp | dflash2 | off. */ drafting ?:string ; /** The context was picked by a person (a marker saved with the model); a saved 32K without it is the old default. */ contextChosen ?:boolean ; dataFolder ?:string ; diskGb ?:number ; diskBytes ?:number ; /** Files this model shares with another (the picture reader, the draft layer): counted once, in `usage`. */ sharedBytes ?:number ; /** What Delete frees: files in Strata's own folders that no other model uses. */ removableGb ?:number ; missing_files ?:string []}[]; /** What all models take on disk, from their real files, shared files counted once. */ usage ?:{ total_bytes :number ; total_gb :number ; shared_bytes :number ; shared_gb :number ; models :Record < string ,{ bytes :number ; gb :number }>} | null ; data_folder :string ; engine :{ version :string | null }} | null ; hardware :{ ram_gb :number ; engine_vendor ?:string ; gpus :{ index :number ; name :string ; vram_gb :number ; usable :boolean ; /** SMs x clock: only the order between cards matters (strata_control/gpu_roles.py). */ speed ?:number ; vendor ?:string ; uma ?:boolean ; /** a unified-memory chip's BIOS carve-out (setup.py amd_apply_uma) */ dedicated_gb ?:number ; integrated ?:boolean ; unified_gb ?:number ; problem ?:string | null }[]} | null ; fields :NativeField []; instances :NativeInstance []; defaultId :string | null ; /** Models an engine update stopped, to offer starting them again with the same settings (updated: false = the update did not finish, the old engine is unchanged). */ stoppedForUpdate ?:{ updated :boolean ; at :string ; models :{ id :string ; label :string }[]} | null ; jobs :NativeJob []; capabilities :{ /** The engine serves DFlash2 (Control says): the Drafting choice then offers it. */ dflash2 ?:boolean ; /** Control's word that DFlash2 uses the converted pack (the selector as Q4_0 rows, 175 MiB less graphics memory): the fit says 0.72 GB instead of 0.89 */ dflash2Selq4 ?:boolean ; /** Control's plan: the all-cards split engine takes an Auto start on this computer (it runs the MTP, so Auto is the MTP, not DFlash2) */ dflash2Split ?:boolean ; /** What was measured on this kind of card, in Control's words (null until Control answers). */ dflash2Support ?:{ level :'tested' | 'slower' | 'untested' ; line :string } | null ; responses ?:boolean ; updates :boolean ; rollback :boolean ; calibration :boolean ; componentUpdates ?:boolean }} 18let state :NativeState | null = null ; 19const listeners = new Set <() => void >(); 20/** True once the first read of the native state has ended, whatever its answer: until then the Models page must not pick a model of its own (#3740). */ 21let settled = false ; 22export const nativeSettled = () => settled ; 23export function subscribeNative ( fn :() => void ){ listeners . add ( fn ); return () => { listeners . delete ( fn );};} 24function put ( value :NativeState ){ if ( state && JSON . stringify ( state ) === JSON . stringify ( value )) return ; state = value ; listeners . forEach ( fn => fn ());} 25/** The host pushes its whole native state (jobs included) at every progress step: the page shows it at once, without waiting for the next poll. */ 26export function applyNativeEvent ( value :unknown ){ if ( value && typeof value === 'object' && 'available' in value && 'jobs' in value ) put ( value as NativeState );} 27const READ_LIMIT_MS = 60000 ; 28function read ( force :boolean ) :Promise < void >{ 29const request = api . get < NativeState >( `/api/native ${ force ? '?refresh=1' : '' } ` ); 30// A read that never answers must not freeze the page: after the limit the next poll starts a fresh one. 31let stop :ReturnType < typeof setTimeout > | undefined ; 32const limit = new Promise < never >(( _ , reject ) => { stop = setTimeout (() => reject ( new Error ( 'The host did not answer in time.' )), READ_LIMIT_MS );}); 33return Promise . race ([ request , limit ]). then ( put ). catch ( error => { if ( state ){ state = { ...state , error :error . message , instances :state . instances . map ( i => ({ ...i , metrics :null , phase :'unavailable' }))}; listeners . forEach ( fn => fn ());}}). finally (() => { clearTimeout ( stop ); if ( ! settled ){ settled = true ; listeners . forEach ( fn => fn ());}}); 34} 35/* Two kinds of read that never wait for each other: the light poll (the host answers at once with the jobs it holds) and the forced read after an action (the host 36re-reads the installation, which can take many seconds while a download is writing). A slow forced read used to hold every later poll, so a running download looked stuck. */ 37let light :Promise < void > | null = null , forced :Promise < void > | null = null , again = false ; 38/** A forced refresh (after an action) never joins a read that began before the action: that read cannot know about the new job. It waits for it, then reads again. */ 39export function refreshNative ( force = false ) :Promise < void >{ 40if ( ! force ){ if ( light ) return light ; light = read ( false ). finally (() => { light = null ;}); return light ;} 41if ( forced ){ if ( again ) return forced ; again = true ; return forced . then (() => { again = false ; return refreshNative ( true );});} 42forced = read ( true ). finally (() => { forced = null ;}); return forced ; 43} 44const POLL_MS = 2500 , RUNNING_POLL_MS = 1000 ; 45let users = 0 , timeout :ReturnType < typeof setTimeout > | null = null , watching = false ; 46const hidden = () => typeof document !== 'undefined' && document . visibilityState === 'hidden' ; 47/** How long until the next poll: quicker while a download or load is running, none while the window is hidden (it reads again the moment it is shown). */ 48export const nativePollDelay = ( value :NativeState | null , isHidden = false ) :number | null => isHidden ?null :value ?. jobs ?. some ( j => j . state === 'running' ) ?RUNNING_POLL_MS :POLL_MS ; 49function schedule (){ 50if ( timeout ){ clearTimeout ( timeout ); timeout = null ;} 51if ( ! users ) return ; 52const wait = nativePollDelay ( state , hidden ()); 53if ( wait === null ) return ; 54timeout = setTimeout (() => { timeout = null ; void refreshNative (). finally ( schedule );}, wait ); 55} 56const shown = () => { if ( ! hidden () && users ){ void refreshNative (); schedule ();}}; 57export function useNative (){ 58const value = useSyncExternalStore ( fn => { listeners . add ( fn ); return () => listeners . delete ( fn );},() => state ); 59useEffect (() => { 60users ++ ; void refreshNative (). finally ( schedule ); 61if ( ! watching && typeof document !== 'undefined' ){ watching = true ; document . addEventListener ( 'visibilitychange' , shown ); window . addEventListener ( 'focus' , shown );} 62return () => { users -- ; if ( ! users ){ if ( timeout ){ clearTimeout ( timeout ); timeout = null ;} if ( watching && typeof document !== 'undefined' ){ watching = false ; document . removeEventListener ( 'visibilitychange' , shown ); window . removeEventListener ( 'focus' , shown );}}}; 63},[]); 64// a new job (or its end) changes how fast the next poll should come 65const running = value ?. jobs ?. some ( j => j . state === 'running' ) ?? false ; 66useEffect (() => { schedule ();},[ running ]); 67return value ; 68} 69export const nativeState = () => state ; 70/** QAGUI 0213: the engine answered but Control's list of installed models has not arrived yet (the first seconds after a start): a downloaded model must not be offered as a download. */ 71export const installedListPending = ( value :Pick < NativeState , 'available' | 'installed' | 'error' | 'checking' > | null | undefined ) :boolean =>!! value && ( value . checking === true || value . available === true &&! value . installed &&! value . error ); 72/** Connection-list projection only; memory reservations are not inferred. */ 73export function nativeRouting ( native :NativeState | null , hardware :Hardware | null ) :RuntimeModels | null { 74if ( ! native ?. instances . length ) return null ; 75return { revision :Date . now (), mode :'real' , capabilities :{ multipleModels :true , deviceAssignment :true }, defaultId :native . defaultId , instances :native . instances . filter ( i => i . status === 'ready' ). map ( i => ({ id :i . id , apiModelId :i . apiModelId , variantId :i . model , family :i . label , name :i . model , quant :i . model , loadedAt :i . loadedAt , status :'ready' , phase :i . phase as RuntimeModels [ 'instances' ][ number ][ 'phase' ], active :i . metrics ?. live ?. running ?? 0 , queued :i . metrics ?. live ?. queued ?? 0 , prompt :{ done :0 , total :0 , pct :0 }, outputTps :i . metrics ?. live ?. tok_s ?? 0 , promptTps :i . metrics ?. live ?. prefill_tok_s_mean ?? 0 , context :{ usedTokens :0 , maxTokens :i . metrics ?. engine ?. max_context ?? i . context ?? 0 , pct :0 }, gpuIds :/* Automatic placement names no card in the config: the cards the engine reports (or the only card) are the ones it runs on. */ ( i . gpus . length ?i . gpus :( i . metrics ?. hardware ?. gpus ?? []). map (( g :{ index :number }) => g . index ). filter (( n :unknown ) => typeof n === 'number' )). map (( index :number ) => { const g = hardware ?. gpus . find ( g => g . index === index ); return g ?. id ?? ` ${ g ?. vendor ?? 'native' } : ${ index } ` ;}), memory :{ context :i . metrics ?. engine ?. max_context ?? i . context ?? 0 , ramGb :0 , gpuGb :0 , spillGb :0 , reserveGb :0 , totalGb :0 , devices :[]}})), memory :{ ramGb :0 , budgetGb :0 , reserveGb :0 , totalGb :0 , fits :true , devices :[]}}; 76} 77export async function nativeAction < T = unknown > ( path :string , body :unknown = {}){ const result = await api . post < T >( `/api/native/ ${ path } ` , body ); 78// A started operation is a job: show it at once instead of waiting for the next read (the owner saw download progress only after switching tabs). 79const job = result as unknown as NativeJob | undefined ; 80if ( state && job && typeof job . id === 'string' && path === 'operations' &&! state . jobs . some ( j => j . id === job . id )){ state = { ...state , jobs :[ ...state . jobs , job ]}; listeners . forEach ( fn => fn ());} 81await refreshNative ( true ); return result ;} 82 83export { nativeVersionLabel } from './native-catalog' ;