@claude-flow/embeddings
Version:
V3 Embedding Service - OpenAI, Transformers.js, Agentic-Flow (ONNX), Mock providers with hyperbolic embeddings, normalization, and chunking
118 lines • 3.67 kB
TypeScript
/**
* RVF Embedding Cache - Pure TypeScript Binary File Cache
*
* Replaces the sql.js-based PersistentEmbeddingCache with a lightweight
* pure-TS binary file format. No native dependencies required.
*
* Features:
* - Map-based in-memory cache with periodic flush to binary file
* - LRU eviction tracked via access timestamps
* - TTL support for cache entries
* - Deterministic FNV-1a text hashing for keys
* - Binary format: RVEC magic + entry records
*
* Binary entry format:
* [4-byte key-hash][4-byte dims][dims*4 bytes float32][8-byte timestamp][8-byte access-count]
*
* @module @claude-flow/embeddings
*/
/**
* Configuration for RVF embedding cache
*/
export interface RvfEmbeddingCacheConfig {
/** Path to the binary cache file */
cachePath: string;
/** Maximum number of entries (default: 10000) */
maxSize?: number;
/** TTL in milliseconds (default: 7 days) */
ttlMs?: number;
/** Embedding dimensions (used for validation) */
dimensions?: number;
}
/**
* Pure-TS binary file embedding cache with LRU eviction and TTL support.
*
* Stores embeddings as raw Float32Array bytes keyed by FNV-1a text hashes.
* Uses an in-memory Map with periodic flush to a compact binary file.
*/
export declare class RvfEmbeddingCache {
private readonly cachePath;
private readonly maxSize;
private readonly ttlMs;
private readonly dimensions;
private entries;
private textToHash;
private dirty;
private flushTimer;
private initialized;
constructor(config: RvfEmbeddingCacheConfig);
/**
* Lazily initialize the cache: load from disk if file exists, start auto-flush.
*/
private ensureInitialized;
/**
* Get an embedding from the cache by text.
* Returns null if not found or expired.
*/
get(text: string): Promise<Float32Array | null>;
/**
* Store an embedding in the cache.
* Triggers LRU eviction if the cache exceeds maxSize.
*/
set(text: string, embedding: Float32Array): Promise<void>;
/**
* Check whether the cache contains an embedding for the given text.
*/
has(text: string): Promise<boolean>;
/**
* Delete a specific entry from the cache.
* Returns true if the entry existed and was deleted.
*/
delete(text: string): Promise<boolean>;
/**
* Clear all entries from the cache and persist the empty state.
*/
clear(): Promise<void>;
/**
* Return the number of entries currently in the cache.
*/
size(): Promise<number>;
/**
* Flush pending changes to disk and stop the auto-flush timer.
*/
close(): Promise<void>;
/**
* FNV-1a 32-bit hash of the input text.
* Deterministic: same input always produces the same hash.
*/
private hashText;
/**
* If the cache exceeds maxSize, evict the least-recently-accessed entries
* until we are back at 90% capacity.
*/
private evictIfNeeded;
/**
* Remove all entries whose createdAt timestamp is older than TTL.
*/
private cleanExpired;
private startAutoFlush;
private stopAutoFlush;
/**
* Write all entries to the binary cache file.
*
* Format:
* [4-byte magic "RVEC"]
* For each entry:
* [4-byte key-hash (uint32)]
* [4-byte dims (uint32)]
* [dims * 4 bytes float32 data]
* [8-byte createdAt (float64, used as timestamp)]
* [8-byte accessCount (float64)]
*/
private flushToFile;
/**
* Load entries from the binary cache file.
*/
private loadFromFile;
}
//# sourceMappingURL=rvf-embedding-cache.d.ts.map