@@ -6,23 +6,22 @@ import {
66 AgentState ,
77 BarVisualizer ,
88 DisconnectButton ,
9- LiveKitRoom ,
109 RoomAudioRenderer ,
10+ RoomContext ,
1111 VoiceAssistantControlBar ,
1212 useVoiceAssistant ,
1313} from "@livekit/components-react" ;
1414import { useKrispNoiseFilter } from "@livekit/components-react/krisp" ;
1515import { AnimatePresence , motion } from "framer-motion" ;
16- import { MediaDeviceFailure } from "livekit-client" ;
16+ import { Room , RoomEvent } from "livekit-client" ;
1717import { useCallback , useEffect , useState } from "react" ;
1818import type { ConnectionDetails } from "./api/connection-details/route" ;
1919
2020export default function Page ( ) {
21- const [ connectionDetails , updateConnectionDetails ] = useState < ConnectionDetails | undefined > (
22- undefined
23- ) ;
2421 const [ agentState , setAgentState ] = useState < AgentState > ( "disconnected" ) ;
2522
23+ const [ room ] = useState ( new Room ( ) ) ;
24+
2625 const onConnectButtonClicked = useCallback ( async ( ) => {
2726 // Generate room connection details, including:
2827 // - A random Room name
@@ -38,29 +37,29 @@ export default function Page() {
3837 window . location . origin
3938 ) ;
4039 const response = await fetch ( url . toString ( ) ) ;
41- const connectionDetailsData = await response . json ( ) ;
42- updateConnectionDetails ( connectionDetailsData ) ;
43- } , [ ] ) ;
40+ const connectionDetailsData : ConnectionDetails = await response . json ( ) ;
41+
42+ await room . connect ( connectionDetailsData . serverUrl , connectionDetailsData . participantToken ) ;
43+ } , [ room ] ) ;
44+
45+ useEffect ( ( ) => {
46+ room . on ( RoomEvent . MediaDevicesError , onDeviceFailure ) ;
47+
48+ return ( ) => {
49+ room . off ( RoomEvent . MediaDevicesError , onDeviceFailure ) ;
50+ } ;
51+ } , [ room ] ) ;
4452
4553 return (
4654 < main data-lk-theme = "default" className = "h-full grid content-center bg-[var(--lk-bg)]" >
47- < LiveKitRoom
48- token = { connectionDetails ?. participantToken }
49- serverUrl = { connectionDetails ?. serverUrl }
50- connect = { connectionDetails !== undefined }
51- audio = { true }
52- video = { false }
53- onMediaDeviceFailure = { onDeviceFailure }
54- onDisconnected = { ( ) => {
55- updateConnectionDetails ( undefined ) ;
56- } }
57- className = "grid grid-rows-[2fr_1fr] items-center"
58- >
59- < SimpleVoiceAssistant onStateChange = { setAgentState } />
60- < ControlBar onConnectButtonClicked = { onConnectButtonClicked } agentState = { agentState } />
61- < RoomAudioRenderer />
62- < NoAgentNotification state = { agentState } />
63- </ LiveKitRoom >
55+ < RoomContext . Provider value = { room } >
56+ < div className = "lk-room-container grid grid-rows-[2fr_1fr] items-center" >
57+ < SimpleVoiceAssistant onStateChange = { setAgentState } />
58+ < ControlBar onConnectButtonClicked = { onConnectButtonClicked } agentState = { agentState } />
59+ < RoomAudioRenderer />
60+ < NoAgentNotification state = { agentState } />
61+ </ div >
62+ </ RoomContext . Provider >
6463 </ main >
6564 ) ;
6665}
@@ -129,7 +128,7 @@ function ControlBar(props: { onConnectButtonClicked: () => void; agentState: Age
129128 ) ;
130129}
131130
132- function onDeviceFailure ( error ?: MediaDeviceFailure ) {
131+ function onDeviceFailure ( error : Error ) {
133132 console . error ( error ) ;
134133 alert (
135134 "Error acquiring camera or microphone permissions. Please make sure you grant the necessary permissions in your browser and reload the tab"
0 commit comments