ripe-address-68319
11/02/2025, 5:18 AM'use client';
import React, { useState, useEffect, useCallback, useRef } from 'react';
import {
LiveKitRoom,
useVoiceAssistant,
RoomAudioRenderer,
BarVisualizer,
useConnectionState,
useRoomContext,
VoiceAssistantControlBar,
ConnectionState as LiveKitConnectionState,
AudioTrack,
DisconnectButton,
} from '@livekit/components-react';
import { ConnectionState, Track, RoomEvent, RemoteParticipant, DataPacket_Kind } from 'livekit-client';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Mic, MicOff, PhoneOff, Loader2, Radio, Brain, Ear } from 'lucide-react';
import { cn } from '@/lib/utils';
import '@livekit/components-styles';
import { Logo } from '@/src/app/[locale]/(landing-public)/_components/logo';
interface VoiceChatInterfaceProps {
token: string;
wsUrl: string;
onDisconnect: () => void;
}
function VoiceAssistantControls({ onDisconnect }: { onDisconnect: () => void }) {
const connectionState = useConnectionState();
const room = useRoomContext();
const { state, audioTrack } = useVoiceAssistant();
const [isMuted, setIsMuted] = useState(false);
const [transcripts, setTranscripts] = useState<{role: string; content: string}[]>([]);
const messagesRef = useRef<HTMLDivElement>(null);
return (
<div className="flex flex-col h-full bg-gradient-to-b from-background to-brand/5">
{/* ----------------------------- Connection Status ----------------------------- */}
<div className="flex items-center justify-between px-6 py-4 backdrop-blur-lg border-b border-border">
<div className="flex items-center gap-3">
<div
className={cn(
'w-3 h-3 rounded-full transition-colors',
getConnectionColor(),
connectionState === ConnectionState.Connecting && 'animate-pulse'
)}
/>
<span className="text-sm font-medium text-foreground">
{connectionState === ConnectionState.Connected
? 'Connected'
: connectionState === ConnectionState.Connecting
? 'Connecting...'
: connectionState === ConnectionState.Reconnecting
? 'Reconnecting...'
: 'Disconnected'}
</span>
</div>
<StatusIndicator state={state} connectionState={connectionState} />
</div>
<div className="flex-1 flex flex-col items-center justify-start px-8 py-12 space-y-8 overflow-y-auto">
<AnimatedLogo state={state} connectionState={connectionState} />
<SoundWaveVisualizer state={state} audioTrack={audioTrack} />
{connectionState === ConnectionState.Connecting && (
<div className="flex flex-col items-center gap-3 text-slate-600 dark:text-slate-400">
<Loader2 className="w-6 h-6 animate-spin text-amber-500" />
<p className="text-sm font-medium">Establishing connection...</p>
</div>
)}
{state === 'listening' && connectionState === ConnectionState.Connected && (
<div className="text-center space-y-2">
<p className="text-lg font-semibold text-blue-600 dark:text-blue-400">I'm listening</p>
<p className="text-sm text-slate-600 dark:text-slate-400">
Speak now to continue the conversation
</p>
</div>
)}
{state === 'thinking' && (
<div className="text-center space-y-2">
<p className="text-lg font-semibold text-violet-600 dark:text-violet-400">
Processing your request
</p>
<p className="text-sm text-slate-600 dark:text-slate-400">Give me a moment to think</p>
</div>
)}
{state === 'speaking' && (
<div className="text-center space-y-2">
<p className="text-lg font-semibold text-emerald-600 dark:text-emerald-400">
I'm speaking
</p>
<p className="text-sm text-slate-600 dark:text-slate-400">
Listen carefully to my response
</p>
</div>
)}
{/* Live Transcripts */}
<div
ref={messagesRef}
className="w-full max-w-2xl overflow-y-auto max-h-96 mt-4 space-y-4 flex flex-col"
>
{transcripts.map((msg, i) => (
<div
key={i}
className={cn(
"p-3 rounded-lg max-w-[80%]",
msg.role === 'user' ? 'bg-blue-100 text-blue-900 self-end' : 'bg-gray-100 text-gray-900 self-start'
)}
>
<p className="font-bold">{msg.role === 'user' ? 'You' : 'Assistant'}</p>
<p>{msg.content}</p>
</div>
))}
</div>
</div>
<div className="px-8 py-6 bg-white/80 dark:bg-slate-900/80 backdrop-blur-lg border-t border-slate-200 dark:border-slate-800">
<div className="flex items-center justify-center gap-6 mb-4">
<Button
size="lg"
variant={isMuted ? 'destructive' : 'outline'}
className={cn(
'w-16 h-16 rounded-full shadow-lg transition-all duration-300',
!isMuted && 'hover:scale-110 hover:shadow-xl'
)}
onClick={toggleMute}
disabled={connectionState !== ConnectionState.Connected}
>
{isMuted ? <MicOff className="w-6 h-6" /> : <Mic className="w-6 h-6" />}
</Button>
<Button
size="lg"
variant="destructive"
className="w-20 h-20 rounded-full shadow-xl hover:scale-110 transition-all duration-300 bg-red-500 hover:bg-red-600"
onClick={onDisconnect}
>
<PhoneOff className="w-8 h-8" />
</Button>
</div>
{/* <VoiceAssistantControlBar className="" /> */}
<p className="text-center text-xs text-slate-500 dark:text-slate-400">
{isMuted ? 'Unmute to speak' : 'Tap the phone icon to end the call'}
</p>
</div>
</div>
);
}
export function VoiceChatInterface({ token, wsUrl, onDisconnect }: VoiceChatInterfaceProps) {
const [error, setError] = useState<string | null>(null);
const handleError = useCallback((error: Error) => {
console.error('LiveKit room error:', error);
setError(error.message);
}, []);
if (error) {
return (
<div className="flex flex-col items-center justify-center h-full p-8 bg-gradient-to-b from-slate-50 to-slate-100 dark:from-slate-950 dark:to-slate-900">
<Card className="max-w-md shadow-xl">
<CardContent className="p-8">
<div className="flex flex-col items-center text-center space-y-4">
<div className="w-16 h-16 rounded-full bg-red-100 dark:bg-red-900/30 flex items-center justify-center">
<PhoneOff className="w-8 h-8 text-red-600 dark:text-red-400" />
</div>
<div>
<h3 className="text-xl font-semibold text-slate-900 dark:text-slate-100 mb-2">
Connection Error
</h3>
<p className="text-sm text-slate-600 dark:text-slate-400">{error}</p>
</div>
<Button onClick={onDisconnect} variant="outline" className="w-full mt-4">
Close
</Button>
</div>
</CardContent>
</Card>
</div>
);
}
return (
<LiveKitRoom
token={token}
serverUrl={wsUrl}
connect={true}
audio={true}
video={false}
onError={handleError}
onDisconnected={reason => {
console.log('[VoiceChatInterface] Disconnected:', reason);
}}
className="h-full"
>
<DisconnectButton className="btn btn-sm bg-red-500 text-white hover:bg-red-600" />
<VoiceAssistantControls onDisconnect={onDisconnect} />
<RoomAudioRenderer />
</LiveKitRoom>
);
}
package.json:
"@livekit/components-react": "^2.9.15",
"@livekit/components-styles": "^1.1.6",tall-airline-16756
11/02/2025, 5:19 AMtall-airline-16756
11/02/2025, 5:19 AMBarVisualizer and DisconnectButton not showing up. Let's work through some common steps together to get this sorted out:
1. Ensure Styles Are Imported: These components depend on CSS styles, so please double-check that you’ve included import '@livekit/components-styles'; in your app, preferably in a central place like App.tsx.
2. Check Context Provider: Components such as BarVisualizer and DisconnectButton need to be used inside a LiveKitRoom or RoomContext.Provider. Make sure they are properly wrapped to work correctly.
3. Verify Audio Track: The BarVisualizer requires a valid audio track. You can use console.log to see if the audioTrack from useVoiceAssistant() is present and being passed correctly.
4. Conditional Rendering: Sometimes these components might be hidden based on connection state or other conditions. For example, BarVisualizer might not show if the room is still connecting. It’s worth checking this.
5. RoomAudioRenderer: Don’t forget to include <RoomAudioRenderer /> inside your LiveKitRoom to enable audio playback and visualization.
6. Styling Issues: If the components render but aren’t visible, there might be CSS conflicts or missing styles. You can customize styles using CSS variables or classes if needed.
7. DisconnectButton: Remember to pass children (like text or an icon) to the DisconnectButton, since it won’t render anything without content.
If you want to dig deeper, try logging the connection state and audio track availability. And please don’t hesitate to share any console logs or error messages you’re seeing — I’m here to help you figure this out!
Sources: Community & Support | livekit/livekit_composite | DeepWiki | livekit-rtc/jupyter-html/src/App.tsx | How do I configure BarVisualizer in React? | Web and mobile frontends | LiveKit Docs | avatars/tavus/voice-assistant-frontend/app/page.tsx