From 1a5dd2d9d9c296a5a83a3d6741a001864700f47d Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Wed, 18 Mar 2026 16:40:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 15 +++++++++++++-- main/xiaozhi-server/core/handle/reportHandle.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 3ff9865a..9808ba0b 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -1074,11 +1074,22 @@ class ConnectionHandler: # 如需要大模型先处理一轮,添加相关处理后的日志情况 content_parts = [] + tool_speak_opus_data = None if len(response_message) > 0: text_buff = "".join(response_message) content_parts.append({"type": "text", "text": text_buff}) self.tts_MessageText = text_buff self.dialogue.put(Message(role="assistant", content=text_buff)) + # 与主流程独立开来合成相关音频 + try: + future = self.executor.submit(self.tts.to_tts, text_buff) + audio_datas = future.result() + if audio_datas: + tool_speak_opus_data = audio_datas + for audio_data in audio_datas: + self.tts.tts_audio_queue.put((SentenceType.MIDDLE, audio_data, text_buff)) + except Exception as e: + self.logger.bind(tag=TAG).warning(f"工具调用前文本音频合成失败: {e}") response_message.clear() tool_call_data = tool_calls_list[0] # 只取第一个工具 @@ -1093,10 +1104,10 @@ class ConnectionHandler: tool_text = f"{tool_call_data['name']}({json.dumps(tool_input, ensure_ascii=False)})" content_parts.append({"type": "tool", "text": tool_text}) - # 先上报包含文本和工具调用的内容(使用chatType=3表示工具调用) + # 上报包含文本、音频和工具调用的内容(使用chatType=3表示工具调用) tool_call_timestamp = int(time.time()) report_content = json.dumps(content_parts, ensure_ascii=False) - self.report_queue.put((3, report_content, None, tool_call_timestamp)) + self.report_queue.put((3, report_content, tool_speak_opus_data, tool_call_timestamp)) self.logger.bind(tag=TAG).debug( f"function_name={tool_call_data['name']}, function_id={tool_call_data['id']}, function_arguments={tool_call_data['arguments']}" diff --git a/main/xiaozhi-server/core/handle/reportHandle.py b/main/xiaozhi-server/core/handle/reportHandle.py index b4f2d3f6..50617852 100644 --- a/main/xiaozhi-server/core/handle/reportHandle.py +++ b/main/xiaozhi-server/core/handle/reportHandle.py @@ -26,7 +26,7 @@ async def report(conn: "ConnectionHandler", type, text, opus_data, report_time): Args: conn: 连接对象 - type: 上报类型,1为用户,2为智能体 + type: 上报类型,1为用户,2为智能体,3为工具调用 text: 合成文本 opus_data: opus音频数据 report_time: 上报时间