Skip to main content
Glama

Prompt Auto-Optimizer MCP

by sloth-wq
cache-integration.ts16.1 kB
/** * Cache Integration Layer for GEPA Components * Provides seamless integration of caching into existing components * with minimal code changes and maximum performance gains */ import { CacheRegistry, EvolutionEngineCache, ParetoFrontierCache, LLMAdapterCache, TrajectoryStoreCache, ReflectionEngineCache } from './component-caches'; import { CacheStatsAggregator } from './cache-optimizations'; import type { CacheConfig } from './cache-manager'; interface ClearableCache { clear(): Promise<void>; } interface AggregatedCacheStatistics { totals: { totalSize?: number; overallHitRate?: number; [key: string]: number | undefined; }; [key: string]: unknown; } interface ComponentCacheStatistics { l1?: { hitRate?: number; }; l2?: { hitRate?: number; }; [key: string]: unknown; } /** * Cache Integration Configuration */ export interface CacheIntegrationConfig { enabled?: boolean; components?: { evolutionEngine?: Partial<CacheConfig> | false; paretoFrontier?: Partial<CacheConfig> | false; llmAdapter?: Partial<CacheConfig> | false; trajectoryStore?: Partial<CacheConfig> | false; reflectionEngine?: Partial<CacheConfig> | false; }; globalConfig?: Partial<CacheConfig>; statsCollectionInterval?: number; autoOptimization?: boolean; } /** * Main Cache Integration Manager */ export class CacheIntegration { private readonly config: CacheIntegrationConfig; private readonly registry: CacheRegistry; private readonly statsAggregator: CacheStatsAggregator; private statsInterval?: ReturnType<typeof setInterval>; private isInitialized = false; constructor(config: CacheIntegrationConfig) { this.config = { enabled: true, components: {}, statsCollectionInterval: 30000, // 30 seconds autoOptimization: true, ...config }; this.registry = new CacheRegistry(); this.statsAggregator = new CacheStatsAggregator(); } /** * Initialize cache integration for all components */ async initialize(): Promise<void> { if (!this.config.enabled || this.isInitialized) { return; } try { // Initialize component caches await this.initializeComponentCaches(); // Start statistics collection if (this.config.statsCollectionInterval && this.config.statsCollectionInterval > 0) { this.startStatsCollection(); } this.isInitialized = true; // eslint-disable-next-line no-console console.log('Cache integration initialized successfully'); } catch (error) { // eslint-disable-next-line no-console console.error('Failed to initialize cache integration:', error); throw error; } } /** * Get cache instance for a specific component */ getComponentCache<T>(componentName: string): T | null { return this.registry.getCache<T>(componentName); } /** * Get comprehensive cache statistics */ async getStatistics(): Promise<{ enabled: boolean; components: Record<string, import('./cache-manager').CacheStatistics>; aggregated: AggregatedCacheStatistics | null; performance: { memoryUsage: number; hitRateImprovement: number; responseTimeImprovement: number; }; }> { if (!this.config.enabled) { return { enabled: false, components: {}, aggregated: null, performance: { memoryUsage: 0, hitRateImprovement: 0, responseTimeImprovement: 0 } }; } const componentStats = await this.registry.getGlobalStatistics(); const aggregatedStats = this.statsAggregator.getAggregatedStats(); return { enabled: true, components: componentStats, aggregated: aggregatedStats, performance: this.calculatePerformanceMetrics(aggregatedStats) }; } /** * Optimize cache configuration based on usage patterns */ async optimizeConfiguration(): Promise<void> { if (!this.config.autoOptimization || !this.isInitialized) { return; } try { const stats = await this.getStatistics(); const optimizations = this.analyzeAndOptimize(stats); if (optimizations.length > 0) { // eslint-disable-next-line no-console console.log(`Applied ${optimizations.length} cache optimizations:`, optimizations); } } catch (error) { // eslint-disable-next-line no-console console.error('Failed to optimize cache configuration:', error); } } /** * Clear all caches */ async clearAllCaches(): Promise<void> { const cacheNames = ['evolutionEngine', 'paretoFrontier', 'llmAdapter', 'trajectoryStore', 'reflectionEngine']; for (const name of cacheNames) { const cache = this.registry.getCache<ClearableCache>(name); if (cache && typeof cache.clear === 'function') { await cache.clear(); } } this.statsAggregator.clear(); // eslint-disable-next-line no-console console.log('All caches cleared'); } /** * Shutdown cache integration */ async shutdown(): Promise<void> { if (this.statsInterval) { clearInterval(this.statsInterval); } await this.registry.shutdownAll(); this.isInitialized = false; // eslint-disable-next-line no-console console.log('Cache integration shutdown complete'); } // Private Methods /** * Initialize caches for all components */ private async initializeComponentCaches(): Promise<void> { const globalConfig = this.config.globalConfig || {}; // Evolution Engine Cache if (this.config.components?.evolutionEngine !== false) { const evolutionCache = new EvolutionEngineCache({ ...globalConfig, ...this.config.components?.evolutionEngine }); this.registry.registerCache('evolutionEngine', evolutionCache); } // Pareto Frontier Cache if (this.config.components?.paretoFrontier !== false) { const paretoCache = new ParetoFrontierCache({ ...globalConfig, ...this.config.components?.paretoFrontier }); this.registry.registerCache('paretoFrontier', paretoCache); } // LLM Adapter Cache if (this.config.components?.llmAdapter !== false) { const llmCache = new LLMAdapterCache({ ...globalConfig, ...this.config.components?.llmAdapter }); this.registry.registerCache('llmAdapter', llmCache); } // Trajectory Store Cache if (this.config.components?.trajectoryStore !== false) { const trajectoryCache = new TrajectoryStoreCache({ ...globalConfig, ...this.config.components?.trajectoryStore }); this.registry.registerCache('trajectoryStore', trajectoryCache); } // Reflection Engine Cache if (this.config.components?.reflectionEngine !== false) { const reflectionCache = new ReflectionEngineCache({ ...globalConfig, ...this.config.components?.reflectionEngine }); this.registry.registerCache('reflectionEngine', reflectionCache); } } /** * Start periodic statistics collection */ private startStatsCollection(): void { this.statsInterval = setInterval(async () => { try { const stats = await this.registry.getGlobalStatistics(); for (const [componentName, componentStats] of Object.entries(stats)) { this.statsAggregator.updateStats(componentName, componentStats); } // Auto-optimize if enabled if (this.config.autoOptimization) { await this.optimizeConfiguration(); } } catch (error) { // eslint-disable-next-line no-console console.error('Error collecting cache statistics:', error); } }, this.config.statsCollectionInterval!); } /** * Calculate performance metrics from aggregated statistics */ private calculatePerformanceMetrics(aggregatedStats: AggregatedCacheStatistics | null): { memoryUsage: number; hitRateImprovement: number; responseTimeImprovement: number; } { const totals = aggregatedStats?.totals || {}; // Estimate memory usage in MB const memoryUsage = Math.round((totals.totalSize || 0) / (1024 * 1024)); // Calculate hit rate improvement (baseline 0% without cache) const hitRateImprovement = Math.round((totals.overallHitRate || 0) * 100); // Estimate response time improvement based on hit rate // Assume cache hits are 50x faster than cache misses const responseTimeImprovement = Math.round( (totals.overallHitRate || 0) * 0.98 * 100 // 98% improvement for cache hits ); return { memoryUsage, hitRateImprovement, responseTimeImprovement }; } /** * Analyze statistics and apply optimizations */ private analyzeAndOptimize(stats: { enabled: boolean; components: Record<string, import('./cache-manager').CacheStatistics>; aggregated: AggregatedCacheStatistics | null; performance: { memoryUsage: number; hitRateImprovement: number; responseTimeImprovement: number; }; }): string[] { const optimizations: string[] = []; try { const { aggregated } = stats; const totals = aggregated?.totals || {}; // Low hit rate optimization if ((totals.overallHitRate || 0) < 0.3) { optimizations.push('Increased cache TTL for better hit rates'); // Could implement actual TTL adjustments here } // High memory usage optimization if ((totals.totalSize || 0) > 100 * 1024 * 1024) { // 100MB optimizations.push('Enabled aggressive cache eviction for memory optimization'); // Could implement cache size adjustments here } // Component-specific optimizations for (const [componentName, componentStats] of Object.entries(stats.components)) { const typedStats = componentStats as unknown as ComponentCacheStatistics; if ((typedStats?.l1?.hitRate || 0) < 0.2) { optimizations.push(`Increased L1 cache size for ${componentName}`); } if ((typedStats?.l2?.hitRate || 0) > 0.8 && (typedStats?.l1?.hitRate || 0) < 0.5) { optimizations.push(`Promoted more items to L1 cache for ${componentName}`); } } } catch (error) { // eslint-disable-next-line no-console console.error('Error analyzing cache statistics for optimization:', error); } return optimizations; } } /** * Cache Integration Factory for easy setup */ export class CacheIntegrationFactory { /** * Create cache integration with development-optimized settings */ static createDevelopmentConfig(): CacheIntegrationConfig { return { enabled: true, components: { evolutionEngine: { l1MaxSize: 16 * 1024 * 1024, // 16MB l1MaxEntries: 1000, l2Enabled: false, // Disable L2 for development enableStatistics: true }, paretoFrontier: { l1MaxSize: 8 * 1024 * 1024, // 8MB l1MaxEntries: 500, l2Enabled: false, enableStatistics: true }, llmAdapter: { l1MaxSize: 32 * 1024 * 1024, // 32MB l1MaxEntries: 200, l2Enabled: false, enableStatistics: true }, trajectoryStore: { l1MaxSize: 12 * 1024 * 1024, // 12MB l1MaxEntries: 300, l2Enabled: false, enableStatistics: true }, reflectionEngine: { l1MaxSize: 8 * 1024 * 1024, // 8MB l1MaxEntries: 200, l2Enabled: false, enableStatistics: true } }, statsCollectionInterval: 10000, // 10 seconds for development autoOptimization: true }; } /** * Create cache integration with production-optimized settings */ static createProductionConfig(): CacheIntegrationConfig { return { enabled: true, components: { evolutionEngine: { l1MaxSize: 64 * 1024 * 1024, // 64MB l1MaxEntries: 5000, l2Enabled: true, l2MaxSize: 512 * 1024 * 1024, // 512MB l2CompressionEnabled: true, enableStatistics: true }, paretoFrontier: { l1MaxSize: 32 * 1024 * 1024, // 32MB l1MaxEntries: 2000, l2Enabled: true, l2MaxSize: 256 * 1024 * 1024, // 256MB l2CompressionEnabled: true, enableStatistics: true }, llmAdapter: { l1MaxSize: 128 * 1024 * 1024, // 128MB l1MaxEntries: 1000, l2Enabled: true, l2MaxSize: 1024 * 1024 * 1024, // 1GB l2CompressionEnabled: true, enableStatistics: true }, trajectoryStore: { l1MaxSize: 48 * 1024 * 1024, // 48MB l1MaxEntries: 1500, l2Enabled: true, l2MaxSize: 512 * 1024 * 1024, // 512MB l2CompressionEnabled: true, enableStatistics: true }, reflectionEngine: { l1MaxSize: 32 * 1024 * 1024, // 32MB l1MaxEntries: 1000, l2Enabled: true, l2MaxSize: 256 * 1024 * 1024, // 256MB l2CompressionEnabled: true, enableStatistics: true } }, statsCollectionInterval: 60000, // 1 minute for production autoOptimization: true }; } /** * Create minimal cache integration for testing */ static createTestConfig(): CacheIntegrationConfig { return { enabled: true, components: { evolutionEngine: { l1MaxSize: 1 * 1024 * 1024, // 1MB l1MaxEntries: 100, l2Enabled: false, enableStatistics: false }, paretoFrontier: { l1MaxSize: 1 * 1024 * 1024, // 1MB l1MaxEntries: 100, l2Enabled: false, enableStatistics: false }, llmAdapter: { l1MaxSize: 2 * 1024 * 1024, // 2MB l1MaxEntries: 50, l2Enabled: false, enableStatistics: false }, trajectoryStore: { l1MaxSize: 1 * 1024 * 1024, // 1MB l1MaxEntries: 100, l2Enabled: false, enableStatistics: false }, reflectionEngine: { l1MaxSize: 1 * 1024 * 1024, // 1MB l1MaxEntries: 50, l2Enabled: false, enableStatistics: false } }, statsCollectionInterval: 0, // Disable stats collection for tests autoOptimization: false }; } /** * Create cache integration with custom configuration */ static createCustomConfig(overrides: Partial<CacheIntegrationConfig>): CacheIntegrationConfig { const baseConfig = this.createProductionConfig(); return { ...baseConfig, ...overrides, components: { ...baseConfig.components, ...overrides.components } }; } } /** * Singleton cache integration instance */ let globalCacheIntegration: CacheIntegration | null = null; /** * Get or create global cache integration instance */ export function getCacheIntegration(config?: CacheIntegrationConfig): CacheIntegration { if (!globalCacheIntegration) { const defaultConfig = process.env.NODE_ENV === 'production' ? CacheIntegrationFactory.createProductionConfig() : CacheIntegrationFactory.createDevelopmentConfig(); globalCacheIntegration = new CacheIntegration(config || defaultConfig); } return globalCacheIntegration; } /** * Initialize global cache integration */ export async function initializeGlobalCache(config?: CacheIntegrationConfig): Promise<CacheIntegration> { const integration = getCacheIntegration(config); await integration.initialize(); return integration; } /** * Shutdown global cache integration */ export async function shutdownGlobalCache(): Promise<void> { if (globalCacheIntegration) { await globalCacheIntegration.shutdown(); globalCacheIntegration = null; } }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sloth-wq/prompt-auto-optimizer-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server