diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 9fc827c01..1b8bc4675 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -1535,7 +1535,8 @@ class ConnectionHandler: 2. TTS must be speaking (client_is_speaking = True) 3. Not in manual listen mode 4. Speech duration >= min_interrupt_speech_duration_ms - 5. ASR text buffer length >= min_interrupt_text_length + 5. For streaming ASR: text buffer length >= min_interrupt_text_length + For non-streaming ASR: skip text check (not available during speech) Args: speech_duration_ms: Current speech duration in milliseconds @@ -1547,32 +1548,50 @@ class ConnectionHandler: if self.client_listen_mode == "manual": return - # Check if meeting interruption thresholds - words = tokenize.split_words( - self.asr_text_buffer, - ignore_punctuation=True, - split_character=True, - retain_format=False, - ) - asr_text_len = len(words) + # Check speech duration threshold speech_ok = speech_duration_ms >= self.min_interrupt_speech_duration_ms - text_ok = asr_text_len >= self.min_interrupt_text_length + if not speech_ok: + return - if speech_ok and text_ok: - self.logger.bind(tag=TAG).info( - f"Interrupt triggered: speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms, " + # Check text length threshold (only for streaming ASR) + # Non-streaming ASR doesn't have real-time text during speech + from core.providers.asr.dto import InterfaceType + is_streaming_asr = ( + self.asr is not None + and hasattr(self.asr, 'interface_type') + and self.asr.interface_type == InterfaceType.STREAM + ) + + if is_streaming_asr: + words = tokenize.split_words( + self.asr_text_buffer, + ignore_punctuation=True, + split_character=True, + retain_format=False, + ) + asr_text_len = len(words) + text_ok = asr_text_len >= self.min_interrupt_text_length + if not text_ok: + return + log_msg = ( + f"Interrupt triggered (streaming): speech={speech_duration_ms:.0f}ms, " f"text_len={asr_text_len} >= {self.min_interrupt_text_length}" ) - # Trigger interrupt - self.client_abort = True - self.clear_queues() - # Send stop message to client - async def send_stop_message(): - await self.websocket.send( - json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}) - ) - asyncio.create_task(send_stop_message()) - self.clearSpeakStatus() + else: + log_msg = f"Interrupt triggered (non-streaming): speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms" + + self.logger.bind(tag=TAG).info(log_msg) + + # Trigger interrupt + self.client_abort = True + self.clear_queues() + # Send stop message to client + async def send_stop_message(): + await self.websocket.send( + json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}) + ) + asyncio.create_task(send_stop_message()) + self.clearSpeakStatus() def chat_and_close(self, text): """Chat with the user and then close the connection""" diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index effc6195c..722b60bd3 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -14,7 +14,7 @@ from config.logger import setup_logging from typing import Optional, Tuple, List from core.handle.reportHandle import enqueue_asr_report from core.utils.util import remove_punctuation_and_length -from .dto import ASRMessageType, ASRInputMessage +from .dto import ASRMessageType, ASRInputMessage, InterfaceType from queue import Queue, Empty TAG = __name__ @@ -26,6 +26,8 @@ class ASRProviderBase(ABC): def __init__(self): self.asr_input_queue = Queue[ASRInputMessage]() self.asr_input_audio_format = "pcm" + # Default to non-streaming, subclasses can override + self.interface_type = InterfaceType.NON_STREAM # 打开音频通道 async def open_audio_channels(self, conn): @@ -259,10 +261,10 @@ class ASRProviderBase(ABC): conn.asr_text_buffer = raw_text # Turn Detection: let turn detection handle end of turn - # if conn.turn_detection: - # # Turn detection will wait for endpoint delay, then call on_end_of_turn - # conn.turn_detection.check_end_of_turn(conn) - # return + if conn.turn_detection: + # Turn detection will wait for endpoint delay, then call on_end_of_turn + conn.turn_detection.check_end_of_turn(conn) + return # # No Turn Detection: process immediately # await conn.on_end_of_turn() diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index 288b5cfe3..15e6a469e 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -569,7 +569,7 @@ TTS: selected_module: # VAD: FsmnVAD VAD: SileroVAD - TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable + # TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable ASR: GroqASR # ASR: ByteplusStreamASR # LLM: GroqLLM