char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1/** 2* View layer over contextStatsStore for the context usage gauge: adds 3* color levels, transient detail formatting, on-demand /props fetching 4* and model loading on top of the store's token stats. 5*/ 6 7import { useProcessingState } from './use-processing-state.svelte' ; 8import { colorLevelFromPercent } from '$lib/components/app/chat/ChatForm/ChatFormContextGauge/context-gauge' ; 9import { STATS_UNITS } from '$lib/constants' ; 10import { ColorLevel } from '$lib/enums' ; 11import { contextStatsStore , modelsStore } from '$lib/stores' ; 12 13export interface UseContextGaugeReturn { 14readonly activeModelId :string | null ; 15readonly isActiveModelLoaded :boolean ; 16readonly isActiveModelLoading :boolean ; 17readonly contextTotal :number | null ; 18readonly contextUsed :number ; 19readonly contextAvailable :number | null ; 20readonly currentRead :number ; 21readonly currentFresh :number ; 22readonly currentCache :number ; 23readonly currentOutput :number ; 24readonly kvTotal :number ; 25readonly cumulativeRead :number ; 26readonly cumulativeOutput :number ; 27readonly cumulativeCacheTotal :number ; 28readonly averageTokensPerSecond :number | null ; 29readonly contextPercent :number | null ; 30readonly colorLevel :ColorLevel ; 31readonly transientDetails :string []; 32readonly hasAnyUsage :boolean ; 33loadModel () :Promise < void >; 34startMonitoring () :void ; 35} 36 37const TRANSIENT_DETAILS_EXCLUDED_PREFIXES = [ 'Context:' , 'Output:' ]; 38 39function filterTransientDetails ( raw :string []) :string [] { 40return raw . filter (( detail ) => { 41if ( TRANSIENT_DETAILS_EXCLUDED_PREFIXES . some (( prefix ) => detail . startsWith ( prefix ))) { 42return false ; 43} 44 45return ! detail . includes ( STATS_UNITS . TOKENS_PER_SECOND ); 46}); 47} 48 49export function useContextGauge () :UseContextGaugeReturn { 50const processingState = useProcessingState (); 51 52// Pull /props on demand so n_ctx surfaces before the first chat request. 53$effect (() => { 54const modelId = contextStatsStore . activeModelId ; 55 56if ( modelId && contextStatsStore . isActiveModelLoaded ) { 57const cached = modelsStore . props . getModelProps ( modelId ); 58 59if ( ! cached ) { 60void modelsStore . props . fetchModelProps ( modelId ); 61} 62} 63}); 64 65const colorLevel = $derived ( colorLevelFromPercent ( contextStatsStore . contextPercent )); 66// Drop lines the surrounding Context / Output / speed rows already render. 67const transientDetails = $derived ( filterTransientDetails ( processingState . getTechnicalDetails ())); 68const hasAnyUsage = $derived ( 69contextStatsStore . cumulativeRead > 0 || 70contextStatsStore . cumulativeOutput > 0 || 71contextStatsStore . currentRead > 0 || 72contextStatsStore . currentOutput > 0 || 73contextStatsStore . averageTokensPerSecond !== null || 74transientDetails . length > 0 75); 76 77async function loadModel () { 78const modelId = contextStatsStore . activeModelId ; 79 80if ( ! modelId || contextStatsStore . isActiveModelLoading ) return ; 81 82try { 83await modelsStore . status . load ( modelId ); 84} catch { 85// toast already surfaced by modelsStore.status.load 86} 87} 88 89return { 90get activeModelId () { 91return contextStatsStore . activeModelId ; 92}, 93get averageTokensPerSecond () { 94return contextStatsStore . averageTokensPerSecond ; 95}, 96get colorLevel () { 97return colorLevel ; 98}, 99get contextAvailable () { 100return contextStatsStore . contextAvailable ; 101}, 102get contextPercent () { 103return contextStatsStore . contextPercent ; 104}, 105get contextTotal () { 106return contextStatsStore . contextTotal ; 107}, 108get contextUsed () { 109return contextStatsStore . contextUsed ; 110}, 111get cumulativeCacheTotal () { 112return contextStatsStore . cumulativeCacheTotal ; 113}, 114get cumulativeOutput () { 115return contextStatsStore . cumulativeOutput ; 116}, 117get cumulativeRead () { 118return contextStatsStore . cumulativeRead ; 119}, 120get currentCache () { 121return contextStatsStore . currentCache ; 122}, 123get currentFresh () { 124return contextStatsStore . currentFresh ; 125}, 126get currentOutput () { 127return contextStatsStore . currentOutput ; 128}, 129get currentRead () { 130return contextStatsStore . currentRead ; 131}, 132get hasAnyUsage () { 133return hasAnyUsage ; 134}, 135get isActiveModelLoaded () { 136return contextStatsStore . isActiveModelLoaded ; 137}, 138get isActiveModelLoading () { 139return contextStatsStore . isActiveModelLoading ; 140}, 141get kvTotal () { 142return contextStatsStore . kvTotal ; 143}, 144 loadModel, 145startMonitoring :() => processingState . startMonitoring (), 146get transientDetails () { 147return transientDetails ; 148} 149}; 150}