From dd38fc74db8445f5bc631e9a2f6e43924c52300f Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 12 May 2025 18:06:58 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=8F=B3=E6=99=BA=E6=8E=A7=E5=8F=B0?= =?UTF-8?q?=E4=B8=8B=E5=8F=91=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ConfigServiceImpl.java | 12 ++++++ main/xiaozhi-server/core/connection.py | 38 ++++++++++++------- .../core/handle/ttsReportHandle.py | 17 ++++++--- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java index ee42c934..6ce70ba7 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/config/service/impl/ConfigServiceImpl.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import lombok.AllArgsConstructor; +import xiaozhi.common.constant.Constant; import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.RenException; import xiaozhi.common.redis.RedisKeys; @@ -108,6 +109,17 @@ public class ConfigServiceImpl implements ConfigService { // 获取单台设备每天最多输出字数 String deviceMaxOutputSize = sysParamsService.getValue("device_max_output_size", true); result.put("device_max_output_size", deviceMaxOutputSize); + + // 获取聊天记录配置 + Integer chatHistoryConf = agent.getChatHistoryConf(); + if (agent.getMemModelId() != null && agent.getMemModelId().equals(Constant.MEMORY_NO_MEM)) { + chatHistoryConf = Constant.ChatHistoryConfEnum.IGNORE.getCode(); + } else if (agent.getMemModelId() != null + && !agent.getMemModelId().equals(Constant.MEMORY_NO_MEM) + && agent.getChatHistoryConf() == null) { + chatHistoryConf = Constant.ChatHistoryConfEnum.RECORD_TEXT_AUDIO.getCode(); + } + result.put("chat_history_conf", chatHistoryConf); // 如果客户端已实例化模型,则不返回 String alreadySelectedVadModelId = (String) selectedModule.get("VAD"); if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) { diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 2a85c0ad..5f2286db 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -75,6 +75,7 @@ class ConnectionHandler: self.prompt = None self.welcome_msg = None self.max_output_size = 0 + self.chat_history_conf = 0 # 客户端状态相关 self.client_abort = False @@ -250,11 +251,15 @@ class ConnectionHandler: self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...") # 发送确认响应 - await self.websocket.send(json.dumps({ - "type": "server_response", - "status": "success", - "message": "服务器重启中..." - })) + await self.websocket.send( + json.dumps( + { + "type": "server_response", + "status": "success", + "message": "服务器重启中...", + } + ) + ) # 异步执行重启操作 def restart_server(): @@ -266,7 +271,7 @@ class ConnectionHandler: stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, - start_new_session=True + start_new_session=True, ) os._exit(0) @@ -275,11 +280,15 @@ class ConnectionHandler: except Exception as e: self.logger.bind(tag=TAG).error(f"重启失败: {str(e)}") - await self.websocket.send(json.dumps({ - "type": "server_response", - "status": "error", - "message": f"Restart failed: {str(e)}" - })) + await self.websocket.send( + json.dumps( + { + "type": "server_response", + "status": "error", + "message": f"Restart failed: {str(e)}", + } + ) + ) def _initialize_components(self): """初始化组件""" @@ -306,6 +315,8 @@ class ConnectionHandler: """初始化ASR和TTS上报线程""" if not self.read_config_from_api or self.need_bind: return + if self.chat_history_conf == 0: + return if self.tts_report_thread is None or not self.tts_report_thread.is_alive(): self.tts_report_thread = threading.Thread( target=self._tts_report_worker, daemon=True @@ -379,7 +390,8 @@ class ConnectionHandler: self.config["prompt"] = private_config["prompt"] if private_config.get("device_max_output_size", None) is not None: self.max_output_size = int(private_config["device_max_output_size"]) - + if private_config.get("chat_history_conf", None) is not None: + self.chat_history_conf = int(private_config["chat_history_conf"]) try: modules = initialize_modules( self.logger, @@ -838,7 +850,7 @@ class ConnectionHandler: if future is None: continue text = None - opus_datas, tts_file = [], None + audio_datas, tts_file = [], None try: self.logger.bind(tag=TAG).debug("正在处理TTS任务...") tts_timeout = int(self.config.get("tts_timeout", 10)) diff --git a/main/xiaozhi-server/core/handle/ttsReportHandle.py b/main/xiaozhi-server/core/handle/ttsReportHandle.py index ae8928ce..a6605e29 100644 --- a/main/xiaozhi-server/core/handle/ttsReportHandle.py +++ b/main/xiaozhi-server/core/handle/ttsReportHandle.py @@ -92,6 +92,8 @@ def opus_to_wav(conn, opus_data): def enqueue_tts_report(conn, type, text, opus_data): if not conn.read_config_from_api or conn.need_bind: return + if conn.chat_history_conf == 0: + return """将TTS数据加入上报队列 Args: @@ -101,10 +103,15 @@ def enqueue_tts_report(conn, type, text, opus_data): """ try: # 使用连接对象的队列,传入文本和二进制数据而非文件路径 - conn.tts_report_queue.put((type, text, opus_data)) - - conn.logger.bind(tag=TAG).debug( - f"TTS数据已加入上报队列: {conn.device_id}, 音频大小: {len(opus_data)} " - ) + if conn.chat_history_conf == 2: + conn.tts_report_queue.put((type, text, opus_data)) + conn.logger.bind(tag=TAG).debug( + f"TTS数据已加入上报队列: {conn.device_id}, 音频大小: {len(opus_data)} " + ) + else: + conn.tts_report_queue.put((type, text, None)) + conn.logger.bind(tag=TAG).debug( + f"TTS数据已加入上报队列: {conn.device_id}, 不上报音频" + ) except Exception as e: conn.logger.bind(tag=TAG).error(f"加入TTS上报队列失败: {text}, {e}")