Skip to content

The agent that grows with you

fix(webapp): report "microphone unsupported" for live voice on plain-HTTP origins

David Dudok de Wit28dd19ff7b9d
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-mic-recorder.ts b/apps/desktop/src/app/chat/composer/hooks/use-mic-recorder.ts
index 9ad6b38c54f..566751e31dd 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-mic-recorder.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-mic-recorder.ts
@@ -68,6 +68,10 @@ export function micError(error: unknown, copy: MicRecorderErrorCopy): Error {
return new Error(copy.microphoneConstraintsUnsupported)
}
+ if (name === 'NotSupportedError') {
+ return new Error(copy.microphoneUnsupported)
+ }
+
if (error instanceof DOMException) {
return new Error(copy.microphoneStartFailed)
}
diff --git a/apps/desktop/src/lib/voice-barge-in.ts b/apps/desktop/src/lib/voice-barge-in.ts
index 22d5582f10e..2098885d887 100644
--- a/apps/desktop/src/lib/voice-barge-in.ts
+++ b/apps/desktop/src/lib/voice-barge-in.ts
@@ -192,6 +192,12 @@ export function monitorSpeechDuringPlayback(callbacks: BargeMonitorCallbacks): (
active.stop()
}
+
+ // No mediaDevices (an insecure origin): there is no mic to monitor. The conversation's own
+ // listening start already reports that, through the recorder's "microphone unsupported" copy.
+ if (!navigator.mediaDevices?.getUserMedia) {
+ return cleanup
+ }
void (async () => {
try {
// Don't open a second capture context while the recorder's is still
diff --git a/apps/desktop/src/lib/voice-live.ts b/apps/desktop/src/lib/voice-live.ts
index 2352e19b6f5..6bd94066c8c 100644
--- a/apps/desktop/src/lib/voice-live.ts
+++ b/apps/desktop/src/lib/voice-live.ts
@@ -280,6 +280,13 @@ export class VoiceLiveSession {
throw new Error('GPT-Live session already started')
}
+ // An insecure origin (a Webapp opened over plain HTTP from another machine) has no
+ // mediaDevices. Fail before opening a peer, with the error the recorder's copy maps to
+ // "microphone unsupported", instead of a TypeError on the missing API.
+ if (!navigator.mediaDevices?.getUserMedia) {
+ throw new DOMException('Microphone capture is unavailable here', 'NotSupportedError')
+ }
+
const connection = new RTCPeerConnection()
this.peer = connection
diff --git a/apps/desktop/src/lib/voice-microphone-unsupported.test.ts b/apps/desktop/src/lib/voice-microphone-unsupported.test.ts
new file mode 100644
index 00000000000..d24368a135d
--- /dev/null
+++ b/apps/desktop/src/lib/voice-microphone-unsupported.test.ts
@@ -0,0 +1,37 @@
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { micError } from '@/app/chat/composer/hooks/use-mic-recorder'
+import { en } from '@/i18n/en'
+
+import { monitorSpeechDuringPlayback } from './voice-barge-in'
+import { VoiceLiveSession } from './voice-live'
+
+// A Webapp opened over plain HTTP from another machine has no navigator.mediaDevices; nor has jsdom.
+afterEach(() => vi.unstubAllGlobals())
+
+it('live voice fails with "microphone unsupported" without mediaDevices, before opening a peer', async () => {
+ expect(navigator.mediaDevices).toBeUndefined()
+ const peers = vi.fn()
+
+ vi.stubGlobal('RTCPeerConnection', class {
+ constructor() { peers() }
+ addEventListener() {}
+ close() {}
+ })
+
+ const session = new VoiceLiveSession({ onClosed: vi.fn(), onDelegation: vi.fn(), onError: vi.fn() })
+ const failure = await session.start([]).catch((error: unknown) => error)
+
+ expect(micError(failure, en.notifications.voice).message).toBe(en.notifications.voice.microphoneUnsupported)
+ expect(peers).not.toHaveBeenCalled()
+})
+
+it('barge-in never reaches for a microphone it cannot have', () => {
+ const audioContext = vi.fn()
+ vi.stubGlobal('AudioContext', audioContext)
+
+ const stop = monitorSpeechDuringPlayback({ onSpeech: vi.fn(), onUtterance: vi.fn() })
+
+ expect(audioContext).not.toHaveBeenCalled()
+ stop()
+})