```
fix: improve audio playback interruption and response persistence - Remove TTS timeout to prevent premature playback cancellation - Save assistant responses to history before waiting for TTS completion - Stop audio playback when sending message or starting recording - Properly reject current audio promise when stopping playback - Ensure partial responses are saved even if stream is aborted ```
This commit is contained in:
@@ -72,11 +72,10 @@ export async function processUserMessage(params: {
|
||||
// No need to broadcast again here to avoid duplication
|
||||
|
||||
let assistantResponse = "";
|
||||
// Track pending TTS playback promise outside of streamLLM scope
|
||||
let pendingTTS: Promise<void> | null = null;
|
||||
|
||||
try {
|
||||
// Track pending TTS playback promise
|
||||
let pendingTTS: Promise<void> | null = null;
|
||||
|
||||
// Stream LLM response with tool execution
|
||||
assistantResponse = await streamLLM({
|
||||
systemPrompt: getSystemPrompt(),
|
||||
@@ -161,20 +160,35 @@ export async function processUserMessage(params: {
|
||||
}
|
||||
},
|
||||
onFinish: async () => {
|
||||
// Wait for any final pending TTS to complete
|
||||
if (pendingTTS) {
|
||||
await pendingTTS;
|
||||
pendingTTS = null;
|
||||
}
|
||||
// Don't wait for TTS here - we'll handle it after adding to history
|
||||
},
|
||||
});
|
||||
|
||||
// Add assistant response to context
|
||||
// Add assistant response to context IMMEDIATELY after stream completes
|
||||
// This ensures partial responses are saved even if TTS fails or is interrupted
|
||||
conversation.messages.push({
|
||||
role: "assistant",
|
||||
content: assistantResponse,
|
||||
});
|
||||
|
||||
// Now wait for any pending TTS, but don't fail the entire operation if it times out
|
||||
if (pendingTTS) {
|
||||
try {
|
||||
await pendingTTS;
|
||||
} catch (ttsError) {
|
||||
// TTS failed but message is already in history - just log the error
|
||||
console.error("TTS playback failed (message already saved):", ttsError);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// If stream itself failed or was aborted, still save any partial response to history
|
||||
if (assistantResponse) {
|
||||
conversation.messages.push({
|
||||
role: "assistant",
|
||||
content: assistantResponse,
|
||||
});
|
||||
}
|
||||
|
||||
// Broadcast error to WebSocket
|
||||
if (params.wsServer) {
|
||||
params.wsServer.broadcastActivityLog({
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { VoiceAssistantWebSocketServer } from "../websocket-server.js";
|
||||
interface PendingPlayback {
|
||||
resolve: () => void;
|
||||
reject: (error: Error) => void;
|
||||
timeout: NodeJS.Timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -14,11 +13,6 @@ interface PendingPlayback {
|
||||
*/
|
||||
const pendingPlaybacks = new Map<string, PendingPlayback>();
|
||||
|
||||
/**
|
||||
* Timeout for waiting on client playback confirmation (30 seconds)
|
||||
*/
|
||||
const PLAYBACK_TIMEOUT_MS = 30000;
|
||||
|
||||
/**
|
||||
* Generate TTS audio, broadcast to clients, and wait for playback confirmation
|
||||
* Returns a Promise that resolves when the client confirms playback completed
|
||||
@@ -35,14 +29,8 @@ export async function generateTTSAndWaitForPlayback(
|
||||
|
||||
// Create promise that will be resolved when client confirms playback
|
||||
const playbackPromise = new Promise<void>((resolve, reject) => {
|
||||
// Set timeout to prevent hanging forever
|
||||
const timeout = setTimeout(() => {
|
||||
pendingPlaybacks.delete(audioId);
|
||||
reject(new Error(`Audio playback timeout for ${audioId}`));
|
||||
}, PLAYBACK_TIMEOUT_MS);
|
||||
|
||||
// Store handlers
|
||||
pendingPlaybacks.set(audioId, { resolve, reject, timeout });
|
||||
// Store handlers (no timeout - will resolve when client confirms or connection closes)
|
||||
pendingPlaybacks.set(audioId, { resolve, reject });
|
||||
});
|
||||
|
||||
// Broadcast audio to clients
|
||||
@@ -81,8 +69,7 @@ export function confirmAudioPlayed(audioId: string): void {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear timeout and resolve promise
|
||||
clearTimeout(pending.timeout);
|
||||
// Resolve promise and cleanup
|
||||
pending.resolve();
|
||||
pendingPlaybacks.delete(audioId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user