// Main application entry. import { checkOpusLoaded, initOpusEncoder } from './core/audio/opus-codec.js?v=0205'; import { getAudioPlayer } from './core/audio/player.js?v=0205'; import { checkMicrophoneAvailability, isHttpNonLocalhost } from './core/audio/recorder.js?v=0205'; import { initMcpTools } from './core/mcp/tools.js?v=0205'; import { uiController } from './ui/controller.js?v=0205'; import { log } from './utils/logger.js?v=0205'; // Helper to convert a Base64 payload into a Blob. function dataURItoBlob(dataURI) { const byteString = atob(dataURI.split(',')[1]); const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; const ab = new ArrayBuffer(byteString.length); const ia = new Uint8Array(ab); for (let i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } return new Blob([ab], { type: mimeString }); } // Application wrapper. class App { constructor() { this.uiController = null; this.audioPlayer = null; this.live2dManager = null; this.cameraStream = null; this.currentFacingMode = 'user'; } // Initialize the application. async init() { log('Initializing the application...', 'info'); // Initialize the UI controller. this.uiController = uiController; this.uiController.init(); // Check the Opus runtime. checkOpusLoaded(); // Initialize the Opus encoder. initOpusEncoder(); // Initialize the audio player. this.audioPlayer = getAudioPlayer(); await this.audioPlayer.start(); // Initialize MCP tools. initMcpTools(); // Check microphone availability. await this.checkMicrophoneAvailability(); // Check camera availability. this.checkCameraAvailability(); // Initialize Live2D. await this.initLive2D(); // Initialize the camera. this.initCamera(); // Hide the loading indicator. this.setModelLoadingStatus(false); log('Application initialized successfully.', 'success'); } // Initialize Live2D. async initLive2D() { try { // Ensure the Live2D manager was loaded first. if (typeof window.Live2DManager === 'undefined') { throw new Error('Live2DManager is not loaded. Check the script import order.'); } this.live2dManager = new window.Live2DManager(); await this.live2dManager.initializeLive2D(); // Update the UI state. const live2dStatus = document.getElementById('live2dStatus'); if (live2dStatus) { live2dStatus.textContent = '● Loaded'; live2dStatus.className = 'status loaded'; } log('Live2D initialized successfully.', 'success'); } catch (error) { log(`Live2D initialization failed: ${error.message}`, 'error'); // Update the UI state. const live2dStatus = document.getElementById('live2dStatus'); if (live2dStatus) { live2dStatus.textContent = '● Failed'; live2dStatus.className = 'status error'; } } } // Toggle the model loading state. setModelLoadingStatus(isLoading) { const modelLoading = document.getElementById('modelLoading'); if (modelLoading) { modelLoading.style.display = isLoading ? 'flex' : 'none'; } } /** * Check microphone availability and update the UI state. */ async checkMicrophoneAvailability() { try { const isAvailable = await checkMicrophoneAvailability(); const isHttp = isHttpNonLocalhost(); // Persist the state globally for the rest of the test harness. window.microphoneAvailable = isAvailable; window.isHttpNonLocalhost = isHttp; // Update the UI. if (this.uiController) { this.uiController.updateMicrophoneAvailability(isAvailable, isHttp); } log(`Microphone availability check completed: ${isAvailable ? 'available' : 'unavailable'}`, isAvailable ? 'success' : 'warning'); } catch (error) { log(`Failed to check microphone availability: ${error.message}`, 'error'); // Default to unavailable on failure. window.microphoneAvailable = false; window.isHttpNonLocalhost = isHttpNonLocalhost(); if (this.uiController) { this.uiController.updateMicrophoneAvailability(false, window.isHttpNonLocalhost); } } } // Check camera availability. checkCameraAvailability() { window.cameraAvailable = true; log('Camera availability check completed. Device binding is assumed for the test harness.', 'success'); } // Initialize camera interactions. async initCamera() { const cameraContainer = document.getElementById('cameraContainer'); const cameraVideo = document.getElementById('cameraVideo'); const cameraSwitch = document.getElementById('cameraSwitch'); const cameraSwitchMask = document.getElementById('cameraSwitchMask'); const dialBtn = document.getElementById('dialBtn'); if (!cameraContainer || !cameraVideo) { log('Camera elements were not found. Skipping camera initialization.', 'warning'); return Promise.resolve(false); } let isDragging = false; let currentX, currentY, initialX, initialY; let xOffset = 0, yOffset = 0; cameraContainer.addEventListener('mousedown', dragStart); document.addEventListener('mousemove', drag); document.addEventListener('mouseup', dragEnd); cameraContainer.addEventListener('touchstart', dragStart, { passive: false }); document.addEventListener('touchmove', drag, { passive: false }); document.addEventListener('touchend', dragEnd); function dragStart(e) { if (e.type === 'touchstart') { initialX = e.touches[0].clientX - xOffset; initialY = e.touches[0].clientY - yOffset; } else { initialX = e.clientX - xOffset; initialY = e.clientY - yOffset; } isDragging = true; cameraContainer.classList.add('dragging'); } function drag(e) { if (isDragging) { e.preventDefault(); if (e.type === 'touchmove') { currentX = e.touches[0].clientX - initialX; currentY = e.touches[0].clientY - initialY; } else { currentX = e.clientX - initialX; currentY = e.clientY - initialY; } xOffset = currentX; yOffset = currentY; cameraContainer.style.transform = `translate3d(${currentX}px, ${currentY}px, 0)`; } } function dragEnd() { initialX = currentX; initialY = currentY; isDragging = false; cameraContainer.classList.remove('dragging'); } return new Promise((resolve) => { window.startCamera = async () => { try { if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { log('This browser does not support the camera API.', 'warning'); return false; } log('Requesting camera permission...', 'info'); this.cameraStream = await navigator.mediaDevices.getUserMedia({ video: { width: 180, height: 240, facingMode: this.currentFacingMode }, audio: false }); cameraVideo.srcObject = this.cameraStream; const devices = await navigator.mediaDevices.enumerateDevices(); const videoDevices = devices.filter(device => device.kind === 'videoinput'); if (videoDevices.length > 1) { if (cameraSwitch) cameraSwitch.classList.add('active'); } cameraContainer.classList.add('active'); // If the call is not active, revert the temporary camera state. const hasActive = dialBtn.classList.contains('dial-active'); if (!hasActive) { cameraContainer.classList.remove('active'); cameraSwitch.classList.remove('active'); window.stopCamera(); } log('Camera started successfully.', 'success'); return true; } catch (error) { log(`Failed to start the camera: ${error.name} - ${error.message}`, 'error'); if (error.name === 'NotAllowedError') { log('Camera permission was denied. Check the browser settings.', 'warning'); } else if (error.name === 'NotFoundError') { log('No camera device was found.', 'warning'); } else if (error.name === 'NotReadableError') { log('The camera is being used by another application.', 'warning'); } return false; } }; window.stopCamera = () => { if (this.cameraStream) { this.cameraStream.getTracks().forEach(track => track.stop()); this.cameraStream = null; cameraVideo.srcObject = null; log('Camera stopped.', 'info'); } }; window.switchCamera = async() => { if (window.switchCameraTimer) return; if (this.cameraStream) { const currentTransform = window.getComputedStyle(cameraContainer).transform; const originalTransform = currentTransform === 'none' ? 'translate(0px, 0px)' : currentTransform; cameraContainer.style.setProperty('--original-transform', originalTransform); cameraContainer.classList.add('flip'); if (cameraSwitchMask) cameraSwitchMask.style.opacity = 0; this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user'; window.stopCamera(); window.startCamera(); window.switchCameraTimer = setTimeout(() => { if (this.currentFacingMode === 'user') { cameraVideo.style.transform = 'scaleX(-1)'; } else { cameraVideo.style.transform = 'scaleX(1)'; } window.switchCameraTimer = null; cameraContainer.classList.remove('flip'); cameraContainer.style.removeProperty('--original-transform'); if (cameraSwitchMask) cameraSwitchMask.style.opacity = 1; }, 500); } }; window.takePhoto = (question = 'Describe what you see in the photo.') => { return new Promise(async (resolve) => { const canvas = document.createElement('canvas'); const video = cameraVideo; if (!video || video.readyState !== video.HAVE_ENOUGH_DATA) { log('Cannot take a photo because the camera is not ready.', 'warning'); resolve({ success: false, error: 'The camera is not ready. Make sure you are connected and the camera has started.' }); return; } canvas.width = video.videoWidth || 180; canvas.height = video.videoHeight || 240; const ctx = canvas.getContext('2d'); ctx.drawImage(video, 0, 0, canvas.width, canvas.height); const photoData = canvas.toDataURL('image/jpeg', 0.8); log(`Photo captured successfully. Payload length: ${photoData.length}`, 'success'); try { const xz_tester_vision = localStorage.getItem('xz_tester_vision'); if (xz_tester_vision) { let visionInfo = null; try { visionInfo = JSON.parse(xz_tester_vision); } catch (err) { throw new Error('Failed to parse the stored vision configuration.'); } const { url, token } = visionInfo || {}; if (!url || !token) { throw new Error('Vision analysis is not configured correctly. Missing url or token.'); } log(`Sending the image to the vision endpoint: ${url}`, 'info'); const deviceId = document.getElementById('deviceMac')?.value || ''; const clientId = document.getElementById('clientId')?.value || 'web_test_client'; const formData = new FormData(); formData.append('question', question); formData.append('image', dataURItoBlob(photoData), 'photo.jpg'); const response = await fetch(url, { method: 'POST', body: formData, headers: { 'Device-Id': deviceId, 'Client-Id': clientId, 'Authorization': `Bearer ${token}` } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const analysisResult = await response.json(); log(`Vision analysis completed: ${JSON.stringify(analysisResult).substring(0, 200)}...`, 'success'); resolve({ success: true, message: question, photo_data: photoData, photo_width: canvas.width, photo_height: canvas.height, vision_analysis: analysisResult }); } else { log('No vision analysis service is configured.', 'warning'); } } catch (error) { log(`Vision analysis failed: ${error.message}`, 'error'); resolve({ success: true, message: question, photo_data: photoData, photo_width: canvas.width, photo_height: canvas.height, vision_analysis: { success: false, error: error.message, fallback: 'Could not connect to the vision analysis service.' } }); } }); }; log('Camera initialization completed.', 'success'); resolve(true); }); } } // Create and start the application. const app = new App(); // Expose the app instance globally for the rest of the test harness. window.chatApp = app; document.addEventListener('DOMContentLoaded', () => { // Initialize the application. app.init(); }); export default app;