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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Main application entry point
|
| 3 |
+
* Coordinates WebSockets, Audio Hardware, and UI Elements
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
import { AudioManager } from './audio-manager.js';
|
| 7 |
+
import { VideoManager } from './video-manager.js';
|
| 8 |
+
import { WebSocketClient } from './websocket-client.js';
|
| 9 |
+
import { UIController } from './ui-controller.js';
|
| 10 |
+
|
| 11 |
+
class GeminiApp {
|
| 12 |
+
constructor() {
|
| 13 |
+
this.audioManager = new AudioManager();
|
| 14 |
+
this.videoManager = new VideoManager();
|
| 15 |
+
this.webSocketClient = new WebSocketClient();
|
| 16 |
+
this.uiController = new UIController();
|
| 17 |
+
|
| 18 |
+
this.isStreaming = false;
|
| 19 |
+
this.currentMode = null;
|
| 20 |
+
this.currentResponseElement = null; // Tracks active text row for token appending
|
| 21 |
+
|
| 22 |
+
this.initEventListeners();
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
initEventListeners() {
|
| 26 |
+
this.startAudioBtn = document.getElementById('startAudioBtn');
|
| 27 |
+
this.startCameraBtn = document.getElementById('startCameraBtn');
|
| 28 |
+
this.startScreenBtn = document.getElementById('startScreenBtn');
|
| 29 |
+
this.stopButton = document.getElementById('stopButton');
|
| 30 |
+
|
| 31 |
+
this.startAudioBtn.addEventListener('click', () => this.startStream('audio'));
|
| 32 |
+
this.startCameraBtn.addEventListener('click', () => this.startStream('camera'));
|
| 33 |
+
this.startScreenBtn.addEventListener('click', () => this.startStream('screen'));
|
| 34 |
+
this.stopButton.addEventListener('click', () => this.stopStream());
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
async startStream(mode) {
|
| 38 |
+
if (this.isStreaming) return;
|
| 39 |
+
this.currentMode = mode;
|
| 40 |
+
this.currentResponseElement = null;
|
| 41 |
+
|
| 42 |
+
try {
|
| 43 |
+
const config = this.uiController.getConfig();
|
| 44 |
+
|
| 45 |
+
// 1. Establish core connection & automatically transmit initial configuration
|
| 46 |
+
await this.webSocketClient.connect(config, {
|
| 47 |
+
onMessage: this.handleWebSocketMessage.bind(this),
|
| 48 |
+
onClose: this.handleWebSocketClose.bind(this),
|
| 49 |
+
onError: this.handleWebSocketError.bind(this)
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
// 2. Spool up hardware media paths safely now that the network connection is warm
|
| 53 |
+
await this.audioManager.startCapture((audioData) => {
|
| 54 |
+
this.webSocketClient.sendAudio(audioData);
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
if (mode !== 'audio') {
|
| 58 |
+
await this.videoManager.startCapture(mode, (imageData) => {
|
| 59 |
+
this.webSocketClient.sendImage(imageData);
|
| 60 |
+
});
|
| 61 |
+
this.uiController.showVideoPreview();
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
this.isStreaming = true;
|
| 65 |
+
this.uiController.updateUIForStreaming(true);
|
| 66 |
+
|
| 67 |
+
} catch (error) {
|
| 68 |
+
this.uiController.showError(`Failed to initialize stream: ${error.message}`);
|
| 69 |
+
this.stopStream();
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
stopStream() {
|
| 74 |
+
if (!this.isStreaming) return;
|
| 75 |
+
|
| 76 |
+
this.webSocketClient.disconnect();
|
| 77 |
+
|
| 78 |
+
// Defensive audio hardware termination to resolve Uncaught TypeError crashes
|
| 79 |
+
try {
|
| 80 |
+
if (typeof this.audioManager.stopCapture === 'function') {
|
| 81 |
+
this.audioManager.stopCapture();
|
| 82 |
+
} else if (typeof this.audioManager.stop === 'function') {
|
| 83 |
+
this.audioManager.stop();
|
| 84 |
+
} else if (typeof this.audioManager.close === 'function') {
|
| 85 |
+
this.audioManager.close();
|
| 86 |
+
}
|
| 87 |
+
} catch (e) {
|
| 88 |
+
console.warn("Audio hardware cleanup warning:", e);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
try {
|
| 92 |
+
if (typeof this.videoManager.stopCapture === 'function') {
|
| 93 |
+
this.videoManager.stopCapture();
|
| 94 |
+
} else if (typeof this.videoManager.stop === 'function') {
|
| 95 |
+
this.videoManager.stop();
|
| 96 |
+
}
|
| 97 |
+
} catch (e) {
|
| 98 |
+
console.warn("Video hardware cleanup warning:", e);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
this.isStreaming = false;
|
| 102 |
+
this.currentMode = null;
|
| 103 |
+
this.currentResponseElement = null;
|
| 104 |
+
|
| 105 |
+
this.uiController.updateUIForStreaming(false);
|
| 106 |
+
this.uiController.hideVideoPreview();
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
handleWebSocketMessage(response) {
|
| 110 |
+
const messagesContainer = document.getElementById('messages');
|
| 111 |
+
|
| 112 |
+
switch (response.type) {
|
| 113 |
+
case 'audio':
|
| 114 |
+
this.audioManager.playAudio(response.data);
|
| 115 |
+
break;
|
| 116 |
+
|
| 117 |
+
case 'user_text':
|
| 118 |
+
// Cut off any previous Gemini text streaming tracking container row since the user is speaking now
|
| 119 |
+
this.currentResponseElement = null;
|
| 120 |
+
if (messagesContainer) {
|
| 121 |
+
const userElement = document.createElement('div');
|
| 122 |
+
userElement.className = 'message-row user-response';
|
| 123 |
+
userElement.style.margin = "8px 0";
|
| 124 |
+
userElement.style.padding = "4px";
|
| 125 |
+
userElement.style.borderLeft = "3px solid #6366f1";
|
| 126 |
+
userElement.innerHTML = `<strong>User:</strong> <span class="text-payload">${response.text || response.data || ''}</span>`;
|
| 127 |
+
messagesContainer.appendChild(userElement);
|
| 128 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 129 |
+
}
|
| 130 |
+
break;
|
| 131 |
+
|
| 132 |
+
case 'gemini_text':
|
| 133 |
+
case 'text':
|
| 134 |
+
// Fall back to response.data if response.text is empty
|
| 135 |
+
const tokenValue = response.text !== undefined ? response.text : response.data;
|
| 136 |
+
if (tokenValue) {
|
| 137 |
+
this.appendGeminiToken(tokenValue);
|
| 138 |
+
}
|
| 139 |
+
break;
|
| 140 |
+
|
| 141 |
+
case 'turn_complete':
|
| 142 |
+
this.currentResponseElement = null; // Clean active line reference trackers
|
| 143 |
+
if (messagesContainer) {
|
| 144 |
+
const hr = document.createElement('hr');
|
| 145 |
+
hr.className = 'turn-divider';
|
| 146 |
+
hr.style.opacity = "0.1";
|
| 147 |
+
hr.style.margin = "12px 0";
|
| 148 |
+
messagesContainer.appendChild(hr);
|
| 149 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 150 |
+
}
|
| 151 |
+
break;
|
| 152 |
+
|
| 153 |
+
default:
|
| 154 |
+
console.log("Unhandled real-time frame event:", response);
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
/**
|
| 159 |
+
* Appends incoming speech words smoothly into a single active chat row.
|
| 160 |
+
* Filters out internal reasoning chunks / backend model thoughts automatically.
|
| 161 |
+
*/
|
| 162 |
+
appendGeminiToken(textToken) {
|
| 163 |
+
if (!textToken) return;
|
| 164 |
+
|
| 165 |
+
// Strict pattern matching to exclude thought tags, internal tool calls, or reasoning chains
|
| 166 |
+
const checkToken = textToken.trim();
|
| 167 |
+
if (
|
| 168 |
+
checkToken.startsWith('**') ||
|
| 169 |
+
checkToken.toLowerCase().includes('thought') ||
|
| 170 |
+
checkToken.includes('Analyzing') ||
|
| 171 |
+
checkToken.includes('Clarifying') ||
|
| 172 |
+
checkToken.includes('Location Details')
|
| 173 |
+
) {
|
| 174 |
+
return; // Skip thought blocks entirely
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
const messagesContainer = document.getElementById('messages');
|
| 178 |
+
if (!messagesContainer) return;
|
| 179 |
+
|
| 180 |
+
// If we aren't tracking a live conversational container, make a new clean layout structure
|
| 181 |
+
if (!this.currentResponseElement) {
|
| 182 |
+
this.currentResponseElement = document.createElement('div');
|
| 183 |
+
this.currentResponseElement.className = 'message-row gemini-response';
|
| 184 |
+
this.currentResponseElement.style.margin = "8px 0";
|
| 185 |
+
this.currentResponseElement.style.padding = "4px";
|
| 186 |
+
this.currentResponseElement.style.borderLeft = "3px solid #10b981";
|
| 187 |
+
this.currentResponseElement.innerHTML = `<strong>Gemini:</strong> <span class="text-payload"></span>`;
|
| 188 |
+
messagesContainer.appendChild(this.currentResponseElement);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
// Append raw word tokens into the layout wrapper span elements smoothly
|
| 192 |
+
const textContainer = this.currentResponseElement.querySelector('.text-payload');
|
| 193 |
+
if (textContainer) {
|
| 194 |
+
textContainer.textContent += textToken;
|
| 195 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight; // Keep scrolling view anchored
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
handleWebSocketClose() {
|
| 200 |
+
this.uiController.appendMessage('Connection stream completed.');
|
| 201 |
+
this.stopStream();
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
handleWebSocketError(error) {
|
| 205 |
+
this.uiController.showError(`WebSocket pipeline connection issue: ${error.message}`);
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 210 |
+
window.app = new GeminiApp();
|
| 211 |
+
});
|