Skip to content

Commit c45c0cd

Browse files
authored
Disable mic enumeration until audio input is enabled, wire voice button to refresh devices. (#18)
1 parent 4cf5782 commit c45c0cd

File tree

3 files changed

+42
-91
lines changed

3 files changed

+42
-91
lines changed

package-lock.json

Lines changed: 27 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
"access": "public"
4040
},
4141
"dependencies": {
42+
<<<<<<< HEAD
43+
"@layercode/js-sdk": "^2.8.4"
44+
=======
4245
"@layercode/js-sdk": "2.8.3"
46+
>>>>>>> main
4347
},
4448
"devDependencies": {
4549
"@rollup/plugin-commonjs": "^25.0.8",

src/index.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2934
const 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

Comments
 (0)