char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1/** 2* serverStore - Server connection state, configuration and role detection 3* 4* Owns the connection state and properties fetched from /props, plus MODEL 5* vs ROUTER role detection and server-wide generation defaults. Uses 6* PropsService for the /props fetch. 7*/ 8 9import { ServerRole } from '$lib/enums' ; 10import { PropsService } from '$lib/services/props.service' ; 11import { ApiError } from '$lib/utils' ; 12 13const LOADING_RETRY_INTERVAL_MS = 1000 ; 14 15class ServerStore { 16error = $state < string | null >( null ); 17loading = $state ( false ); 18props = $state < ApiLlamaCppServerProps | null >( null ); 19role = $state < ServerRole | null >( null ); 20status = $state < number | null >( null ); 21private fetchPromise :Promise < void > | null = null ; 22private retryTimer :ReturnType < typeof setTimeout > | null = null ; 23 24get contextSize () :number | null { 25const nCtx = this . props ?. default_generation_settings ?. n_ctx ; 26 27return typeof nCtx === 'number' ?nCtx :null ; 28} 29 30get defaultParams () :ApiLlamaCppServerProps [ 'default_generation_settings' ][ 'params' ] | null { 31return this . props ?. default_generation_settings ?. params || null ; 32} 33 34get isModelMode () :boolean { 35return this . role === ServerRole . MODEL ; 36} 37 38get isRouterMode () :boolean { 39return this . role === ServerRole . ROUTER ; 40} 41 42get uiSettings () :Record < string , string | number | boolean > | undefined { 43return this . props ?. ui_settings ?? this . props ?. webui_settings ; 44} 45 46clear () :void { 47this . clearRetryTimer (); 48this . props = null ; 49this . error = null ; 50this . status = null ; 51this . loading = false ; 52this . role = null ; 53this . fetchPromise = null ; 54} 55 56/** 57* @param background - Set by the automatic "still loading" poll. Skips the 58* `loading` flag flip so the UI doesn't bounce between the full loading 59* splash and the chat screen every retry tick. 60*/ 61async fetch ({ background= false } :{ background ?:boolean } = {}) :Promise < void > { 62if ( this . fetchPromise ) return this . fetchPromise ; 63 64this . clearRetryTimer (); 65 66if ( ! background ) { 67this . loading = true ; 68} 69 70// Don't clear an existing "still loading" error before a retry - 71// doing so would unmount/remount the error banner every second. 72if ( this . status !== 503 ) { 73this . error = null ; 74} 75 76const fetchPromise = ( async () => { 77try { 78const props = await PropsService . fetch (); 79 80this . props = props ; 81this . error = null ; 82this . status = null ; 83this . detectRole ( props ); 84} catch ( error :unknown ) { 85this . error = error instanceof Error ?error . message :String ( error ); 86this . status = error instanceof ApiError ?error . status :null ; 87console . error ( 'Error fetching server properties:' , error ); 88 89if ( this . status === 503 ) { 90this . scheduleRetry (); 91} 92} finally { 93if ( ! background ) { 94this . loading = false ; 95} 96 97this . fetchPromise = null ; 98} 99})(); 100 101this . fetchPromise = fetchPromise ; 102await fetchPromise ; 103} 104 105private clearRetryTimer () :void { 106if ( this . retryTimer ) { 107clearTimeout ( this . retryTimer ); 108this . retryTimer = null ; 109} 110} 111 112private detectRole ( props :ApiLlamaCppServerProps ) :void { 113const newRole = props ?. role === ServerRole . ROUTER ?ServerRole . ROUTER :ServerRole . MODEL ; 114 115if ( this . role !== newRole ) { 116this . role = newRole ; 117console . info ( `Server running in ${ newRole === ServerRole . ROUTER ? 'ROUTER' : 'MODEL' } mode` ); 118} 119} 120 121private scheduleRetry () :void { 122if ( this . retryTimer ) return ; 123 124this . retryTimer = setTimeout (() => { 125this . retryTimer = null ; 126this . fetch ({ background :true }); 127}, LOADING_RETRY_INTERVAL_MS ); 128} 129} 130 131export const serverStore = new ServerStore ();