/** * Module-level warm client cache for AI adapters * * This cache persists across Svelte component lifecycle, surviving * navigation between pages (SPA) and remounts of SmrtProvider. * * ## Key Benefits * - Avoids re-downloading WASM/models on every navigation * - Keeps WebGPU contexts warm (avoiding cold start delays) * - Enables pre-initialization during idle time * * ## How It Works * The cache is a simple Map at module scope. When SmrtProvider mounts, * it checks the cache before creating new adapters. Adapters stay cached * until explicitly cleared or the page is refreshed. * * ## Usage * ```typescript * import { getCachedSTT, getCacheStats, clearAllCaches } from '@happyvertical/smrt-svelte'; * * // Check if whisper-cpp is already loaded * const cached = getCachedSTT('whisper-cpp'); * if (cached?.initState === 'ready') { * console.log('STT already initialized!'); * } * * // Debug cache status * const stats = getCacheStats(); * console.log('Cached adapters:', stats); * * // Clear cache to free memory * await clearAllCaches(); * ``` * * @module warm-clients */ import type { DownloadProgressInfo, InitState, LLMAdapter, STTAdapter, TTSAdapter } from '../browser-ai/index.js'; /** * Adapter cache entry with metadata */ export interface CachedAdapter { /** The adapter instance */ adapter: T; /** When the adapter was cached */ cachedAt: number; /** Initialization state */ initState: InitState; /** Current download progress (if initializing) */ downloadProgress: DownloadProgressInfo | null; /** Error if initialization failed */ error: Error | null; /** Adapter type identifier (for cache key matching) */ type: string; } /** * STT adapter types that can be cached */ export type STTType = 'browser-speech' | 'whisper-cpp' | 'whisper-wasm'; /** * TTS adapter types that can be cached */ export type TTSType = 'browser-synthesis'; /** * LLM adapter types that can be cached */ export type LLMType = 'webllm' | 'transformers-llm'; /** * Get a cached STT adapter */ export declare function getCachedSTT(type: STTType): CachedAdapter | undefined; /** * Cache an STT adapter */ export declare function setCachedSTT(type: STTType, adapter: STTAdapter, initState?: InitState, error?: Error | null): void; /** * Update STT cache entry state (for progress tracking) */ export declare function updateSTTCacheState(type: STTType, updates: Partial, 'initState' | 'downloadProgress' | 'error'>>): void; /** * Remove an STT adapter from cache */ export declare function removeCachedSTT(type: STTType): void; /** * Get a cached TTS adapter */ export declare function getCachedTTS(type: TTSType): CachedAdapter | undefined; /** * Cache a TTS adapter */ export declare function setCachedTTS(type: TTSType, adapter: TTSAdapter, initState?: InitState, error?: Error | null): void; /** * Update TTS cache entry state */ export declare function updateTTSCacheState(type: TTSType, updates: Partial, 'initState' | 'downloadProgress' | 'error'>>): void; /** * Remove a TTS adapter from cache */ export declare function removeCachedTTS(type: TTSType): void; /** * Get a cached LLM adapter */ export declare function getCachedLLM(type: LLMType, modelId?: string): CachedAdapter | undefined; /** * Cache an LLM adapter */ export declare function setCachedLLM(type: LLMType, adapter: LLMAdapter, modelId?: string, initState?: InitState, error?: Error | null): void; /** * Update LLM cache entry state */ export declare function updateLLMCacheState(type: LLMType, modelId: string | undefined, updates: Partial, 'initState' | 'downloadProgress' | 'error'>>): void; /** * Remove an LLM adapter from cache */ export declare function removeCachedLLM(type: LLMType, modelId?: string): void; /** * Clear all cached adapters */ export declare function clearAllCaches(): Promise; /** * Get cache statistics for debugging */ export declare function getCacheStats(): { stt: { count: number; types: STTType[]; }; tts: { count: number; types: TTSType[]; }; llm: { count: number; keys: string[]; }; }; //# sourceMappingURL=warm-clients.d.ts.map