/**
* Cached Ollama Embeddings
*
* Pre-computed embeddings from real Ollama server (nomic-embed-text model)
* for common test strings. This ensures consistency between tests running
* with real Ollama vs mock embeddings.
*
* Generated by: npm run test:capture-embeddings
* Model: nomic-embed-text
* Dimension: 768
*
* To regenerate:
* 1. Start Ollama server: ollama serve
* 2. Pull model: ollama pull nomic-embed-text
* 3. Run: npm run test:capture-embeddings
*/
/**
* Cache entry with metadata
*/
export interface CachedEmbedding {
text: string;
embedding: number[];
model: string;
dimension: number;
capturedAt: string;
}
/**
* Cache metadata
*/
export interface EmbeddingCacheMetadata {
version: string;
model: string;
dimension: number;
capturedAt: string;
entryCount: number;
}
/**
* Full cache structure
*/
export interface EmbeddingCache {
metadata: EmbeddingCacheMetadata;
embeddings: Record<string, CachedEmbedding>;
}
/**
* Common test strings used across the test suite
* These are the strings we pre-compute embeddings for
*/
export const COMMON_TEST_STRINGS = [
// Basic test content
"Test memory content",
"Test content for embedding generation",
"Multi-sector embedding test content",
"Cached embedding test content",
// Performance test content
"Performance test memory with sufficient content to generate realistic embeddings",
"Performance test memory for single retrieval",
"Performance test memory for p95 retrieval",
"Performance test memory for p99 retrieval",
"Performance test memory for cache testing",
// Search test content
"Machine learning algorithms",
"Deep learning neural networks",
"Natural language processing",
"Search performance test memory",
"Full-text search test memory",
// Memory lifecycle content
"Integration test memory",
"Recent memory",
"Week old memory",
"Month old memory",
// Sector-specific content
"Episodic memory about a specific event",
"Semantic knowledge about concepts",
"Procedural steps for completing a task",
"Emotional response to a situation",
"Reflective thoughts about past experiences",
// Production test content
"Production test memory with realistic content about cognitive architecture and memory systems",
"Concurrent read test memory",
"Rollback test memory",
"Timeout test memory",
// Query strings
"machine learning",
"neural networks",
"cognitive architecture",
"memory retrieval",
"embedding generation",
];
/**
* Default empty cache (will be populated by capture script)
*/
export const DEFAULT_CACHE: EmbeddingCache = {
metadata: {
version: "1.0.0",
model: "nomic-embed-text",
dimension: 768,
capturedAt: "",
entryCount: 0,
},
embeddings: {},
};
/**
* In-memory cache loaded from JSON file or populated at runtime
*/
let loadedCache: EmbeddingCache | null = null;
/**
* Get the embedding cache
* Attempts to load from file, falls back to empty cache
*/
export function getEmbeddingCache(): EmbeddingCache {
if (loadedCache) {
return loadedCache;
}
try {
// Try to load from the generated cache file
// This will be populated by the capture script
loadedCache = require("./ollama-embeddings-cache.json") as EmbeddingCache;
return loadedCache;
} catch {
// No cache file exists yet, return empty cache
loadedCache = DEFAULT_CACHE;
return loadedCache;
}
}
/**
* Get cached embedding for a text string
* Returns undefined if not in cache
*/
export function getCachedEmbedding(text: string): number[] | undefined {
const cache = getEmbeddingCache();
const entry = cache.embeddings[text];
return entry?.embedding;
}
/**
* Check if a text string has a cached embedding
*/
export function hasCachedEmbedding(text: string): boolean {
const cache = getEmbeddingCache();
return text in cache.embeddings;
}
/**
* Get all cached text strings
*/
export function getCachedTexts(): string[] {
const cache = getEmbeddingCache();
return Object.keys(cache.embeddings);
}
/**
* Get cache metadata
*/
export function getCacheMetadata(): EmbeddingCacheMetadata {
const cache = getEmbeddingCache();
return cache.metadata;
}
/**
* Set the cache (used by capture script)
*/
export function setEmbeddingCache(cache: EmbeddingCache): void {
loadedCache = cache;
}
/**
* Clear the loaded cache (useful for testing)
*/
export function clearCacheMemory(): void {
loadedCache = null;
}