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: