| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { |
| RTVIClient, |
| RTVIClientOptions, |
| RTVIEvent, |
| } from '@pipecat-ai/client-js'; |
| import { |
| WebSocketTransport |
| } from "@pipecat-ai/websocket-transport"; |
|
|
| class WebsocketClientApp { |
| private rtviClient: RTVIClient | null = null; |
| private connectBtn: HTMLButtonElement | null = null; |
| private disconnectBtn: HTMLButtonElement | null = null; |
| private muteBtn: HTMLButtonElement | null = null; |
| private resetBtn: HTMLButtonElement | null = null; |
| private serverSelect: HTMLSelectElement | null = null; |
| private statusSpan: HTMLElement | null = null; |
| private debugLog: HTMLElement | null = null; |
| private volumeBar: HTMLElement | null = null; |
| private volumeText: HTMLElement | null = null; |
| private botAudio: HTMLAudioElement; |
| private isConnecting: boolean = false; |
| private isDisconnecting: boolean = false; |
| private isMuted: boolean = false; |
| private audioContext: AudioContext | null = null; |
| private analyser: AnalyserNode | null = null; |
| private microphone: MediaStreamAudioSourceNode | null = null; |
| private volumeUpdateInterval: number | null = null; |
| private currentBotMessageElement: HTMLDivElement | null = null; |
| private currentBotMessage: string = ''; |
|
|
| |
| private readonly serverConfigs = { |
| websocket: { |
| name: 'WebSocket Server', |
| baseUrl: `http://${window.location.hostname}:7860`, |
| port: 8765 |
| }, |
| fastapi: { |
| name: 'FastAPI Server', |
| baseUrl: `http://${window.location.hostname}:8000`, |
| port: 8000 |
| } |
| }; |
|
|
| constructor() { |
| console.log("WebsocketClientApp"); |
| this.botAudio = document.createElement('audio'); |
| this.botAudio.autoplay = true; |
| |
| document.body.appendChild(this.botAudio); |
|
|
| this.setupDOMElements(); |
| this.setupEventListeners(); |
| } |
|
|
| |
| |
| |
| private setupDOMElements(): void { |
| this.connectBtn = document.getElementById('connect-btn') as HTMLButtonElement; |
| this.disconnectBtn = document.getElementById('disconnect-btn') as HTMLButtonElement; |
| this.muteBtn = document.getElementById('mute-btn') as HTMLButtonElement; |
| this.resetBtn = document.getElementById('reset-btn') as HTMLButtonElement; |
| this.serverSelect = document.getElementById('server-select') as HTMLSelectElement; |
| this.statusSpan = document.getElementById('connection-status'); |
| this.debugLog = document.getElementById('debug-log'); |
| this.volumeBar = document.getElementById('volume-bar'); |
| this.volumeText = document.getElementById('volume-text'); |
| } |
|
|
| |
| |
| |
| private setupEventListeners(): void { |
| this.connectBtn?.addEventListener('click', () => this.connect()); |
| this.disconnectBtn?.addEventListener('click', () => this.disconnect()); |
| this.muteBtn?.addEventListener('click', () => this.toggleMute()); |
| this.resetBtn?.addEventListener('click', () => this.reset()); |
| this.serverSelect?.addEventListener('change', () => this.updateServerUrl()); |
| } |
|
|
| |
| |
| |
| private log(message: string): void { |
| if (!this.debugLog) return; |
| const entry = document.createElement('div'); |
| entry.textContent = `${new Date().toISOString()} - ${message}`; |
| if (message.startsWith('User: ')) { |
| entry.style.color = '#2196F3'; |
| } else if (message.startsWith('Bot: ')) { |
| entry.style.color = '#4CAF50'; |
| } |
| this.debugLog.appendChild(entry); |
| this.debugLog.scrollTop = this.debugLog.scrollHeight; |
| console.log(message); |
| } |
|
|
| |
| |
| |
| private createBotMessageElement(initialText: string): HTMLDivElement | null { |
| if (!this.debugLog) return null; |
| const entry = document.createElement('div'); |
| entry.style.color = '#4CAF50'; |
| entry.textContent = `${new Date().toISOString()} - ${initialText}`; |
| this.debugLog.appendChild(entry); |
| this.debugLog.scrollTop = this.debugLog.scrollHeight; |
| return entry; |
| } |
|
|
| |
| |
| |
| private updateStatus(status: string): void { |
| if (this.statusSpan) { |
| this.statusSpan.textContent = status; |
| } |
| this.log(`Status: ${status}`); |
| } |
|
|
| |
| |
| |
| |
| setupMediaTracks() { |
| if (!this.rtviClient) return; |
| const tracks = this.rtviClient.tracks(); |
| if (tracks.bot?.audio) { |
| this.setupAudioTrack(tracks.bot.audio); |
| } |
| } |
|
|
| |
| |
| |
| |
| setupTrackListeners() { |
| if (!this.rtviClient) { |
| this.log('Cannot setup track listeners: client is null'); |
| return; |
| } |
|
|
| try { |
| |
| this.rtviClient.on(RTVIEvent.TrackStarted, (track, participant) => { |
| |
| if (!participant?.local && track.kind === 'audio') { |
| this.setupAudioTrack(track); |
| } |
| }); |
|
|
| |
| this.rtviClient.on(RTVIEvent.TrackStopped, (track, participant) => { |
| this.log(`Track stopped: ${track.kind} from ${participant?.name || 'unknown'}`); |
| }); |
| } catch (error) { |
| this.log(`Error setting up track listeners: ${error}`); |
| } |
| } |
|
|
| |
| |
| |
| |
| private setupAudioTrack(track: MediaStreamTrack): void { |
| this.log('Setting up audio track'); |
| if (this.botAudio.srcObject && "getAudioTracks" in this.botAudio.srcObject) { |
| const oldTrack = this.botAudio.srcObject.getAudioTracks()[0]; |
| if (oldTrack?.id === track.id) return; |
| } |
| this.botAudio.srcObject = new MediaStream([track]); |
| } |
|
|
| |
| |
| |
| |
| public async connect(): Promise<void> { |
| if (this.isConnecting) { |
| this.log('Connection already in progress, ignoring...'); |
| return; |
| } |
|
|
| try { |
| this.isConnecting = true; |
| const startTime = Date.now(); |
|
|
| |
| const transport = new WebSocketTransport(); |
| const RTVIConfig: RTVIClientOptions = { |
| transport, |
| params: { |
| |
| baseUrl: this.getSelectedServerConfig().baseUrl, |
| endpoints: { connect: '/connect' }, |
| }, |
| enableMic: true, |
| enableCam: false, |
| callbacks: { |
| onConnected: () => { |
| this.updateStatus('Connected'); |
| if (this.connectBtn) this.connectBtn.disabled = true; |
| if (this.disconnectBtn) this.disconnectBtn.disabled = false; |
| if (this.muteBtn) { |
| this.muteBtn.disabled = false; |
| this.muteBtn.textContent = 'Mute'; |
| } |
| if (this.resetBtn) this.resetBtn.disabled = false; |
| if (this.serverSelect) this.serverSelect.disabled = true; |
| |
| if (!this.isMuted) { |
| this.startVolumeMonitoring(); |
| } |
| }, |
| onDisconnected: () => { |
| |
| if (!this.isConnecting) { |
| this.updateStatus('Disconnected'); |
| if (this.connectBtn) this.connectBtn.disabled = false; |
| if (this.disconnectBtn) this.disconnectBtn.disabled = true; |
| if (this.muteBtn) { |
| this.muteBtn.disabled = true; |
| this.muteBtn.textContent = 'Mute'; |
| } |
| if (this.resetBtn) this.resetBtn.disabled = true; |
| if (this.serverSelect) this.serverSelect.disabled = false; |
| |
| this.stopVolumeMonitoring(); |
| this.log('Client disconnected'); |
| } |
| }, |
| onBotReady: (data) => { |
| this.log(`Bot ready: ${JSON.stringify(data)}`); |
| this.setupMediaTracks(); |
| }, |
| onUserTranscript: (data) => { |
| if (data.final) { |
| this.log(`User: ${data.text}`); |
| } |
| }, |
| onBotTranscript: (data) => { |
| |
| if (!this.currentBotMessageElement) { |
| this.currentBotMessage = ''; |
| this.currentBotMessageElement = this.createBotMessageElement('Bot: '); |
| } |
| |
| |
| this.currentBotMessage += data.text; |
| |
| |
| if (this.currentBotMessageElement) { |
| const timestamp = new Date().toISOString(); |
| this.currentBotMessageElement.textContent = `${timestamp} - Bot: ${this.currentBotMessage}`; |
| this.debugLog?.scrollTo({ top: this.debugLog.scrollHeight, behavior: 'smooth' }); |
| } |
| }, |
| onBotLlmStarted: () => { |
| |
| if (this.currentBotMessage !== '') { |
| this.currentBotMessage = ''; |
| this.currentBotMessageElement = this.createBotMessageElement('Bot: '); |
| } else if (!this.currentBotMessageElement) { |
| |
| this.currentBotMessage = ''; |
| this.currentBotMessageElement = this.createBotMessageElement('Bot: '); |
| } |
| }, |
| onMessageError: (error) => console.error('Message error:', error), |
| onError: (error) => console.error('Error:', error), |
| }, |
| } |
| |
| |
| try { |
| this.rtviClient = new RTVIClient(RTVIConfig); |
| this.setupTrackListeners(); |
| } catch (clientError) { |
| this.log(`Error creating RTVI client: ${clientError}`); |
| throw clientError; |
| } |
|
|
| this.log('Initializing devices...'); |
| await this.rtviClient.initDevices(); |
| this.log('Devices initialized successfully'); |
|
|
| this.log('Connecting to bot...'); |
| await this.rtviClient.connect(); |
|
|
| const timeTaken = Date.now() - startTime; |
| this.log(`Connection complete, timeTaken: ${timeTaken}`); |
| } catch (error) { |
| this.log(`Error connecting: ${(error as Error).message}`); |
| this.updateStatus('Error'); |
| |
| await this.cleanupOnError(); |
| } finally { |
| this.isConnecting = false; |
| } |
| } |
|
|
| |
| |
| |
| private async cleanupOnError(): Promise<void> { |
| |
| this.isDisconnecting = true; |
| |
| |
| const client = this.rtviClient; |
| |
| if (client) { |
| try { |
| |
| if (typeof client.disconnect === 'function') { |
| await client.disconnect(); |
| } |
| } catch (disconnectError) { |
| this.log(`Error during cleanup disconnect: ${disconnectError}`); |
| } finally { |
| |
| this.rtviClient = null; |
| } |
| } else { |
| this.log('Client was already null during cleanup'); |
| } |
| |
| |
| if (this.connectBtn) this.connectBtn.disabled = false; |
| if (this.disconnectBtn) this.disconnectBtn.disabled = true; |
| if (this.muteBtn) { |
| this.muteBtn.disabled = true; |
| this.muteBtn.textContent = 'Mute'; |
| } |
| if (this.resetBtn) this.resetBtn.disabled = true; |
| if (this.serverSelect) this.serverSelect.disabled = false; |
| |
| |
| this.stopVolumeMonitoring(); |
| |
| |
| this.currentBotMessage = ''; |
| this.currentBotMessageElement = null; |
| |
| |
| this.isMuted = false; |
| |
| |
| this.isDisconnecting = false; |
| } |
|
|
| |
| |
| |
| public async disconnect(): Promise<void> { |
| if (this.isDisconnecting) { |
| this.log('Disconnection already in progress, ignoring...'); |
| return; |
| } |
|
|
| this.isDisconnecting = true; |
| |
| |
| const client = this.rtviClient; |
| |
| if (client) { |
| try { |
| |
| if (typeof client.disconnect === 'function') { |
| await client.disconnect(); |
| } |
| } catch (error) { |
| this.log(`Error disconnecting: ${(error as Error).message}`); |
| } finally { |
| |
| this.rtviClient = null; |
| if (this.botAudio.srcObject && "getAudioTracks" in this.botAudio.srcObject) { |
| this.botAudio.srcObject.getAudioTracks().forEach((track) => track.stop()); |
| this.botAudio.srcObject = null; |
| } |
| } |
| } else { |
| this.log('Client was already null during disconnect'); |
| } |
| |
| |
| this.stopVolumeMonitoring(); |
| |
| |
| this.currentBotMessage = ''; |
| this.currentBotMessageElement = null; |
| |
| |
| this.isMuted = false; |
| |
| this.isDisconnecting = false; |
| } |
|
|
| |
| |
| |
| private toggleMute(): void { |
| if (!this.rtviClient) { |
| this.log('Cannot toggle mute: client is null'); |
| return; |
| } |
|
|
| this.isMuted = !this.isMuted; |
| this.rtviClient.enableMic(!this.isMuted); |
| |
| |
| if (this.muteBtn) { |
| this.muteBtn.textContent = this.isMuted ? 'Unmute' : 'Mute'; |
| } |
| |
| |
| if (this.isMuted) { |
| this.stopVolumeMonitoring(); |
| } else { |
| this.startVolumeMonitoring(); |
| } |
| |
| this.log(this.isMuted ? 'Microphone muted' : 'Microphone unmuted'); |
| } |
|
|
| |
| |
| |
| private async startVolumeMonitoring(): Promise<void> { |
| try { |
| if (!this.audioContext) { |
| this.audioContext = new AudioContext(); |
| } |
|
|
| const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
| |
| this.analyser = this.audioContext.createAnalyser(); |
| this.analyser.fftSize = 256; |
| this.analyser.smoothingTimeConstant = 0.8; |
| |
| this.microphone = this.audioContext.createMediaStreamSource(stream); |
| this.microphone.connect(this.analyser); |
| |
| |
| this.volumeUpdateInterval = window.setInterval(() => { |
| this.updateVolumeDisplay(); |
| }, 100); |
| |
| this.log('Volume monitoring started'); |
| } catch (error) { |
| this.log(`Error starting volume monitoring: ${error}`); |
| } |
| } |
|
|
| |
| |
| |
| private stopVolumeMonitoring(): void { |
| if (this.volumeUpdateInterval) { |
| clearInterval(this.volumeUpdateInterval); |
| this.volumeUpdateInterval = null; |
| } |
| |
| if (this.microphone) { |
| this.microphone.disconnect(); |
| this.microphone = null; |
| } |
| |
| |
| this.updateVolumeDisplay(0); |
| this.log('Volume monitoring stopped'); |
| } |
|
|
| |
| |
| |
| private updateVolumeDisplay(volume?: number): void { |
| if (!this.volumeBar || !this.volumeText) return; |
|
|
| if (volume === undefined && this.analyser) { |
| const dataArray = new Uint8Array(this.analyser.frequencyBinCount); |
| this.analyser.getByteFrequencyData(dataArray); |
| |
| |
| const average = dataArray.reduce((sum, value) => sum + value, 0) / dataArray.length; |
| volume = (average / 255) * 100; |
| } |
|
|
| const displayVolume = volume || 0; |
| const clampedVolume = Math.min(100, Math.max(0, displayVolume)); |
| |
| this.volumeBar.style.width = `${clampedVolume}%`; |
| this.volumeText.textContent = `${Math.round(clampedVolume)}%`; |
| |
| |
| if (clampedVolume < 30) { |
| this.volumeBar.style.background = '#4caf50'; |
| } else if (clampedVolume < 70) { |
| this.volumeBar.style.background = '#ff9800'; |
| } else { |
| this.volumeBar.style.background = '#f44336'; |
| } |
| } |
|
|
| |
| |
| |
| private async reset(): Promise<void> { |
| if (!this.rtviClient) { |
| this.log('Cannot reset: not connected to server'); |
| return; |
| } |
|
|
| try { |
| this.log('Resetting conversation context...'); |
| |
| |
| const result = await this.rtviClient.action({ service: 'context', action: 'reset', arguments: [] }); |
| |
| if (result) { |
| this.log('Conversation context reset successfully'); |
| } else { |
| this.log('Failed to reset conversation context'); |
| } |
| } catch (error) { |
| this.log(`Error resetting context: ${error}`); |
| } |
| } |
|
|
| private getSelectedServerConfig(): { name: string; baseUrl: string; port: number } { |
| const selectedValue = this.serverSelect?.value || 'websocket'; |
| return this.serverConfigs[selectedValue as keyof typeof this.serverConfigs]; |
| } |
|
|
| private updateServerUrl(): void { |
| const selectedConfig = this.getSelectedServerConfig(); |
| this.log(`Server changed to: ${selectedConfig.name} (${selectedConfig.baseUrl})`); |
| |
| |
| if (this.rtviClient) { |
| this.log('Please disconnect and reconnect to use the new server'); |
| } |
| } |
| } |
|
|
| declare global { |
| interface Window { |
| WebsocketClientApp: typeof WebsocketClientApp; |
| } |
| } |
|
|
| window.addEventListener('DOMContentLoaded', () => { |
| window.WebsocketClientApp = WebsocketClientApp; |
| new WebsocketClientApp(); |
| }); |
|
|