|
1 | 1 | import React, { useState, useEffect, useCallback, useRef } from 'react'; |
2 | | -import LayercodeClient, { type AgentConfig, type AuthorizeSessionRequest, type LayercodeAudioInputDevice, listAudioInputDevices, watchAudioInputDevices } from '@layercode/js-sdk'; |
| 2 | +import LayercodeClient, { type AgentConfig, type AuthorizeSessionRequest, type LayercodeAudioInputDevice, listAudioInputDevices, watchAudioInputDevices } from 'layercode-js-sdk'; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * Configuration options for the useLayercodeAgent hook. |
@@ -98,7 +98,7 @@ const useLayercodeAgent = ( |
98 | 98 |
|
99 | 99 | setIsInputDeviceListLoading(true); |
100 | 100 | try { |
101 | | - const devices = await listAudioInputDevices(); |
| 101 | + const devices = await listAudioInputDevices({ requestPermission: false }); |
102 | 102 | if (!mountedRef.current) { |
103 | 103 | return devices; |
104 | 104 | } |
@@ -321,8 +321,15 @@ const useLayercodeAgent = ( |
321 | 321 | _setAudioInput(state); |
322 | 322 | const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioInput) : state; |
323 | 323 | await clientRef.current?.setAudioInput(next); |
| 324 | + |
| 325 | + // After the mic is enabled (permission granted), refresh device labels without prompting again. |
| 326 | + if (next) { |
| 327 | + refreshInputDevices().catch((error) => { |
| 328 | + console.warn('Layercode: failed to refresh microphones after enabling audio input', error); |
| 329 | + }); |
| 330 | + } |
324 | 331 | }, |
325 | | - [_setAudioInput, clientRef, audioInput] |
| 332 | + [_setAudioInput, clientRef, audioInput, refreshInputDevices] |
326 | 333 | ); |
327 | 334 |
|
328 | 335 | const setAudioOutput = useCallback( |
@@ -382,12 +389,18 @@ const useLayercodeAgent = ( |
382 | 389 | try { |
383 | 390 | await client.setPreferredInputDevice(preferredInputDeviceRef.current); |
384 | 391 | await client.connect(); |
| 392 | + |
| 393 | + if (client.audioInputEnabled) { |
| 394 | + refreshInputDevices().catch((error) => { |
| 395 | + console.warn('Layercode: failed to refresh microphones after connect', error); |
| 396 | + }); |
| 397 | + } |
385 | 398 | } catch (error) { |
386 | 399 | console.error('Failed to connect to agent:', error); |
387 | 400 | onError?.(error as Error); |
388 | 401 | throw error; |
389 | 402 | } |
390 | | - }, [createClient, internalConversationId, onError]); |
| 403 | + }, [createClient, internalConversationId, onError, refreshInputDevices]); |
391 | 404 | const disconnect = useCallback(async () => { |
392 | 405 | if (!clientRef.current) { |
393 | 406 | return; |
|
0 commit comments