Upload 2 files
Browse filesFiles changed (2)
- frontend/js/main.js+77-40
- frontend/js/ui-controller.js+18-10
33 unmodified lines34353637383940414243444573 unmodified lines1191201211221231241251261271281291301313 unmodified lines13513613713813914014114214314413 unmodified lines15815916016116216316416516616716816917023 unmodified lines1941951961971981992002012024 unmodified lines2072082092102112122132142152162176 unmodified lines2242252262272282292302312322332342352362372382392402412422432442452462472482492502512521 unmodified line25425525625725825926026126226326433 unmodified linesthis.startScreenBtn = document.getElementById('startScreenBtn');this.stopButton = document.getElementById('stopButton');this.switchCameraBtn = document.getElementById('switchCameraBtn');this.startAudioBtn.addEventListener('click', () => this.startStream('audio'));this.startCameraBtn.addEventListener('click', () => this.startStream('camera'));this.startScreenBtn.addEventListener('click', () => this.startStream('screen'));this.stopButton.addEventListener('click', () => this.stopStream());if (this.switchCameraBtn) {this.switchCameraBtn.addEventListener('click', () => this.switchCamera());}73 unmodified linesthis.uiController.setFileUploadStatus(`Uploading ${file.name}...`);try {const base64 = await this._fileToBase64(file);if (this.webSocketClient.isConnected()) {// WebSocket proxy upload (backend uploads to Gemini Files API)this.webSocketClient.sendFileUpload(base64, file.type || 'application/octet-stream');} else {// REST upload as fallbackconst formData = new FormData();formData.append('file', file);const resp = await fetch('/api/upload', { method: 'POST', body: formData });3 unmodified linesif (this.webSocketClient.isConnected()) {this.webSocketClient.sendFile(data.uri, data.mime_type);}}} catch (err) {this.uiController.showError(`File upload failed: ${err.message}`);this.uiController.setFileUploadStatus(err.message, true);}}13 unmodified lines/*** Reads current text frame, displays it directly in the conversation window,* and pushes it down the streaming WebSocket pipeline.*/handleTextSubmission() {if (!this.isStreaming || !this.webSocketClient.isConnected()) return;const text = this.textInput.value.trim();if (!text) return;this.webSocketClient.sendText(text);const messagesContainer = document.getElementById('messages');23 unmodified linesthis.textInput.style.height = 'auto';}async startStream(mode) {if (this.isStreaming) return;this.currentMode = mode;this.currentResponseElement = null;this.isReasoningMode = false;4 unmodified linestry {const config = this.uiController.getConfig();// Initialize audio playback context during user gesture to satisfy browser autoplay policyawait this.audioManager.init();// Resume previous session if a handle was storedconst savedHandle = localStorage.getItem('gemini_resumption_handle');if (savedHandle) {config.resumption_handle = savedHandle;6 unmodified linesonError: this.handleWebSocketError.bind(this)});await this.audioManager.startCapture((audioData) => {this.webSocketClient.sendAudio(audioData);});if (mode !== 'audio') {const facingMode = mode === 'camera' ? 'environment' : null;await this.videoManager.startCapture(mode, (imageData) => {this.webSocketClient.sendImage(imageData);}, facingMode);this.uiController.showVideoPreview();if (mode === 'camera' && this.switchCameraBtn) {this.switchCameraBtn.style.display = 'inline-flex';}}this.isStreaming = true;this.uiController.updateUIForStreaming(true);this.uiController.setIndicatorState('conn', true, false);this.uiController.setIndicatorState('mic', true, true);if (mode !== 'audio') {this.uiController.setIndicatorState('video', true, true);}const messagesContainer = document.getElementById('messages');if (messagesContainer) {1 unmodified line}} catch (error) {this.uiController.showError(`Failed to initialize stream: ${error.message}`);this.stopStream();}}async switchCamera() {if (this.videoManager && this.currentMode === 'camera') {const success = await this.videoManager.switchCamera();33 unmodified lines34353637383940414243444546474849505152535473 unmodified lines1281291301311321331341351361371381391401411423 unmodified lines14614714814915015115215315415513 unmodified lines16917017117217317417517617717817918018118218318423 unmodified lines2082092102112122132142154 unmodified lines2202212222232242252262276 unmodified lines2342352362372382392402412421 unmodified line24424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030133 unmodified linesthis.startScreenBtn = document.getElementById('startScreenBtn');this.stopButton = document.getElementById('stopButton');this.switchCameraBtn = document.getElementById('switchCameraBtn');this.btnMore = document.getElementById('btnMore');this.morePanel = document.getElementById('morePanel');this.startAudioBtn.addEventListener('click', () => this.startStream('audio'));this.startCameraBtn.addEventListener('click', () => this.startStream('camera'));this.startScreenBtn.addEventListener('click', () => this.startStream('screen'));this.stopButton.addEventListener('click', () => this.stopStream());if (this.btnMore && this.morePanel) {this.btnMore.addEventListener('click', () => {const isHidden = this.morePanel.classList.toggle('hidden');this.btnMore.classList.toggle('expanded', !isHidden);});}if (this.switchCameraBtn) {this.switchCameraBtn.addEventListener('click', () => this.switchCamera());}73 unmodified linesthis.uiController.setFileUploadStatus(`Uploading ${file.name}...`);try {if (!this.webSocketClient.isConnected()) {await this.connect();}const base64 = await this._fileToBase64(file);this.webSocketClient.sendFileUpload(base64, file.type || 'application/octet-stream');} catch (err) {// REST fallback if WS not availabletry {const formData = new FormData();formData.append('file', file);const resp = await fetch('/api/upload', { method: 'POST', body: formData });3 unmodified linesif (this.webSocketClient.isConnected()) {this.webSocketClient.sendFile(data.uri, data.mime_type);}} catch (err2) {this.uiController.showError(`File upload failed: ${err.message}`);this.uiController.setFileUploadStatus(err.message, true);}}}13 unmodified lines/*** Reads current text frame, displays it directly in the conversation window,* and pushes it down the streaming WebSocket pipeline.* Auto-connects if not already streaming.*/async handleTextSubmission() {const text = this.textInput.value.trim();if (!text) return;if (!this.isStreaming || !this.webSocketClient.isConnected()) {await this.connect();}this.webSocketClient.sendText(text);const messagesContainer = document.getElementById('messages');23 unmodified linesthis.textInput.style.height = 'auto';}async connect() {if (this.isStreaming && this.webSocketClient.isConnected()) return;this.currentResponseElement = null;this.isReasoningMode = false;4 unmodified linestry {const config = this.uiController.getConfig();await this.audioManager.init();const savedHandle = localStorage.getItem('gemini_resumption_handle');if (savedHandle) {config.resumption_handle = savedHandle;6 unmodified linesonError: this.handleWebSocketError.bind(this)});this.isStreaming = true;this.uiController.updateUIForStreaming(true, this.currentMode);this.uiController.setIndicatorState('conn', true, false);const messagesContainer = document.getElementById('messages');if (messagesContainer) {1 unmodified line}} catch (error) {this.uiController.showError(`Failed to connect: ${error.message}`);this.stopStream();}}async startStream(mode) {if (!this.isStreaming) {await this.connect();}// Hot-switch: stop current video if switching away from a video modeif (this.currentMode && this.currentMode !== 'audio' && mode !== this.currentMode) {try {this.videoManager.stopCapture();} catch (e) {console.warn("Video stop warning:", e);}this.uiController.hideVideoPreview();this.uiController.setIndicatorState('video', false, false);if (this.switchCameraBtn) {this.switchCameraBtn.style.display = 'none';}}this.currentMode = mode;if (!this.audioManager.isRecording) {await this.audioManager.startCapture((audioData) => {this.webSocketClient.sendAudio(audioData);});this.uiController.setIndicatorState('mic', true, true);}if (mode !== 'audio') {if (this.videoManager.videoStream) {try { this.videoManager.stopCapture(); } catch (e) {}}const facingMode = mode === 'camera' ? 'environment' : null;await this.videoManager.startCapture(mode, (imageData) => {this.webSocketClient.sendImage(imageData);}, facingMode);this.uiController.showVideoPreview();this.uiController.setIndicatorState('video', true, true);if (mode === 'camera' && this.switchCameraBtn) {this.switchCameraBtn.style.display = 'inline-flex';}}this.uiController.updateUIForStreaming(true, this.currentMode);}async switchCamera() {if (this.videoManager && this.currentMode === 'camera') {const success = await this.videoManager.switchCamera();
78 unmodified lines7980818283848586878889909192939495964 unmodified lines10110210310410510678 unmodified lines}/*** Update UI elements based on streaming state* @param {boolean} isStreaming - Whether streaming is active*/updateUIForStreaming(isStreaming) {this.elements.startAudioBtn.disabled = isStreaming;this.elements.startCameraBtn.disabled = isStreaming;this.elements.startScreenBtn.disabled = isStreaming;this.elements.stopButton.disabled = !isStreaming;if (this.elements.textInput) this.elements.textInput.disabled = !isStreaming;if (this.elements.sendTextBtn) this.elements.sendTextBtn.disabled = !isStreaming;if (this.elements.btnAttach) this.elements.btnAttach.disabled = !isStreaming;if (this.elements.statusBadge) {if (isStreaming) {4 unmodified linesthis.elements.statusBadge.className = "status-badge disconnected";}}}setReconnectingState() {78 unmodified lines79808182838485868788899091924 unmodified lines97989910010110210310410510610710810911011111211311478 unmodified lines}/*** 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) {4 unmodified linesthis.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() {
frontend/js/main.js+77-40
frontend/js/ui-controller.js+18-10