char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
5b6ddc967
char/aliceai-t5-moe
1import { browser } from '$app/environment' ; 2import { CONFIG_LOCALSTORAGE_KEY , USER_OVERRIDES_LOCALSTORAGE_KEY } from '$lib/constants' ; 3 4/** 5* SettingsService - localStorage persistence layer for settings 6* 7* Stateless read/write of the settings config and user-override keys. Business 8* logic (default merging, mobile defaults, theme migration) stays in the store. 9* 10* **Architecture & Relationships:** 11* - **settingsStore**: Primary consumer - loads config on init and persists on change 12* 13* @see settingsStore in stores/settings/index.svelte.ts - reactive state + business logic 14*/ 15export class SettingsService { 16/** 17* Read the raw config and user overrides from localStorage. 18* @returns Parsed values, or empty defaults when nothing is stored or parsing fails. 19*/ 20static loadConfig () :{ 21config :Record < string , unknown >; 22userOverrides :string []; 23isFirstVisit :boolean ; 24} { 25if ( ! browser ) { 26return { config :{}, isFirstVisit :false , userOverrides :[] }; 27} 28 29try { 30const storedConfigRaw = localStorage . getItem ( CONFIG_LOCALSTORAGE_KEY ); 31const isFirstVisit = storedConfigRaw === null ; 32const config = JSON . parse ( storedConfigRaw || '{}' ) as Record < string , unknown >; 33const userOverrides = JSON . parse ( 34localStorage . getItem ( USER_OVERRIDES_LOCALSTORAGE_KEY ) || '[]' 35) as string []; 36 37return { config, isFirstVisit, userOverrides}; 38} catch ( error ) { 39console . warn ( 'Failed to parse config from localStorage, using defaults:' , error ); 40 41return { config :{}, isFirstVisit :false , userOverrides :[] }; 42} 43} 44 45/** 46* Migrate the legacy un-namespaced "theme" localStorage key. 47* Returns the legacy theme value (and removes the key) when present, else null. 48*/ 49static migrateLegacyTheme () :string | null { 50if ( ! browser ) return null ; 51 52const legacyTheme = localStorage . getItem ( 'theme' ); 53 54if ( legacyTheme ) { 55localStorage . removeItem ( 'theme' ); 56 57return legacyTheme ; 58} 59 60return null ; 61} 62 63/** 64* Persist the config and user overrides to localStorage. 65*/ 66static saveConfig ( config :Record < string , unknown >, userOverrides :string []) :void { 67if ( ! browser ) return ; 68 69try { 70localStorage . setItem ( CONFIG_LOCALSTORAGE_KEY , JSON . stringify ( config )); 71localStorage . setItem ( USER_OVERRIDES_LOCALSTORAGE_KEY , JSON . stringify ( userOverrides )); 72} catch ( error ) { 73console . error ( 'Failed to save config to localStorage:' , error ); 74} 75} 76}