Skip to content

Commit f03b03e

Browse files
committed
make the mic not open in text mode
1 parent c86e724 commit f03b03e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/index.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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';
33

44
/**
55
* Configuration options for the useLayercodeAgent hook.
@@ -98,7 +98,7 @@ const useLayercodeAgent = (
9898

9999
setIsInputDeviceListLoading(true);
100100
try {
101-
const devices = await listAudioInputDevices();
101+
const devices = await listAudioInputDevices({ requestPermission: false });
102102
if (!mountedRef.current) {
103103
return devices;
104104
}
@@ -321,8 +321,15 @@ const useLayercodeAgent = (
321321
_setAudioInput(state);
322322
const next = typeof state === 'function' ? (state as (prev: boolean) => boolean)(audioInput) : state;
323323
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+
}
324331
},
325-
[_setAudioInput, clientRef, audioInput]
332+
[_setAudioInput, clientRef, audioInput, refreshInputDevices]
326333
);
327334

328335
const setAudioOutput = useCallback(
@@ -382,12 +389,18 @@ const useLayercodeAgent = (
382389
try {
383390
await client.setPreferredInputDevice(preferredInputDeviceRef.current);
384391
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+
}
385398
} catch (error) {
386399
console.error('Failed to connect to agent:', error);
387400
onError?.(error as Error);
388401
throw error;
389402
}
390-
}, [createClient, internalConversationId, onError]);
403+
}, [createClient, internalConversationId, onError, refreshInputDevices]);
391404
const disconnect = useCallback(async () => {
392405
if (!clientRef.current) {
393406
return;

0 commit comments

Comments
 (0)