From 82d2acc78c73079e4d6a2419becd4efca03b4f7a Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Mon, 26 Oct 2026 17:09:09 +0800 Subject: [PATCH] =?UTF-8?q?-=20audio=5Fto=5Fdata=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=94=AE=E5=8A=A0=E5=85=A5=E6=96=87=E4=BB=B6=20mtime=EF=BC=9Ar?= =?UTF-8?q?egen=20=E8=A6=86=E5=86=99=20wav=20=E5=90=8E=E6=97=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E8=87=AA=E5=8A=A8=E5=A4=B1=E6=95=88=EF=BC=8C=20=20=20?= =?UTF-8?q?=E5=B9=B6=E5=9C=A8=20regen=20=E5=90=8E=E9=A2=84=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=8C=E5=94=A4=E9=86=92?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E4=B8=8D=E5=86=8D=E6=92=AD=E6=97=A7=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E4=B8=94=E5=BB=B6=E8=BF=9F=E6=94=B6=E7=9B=8A=E4=BF=9D?= =?UTF-8?q?=E7=95=99=20-=20enhanced=5Fprompt=20=E7=BC=93=E5=AD=98=E9=94=AE?= =?UTF-8?q?=E8=A1=A5=E4=B8=8A=20emoji=5Fenabled=20=E4=B8=8E=20prompt=20?= =?UTF-8?q?=E5=93=88=E5=B8=8C=EF=BC=9A=20=20=20=E5=90=8C=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=88ESP32/dig?= =?UTF-8?q?ital-human=EF=BC=89=E4=B8=8D=E5=86=8D=E4=BA=92=E7=9B=B8?= =?UTF-8?q?=E6=B1=A1=E6=9F=93=EF=BC=8C=E6=94=B9=E9=85=8D=E7=BD=AE=E5=8D=B3?= =?UTF-8?q?=E6=97=B6=E7=94=9F=E6=95=88=20-=20=E5=88=A0=E9=99=A4=20initiali?= =?UTF-8?q?ze=20=E5=93=8D=E5=BA=94=E5=90=8E=E7=9A=84=20tools/list=20?= =?UTF-8?q?=E5=85=9C=E5=BA=95=E8=A1=A5=E5=8F=91=EF=BC=9Aid=3D2=20=E5=90=8C?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=A4=8D=E7=94=A8=20=20=20=E8=BF=9D=E5=8F=8D?= =?UTF-8?q?=20JSON-RPC=20=E4=B8=8E=E8=AE=BE=E5=A4=87=E7=AB=AF=E4=B8=A5?= =?UTF-8?q?=E6=A0=BC=E9=80=92=E5=A2=9E=E7=BA=A6=E5=AE=9A=EF=BC=88#3461/#34?= =?UTF-8?q?85=EF=BC=89=EF=BC=8C=E4=B8=94=E5=85=9C=E4=B8=8D=E4=BD=8F?= =?UTF-8?q?=E4=BB=BB=E4=BD=95=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/handle/helloHandle.py | 3 +++ .../core/providers/tools/device_mcp/mcp_handler.py | 4 ---- main/xiaozhi-server/core/utils/prompt_manager.py | 8 +++++++- main/xiaozhi-server/core/utils/util.py | 8 ++++++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index ade90403f..630c843bf 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -168,6 +168,9 @@ async def wakeupWordsResponse(conn: "ConnectionHandler"): f.write(wav_bytes) # 更新配置 wakeup_words_config.update_wakeup_response(voice, file_path, result) + # 覆写文件后 mtime 变化使旧缓存失效,此处立刻预编码新文件, + # 下次唤醒直接命中缓存,编码成本不占用唤醒响应路径 + await audio_to_data(file_path, use_cache=True) finally: # 确保在任何情况下都释放锁 if _wakeup_response_lock.locked(): diff --git a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py index 74d242a97..39fd8371c 100644 --- a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py +++ b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py @@ -147,10 +147,6 @@ async def handle_mcp_message( logger.bind(tag=TAG).debug( f"客户端MCP服务器信息: name={name}, version={version}" ) - - # helloHandle 握手初期已并发预发 tools/list;此处再补一次作为兜底,client 收到两次也能正常处理 - asyncio.create_task(send_mcp_tools_list_request(conn)) - return elif msg_id == 2: # mcpToolsListID diff --git a/main/xiaozhi-server/core/utils/prompt_manager.py b/main/xiaozhi-server/core/utils/prompt_manager.py index 446b6359c..8a5e7c988 100644 --- a/main/xiaozhi-server/core/utils/prompt_manager.py +++ b/main/xiaozhi-server/core/utils/prompt_manager.py @@ -5,6 +5,7 @@ import os import asyncio +import hashlib import threading from typing import Dict, Any, TYPE_CHECKING @@ -254,7 +255,12 @@ class PromptManager: if not self.base_prompt_template: return user_prompt - enhanced_cache_key = f"enhanced_prompt:{device_id}" + # 缓存键须覆盖所有渲染输入:emoji 开关、设备 prompt 变更否则会命中过期提示词 + emoji_enabled = kwargs.get("emoji_enabled", True) + prompt_hash = hashlib.md5(user_prompt.encode("utf-8")).hexdigest()[:8] + enhanced_cache_key = ( + f"enhanced_prompt:{device_id}:{emoji_enabled}:{prompt_hash}" + ) cached_prompt = self.cache_manager.get( self.CacheType.DEVICE_PROMPT, enhanced_cache_key ) diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index c5fa3fa9b..af113a36f 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -310,8 +310,12 @@ async def audio_to_data( from core.utils.cache.manager import cache_manager from core.utils.cache.config import CacheType - # 生成缓存键,包含文件路径和编码类型 - cache_key = f"{audio_file_path}:{is_opus}" + # 缓存键含 mtime:文件被原地覆写时旧缓存自动失效(如唤醒词回复重新 TTS),避免播到过期音频 + try: + mtime = os.path.getmtime(audio_file_path) + cache_key = f"{audio_file_path}:{mtime}:{is_opus}" + except OSError: + cache_key = f"{audio_file_path}:{is_opus}" # 尝试从缓存获取结果 if use_cache: