@@ -24,6 +24,11 @@ interface UseLayercodeAgentOptions {
2424 onAudioInputChanged ?: ( audioInput : boolean ) => void ;
2525 onAudioOutputChanged ?: ( audioOutput : boolean ) => void ;
2626 enableAmplitudeMonitoring ?: boolean ;
27+ /**
28+ * When false, skips microphone device enumeration/watchers until audio input is enabled.
29+ * This prevents getUserMedia from being invoked before the user opts into voice mode.
30+ */
31+ autoLoadInputDevices ?: boolean ;
2732}
2833
2934const normalizeDeviceId = ( deviceId ?: string | null ) : string | null => {
@@ -63,6 +68,7 @@ const useLayercodeAgent = (
6368 } = options ;
6469 const websocketUrlOverride = options [ '_websocketUrl' ] ;
6570 const enableAmplitudeMonitoring = options . enableAmplitudeMonitoring ?? true ;
71+ const autoLoadInputDevices = options . autoLoadInputDevices ?? true ;
6672
6773 const [ status , setStatus ] = useState ( 'initializing' ) ;
6874 const [ userAudioAmplitude , setUserAudioAmplitude ] = useState ( 0 ) ;
@@ -83,6 +89,7 @@ const useLayercodeAgent = (
8389 const mountedRef = useRef ( true ) ;
8490 // Reference to the LayercodeClient instance
8591 const clientRef = useRef < LayercodeClient | null > ( null ) ;
92+ const shouldManageInputDevices = autoLoadInputDevices || audioInput ;
8693
8794 const refreshInputDevices = useCallback ( async ( ) => {
8895 if ( typeof window === 'undefined' || typeof navigator === 'undefined' ) {
@@ -148,21 +155,17 @@ const useLayercodeAgent = (
148155 } , [ preferredInputDeviceId ] ) ;
149156
150157 useEffect ( ( ) => {
151- // Only refresh input devices if audioInput is enabled
152- // This prevents requesting microphone permissions when audio is disabled
153- if ( typeof window === 'undefined' || ! audioInput ) {
158+ if ( typeof window === 'undefined' || ! shouldManageInputDevices ) {
154159 return ;
155160 }
156161
157162 refreshInputDevices ( ) . catch ( ( error ) => {
158163 console . warn ( 'Layercode: failed to load microphone devices' , error ) ;
159164 } ) ;
160- } , [ refreshInputDevices , audioInput ] ) ;
165+ } , [ refreshInputDevices , shouldManageInputDevices ] ) ;
161166
162167 useEffect ( ( ) => {
163- // Only watch for device changes if audioInput is enabled
164- // This prevents requesting microphone permissions when audio is disabled
165- if ( typeof window === 'undefined' || typeof navigator === 'undefined' || ! audioInput ) {
168+ if ( ! shouldManageInputDevices || typeof window === 'undefined' || typeof navigator === 'undefined' ) {
166169 return ;
167170 }
168171
@@ -180,7 +183,7 @@ const useLayercodeAgent = (
180183 return ( ) => {
181184 unsubscribe ?.( ) ;
182185 } ;
183- } , [ audioInput ] ) ;
186+ } , [ shouldManageInputDevices ] ) ;
184187
185188 const createClient = useCallback (
186189 ( initialConversationId : string | null ) => {
0 commit comments