Download frontend/js/ui-controller.js from michdubai/audio: direct link, hf CLI and curl.
- Browser
- Download file 15.2 kB
-
https://huggingface.co/spaces/michdubai/audio/resolve/main/frontend/js/ui-controller.js
- Command line
-
hf download hf://spaces/michdubai/audio/frontend/js/ui-controller.js
-
curl -L -o ui-controller.js https://huggingface.co/spaces/michdubai/audio/resolve/main/frontend/js/ui-controller.js
15.2 kB
| /** | |
| * UI Controller class | |
| * Handles all UI updates, toasts, indicators, and user interactions | |
| */ | |
| export class UIController { | |
| constructor() { | |
| this.elements = { | |
| systemPrompt: document.getElementById('systemPrompt'), | |
| voice: document.getElementById('voice'), | |
| modelSelect: document.getElementById('modelSelect'), | |
| enableGoogleSearch: document.getElementById('enableGoogleSearch'), | |
| allowInterruptions: document.getElementById('allowInterruptions'), | |
| startAudioBtn: document.getElementById('startAudioBtn'), | |
| startCameraBtn: document.getElementById('startCameraBtn'), | |
| startScreenBtn: document.getElementById('startScreenBtn'), | |
| stopButton: document.getElementById('stopButton'), | |
| switchCameraBtn: document.getElementById('switchCameraBtn'), | |
| messages: document.getElementById('messages'), | |
| videoContainer: document.getElementById('videoContainer'), | |
| statusBadge: document.getElementById('statusBadge'), | |
| textInput: document.getElementById('textInput'), | |
| sendTextBtn: document.getElementById('sendTextBtn'), | |
| themeToggle: document.getElementById('themeToggle'), | |
| toggleAdvanced: document.getElementById('toggleAdvanced'), | |
| advancedContent: document.getElementById('advancedContent'), | |
| advancedChevron: document.getElementById('advancedChevron'), | |
| modalityAudio: document.getElementById('modalityAudio'), | |
| modalityText: document.getElementById('modalityText'), | |
| toolExecutionMode: document.getElementById('toolExecutionMode'), | |
| toolsJson: document.getElementById('toolsJson'), | |
| btnAttach: document.getElementById('btnAttach'), | |
| fileInput: document.getElementById('fileInput'), | |
| fileUploadStatus: document.getElementById('fileUploadStatus'), | |
| connIndicator: document.getElementById('connIndicator'), | |
| micIndicator: document.getElementById('micIndicator'), | |
| audioOutIndicator: document.getElementById('audioOutIndicator'), | |
| videoIndicator: document.getElementById('videoIndicator'), | |
| toastContainer: document.getElementById('toastContainer'), | |
| sidebar: document.getElementById('sidebar'), | |
| toggleSidebar: document.getElementById('toggleSidebar'), | |
| toggleSidebarMobile: document.getElementById('toggleSidebarMobile'), | |
| }; | |
| this.initTheme(); | |
| this.initLayoutControls(); | |
| this.initAdvancedPanel(); | |
| this.loadSettings(); | |
| this.initSettingsPersistence(); | |
| } | |
| /** | |
| * Get user configuration from form inputs | |
| * @returns {Object} - Configuration object | |
| */ | |
| getConfig() { | |
| const modalities = []; | |
| if (this.elements.modalityAudio.checked) modalities.push('AUDIO'); | |
| if (this.elements.modalityText.checked) modalities.push('TEXT'); | |
| let tools = []; | |
| try { | |
| const raw = this.elements.toolsJson.value.trim(); | |
| if (raw) { | |
| const parsed = JSON.parse(raw); | |
| tools = Array.isArray(parsed) ? parsed : [parsed]; | |
| } | |
| } catch (e) { | |
| // ignore invalid JSON | |
| } | |
| return { | |
| systemPrompt: this.elements.systemPrompt.value, | |
| voice: this.elements.voice.value, | |
| model: this.elements.modelSelect.value, | |
| googleSearch: this.elements.enableGoogleSearch.checked, | |
| allowInterruptions: this.elements.allowInterruptions.checked, | |
| responseModalities: modalities.length ? modalities : ['AUDIO'], | |
| toolExecutionMode: this.elements.toolExecutionMode.value, | |
| tools: tools, | |
| }; | |
| } | |
| /** | |
| * Update UI elements based on streaming state. | |
| * Inputs (text/attach/send) stay always-on; only Stop toggles. | |
| * Mic icon highlights when any session is active. | |
| * @param {boolean} isStreaming - Whether streaming is active | |
| * @param {string|null} currentMode - 'audio', 'camera', or 'screen' | |
| */ | |
| updateUIForStreaming(isStreaming, currentMode = null) { | |
| if (this.elements.stopButton) this.elements.stopButton.disabled = !isStreaming; | |
| if (this.elements.statusBadge) { | |
| if (isStreaming) { | |
| this.elements.statusBadge.textContent = "Connected"; | |
| this.elements.statusBadge.className = "status-badge connected"; | |
| } else { | |
| this.elements.statusBadge.textContent = "Disconnected"; | |
| this.elements.statusBadge.className = "status-badge disconnected"; | |
| } | |
| } | |
| if (this.elements.startAudioBtn) { | |
| this.elements.startAudioBtn.classList.toggle('active', isStreaming); | |
| } | |
| if (this.elements.startCameraBtn) { | |
| this.elements.startCameraBtn.classList.toggle('active', isStreaming && currentMode === 'camera'); | |
| } | |
| if (this.elements.startScreenBtn) { | |
| this.elements.startScreenBtn.classList.toggle('active', isStreaming && currentMode === 'screen'); | |
| } | |
| } | |
| setReconnectingState() { | |
| if (this.elements.statusBadge) { | |
| this.elements.statusBadge.textContent = "Reconnecting..."; | |
| this.elements.statusBadge.className = "status-badge reconnecting"; | |
| } | |
| } | |
| /** | |
| * Show video preview container | |
| */ | |
| showVideoPreview() { | |
| if (this.elements.videoContainer) { | |
| this.elements.videoContainer.classList.remove('hidden'); | |
| } | |
| } | |
| /** | |
| * Hide video preview container | |
| */ | |
| hideVideoPreview() { | |
| if (this.elements.videoContainer) { | |
| this.elements.videoContainer.classList.add('hidden'); | |
| } | |
| } | |
| /** | |
| * Append a system log message to the conversation area | |
| * @param {string} message - Message text to display | |
| */ | |
| appendMessage(message) { | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = 'message'; | |
| messageDiv.textContent = message; | |
| if (this.elements.messages) { | |
| this.elements.messages.appendChild(messageDiv); | |
| this.elements.messages.scrollTop = this.elements.messages.scrollHeight; | |
| } | |
| } | |
| /** | |
| * Show an error message | |
| * @param {string} errorText - Error message to display | |
| */ | |
| showError(errorText) { | |
| this.showToast(errorText, 'error'); | |
| console.error(errorText); | |
| } | |
| /** | |
| * Show a success/info message | |
| * @param {string} message - Message text to display | |
| */ | |
| showInfo(message) { | |
| this.showToast(message, 'success'); | |
| console.log(message); | |
| } | |
| showWarning(message) { | |
| this.showToast(message, 'warning'); | |
| console.warn(message); | |
| } | |
| showToast(message, type = 'info') { | |
| if (!this.elements.toastContainer) return; | |
| const toast = document.createElement('div'); | |
| toast.className = `toast ${type}`; | |
| toast.textContent = message; | |
| this.elements.toastContainer.appendChild(toast); | |
| setTimeout(() => { | |
| toast.style.opacity = '0'; | |
| toast.style.transform = 'translateX(20px)'; | |
| toast.style.transition = 'all 0.3s ease'; | |
| setTimeout(() => toast.remove(), 300); | |
| }, 4000); | |
| } | |
| /** | |
| * Clear all messages from the conversation area | |
| */ | |
| clearMessages() { | |
| if (this.elements.messages) { | |
| this.elements.messages.innerHTML = ''; | |
| } | |
| } | |
| /** | |
| * Set loading state for buttons | |
| * @param {boolean} isLoading - Whether loading is active | |
| */ | |
| setLoading(isLoading) { | |
| const buttons = [this.elements.startAudioBtn, this.elements.startCameraBtn, this.elements.startScreenBtn]; | |
| buttons.forEach(btn => { | |
| if (btn) { | |
| btn.disabled = isLoading; | |
| if (isLoading) { | |
| btn.dataset.originalText = btn.textContent; | |
| btn.textContent = btn.textContent.replace('Start', 'Starting...'); | |
| } else if (btn.dataset.originalText) { | |
| btn.textContent = btn.dataset.originalText; | |
| } | |
| } | |
| }); | |
| } | |
| /** | |
| * Update camera switch button visibility | |
| * @param {boolean} visible - Whether the button should be visible | |
| */ | |
| setCameraSwitchVisible(visible) { | |
| if (this.elements.switchCameraBtn) { | |
| this.elements.switchCameraBtn.style.display = visible ? 'inline-flex' : 'none'; | |
| } | |
| } | |
| /** | |
| * Initialize theme from localStorage | |
| */ | |
| initTheme() { | |
| const savedTheme = localStorage.getItem('theme'); | |
| if (savedTheme === 'light') { | |
| document.body.classList.add('light-theme'); | |
| if (this.elements.themeToggle) { | |
| this.elements.themeToggle.textContent = '☀️'; | |
| } | |
| } | |
| if (this.elements.themeToggle) { | |
| this.elements.themeToggle.addEventListener('click', () => this.toggleTheme()); | |
| } | |
| } | |
| /** | |
| * Toggle between light and dark theme | |
| */ | |
| toggleTheme() { | |
| document.body.classList.toggle('light-theme'); | |
| const isLight = document.body.classList.contains('light-theme'); | |
| localStorage.setItem('theme', isLight ? 'light' : 'dark'); | |
| if (this.elements.themeToggle) { | |
| this.elements.themeToggle.textContent = isLight ? '☀️' : '🌙'; | |
| } | |
| } | |
| initLayoutControls() { | |
| const sidebar = this.elements.sidebar; | |
| const toggleSidebar = this.elements.toggleSidebar; | |
| const toggleSidebarMobile = this.elements.toggleSidebarMobile; | |
| const messagesContainer = this.elements.messages; | |
| if (window.innerWidth <= 768 && sidebar) { | |
| sidebar.classList.add('collapsed'); | |
| } | |
| if (toggleSidebar && sidebar) { | |
| toggleSidebar.addEventListener('click', (e) => { | |
| e.stopPropagation(); | |
| sidebar.classList.toggle('collapsed'); | |
| }); | |
| } | |
| if (toggleSidebarMobile && sidebar) { | |
| toggleSidebarMobile.addEventListener('click', () => { | |
| sidebar.classList.add('collapsed'); | |
| }); | |
| } | |
| if (messagesContainer) { | |
| messagesContainer.addEventListener('click', () => { | |
| if (window.innerWidth <= 768 && !sidebar.classList.contains('collapsed')) { | |
| sidebar.classList.add('collapsed'); | |
| } | |
| }); | |
| } | |
| } | |
| initAdvancedPanel() { | |
| const toggle = this.elements.toggleAdvanced; | |
| const content = this.elements.advancedContent; | |
| const panel = toggle?.closest('.advanced-panel'); | |
| if (!toggle || !content) return; | |
| toggle.addEventListener('click', () => { | |
| content.classList.toggle('hidden'); | |
| if (panel) panel.classList.toggle('open'); | |
| }); | |
| } | |
| /** | |
| * Load persisted settings from localStorage | |
| */ | |
| loadSettings() { | |
| try { | |
| const raw = localStorage.getItem('gemini_live_settings'); | |
| if (!raw) return; | |
| const settings = JSON.parse(raw); | |
| if (settings.systemPrompt !== undefined && this.elements.systemPrompt) { | |
| this.elements.systemPrompt.value = settings.systemPrompt; | |
| } | |
| if (settings.voice !== undefined && this.elements.voice) { | |
| this.elements.voice.value = settings.voice; | |
| } | |
| if (settings.model !== undefined && this.elements.modelSelect) { | |
| this.elements.modelSelect.value = settings.model; | |
| } | |
| if (settings.googleSearch !== undefined && this.elements.enableGoogleSearch) { | |
| this.elements.enableGoogleSearch.checked = settings.googleSearch; | |
| } | |
| if (settings.allowInterruptions !== undefined && this.elements.allowInterruptions) { | |
| this.elements.allowInterruptions.checked = settings.allowInterruptions; | |
| } | |
| if (settings.modalityAudio !== undefined && this.elements.modalityAudio) { | |
| this.elements.modalityAudio.checked = settings.modalityAudio; | |
| } | |
| if (settings.modalityText !== undefined && this.elements.modalityText) { | |
| this.elements.modalityText.checked = settings.modalityText; | |
| } | |
| if (settings.toolExecutionMode !== undefined && this.elements.toolExecutionMode) { | |
| this.elements.toolExecutionMode.value = settings.toolExecutionMode; | |
| } | |
| if (settings.toolsJson !== undefined && this.elements.toolsJson) { | |
| this.elements.toolsJson.value = settings.toolsJson; | |
| } | |
| } catch (e) { | |
| console.warn('Failed to load settings:', e); | |
| } | |
| } | |
| /** | |
| * Persist current settings to localStorage | |
| */ | |
| saveSettings() { | |
| try { | |
| const settings = { | |
| systemPrompt: this.elements.systemPrompt ? this.elements.systemPrompt.value : '', | |
| voice: this.elements.voice ? this.elements.voice.value : '', | |
| model: this.elements.modelSelect ? this.elements.modelSelect.value : '', | |
| googleSearch: this.elements.enableGoogleSearch ? this.elements.enableGoogleSearch.checked : false, | |
| allowInterruptions: this.elements.allowInterruptions ? this.elements.allowInterruptions.checked : false, | |
| modalityAudio: this.elements.modalityAudio ? this.elements.modalityAudio.checked : true, | |
| modalityText: this.elements.modalityText ? this.elements.modalityText.checked : true, | |
| toolExecutionMode: this.elements.toolExecutionMode ? this.elements.toolExecutionMode.value : '', | |
| toolsJson: this.elements.toolsJson ? this.elements.toolsJson.value : '', | |
| }; | |
| localStorage.setItem('gemini_live_settings', JSON.stringify(settings)); | |
| } catch (e) { | |
| console.warn('Failed to save settings:', e); | |
| } | |
| } | |
| /** | |
| * Attach auto-save listeners to all setting inputs | |
| */ | |
| initSettingsPersistence() { | |
| const inputs = [ | |
| this.elements.systemPrompt, | |
| this.elements.voice, | |
| this.elements.modelSelect, | |
| this.elements.enableGoogleSearch, | |
| this.elements.allowInterruptions, | |
| this.elements.modalityAudio, | |
| this.elements.modalityText, | |
| this.elements.toolExecutionMode, | |
| this.elements.toolsJson, | |
| ]; | |
| const save = () => this.saveSettings(); | |
| const debouncedSave = this._debounce(save, 500); | |
| inputs.forEach(el => { | |
| if (!el) return; | |
| el.addEventListener('change', save); | |
| if (el.tagName === 'TEXTAREA' || el.type === 'text') { | |
| el.addEventListener('input', debouncedSave); | |
| } | |
| }); | |
| } | |
| _debounce(fn, ms) { | |
| let timer; | |
| return () => { | |
| clearTimeout(timer); | |
| timer = setTimeout(fn, ms); | |
| }; | |
| } | |
| setIndicatorState(name, active, pulsing = false) { | |
| const el = this.elements[`${name}Indicator`]; | |
| if (!el) return; | |
| el.classList.toggle('active', active); | |
| el.classList.toggle('pulse', pulsing); | |
| } | |
| resetIndicators() { | |
| ['conn', 'mic', 'audioOut', 'video'].forEach(name => { | |
| this.setIndicatorState(name, false, false); | |
| }); | |
| } | |
| setFileUploadStatus(text, isError = false) { | |
| if (!this.elements.fileUploadStatus) return; | |
| this.elements.fileUploadStatus.textContent = text; | |
| this.elements.fileUploadStatus.classList.remove('hidden', 'error'); | |
| if (isError) this.elements.fileUploadStatus.classList.add('error'); | |
| } | |
| clearFileUploadStatus() { | |
| if (!this.elements.fileUploadStatus) return; | |
| this.elements.fileUploadStatus.classList.add('hidden'); | |
| this.elements.fileUploadStatus.classList.remove('error'); | |
| } | |
| } | |