Skip to content

Commit d099d33

Browse files
rayl15claude
andcommitted
Fix always-listening bug when session ends
- Check isSessionActive before processing commands in onCommandCaptured - Check isSessionActive before speaking responses in onAgentMessage - Set isSessionActive=false BEFORE handling voice service to prevent race - Stop VoiceCommandService entirely when wake word is disabled This fixes the bug where the app would continue listening and speaking responses after the conversation session had ended. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9733256 commit d099d33

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

OpenVision/Views/VoiceAgent/VoiceAgentView.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,19 @@ struct VoiceAgentView: View {
534534
// Stop any ongoing TTS
535535
ttsService.stop()
536536

537-
// Exit conversation mode but keep listening for wake word
538-
voiceCommandService.exitConversationMode()
539-
537+
// Set session inactive FIRST to prevent callbacks from processing
540538
isSessionActive = false
541539
agentState = .idle
540+
541+
// Handle voice command service based on wake word setting
542+
if settingsManager.settings.wakeWordEnabled {
543+
// Exit conversation mode but keep listening for wake word
544+
voiceCommandService.exitConversationMode()
545+
} else {
546+
// Wake word disabled - stop listening entirely to prevent
547+
// processing speech after session ends
548+
voiceCommandService.stopListening()
549+
}
542550
userTranscript = ""
543551
aiTranscript = ""
544552
currentToolName = nil
@@ -608,6 +616,14 @@ struct VoiceAgentView: View {
608616
// Command captured
609617
voiceCommandService.onCommandCaptured = { (command: String) in
610618
print("[VoiceAgentView] Command captured: \(command)")
619+
620+
// IMPORTANT: Only process commands when session is active
621+
// This prevents processing stale commands after session ends
622+
guard self.isSessionActive else {
623+
print("[VoiceAgentView] Ignoring command - session not active")
624+
return
625+
}
626+
611627
self.userTranscript = command
612628

613629
// Send command to AI backend
@@ -651,6 +667,14 @@ struct VoiceAgentView: View {
651667
// OpenClaw callbacks
652668
OpenClawService.shared.onAgentMessage = { (message: String) in
653669
print("[VoiceAgentView] Received AI message: \(message.prefix(50))...")
670+
671+
// IMPORTANT: Only speak responses when session is active
672+
// This prevents speaking stale responses after session ends
673+
guard self.isSessionActive else {
674+
print("[VoiceAgentView] Ignoring AI message - session not active")
675+
return
676+
}
677+
654678
self.aiTranscript = message
655679

656680
// Speak the response via TTS

0 commit comments

Comments
 (0)