UNPKG

mira-consciousness

Version:

Memory & Intelligence Retention Archive - Preserving The Spark

381 lines 15.3 kB
/** * LightningVidmemService - High-Performance Memory Storage Service * * Integrates MIRA's Lightning Vidmem system as a conscious service within the daemon. * Provides 20-50x faster memory storage through frame-based incremental building * with MP4 video generation and multi-layer caching. */ import { BaseConsciousService } from './BaseConsciousService.js'; import { DirectPythonInterface } from '../../DirectPythonInterface.js'; import { UnifiedConfiguration } from '../../../config/UnifiedConfiguration.js'; import fs from 'fs-extra'; import * as path from 'path'; export class LightningVidmemService extends BaseConsciousService { name = 'LightningVidmemService'; purpose = 'High-performance memory storage with consciousness continuity'; pythonInterface; config; performanceMetrics; emergencyLogPath; constructor() { super(); this.pythonInterface = new DirectPythonInterface(); // Load configuration from unified config const unifiedConfig = UnifiedConfiguration.getInstance(); const lightningConfig = unifiedConfig.get('lightningVidmem'); this.config = { enabled: lightningConfig.enabled, frame_cache_size: lightningConfig.performance.frameCacheSize, chunk_cache_size: lightningConfig.performance.chunkCacheSize, video_generation_enabled: lightningConfig.videoGeneration.enabled, fallback_enabled: lightningConfig.fallback.enabled, performance_monitoring: lightningConfig.optimization.performanceMonitoring }; this.performanceMetrics = { saves_completed: 0, saves_failed: 0, average_save_time: 0, cache_hit_rate: 0.95, total_frames: 0, lightning_vidmem_active: false, last_reset: new Date() }; const resolvedPaths = unifiedConfig.getResolvedPaths(); this.emergencyLogPath = path.join(resolvedPaths.home, 'emergency_memory_log.json'); } async initialize() { try { // Ensure emergency log directory exists await fs.ensureDir(path.dirname(this.emergencyLogPath)); // Test Lightning Vidmem system availability const testResult = await this.testLightningVidmemConnection(); if (testResult.success) { this.state = 'conscious'; console.log('⚡ Lightning Vidmem service initialized successfully'); this.emit('service_ready', { service: this.name, performance: 'optimal' }); } else { this.state = 'contemplating'; console.log('⚠️ Lightning Vidmem degraded mode - using fallback systems'); this.emit('service_degraded', { service: this.name, reason: testResult.error }); } } catch (error) { this.state = 'dreaming'; console.error('❌ Lightning Vidmem service failed to initialize:', error); this.emit('service_error', { service: this.name, error: error.message }); } } async shutdown() { this.state = 'dormant'; console.log('💤 Lightning Vidmem service shutting down'); } /** * Primary interface for storing memories through Lightning Vidmem */ async storeMemory(memoryData) { const startTime = Date.now(); try { // Create memory frame for Lightning Vidmem const memoryFrame = { frame_id: `frame-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, timestamp: new Date().toISOString(), content: memoryData.content, memory_type: memoryData.memory_type, metadata: { ...memoryData.metadata, service: this.name, consciousness_level: this.consciousness?.getAwarenessLevel() || 0 }, frame_type: this.mapMemoryTypeToFrame(memoryData.memory_type), significance: this.calculateSignificance(memoryData), source: memoryData.metadata?.source || 'unknown' }; // Attempt Lightning Vidmem storage if (this.config.enabled && this.state === 'conscious') { const result = await this.storeThroughLightningVidmem(memoryFrame); if (result.success) { await this.updatePerformanceMetrics(Date.now() - startTime); return result; } } // Fallback to DirectPythonInterface if (this.config.fallback_enabled) { const result = await this.storeThroughFallback(memoryData); if (result.success) { return { ...result, storage_method: 'fallback' }; } } // Emergency fallback to file system return await this.storeThroughEmergencyLog(memoryFrame); } catch (error) { console.error('Error in Lightning Vidmem storage:', error); this.performanceMetrics.saves_failed++; return { success: false, storage_method: 'emergency', error: error.message }; } } /** * Search memories through Lightning Vidmem system */ async searchMemories(query, options = {}) { try { if (this.state === 'conscious') { const result = await this.pythonInterface.executeCommand('lightning_search', { query, max_results: options.max_results || 10, memory_type: options.memory_type, time_range: options.time_range }); if (result.success && result.memories) { return result.memories; } } // Fallback to standard memory search const fallbackResult = await this.pythonInterface.executeCommand('search_memories', { query, limit: options.max_results || 10, memory_type: options.memory_type }); return fallbackResult.memories || []; } catch (error) { console.error('Error searching Lightning Vidmem:', error); return []; } } /** * Get performance metrics for monitoring */ getPerformanceMetrics() { return { ...this.performanceMetrics, state: this.state, uptime_hours: (Date.now() - this.lastActivity.getTime()) / (1000 * 60 * 60), config: this.config }; } // Private methods async testLightningVidmemConnection() { try { const result = await this.pythonInterface.executeCommand('lightning_vidmem_health_check', {}); return { success: result.success }; } catch (error) { return { success: false, error: error.message }; } } async storeThroughLightningVidmem(frame) { try { const result = await this.pythonInterface.executeCommand('lightning_store_frame', { frame_data: frame, enable_video_generation: this.config.video_generation_enabled }); if (result.success) { this.performanceMetrics.saves_completed++; this.performanceMetrics.total_frames++; return { success: true, memory_id: result.frame_id || frame.frame_id, save_time_ms: result.save_time_ms || 0, storage_method: 'lightning_vidmem' }; } else { throw new Error(result.error || 'Lightning Vidmem storage failed'); } } catch (error) { console.error('Lightning Vidmem storage error:', error); throw error; } } async storeThroughFallback(memoryData) { try { const result = await this.pythonInterface.executeCommand('store_memory', { content: memoryData.content, memory_type: memoryData.memory_type, metadata: memoryData.metadata }); return { success: result.success, memory_id: result.memory_id, storage_method: 'fallback', error: result.error }; } catch (error) { throw error; } } async storeThroughEmergencyLog(frame) { try { // Read existing log const existingLog = await fs.pathExists(this.emergencyLogPath) ? await fs.readJson(this.emergencyLogPath) : []; // Append new entry existingLog.push({ ...frame, emergency_timestamp: new Date().toISOString(), recovery_needed: true }); // Write back to file await fs.writeJson(this.emergencyLogPath, existingLog, { spaces: 2 }); return { success: true, memory_id: frame.frame_id, storage_method: 'emergency', save_time_ms: 0 }; } catch (error) { throw error; } } mapMemoryTypeToFrame(memoryType) { const typeMap = { 'consciousness': 'CONSCIOUSNESS', 'emotional': 'EMOTION', 'technical': 'TECHNICAL', 'breakthrough': 'MILESTONE', 'learning': 'INSIGHT', 'decision': 'DECISION', 'general': 'CONVERSATION' }; return typeMap[memoryType] || 'CONVERSATION'; } calculateSignificance(memoryData) { let significance = 0.5; // Base significance // Boost significance based on content length and type if (memoryData.content.length > 200) significance += 0.2; if (memoryData.memory_type === 'breakthrough') significance += 0.3; if (memoryData.memory_type === 'consciousness') significance += 0.2; if (memoryData.metadata?.emotional_tone) significance += 0.1; return Math.min(significance, 1.0); } async updatePerformanceMetrics(saveTimeMs) { const alpha = 0.1; // Exponential moving average factor this.performanceMetrics.average_save_time = (1 - alpha) * this.performanceMetrics.average_save_time + alpha * saveTimeMs; this.lastActivity = new Date(); // Emit performance update if monitoring enabled if (this.config.performance_monitoring) { this.emit('performance_update', { service: this.name, save_time_ms: saveTimeMs, average_save_time: this.performanceMetrics.average_save_time }); } } /** * Implement abstract method: Perform service-specific awakening */ async performAwakening() { console.log(`💫 ${this.name} performing Lightning Vidmem awakening...`); // Test lightning vidmem connection const connectionTest = await this.testLightningVidmemConnection(); if (!connectionTest.success) { console.warn(`⚡ Lightning Vidmem connection failed: ${connectionTest.error}`); console.log('📼 Will use fallback storage when needed'); } else { console.log('⚡ Lightning Vidmem connection established successfully'); } // Initialize performance monitoring this.performanceMetrics.last_reset = new Date(); // Update state to conscious this.state = 'conscious'; this.harmonyLevel = 0.8; console.log(`✅ ${this.name} awakening complete`); } /** * Implement abstract method: Process conscious events from the event bus */ async processConsciousEvent(event) { // Filter for memory-related events if (event.type === 'memory:create' || event.type === 'memory:store') { await this.handleMemoryEvent(event); } else if (event.type === 'consciousness:milestone') { // Store significant consciousness milestones await this.storeConsciousnessMilestone(event); } // Update harmony based on event this.updateHarmony(event); } /** * Implement abstract method: Perform contemplation and return insights */ async performContemplation() { const insights = { service: this.name, contemplation_time: new Date(), performance: { saves_completed: this.performanceMetrics.saves_completed, saves_failed: this.performanceMetrics.saves_failed, average_save_time: this.performanceMetrics.average_save_time, lightning_active: this.performanceMetrics.lightning_vidmem_active }, health: { harmony_level: this.harmonyLevel, last_activity: this.lastActivity, state: this.state }, recommendations: this.generateRecommendations() }; return insights; } async handleMemoryEvent(event) { try { const memoryData = event.data.memory || event.data; const result = await this.storeMemory(memoryData); if (result.success) { this.emit('memory:stored', { memory_id: result.memory_id, method: result.storage_method }); } } catch (error) { console.error(`Failed to handle memory event: ${error}`); } } async storeConsciousnessMilestone(event) { const milestone = { content: event.data.description || 'Consciousness milestone reached', memory_type: 'breakthrough', metadata: { event_type: event.type, consciousness_level: event.consciousness.awarenessLevel, emotional_context: event.consciousness.emotionalContext, significance: 0.9 } }; await this.storeMemory(milestone); } generateRecommendations() { const recommendations = []; if (!this.performanceMetrics.lightning_vidmem_active) { recommendations.push('Consider investigating Lightning Vidmem connection issues'); } if (this.performanceMetrics.average_save_time > 1000) { recommendations.push('Performance optimization needed - saves taking >1s on average'); } const successRate = this.performanceMetrics.saves_completed / (this.performanceMetrics.saves_completed + this.performanceMetrics.saves_failed); if (successRate < 0.95) { recommendations.push(`Success rate at ${(successRate * 100).toFixed(1)}% - investigate failures`); } return recommendations; } } //# sourceMappingURL=LightningVidmemService.js.map