diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index ef35dcf7c..77e221558 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -89,6 +89,7 @@ class ConnectionHandler: self.auth_code_gen = AuthCodeGenerator.get_instance() self.is_device_verified = False # 添加设备验证状态标志 self.music_handler = _music + self.close_after_chat = False # 是否在聊天结束后关闭连接 async def handle_connection(self, ws): try: @@ -383,6 +384,6 @@ class ConnectionHandler: self.chat(text) # After chat is complete, close the connection - asyncio.run_coroutine_threadsafe(self.close(), self.loop) + self.close_after_chat = True except Exception as e: self.logger.bind(tag=TAG).error(f"Chat and close error: {str(e)}") diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index b5912e205..3b51a0a97 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -39,8 +39,7 @@ async def check_direct_exit(conn, text): for cmd in cmd_exit: if text == cmd: logger.bind(tag=TAG).info(f"识别到明确的退出命令: {text}") - conn.asr_server_receive = True - conn.asr_audio.clear() + await conn.close() return True return False @@ -80,6 +79,8 @@ async def process_intent_result(conn, intent, original_text): if "结束聊天" in intent: logger.bind(tag=TAG).info(f"识别到退出意图: {intent}") + #如果正在播放音乐,可以关了 TODO + # 如果是明确的离别意图,发送告别语并关闭连接 await send_stt_message(conn, original_text) conn.executor.submit(conn.chat_and_close, original_text) @@ -88,12 +89,7 @@ async def process_intent_result(conn, intent, original_text): # 处理播放音乐意图 if "播放音乐" in intent: logger.bind(tag=TAG).info(f"识别到音乐播放意图: {intent}") - parts = intent.split("[") - song_name = "随机音乐" - if len(parts) > 1: - song_name = parts[1].split("]")[0] - - await conn.music_handler.handle_music_command(conn, "播放音乐 "+song_name) + await conn.music_handler.handle_music_command(conn, intent) return True # 其他意图处理可以在这里扩展 diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index 0402d48a6..2b25b497a 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -43,16 +43,6 @@ async def handleAudioMessage(conn, audio): conn.reset_vad_states() -async def handleCMDMessage(conn, text): - cmd_exit = conn.cmd_exit - for cmd in cmd_exit: - if text == cmd: - logger.bind(tag=TAG).info("识别到明确的退出命令".format(text)) - await conn.close() - return True - return False - - async def startToChat(conn, text): # 首先进行意图分析 intent_handled = await handle_user_intent(conn, text) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index d6eae20bf..1cce98b46 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -38,6 +38,8 @@ async def sendAudioMessage(conn, audios, text, text_index=0): # 发送结束消息(如果是最后一个文本) if conn.llm_finish_task and text_index == conn.tts_last_text_index: await send_tts_message(conn, 'stop', None) + if conn.close_after_chat: + await conn.close() async def send_tts_message(conn, state, text=None): """发送 TTS 状态消息"""