/** * Lemma Embedded Mode * * Zero-config semantic cache for any project. * No separate server, no WebSocket, no agents required. * * Usage: * import { Lemma } from 'lemma/embed'; * * const lemma = await Lemma.create(); * const suggest = lemma.wrap(myOpenAIFunction); * const result = await suggest('pollo y arroz'); // cached after first call */ import { EventEmitter } from 'events'; export interface LemmaConfig { /** * Storage backend. * - 'memory': in-process Map, zero dependencies, resets on restart (default) * - 'semantic': in-process semantic cache using lightweight embeddings (requires @xenova/transformers) * - 'chroma': ChromaDB + Ollama for persistent semantic cache * - 'cloud': Lemma Cloud — managed semantic cache, just add an API key */ storage?: 'memory' | 'semantic' | 'chroma' | 'cloud'; /** * Lemma Cloud API key. Required when storage='cloud'. * Get yours at https://lemma.dev */ apiKey?: string; /** * Lemma Cloud API URL. Default: 'https://api.lemma.dev' */ cloudUrl?: string; /** * Similarity threshold for semantic matching (0–1). * Higher = stricter matching. Default: 0.92 */ threshold?: number; /** * ChromaDB URL. Only used when storage='chroma'. * Default: 'http://localhost:8000' */ chromaUrl?: string; /** * Ollama URL for embeddings. Only used when storage='chroma'. * Default: 'http://localhost:11434' */ ollamaUrl?: string; /** * Ollama model for embeddings. Default: 'llama3' */ ollamaModel?: string; /** * Embedding model for semantic storage. Default: 'Xenova/all-MiniLM-L6-v2' * Only used when storage='semantic'. */ embeddingModel?: string; /** * Collection name in ChromaDB. Default: 'lemma_cache' */ collection?: string; /** * TTL in milliseconds. 0 = no expiry. Default: 0 */ ttl?: number; /** * Max entries in memory cache. Default: 10000 */ maxSize?: number; /** * Cleanup interval in milliseconds for expired entries. * Set to 0 to disable automatic cleanup. Default: 60000 (1 minute) */ cleanupInterval?: number; /** * Log cache hits/misses to console. Default: false */ debug?: boolean; /** * Enable automatic fallback to alternative backends on failure. * Default: true */ enableFallback?: boolean; /** * Maximum number of retry attempts for failed operations. * Default: 3 */ maxRetries?: number; /** * Initial delay in milliseconds between retry attempts. * Uses exponential backoff. Default: 1000 */ retryDelay?: number; } export interface CacheResult { data: T; fromCache: boolean; similarity?: number; latencyMs: number; } export interface LemmaMetrics { hits: number; misses: number; total: number; hitRate: number; avgLatencyMs: number; cacheSize: number; backendHealth: 'healthy' | 'degraded' | 'down'; failureCount: number; evictedCount: number; lastCleanupAt: number; } export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN'; export interface BackendHealth { state: CircuitState; failureCount: number; lastFailure: number | null; successCount: number; } export declare class Lemma extends EventEmitter { private backend; private config; private metrics; private ready; private constructor(); /** * Create and initialize a Lemma instance. * * @example * // Zero config — in-memory cache * const lemma = await Lemma.create(); * * @example * // Semantic cache with lightweight embeddings * const lemma = await Lemma.create({ * storage: 'semantic', * threshold: 0.85, * }); * * @example * // Persistent semantic cache with ChromaDB * const lemma = await Lemma.create({ * storage: 'chroma', * threshold: 0.90, * }); */ static create(config?: LemmaConfig): Promise; private init; /** * Wrap any async function with semantic caching. * The first argument of the function is used as the cache key. * * @example * const cachedSuggest = lemma.wrap(async (ingredients: string) => { * return await openai.chat.completions.create({ ... }); * }); * * await cachedSuggest('pollo y arroz'); // calls OpenAI * await cachedSuggest('arroz con pollo'); // returns from cache ⚡ */ wrap(fn: (...args: TArgs) => Promise): (...args: TArgs) => Promise>; /** * Manually check cache and run function if miss. * * @example * const result = await lemma.run( * userMessage, * () => openai.chat.completions.create({ ... }) * ); * if (result.fromCache) console.log('⚡ Saved an API call!'); */ run(input: string, fn: () => Promise): Promise>; /** * Manually get a value from cache. * Returns null if not found. */ get(input: string): Promise | null>; /** * Manually store a value in cache. */ set(input: string, data: any): Promise; /** * Get performance metrics. */ getMetrics(): LemmaMetrics; /** * Get backend health information. */ getBackendHealth(): BackendHealth & { currentBackend: string; totalFailures: number; } | null; /** * Get the configured storage type. */ private getStorageType; /** * Clear all cached entries and reset metrics. */ clear(): void; /** * Update similarity threshold at runtime. */ setThreshold(threshold: number): void; /** * Stop cleanup timer and perform graceful shutdown. * Call this before your application exits to prevent memory leaks. * * @example * const lemma = await Lemma.create(); * // ... use lemma ... * lemma.stop(); // cleanup before exit */ stop(): void; private assertReady; private log; } export default Lemma; //# sourceMappingURL=index.d.ts.map