Skip to content

The agent that grows with you

fix(desktop): speak the first reply of a wake-started voice session

Tekniumf9d7be82fb7b
diff --git a/apps/desktop/electron/session-windows.test.ts b/apps/desktop/electron/session-windows.test.ts
index b5f9cc6a8be..a60ebb37621 100644
--- a/apps/desktop/electron/session-windows.test.ts
+++ b/apps/desktop/electron/session-windows.test.ts
@@ -208,3 +208,13 @@ test('chatWindowWebPreferences passes the preload path through and keeps the har
assert.equal(prefs.sandbox, true)
assert.equal(prefs.nodeIntegration, false)
})
+
+test('chatWindowWebPreferences allows autoplay so wake-started voice speaks its first reply', () => {
+ // Regression: Chromium's default autoplay policy suspends audio until a user
+ // gesture. A wake-word-started voice conversation has no preceding click, so
+ // the first reply's playback was rejected and only turn 2+ spoke. A native
+ // app the user launched should not gate audio on a gesture.
+ const prefs = chatWindowWebPreferences('/tmp/preload.cjs')
+
+ assert.equal(prefs.autoplayPolicy, 'no-user-gesture-required')
+})
diff --git a/apps/desktop/electron/session-windows.ts b/apps/desktop/electron/session-windows.ts
index 46871b5384e..312deb3d1e3 100644
--- a/apps/desktop/electron/session-windows.ts
+++ b/apps/desktop/electron/session-windows.ts
@@ -21,6 +21,16 @@ const SESSION_WINDOW_MIN_HEIGHT = 620
// occluded windows. A streaming chat app must keep painting in the
// background, so every chat window opts out. The preload path is injected
// because it depends on the Electron entry's __dirname.
+//
+// `autoplayPolicy: 'no-user-gesture-required'` is load-bearing for voice:
+// Chromium's default autoplay policy suspends audio (HTMLAudioElement.play()
+// and AudioContext) until the user has interacted with the frame. A voice
+// conversation started by the "Hey Hermes" wake word has NO preceding click,
+// so the FIRST reply's audio playback was rejected (NotAllowedError, silently
+// swallowed) and only turn 2+ spoke — the very "first message in a new voice
+// session is silent" bug. Manual voice-start worked only because the button
+// click counted as the gesture. This is a native app the user deliberately
+// launched; there is no drive-by-autoplay concern to protect against.
function chatWindowWebPreferences(preloadPath: string) {
return {
preload: preloadPath,
@@ -29,7 +39,8 @@ function chatWindowWebPreferences(preloadPath: string) {
sandbox: true,
nodeIntegration: false,
devTools: true,
- backgroundThrottling: false
+ backgroundThrottling: false,
+ autoplayPolicy: 'no-user-gesture-required' as const
}
}
diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.ts
index 80d9a3f22a2..d98d6dace79 100644
--- a/apps/desktop/src/lib/voice-playback.ts
+++ b/apps/desktop/src/lib/voice-playback.ts
@@ -20,6 +20,36 @@ let currentAudio: HTMLAudioElement | null = null
let currentStop: (() => void) | null = null
let sequence = 0
+// A shared, lazily-created AudioContext used only to nudge the browser's
+// autoplay state out of "suspended". A wake-word-started voice turn has no
+// preceding user gesture, so the first HTMLAudioElement.play() can be rejected
+// with NotAllowedError. resume()-ing a context is the documented way to recover
+// once the app is allowed to make sound; on Electron chat windows the
+// no-user-gesture-required policy means this is already unlocked, so this is a
+// cheap no-op fallback for other surfaces.
+let unlockCtx: AudioContext | null = null
+
+async function unlockAutoplay(): Promise<void> {
+ if (typeof window === 'undefined') {
+ return
+ }
+
+ const Ctor =
+ window.AudioContext || (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext
+
+ if (!Ctor) {
+ return
+ }
+
+ if (!unlockCtx) {
+ unlockCtx = new Ctor()
+ }
+
+ if (unlockCtx.state === 'suspended') {
+ await unlockCtx.resume()
+ }
+}
+
function currentState(
status: VoicePlaybackState['status'],
options?: VoicePlaybackOptions,
@@ -235,6 +265,14 @@ function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechS
if (frame.type === 'start') {
streamRate = frame.sample_rate || 24_000
context = new AudioContext()
+ // Autoplay policy can hand back a suspended context when playback wasn't
+ // started by a user gesture (e.g. a wake-word-started voice turn). Resume
+ // it so the first reply is audible instead of silently buffering. Electron
+ // chat windows also set autoplayPolicy: no-user-gesture-required, but the
+ // dashboard-embedded surface relies on this resume.
+ if (context.state === 'suspended') {
+ void context.resume().catch(() => undefined)
+ }
nextStartAt = 0
} else if (frame.type === 'end') {
finishWhenDrained()
@@ -363,7 +401,19 @@ async function playSpeechDataUrl(
audio.addEventListener('error', onError, { once: true })
audio.addEventListener('timeupdate', armStall)
armStall()
- void audio.play().catch(onError)
+ // A wake-word-started turn has no user gesture, so the autoplay policy can
+ // reject the first play() with NotAllowedError. Electron chat windows set
+ // autoplayPolicy: no-user-gesture-required to prevent this, but retry once
+ // after resuming a shared AudioContext as a fallback for other surfaces
+ // (dashboard-embedded) so the first reply isn't silently dropped.
+ void audio.play().catch(async () => {
+ try {
+ await unlockAutoplay()
+ await audio.play()
+ } catch {
+ onError()
+ }
+ })
})
if (!isCurrent()) {