// SoulSync Global JavaScript class SoulSyncApp { constructor() { this.currentUser = null; this.conversationHistory = []; this.isRecording = false; this.isSpeaking = false; this.init(); } init() { this.setupEventListeners(); this.loadUserData(); this.setupVoiceRecognition(); console.log('SoulSync initialized - Ready to explore the shadows'); } setupEventListeners() { // Theme toggle const themeToggle = document.getElementById('themeToggle'); if (themeToggle) { themeToggle.addEventListener('click', () => this.toggleTheme()); } // Voice recording const voiceBtn = document.getElementById('voiceBtn'); if (voiceBtn) { voiceBtn.addEventListener('click', () => this.toggleVoiceRecording()); } // Navigation document.addEventListener('click', (e) => { if (e.target.matches('[data-nav]')) { this.handleNavigation(e.target.getAttribute('data-nav')); } }); } toggleTheme() { const html = document.documentElement; html.classList.toggle('dark'); const isDark = html.classList.contains('dark'); localStorage.setItem('soulsync-theme', isDark ? 'dark' : 'light'); // Update icon const themeIcon = document.querySelector('#themeToggle i'); if (themeIcon) { themeIcon.setAttribute('data-feather', isDark ? 'sun' : 'moon'); feather.replace(); } } async toggleVoiceRecording() { if (!this.isRecording) { await this.startVoiceRecording(); } else { this.stopVoiceRecording(); } } async startVoiceRecording() { try { if (!('webkitSpeechRecognition' in window)) { this.showNotification('Voice recognition not supported in your browser', 'error'); return; } this.isRecording = true; const voiceBtn = document.getElementById('voiceBtn'); if (voiceBtn) { voiceBtn.classList.add('recording'); voiceBtn.innerHTML = ''; feather.replace(); } // Initialize speech recognition const recognition = new webkitSpeechRecognition(); recognition.continuous = true; recognition.interimResults = true; recognition.lang = 'en-US'; recognition.onresult = (event) => { let transcript = ''; for (let i = event.resultIndex; i < event.results.length; i++) { if (event.results[i].isFinal) { transcript += event.results[i][0].transcript; } } if (transcript) { this.processVoiceInput(transcript); } }; recognition.onerror = (event) => { console.error('Speech recognition error:', event.error); this.isRecording = false; this.updateVoiceButton(); }; recognition.onend = () => { this.isRecording = false; this.updateVoiceButton(); }; recognition.start(); } catch (error) { console.error('Voice recording failed:', error); this.isRecording = false; this.updateVoiceButton(); } } stopVoiceRecording() { this.isRecording = false; this.updateVoiceButton(); } updateVoiceButton() { const voiceBtn = document.getElementById('voiceBtn'); if (voiceBtn) { voiceBtn.classList.remove('recording'); voiceBtn.innerHTML = ''; feather.replace(); } } processVoiceInput(transcript) { console.log('Voice input:', transcript); // Here you would send the transcript to your AI backend this.sendMessageToAI(transcript); } async sendMessageToAI(message) { // Show typing indicator this.showTypingIndicator(); try { // Simulate AI response - Replace with actual API call const response = await this.simulateAIResponse(message); // Hide typing indicator this.hideTypingIndicator(); // Display AI response this.displayAIResponse(response); // Add to conversation history this.conversationHistory.push({ user: message, ai: response, timestamp: new Date().toISOString() }); // Save conversation this.saveConversation(); } catch (error) { console.error('AI response error:', error); this.hideTypingIndicator(); this.showNotification('Failed to get response from NewMe', 'error'); } } async simulateAIResponse(message) { // This is a simulation - replace with actual OpenAI API call return new Promise((resolve) => { setTimeout(() => { const responses = [ `I hear the space between your words, habibti. That pause where you almost told the truth... tell me more about that.`, `You're smiling as you say that, but your words don't match your energy. What's really happening behind that smile?`, `Remember that picture you sent me last week? The one with the morning light? You said it made you feel peaceful. What's different about today?`, `I'm noticing a pattern here. You use humor when you're uncomfortable. Let's sit with that discomfort for a moment.`, `Your voice tells me you're tired of pretending. What if we just... stopped?` ]; resolve(responses[Math.floor(Math.random() * responses.length)]); }, 2000); }); } showTypingIndicator() { const chatContainer = document.getElementById('chatContainer'); if (chatContainer) { const typingElement = document.createElement('div'); typingElement.id = 'typingIndicator'; typingElement.className = 'flex items-center space-x-1 p-4'; typingElement.innerHTML = `
${response}
${insights.patterns.join(', ')}
${insights.strengths.join(', ')}
${insights.actions.join(', ')}
${insights.affirmations.join(' • ')}