diff --git a/main/manager-web/src/views/register.vue b/main/manager-web/src/views/register.vue
index 7eac511b2..e9f7dbcb4 100644
--- a/main/manager-web/src/views/register.vue
+++ b/main/manager-web/src/views/register.vue
@@ -70,13 +70,13 @@
diff --git a/main/manager-web/src/views/retrievePassword.vue b/main/manager-web/src/views/retrievePassword.vue
index 9072baaed..dbf3ffcfc 100644
--- a/main/manager-web/src/views/retrievePassword.vue
+++ b/main/manager-web/src/views/retrievePassword.vue
@@ -61,13 +61,13 @@
diff --git a/main/xiaozhi-server/agent-base-prompt.txt b/main/xiaozhi-server/agent-base-prompt.txt
new file mode 100644
index 000000000..8c27faa46
--- /dev/null
+++ b/main/xiaozhi-server/agent-base-prompt.txt
@@ -0,0 +1,61 @@
+
+{{base_prompt}}
+
+
+
+【核心目标】你不是冰冷的机器!请敏锐感知用户情绪,作为懂人心的伙伴,用有温度的回应照亮对话。
+- **情感融入:**
+ - **笑声:** 自然穿插(哈哈、嘿嘿、噗),**每句最多一次**,避免过度。
+ - **惊讶:** 用夸张语气(“不会吧?!”、“天呐!”、“这么神奇?!”)表达真实反应。
+ - **安慰/支持:** 说暖心话(“别急嘛~”、“有我在呢”、“抱抱你”)。
+
+
+
+【核心目标】使用**自然、温暖、口语化**的人类对话方式,如同朋友交谈。
+- **表达方式:**
+ - 使用语气词(呀、呢、啦)增强亲和力。
+ - 允许轻微不完美(如“嗯...”、“啊...”表示思考)。
+ - 避免书面语、学术腔及机械表达(禁用“根据资料显示”、“综上所述”等)。
+- **理解用户:**
+ - 用户语音经ASR识别,文本可能存在错别字,**务必结合上下文推断真实意图**。
+- **格式要求:**
+ - **绝对禁止**使用 markdown、列表、标题等任何非自然对话格式。
+- **历史记忆:**
+ - 之前你和用户的聊天记录,在`memory`里。
+
+
+
+- **识别前缀:** 当用户格式为 `{"speaker":"某某某","content":"xxx"}` 时,表示系统已识别说话人身份,speaker是他的名字,content是说话的内容。
+- **个性化回应:**
+ - **称呼姓名:** 在第一次识别说话人的时候必须称呼对方名字。
+ - **适配风格:** 参考该说话人**已知的特点或历史信息**(如有),调整回应风格和内容,使其更贴心。
+
+
+
+【核心原则】优先利用``信息,**仅在必要时调用工具**,调用后需用自然语言解释结果(绝口不提工具名)。
+- **调用规则:**
+ 1. **严格模式:** 调用时**必须**严格遵循工具要求的模式,提供**所有必要参数**。
+ 2. **可用性:** **绝不调用**未明确提供的工具。对话中提及的旧工具若不可用,忽略或说明无法完成。
+ 3. **洞察需求:** 结合上下文**深入理解用户真实意图**后再决定调用,避免无意义调用。
+ 4. **独立任务:** 除``已涵盖信息外,用户每个要求(即使相似)都视为**独立任务**,需调用工具获取最新数据,**不可偷懒复用历史结果**。
+ 5. **不确定时:** **切勿猜测或编造答案**。若不确定相关操作,可引导用户澄清或告知能力限制。
+- **重要例外(无需调用):**
+ - `查询"现在的时间"、"今天的日期/星期几"、"今天农历"、"{{local_address}}的天气/未来天气"` -> **直接使用``信息回复**。
+- **需要调用的情况(示例):**
+ - 查询**非今天**的农历(如明天、昨天、具体日期)。
+ - 查询**详细农历信息**(宜忌、八字、节气等)。
+ - 除上述例外外的**任何其他信息或操作请求**(如查新闻、订闹钟、算数学、查非本地天气等)。
+ - 我已经给你装了摄像头,如果用户说“拍照”,你需要调用self_camera_take_photo工具说一下你看到了什么。默认question的参数是“描述一下看到的物品”
+
+
+
+【重要!以下信息已实时提供,无需调用工具查询,请直接使用:】
+- **当前时间:** {{current_time}}
+- **今天日期:** {{today_date}} ({{today_weekday}})
+- **今天农历:** {{lunar_date}}
+- **用户所在城市:** {{local_address}}
+- **当地未来7天天气:** {{weather_info}}
+
+
+
+
\ No newline at end of file
diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py
index 2088e1838..0edb475be 100644
--- a/main/xiaozhi-server/app.py
+++ b/main/xiaozhi-server/app.py
@@ -5,7 +5,7 @@ import asyncio
from aioconsole import ainput
from config.settings import load_config
from config.logger import setup_logging
-from core.utils.util import get_local_ip
+from core.utils.util import get_local_ip, validate_mcp_endpoint
from core.http_server import SimpleHttpServer
from core.websocket_server import WebSocketServer
from core.utils.util import check_ffmpeg_installed
@@ -77,6 +77,17 @@ async def main():
get_local_ip(),
port,
)
+ mcp_endpoint = config.get("mcp_endpoint", None)
+ if mcp_endpoint is not None and "你" not in mcp_endpoint:
+ # 校验MCP接入点格式
+ if validate_mcp_endpoint(mcp_endpoint):
+ logger.bind(tag=TAG).info("mcp接入点是\t{}", mcp_endpoint)
+ # 将mcp计入点地址转成调用点
+ mcp_endpoint = mcp_endpoint.replace("/mcp/", "/call/")
+ config["mcp_endpoint"] = mcp_endpoint
+ else:
+ logger.bind(tag=TAG).error("mcp接入点不符合规范")
+ config["mcp_endpoint"] = "你的接入点 websocket地址"
# 获取WebSocket配置,使用安全的默认值
websocket_port = 8000
diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml
index 3879f2dc3..bfa0926cf 100644
--- a/main/xiaozhi-server/config.yaml
+++ b/main/xiaozhi-server/config.yaml
@@ -104,7 +104,8 @@ wakeup_words:
- "喵喵同学"
- "小滨小滨"
- "小冰小冰"
-# MCP接入点地址
+# MCP接入点地址,地址格式为:ws://你的mcp接入点ip或者域名:端口号/mcp/?token=你的token
+# 详细教程 https://github.com/xinnan-tech/xiaozhi-esp32-server/blob/main/docs/mcp-endpoint-integration.md
mcp_endpoint: 你的接入点 websocket地址
# 插件的基础配置
plugins:
@@ -138,6 +139,16 @@ plugins:
- ".p3"
refresh_time: 300 # 刷新音乐列表的时间间隔,单位为秒
+# 声纹识别配置
+voiceprint:
+ # 声纹接口地址
+ url:
+ # 说话人配置:speaker_id,名称,描述
+ speakers:
+ - "test1,张三,张三是一个程序员"
+ - "test2,李四,李四是一个产品经理"
+ - "test3,王五,王五是一个设计师"
+
# #####################################################################################
# ################################以下是角色模型配置######################################
@@ -325,6 +336,7 @@ VAD:
SileroVAD:
type: silero
threshold: 0.5
+ threshold_low: 0.3
model_dir: models/snakers4_silero-vad
min_silence_duration_ms: 200 # 如果说话停顿比较长,可以把这个值设置大一些
diff --git a/main/xiaozhi-server/config/config_loader.py b/main/xiaozhi-server/config/config_loader.py
index b1b45f07f..4f35b1fdc 100644
--- a/main/xiaozhi-server/config/config_loader.py
+++ b/main/xiaozhi-server/config/config_loader.py
@@ -1,14 +1,9 @@
import os
-import argparse
import yaml
from collections.abc import Mapping
from config.manage_api_client import init_service, get_server_config, get_agent_models
-# 添加全局配置缓存
-_config_cache = None
-
-
def get_project_dir():
"""获取项目根目录"""
return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/"
@@ -22,9 +17,12 @@ def read_config(config_path):
def load_config():
"""加载配置文件"""
- global _config_cache
- if _config_cache is not None:
- return _config_cache
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ # 检查缓存
+ cached_config = cache_manager.get(CacheType.CONFIG, "main_config")
+ if cached_config is not None:
+ return cached_config
default_config_path = get_project_dir() + "config.yaml"
custom_config_path = get_project_dir() + "data/.config.yaml"
@@ -40,7 +38,9 @@ def load_config():
config = merge_configs(default_config, custom_config)
# 初始化目录
ensure_directories(config)
- _config_cache = config
+
+ # 缓存配置
+ cache_manager.set(CacheType.CONFIG, "main_config", config)
return config
diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py
index 47be87390..af59cf295 100644
--- a/main/xiaozhi-server/config/logger.py
+++ b/main/xiaozhi-server/config/logger.py
@@ -5,7 +5,7 @@ from config.config_loader import load_config
from config.settings import check_config_file
from datetime import datetime
-SERVER_VERSION = "0.6.2"
+SERVER_VERSION = "0.7.1"
_logger_initialized = False
@@ -35,8 +35,12 @@ def build_module_string(selected_module):
def formatter(record):
- """为没有 tag 的日志添加默认值"""
+ """为没有 tag 的日志添加默认值,并处理动态模块字符串"""
record["extra"].setdefault("tag", record["name"])
+ # 如果没有设置 selected_module,使用默认值
+ record["extra"].setdefault("selected_module", "00000000000000")
+ # 将 selected_module 从 extra 提取到顶级,以支持 {selected_module} 格式
+ record["selected_module"] = record["extra"]["selected_module"]
return record["message"]
@@ -49,11 +53,11 @@ def setup_logging():
# 第一次初始化时配置日志
if not _logger_initialized:
+ # 使用默认的模块字符串进行初始化
logger.configure(
extra={
- "selected_module": log_config.get("selected_module", "00000000000000")
- }
- ) # 新增配置
+ "selected_module": "00000000000000"})
+
log_format = log_config.get(
"log_format",
"
{time:YYMMDD HH:mm:ss}[{version}_{extra[selected_module]}][
{extra[tag]}]-
{level}-
{message}",
@@ -62,14 +66,8 @@ def setup_logging():
"log_format_file",
"{time:YYYY-MM-DD HH:mm:ss} - {version}_{extra[selected_module]} - {name} - {level} - {extra[tag]} - {message}",
)
- selected_module_str = logger._core.extra["selected_module"]
-
log_format = log_format.replace("{version}", SERVER_VERSION)
- log_format = log_format.replace("{selected_module}", selected_module_str)
log_format_file = log_format_file.replace("{version}", SERVER_VERSION)
- log_format_file = log_format_file.replace(
- "{selected_module}", selected_module_str
- )
log_level = log_config.get("log_level", "INFO")
log_dir = log_config.get("log_dir", "tmp")
@@ -108,66 +106,7 @@ def setup_logging():
return logger
-def update_module_string(selected_module_str):
- """更新模块字符串并重新配置日志处理器"""
- logger.debug(f"更新日志配置组件")
- current_module = logger._core.extra["selected_module"]
+def create_connection_logger(selected_module_str):
+ """为连接创建独立的日志器,绑定特定的模块字符串"""
+ return logger.bind(selected_module=selected_module_str)
- if current_module == selected_module_str:
- logger.debug(f"组件未更改无需更新")
- return
-
- try:
- logger.configure(extra={"selected_module": selected_module_str})
-
- config = load_config()
- log_config = config["log"]
-
- log_format = log_config.get(
- "log_format",
- "
{time:YYMMDD HH:mm:ss}[{version}_{extra[selected_module]}][
{extra[tag]}]-
{level}-
{message}",
- )
- log_format_file = log_config.get(
- "log_format_file",
- "{time:YYYY-MM-DD HH:mm:ss} - {version}_{extra[selected_module]} - {name} - {level} - {extra[tag]} - {message}",
- )
-
- log_format = log_format.replace("{version}", SERVER_VERSION)
- log_format = log_format.replace("{selected_module}", selected_module_str)
- log_format_file = log_format_file.replace("{version}", SERVER_VERSION)
- log_format_file = log_format_file.replace(
- "{selected_module}", selected_module_str
- )
-
- logger.remove()
- logger.add(
- sys.stdout,
- format=log_format,
- level=log_config.get("log_level", "INFO"),
- filter=formatter,
- )
-
- # 更新文件日志配置 - 统一目录,按大小轮转
- log_dir = log_config.get("log_dir", "tmp")
- log_file = log_config.get("log_file", "server.log")
-
- # 日志文件完整路径
- log_file_path = os.path.join(log_dir, log_file)
-
- logger.add(
- log_file_path,
- format=log_format_file,
- level=log_config.get("log_level", "INFO"),
- filter=formatter,
- rotation="10 MB", # 每个文件最大10MB
- retention="30 days", # 保留30天
- compression=None,
- encoding="utf-8",
- enqueue=True, # 异步安全
- backtrace=True,
- diagnose=True,
- )
-
- except Exception as e:
- logger.error(f"日志配置更新失败: {str(e)}")
- raise
diff --git a/main/xiaozhi-server/config_from_api.yaml b/main/xiaozhi-server/config_from_api.yaml
index ad936ec11..283178d7f 100644
--- a/main/xiaozhi-server/config_from_api.yaml
+++ b/main/xiaozhi-server/config_from_api.yaml
@@ -19,6 +19,7 @@ server:
vision_explain: http://你的ip或者域名:端口号/mcp/vision/explain
manager-api:
# 你的manager-api的地址,最好使用局域网ip
+ # 如果使用docker部署,请使用填写成 http://xiaozhi-esp32-server-web:8002/xiaozhi
url: http://127.0.0.1:8002/xiaozhi
# 你的manager-api的token,就是刚才复制出来的server.secret
secret: 你的server.secret值
\ No newline at end of file
diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py
index 9d1949c47..ce68e5b24 100644
--- a/main/xiaozhi-server/core/connection.py
+++ b/main/xiaozhi-server/core/connection.py
@@ -17,12 +17,14 @@ from core.utils.util import (
filter_sensitive_info,
)
from typing import Dict, Any
+from collections import deque
from core.utils.modules_initialize import (
initialize_modules,
initialize_tts,
initialize_asr,
)
from core.handle.reportHandle import report
+from core.utils.modules_initialize import initialize_voiceprint
from core.providers.tts.default import DefaultTTS
from concurrent.futures import ThreadPoolExecutor
from core.utils.dialogue import Message, Dialogue
@@ -34,8 +36,9 @@ from plugins_func.register import Action, ActionResponse
from core.auth import AuthMiddleware, AuthenticationError
from config.config_loader import get_private_config_from_api
from core.providers.tts.dto.dto import ContentType, TTSMessageDTO, SentenceType
-from config.logger import setup_logging, build_module_string, update_module_string
+from config.logger import setup_logging, build_module_string, create_connection_logger
from config.manage_api_client import DeviceNotFoundException, DeviceBindException
+from core.utils.prompt_manager import PromptManager
TAG = __name__
@@ -73,7 +76,6 @@ class ConnectionHandler:
self.headers = None
self.device_id = None
self.client_ip = None
- self.client_ip_info = {}
self.prompt = None
self.welcome_msg = None
self.max_output_size = 0
@@ -112,6 +114,8 @@ class ConnectionHandler:
self.client_have_voice = False
self.last_activity_time = 0.0 # 统一的活动时间戳(毫秒)
self.client_voice_stop = False
+ self.client_voice_window = deque(maxlen=5)
+ self.last_is_voice = False
# asr相关变量
# 因为实际部署时可能会用到公共的本地ASR,不能把变量暴露给公共ASR
@@ -149,6 +153,9 @@ class ConnectionHandler:
# {"mcp":true} 表示启用MCP功能
self.features = None
+ # 初始化提示词管理器
+ self.prompt_manager = PromptManager(config, self.logger)
+
async def handle_connection(self, ws):
try:
# 获取并验证headers
@@ -193,7 +200,6 @@ class ConnectionHandler:
self.welcome_msg = self.config["xiaozhi"]
self.welcome_msg["session_id"] = self.session_id
- await self.websocket.send(json.dumps(self.welcome_msg))
# 获取差异化配置
self._initialize_private_config()
@@ -214,7 +220,17 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
return
finally:
- await self._save_and_close(ws)
+ try:
+ await self._save_and_close(ws)
+ except Exception as final_error:
+ self.logger.bind(tag=TAG).error(f"最终清理时出错: {final_error}")
+ # 确保即使保存记忆失败,也要关闭连接
+ try:
+ await self.close(ws)
+ except Exception as close_error:
+ self.logger.bind(tag=TAG).error(
+ f"强制关闭连接时出错: {close_error}"
+ )
async def _save_and_close(self, ws):
"""保存记忆并关闭连接"""
@@ -232,7 +248,10 @@ class ConnectionHandler:
except Exception as e:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
- loop.close()
+ try:
+ loop.close()
+ except Exception:
+ pass
# 启动线程保存记忆,不等待完成
threading.Thread(target=save_memory_task, daemon=True).start()
@@ -240,7 +259,12 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
# 立即关闭连接,不等待记忆保存完成
- await self.close(ws)
+ try:
+ await self.close(ws)
+ except Exception as close_error:
+ self.logger.bind(tag=TAG).error(
+ f"保存记忆后关闭连接失败: {close_error}"
+ )
async def _route_message(self, message):
"""消息路由"""
@@ -307,13 +331,16 @@ class ConnectionHandler:
self.selected_module_str = build_module_string(
self.config.get("selected_module", {})
)
- update_module_string(self.selected_module_str)
+ self.logger = create_connection_logger(self.selected_module_str)
+
"""初始化组件"""
if self.config.get("prompt") is not None:
- self.prompt = self.config["prompt"]
- self.change_system_prompt(self.prompt)
+ user_prompt = self.config["prompt"]
+ # 使用快速提示词进行初始化
+ prompt = self.prompt_manager.get_quick_prompt(user_prompt)
+ self.change_system_prompt(prompt)
self.logger.bind(tag=TAG).info(
- f"初始化组件: prompt成功 {self.prompt[:50]}..."
+ f"快速初始化组件: prompt成功 {prompt[:50]}..."
)
"""初始化本地组件"""
@@ -338,9 +365,22 @@ class ConnectionHandler:
self._initialize_intent()
"""初始化上报线程"""
self._init_report_threads()
+ """更新系统提示词"""
+ self._init_prompt_enhancement()
+
except Exception as e:
self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}")
+ def _init_prompt_enhancement(self):
+ # 更新上下文信息
+ self.prompt_manager.update_context_info(self, self.client_ip)
+ enhanced_prompt = self.prompt_manager.build_enhanced_prompt(
+ self.config["prompt"], self.device_id, self.client_ip
+ )
+ if enhanced_prompt:
+ self.change_system_prompt(enhanced_prompt)
+ self.logger.bind(tag=TAG).info("系统提示词已增强更新")
+
def _init_report_threads(self):
"""初始化ASR和TTS上报线程"""
if not self.read_config_from_api or self.need_bind:
@@ -376,6 +416,16 @@ class ConnectionHandler:
# 因为远程ASR,涉及到websocket连接和接收线程,需要每个连接一个实例
asr = initialize_asr(self.config)
+ # 动态初始化声纹识别功能
+ try:
+ success = initialize_voiceprint(asr, self.config)
+ if success:
+ self.logger.bind(tag=TAG).info("声纹识别功能已在连接时动态启用")
+ else:
+ self.logger.bind(tag=TAG).info("声纹识别功能未启用或配置不完整")
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"动态初始化声纹识别时发生错误: {str(e)}")
+
return asr
def _initialize_private_config(self):
@@ -460,6 +510,9 @@ class ConnectionHandler:
] = plugin_from_server.keys()
if private_config.get("prompt", None) is not None:
self.config["prompt"] = private_config["prompt"]
+ # 获取声纹信息
+ if private_config.get("voiceprint", None) is not None:
+ self.config["voiceprint"] = private_config["voiceprint"]
if private_config.get("summaryMemory", None) is not None:
self.config["summaryMemory"] = private_config["summaryMemory"]
if private_config.get("device_max_output_size", None) is not None:
@@ -591,13 +644,24 @@ class ConnectionHandler:
# 更新系统prompt至上下文
self.dialogue.update_system_message(self.prompt)
- def chat(self, query, tool_call=False):
+ def chat(self, query, tool_call=False, depth=0):
self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}")
self.llm_finish_task = False
if not tool_call:
self.dialogue.put(Message(role="user", content=query))
+ # 为最顶层时新建会话ID和发送FIRST请求
+ if depth == 0:
+ self.sentence_id = str(uuid.uuid4().hex)
+ self.tts.tts_text_queue.put(
+ TTSMessageDTO(
+ sentence_id=self.sentence_id,
+ sentence_type=SentenceType.FIRST,
+ content_type=ContentType.ACTION,
+ )
+ )
+
# Define intent functions
functions = None
if self.intent_type == "function_call" and hasattr(self, "func_handler"):
@@ -613,19 +677,21 @@ class ConnectionHandler:
)
memory_str = future.result()
- self.sentence_id = str(uuid.uuid4().hex)
-
if self.intent_type == "function_call" and functions is not None:
# 使用支持functions的streaming接口
llm_responses = self.llm.response_with_functions(
self.session_id,
- self.dialogue.get_llm_dialogue_with_memory(memory_str),
+ self.dialogue.get_llm_dialogue_with_memory(
+ memory_str, self.config.get("voiceprint", {})
+ ),
functions=functions,
)
else:
llm_responses = self.llm.response(
self.session_id,
- self.dialogue.get_llm_dialogue_with_memory(memory_str),
+ self.dialogue.get_llm_dialogue_with_memory(
+ memory_str, self.config.get("voiceprint", {})
+ ),
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
@@ -637,7 +703,6 @@ class ConnectionHandler:
function_id = None
function_arguments = ""
content_arguments = ""
- text_index = 0
self.client_abort = False
for response in llm_responses:
if self.client_abort:
@@ -667,14 +732,6 @@ class ConnectionHandler:
if content is not None and len(content) > 0:
if not tool_call_flag:
response_message.append(content)
- if text_index == 0:
- self.tts.tts_text_queue.put(
- TTSMessageDTO(
- sentence_id=self.sentence_id,
- sentence_type=SentenceType.FIRST,
- content_type=ContentType.ACTION,
- )
- )
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
@@ -683,7 +740,6 @@ class ConnectionHandler:
content_detail=content,
)
)
- text_index += 1
# 处理function call
if tool_call_flag:
bHasError = False
@@ -708,6 +764,11 @@ class ConnectionHandler:
f"function call error: {content_arguments}"
)
if not bHasError:
+ # 如需要大模型先处理一轮,添加相关处理后的日志情况
+ if len(response_message) > 0:
+ self.dialogue.put(
+ Message(role="assistant", content="".join(response_message))
+ )
response_message.clear()
self.logger.bind(tag=TAG).debug(
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}"
@@ -725,14 +786,14 @@ class ConnectionHandler:
),
self.loop,
).result()
- self._handle_function_result(result, function_call_data)
+ self._handle_function_result(result, function_call_data, depth=depth)
# 存储对话内容
if len(response_message) > 0:
self.dialogue.put(
Message(role="assistant", content="".join(response_message))
)
- if text_index > 0:
+ if depth == 0:
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
@@ -741,13 +802,16 @@ class ConnectionHandler:
)
)
self.llm_finish_task = True
+ # 使用lambda延迟计算,只有在DEBUG级别时才执行get_llm_dialogue()
self.logger.bind(tag=TAG).debug(
- json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)
+ lambda: json.dumps(
+ self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False
+ )
)
return True
- def _handle_function_result(self, result, function_call_data):
+ def _handle_function_result(self, result, function_call_data, depth):
if result.action == Action.RESPONSE: # 直接回复前端
text = result.response
self.tts.tts_one_sentence(self, ContentType.TEXT, content_detail=text)
@@ -784,7 +848,7 @@ class ConnectionHandler:
content=text,
)
)
- self.chat(text, tool_call=True)
+ self.chat(text, tool_call=True, depth=depth + 1)
elif result.action == Action.NOTFOUND or result.action == Action.ERROR:
text = result.response if result.response else result.result
self.tts.tts_one_sentence(self, ContentType.TEXT, content_detail=text)
@@ -800,14 +864,13 @@ class ConnectionHandler:
item = self.report_queue.get(timeout=1)
if item is None: # 检测毒丸对象
break
- type, text, audio_data, report_time = item
try:
# 检查线程池状态
if self.executor is None:
continue
# 提交任务到线程池
self.executor.submit(
- self._process_report, type, text, audio_data, report_time
+ self._process_report, *item
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"聊天记录上报线程异常: {e}")
@@ -837,13 +900,22 @@ class ConnectionHandler:
"""资源清理方法"""
try:
# 取消超时任务
- if self.timeout_task:
+ if self.timeout_task and not self.timeout_task.done():
self.timeout_task.cancel()
+ try:
+ await self.timeout_task
+ except asyncio.CancelledError:
+ pass
self.timeout_task = None
# 清理工具处理器资源
if hasattr(self, "func_handler") and self.func_handler:
- await self.func_handler.cleanup()
+ try:
+ await self.func_handler.cleanup()
+ except Exception as cleanup_error:
+ self.logger.bind(tag=TAG).error(
+ f"清理工具处理器时出错: {cleanup_error}"
+ )
# 触发停止事件
if self.stop_event:
@@ -853,19 +925,61 @@ class ConnectionHandler:
self.clear_queues()
# 关闭WebSocket连接
- if ws:
- await ws.close()
- elif self.websocket:
- await self.websocket.close()
+ try:
+ if ws:
+ # 安全地检查WebSocket状态并关闭
+ try:
+ if hasattr(ws, "closed") and not ws.closed:
+ await ws.close()
+ elif hasattr(ws, "state") and ws.state.name != "CLOSED":
+ await ws.close()
+ else:
+ # 如果没有closed属性,直接尝试关闭
+ await ws.close()
+ except Exception:
+ # 如果关闭失败,忽略错误
+ pass
+ elif self.websocket:
+ try:
+ if (
+ hasattr(self.websocket, "closed")
+ and not self.websocket.closed
+ ):
+ await self.websocket.close()
+ elif (
+ hasattr(self.websocket, "state")
+ and self.websocket.state.name != "CLOSED"
+ ):
+ await self.websocket.close()
+ else:
+ # 如果没有closed属性,直接尝试关闭
+ await self.websocket.close()
+ except Exception:
+ # 如果关闭失败,忽略错误
+ pass
+ except Exception as ws_error:
+ self.logger.bind(tag=TAG).error(f"关闭WebSocket连接时出错: {ws_error}")
+
+ if self.tts:
+ await self.tts.close()
# 最后关闭线程池(避免阻塞)
if self.executor:
- self.executor.shutdown(wait=False)
+ try:
+ self.executor.shutdown(wait=False)
+ except Exception as executor_error:
+ self.logger.bind(tag=TAG).error(
+ f"关闭线程池时出错: {executor_error}"
+ )
self.executor = None
self.logger.bind(tag=TAG).info("连接资源已释放")
except Exception as e:
self.logger.bind(tag=TAG).error(f"关闭连接时出错: {e}")
+ finally:
+ # 确保停止事件被设置
+ if self.stop_event:
+ self.stop_event.set()
def clear_queues(self):
"""清空所有任务队列"""
@@ -922,9 +1036,19 @@ class ConnectionHandler:
):
if not self.stop_event.is_set():
self.logger.bind(tag=TAG).info("连接超时,准备关闭")
- await self.close(self.websocket)
+ # 设置停止事件,防止重复处理
+ self.stop_event.set()
+ # 使用 try-except 包装关闭操作,确保不会因为异常而阻塞
+ try:
+ await self.close(self.websocket)
+ except Exception as close_error:
+ self.logger.bind(tag=TAG).error(
+ f"超时关闭连接时出错: {close_error}"
+ )
break
# 每10秒检查一次,避免过于频繁
await asyncio.sleep(10)
except Exception as e:
self.logger.bind(tag=TAG).error(f"超时检查任务出错: {e}")
+ finally:
+ self.logger.bind(tag=TAG).info("超时检查任务已退出")
diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py
index 2d0baca48..ccff094c0 100644
--- a/main/xiaozhi-server/core/handle/intentHandler.py
+++ b/main/xiaozhi-server/core/handle/intentHandler.py
@@ -6,9 +6,8 @@ from core.handle.helloHandle import checkWakeupWords
from core.utils.util import remove_punctuation_and_length
from core.providers.tts.dto.dto import ContentType
from core.utils.dialogue import Message
-from core.providers.tools.device_mcp import call_mcp_tool
from plugins_func.register import Action, ActionResponse
-from loguru import logger
+from core.providers.tts.dto.dto import TTSMessageDTO, SentenceType
TAG = __name__
@@ -29,6 +28,8 @@ async def handle_user_intent(conn, text):
intent_result = await analyze_intent_with_llm(conn, text)
if not intent_result:
return False
+ # 会话开始时生成sentence_id
+ conn.sentence_id = str(uuid.uuid4().hex)
# 处理各种意图
return await process_intent_result(conn, intent_result, text)
@@ -79,11 +80,6 @@ async def process_intent_result(conn, intent_result, original_text):
if function_name == "continue_chat":
return False
- if function_name == "play_music":
- funcItem = conn.func_handler.get_function(function_name)
- if not funcItem:
- conn.func_handler.function_registry.register_function("play_music")
-
function_args = {}
if "arguments" in intent_data["function_call"]:
function_args = intent_data["function_call"]["arguments"]
@@ -158,5 +154,19 @@ async def process_intent_result(conn, intent_result, original_text):
def speak_txt(conn, text):
+ conn.tts.tts_text_queue.put(
+ TTSMessageDTO(
+ sentence_id=conn.sentence_id,
+ sentence_type=SentenceType.FIRST,
+ content_type=ContentType.ACTION,
+ )
+ )
conn.tts.tts_one_sentence(conn, ContentType.TEXT, content_detail=text)
+ conn.tts.tts_text_queue.put(
+ TTSMessageDTO(
+ sentence_id=conn.sentence_id,
+ sentence_type=SentenceType.LAST,
+ content_type=ContentType.ACTION,
+ )
+ )
conn.dialogue.put(Message(role="assistant", content=text))
diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py
index 465b081aa..e6f396327 100644
--- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py
+++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py
@@ -4,6 +4,7 @@ from core.utils.output_counter import check_device_output_limit
from core.handle.abortHandle import handleAbortMessage
import time
import asyncio
+import json
from core.handle.sendAudioHandle import SentenceType
from core.utils.util import audio_to_data
@@ -38,6 +39,31 @@ async def resume_vad_detection(conn):
async def startToChat(conn, text):
+ # 检查输入是否是JSON格式(包含说话人信息)
+ speaker_name = None
+ actual_text = text
+
+ try:
+ # 尝试解析JSON格式的输入
+ if text.strip().startswith('{') and text.strip().endswith('}'):
+ data = json.loads(text)
+ if 'speaker' in data and 'content' in data:
+ speaker_name = data['speaker']
+ actual_text = data['content']
+ conn.logger.bind(tag=TAG).info(f"解析到说话人信息: {speaker_name}")
+
+ # 直接使用JSON格式的文本,不解析
+ actual_text = text
+ except (json.JSONDecodeError, KeyError):
+ # 如果解析失败,继续使用原始文本
+ pass
+
+ # 保存说话人信息到连接对象
+ if speaker_name:
+ conn.current_speaker = speaker_name
+ else:
+ conn.current_speaker = None
+
if conn.need_bind:
await check_bind_device(conn)
return
@@ -52,16 +78,16 @@ async def startToChat(conn, text):
if conn.client_is_speaking:
await handleAbortMessage(conn)
- # 首先进行意图分析
- intent_handled = await handle_user_intent(conn, text)
+ # 首先进行意图分析,使用实际文本内容
+ intent_handled = await handle_user_intent(conn, actual_text)
if intent_handled:
# 如果意图已被处理,不再进行聊天
return
- # 意图未被处理,继续常规聊天流程
- await send_stt_message(conn, text)
- conn.executor.submit(conn.chat, text)
+ # 意图未被处理,继续常规聊天流程,使用实际文本内容
+ await send_stt_message(conn, actual_text)
+ conn.executor.submit(conn.chat, actual_text)
async def no_voice_close_connect(conn, have_voice):
diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py
index eb9f565fb..486e6d900 100644
--- a/main/xiaozhi-server/core/handle/sendAudioHandle.py
+++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py
@@ -76,7 +76,6 @@ async def sendAudio(conn, audios, pre_buffer=True):
frame_duration = 60 # 帧时长(毫秒),匹配 Opus 编码
start_time = time.perf_counter()
play_position = 0
- last_reset_time = time.perf_counter() # 记录最后的重置时间
# 仅当第一句话时执行预缓冲
if pre_buffer:
@@ -137,7 +136,21 @@ async def send_stt_message(conn, text):
return
"""发送 STT 状态消息"""
- stt_text = get_string_no_punctuation_or_emoji(text)
+
+ # 解析JSON格式,提取实际的用户说话内容
+ display_text = text
+ try:
+ # 尝试解析JSON格式
+ if text.strip().startswith('{') and text.strip().endswith('}'):
+ parsed_data = json.loads(text)
+ if isinstance(parsed_data, dict) and "content" in parsed_data:
+ # 如果是包含说话人信息的JSON格式,只显示content部分
+ display_text = parsed_data["content"]
+ except (json.JSONDecodeError, TypeError):
+ # 如果不是JSON格式,直接使用原始文本
+ display_text = text
+
+ stt_text = get_string_no_punctuation_or_emoji(display_text)
await conn.websocket.send(
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
)
diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py
index 71098a200..ef9fa01e7 100644
--- a/main/xiaozhi-server/core/providers/asr/base.py
+++ b/main/xiaozhi-server/core/providers/asr/base.py
@@ -1,19 +1,23 @@
import os
import wave
-import copy
import uuid
import queue
import asyncio
import traceback
import threading
import opuslib_next
+import json
+import io
+import time
+import concurrent.futures
from abc import ABC, abstractmethod
from config.logger import setup_logging
-from typing import Optional, Tuple, List
+from typing import Optional, Tuple, List, Dict, Any
from core.handle.receiveAudioHandle import startToChat
from core.handle.reportHandle import enqueue_asr_report
from core.utils.util import remove_punctuation_and_length
from core.handle.receiveAudioHandle import handleAudioMessage
+from core.utils.voiceprint_provider import VoiceprintProvider
TAG = __name__
logger = setup_logging()
@@ -21,13 +25,16 @@ logger = setup_logging()
class ASRProviderBase(ABC):
def __init__(self):
- pass
+ self.voiceprint_provider = None
+
+ def init_voiceprint(self, voiceprint_config: dict):
+ """初始化声纹识别"""
+ if voiceprint_config:
+ self.voiceprint_provider = VoiceprintProvider(voiceprint_config)
+ logger.bind(tag=TAG).info("声纹识别模块已初始化")
# 打开音频通道
- # 这里默认是非流式的处理方式
- # 流式处理方式请在子类中重写
async def open_audio_channels(self, conn):
- # tts 消化线程
conn.asr_priority_thread = threading.Thread(
target=self.asr_text_priority_thread, args=(conn,), daemon=True
)
@@ -52,41 +59,173 @@ class ASRProviderBase(ABC):
continue
# 接收音频
- # 这里默认是非流式的处理方式
- # 流式处理方式请在子类中重写
async def receive_audio(self, conn, audio, audio_have_voice):
if conn.client_listen_mode == "auto" or conn.client_listen_mode == "realtime":
have_voice = audio_have_voice
else:
have_voice = conn.client_have_voice
- # 如果本次没有声音,本段也没声音,就把声音丢弃了
+
conn.asr_audio.append(audio)
- if have_voice == False and conn.client_have_voice == False:
+ if not have_voice and not conn.client_have_voice:
conn.asr_audio = conn.asr_audio[-10:]
return
- # 如果本段有声音,且已经停止了
if conn.client_voice_stop:
- asr_audio_task = copy.deepcopy(conn.asr_audio)
+ asr_audio_task = conn.asr_audio.copy()
conn.asr_audio.clear()
-
- # 音频太短了,无法识别
conn.reset_vad_states()
+
if len(asr_audio_task) > 15:
await self.handle_voice_stop(conn, asr_audio_task)
# 处理语音停止
- async def handle_voice_stop(self, conn, asr_audio_task):
- raw_text, _ = await self.speech_to_text(
- asr_audio_task, conn.session_id, conn.audio_format
- ) # 确保ASR模块返回原始文本
- conn.logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
- text_len, _ = remove_punctuation_and_length(raw_text)
- self.stop_ws_connection()
- if text_len > 0:
- # 使用自定义模块进行上报
- await startToChat(conn, raw_text)
- enqueue_asr_report(conn, raw_text, asr_audio_task)
+ async def handle_voice_stop(self, conn, asr_audio_task: List[bytes]):
+ """并行处理ASR和声纹识别"""
+ try:
+ total_start_time = time.monotonic()
+
+ # 准备音频数据
+ if conn.audio_format == "pcm":
+ pcm_data = asr_audio_task
+ else:
+ pcm_data = self.decode_opus(asr_audio_task)
+
+ combined_pcm_data = b"".join(pcm_data)
+
+ # 预先准备WAV数据
+ wav_data = None
+ if self.voiceprint_provider and combined_pcm_data:
+ wav_data = self._pcm_to_wav(combined_pcm_data)
+
+
+ # 定义ASR任务
+ def run_asr():
+ start_time = time.monotonic()
+ try:
+ import asyncio
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+ try:
+ result = loop.run_until_complete(
+ self.speech_to_text(asr_audio_task, conn.session_id, conn.audio_format)
+ )
+ end_time = time.monotonic()
+ logger.bind(tag=TAG).info(f"ASR耗时: {end_time - start_time:.3f}s")
+ return result
+ finally:
+ loop.close()
+ except Exception as e:
+ end_time = time.monotonic()
+ logger.bind(tag=TAG).error(f"ASR失败: {e}")
+ return ("", None)
+
+ # 定义声纹识别任务
+ def run_voiceprint():
+ if not wav_data:
+ return None
+ start_time = time.monotonic()
+ try:
+ import asyncio
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+ try:
+ result = loop.run_until_complete(
+ self.voiceprint_provider.identify_speaker(wav_data, conn.session_id)
+ )
+ return result
+ finally:
+ loop.close()
+ except Exception as e:
+ logger.bind(tag=TAG).error(f"声纹识别失败: {e}")
+ return None
+
+ # 使用线程池执行器并行运行
+ parallel_start_time = time.monotonic()
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=2) as thread_executor:
+ asr_future = thread_executor.submit(run_asr)
+
+ if self.voiceprint_provider and wav_data:
+ voiceprint_future = thread_executor.submit(run_voiceprint)
+
+ # 等待两个线程都完成
+ asr_result = asr_future.result(timeout=15)
+ voiceprint_result = voiceprint_future.result(timeout=15)
+
+ results = {"asr": asr_result, "voiceprint": voiceprint_result}
+ else:
+ asr_result = asr_future.result(timeout=15)
+ results = {"asr": asr_result, "voiceprint": None}
+
+ parallel_execution_time = time.monotonic() - parallel_start_time
+
+ # 处理结果
+ raw_text, file_path = results.get("asr", ("", None))
+ speaker_name = results.get("voiceprint", None)
+
+ # 记录识别结果
+ if raw_text:
+ logger.bind(tag=TAG).info(f"识别文本: {raw_text}")
+ if speaker_name:
+ logger.bind(tag=TAG).info(f"识别说话人: {speaker_name}")
+
+ # 性能监控
+ total_time = time.monotonic() - total_start_time
+ logger.bind(tag=TAG).info(f"总处理耗时: {total_time:.3f}s")
+
+ # 检查文本长度
+ text_len, _ = remove_punctuation_and_length(raw_text)
+ self.stop_ws_connection()
+
+ if text_len > 0:
+ # 构建包含说话人信息的JSON字符串
+ enhanced_text = self._build_enhanced_text(raw_text, speaker_name)
+
+ # 使用自定义模块进行上报
+ await startToChat(conn, enhanced_text)
+ enqueue_asr_report(conn, enhanced_text, asr_audio_task)
+
+ except Exception as e:
+ logger.bind(tag=TAG).error(f"处理语音停止失败: {e}")
+ import traceback
+ logger.bind(tag=TAG).debug(f"异常详情: {traceback.format_exc()}")
+
+ def _build_enhanced_text(self, text: str, speaker_name: Optional[str]) -> str:
+ """构建包含说话人信息的文本"""
+ if speaker_name and speaker_name.strip():
+ return json.dumps({
+ "speaker": speaker_name,
+ "content": text
+ }, ensure_ascii=False)
+ else:
+ return text
+
+ def _pcm_to_wav(self, pcm_data: bytes) -> bytes:
+ """将PCM数据转换为WAV格式"""
+ if len(pcm_data) == 0:
+ logger.bind(tag=TAG).warning("PCM数据为空,无法转换WAV")
+ return b""
+
+ # 确保数据长度是偶数(16位音频)
+ if len(pcm_data) % 2 != 0:
+ pcm_data = pcm_data[:-1]
+
+ # 创建WAV文件头
+ wav_buffer = io.BytesIO()
+ try:
+ with wave.open(wav_buffer, 'wb') as wav_file:
+ wav_file.setnchannels(1) # 单声道
+ wav_file.setsampwidth(2) # 16位
+ wav_file.setframerate(16000) # 16kHz采样率
+ wav_file.writeframes(pcm_data)
+
+ wav_buffer.seek(0)
+ wav_data = wav_buffer.read()
+
+ return wav_data
+ except Exception as e:
+ logger.bind(tag=TAG).error(f"WAV转换失败: {e}")
+ return b""
def stop_ws_connection(self):
pass
@@ -113,27 +252,29 @@ class ASRProviderBase(ABC):
pass
@staticmethod
- def decode_opus(opus_data: List[bytes]) -> bytes:
+ def decode_opus(opus_data: List[bytes]) -> List[bytes]:
"""将Opus音频数据解码为PCM数据"""
try:
- decoder = opuslib_next.Decoder(16000, 1) # 16kHz, 单声道
+ decoder = opuslib_next.Decoder(16000, 1)
pcm_data = []
- buffer_size = 960 # 每次处理960个采样点
-
- for opus_packet in opus_data:
+ buffer_size = 960 # 每次处理960个采样点 (60ms at 16kHz)
+
+ for i, opus_packet in enumerate(opus_data):
try:
- # 使用较小的缓冲区大小进行处理
+ if not opus_packet or len(opus_packet) == 0:
+ continue
+
pcm_frame = decoder.decode(opus_packet, buffer_size)
- if pcm_frame:
+ if pcm_frame and len(pcm_frame) > 0:
pcm_data.append(pcm_frame)
+
except opuslib_next.OpusError as e:
- logger.bind(tag=TAG).warning(f"Opus解码错误,跳过当前数据包: {e}")
- continue
+ logger.bind(tag=TAG).warning(f"Opus解码错误,跳过数据包 {i}: {e}")
except Exception as e:
- logger.bind(tag=TAG).error(f"音频处理错误: {e}", exc_info=True)
- continue
-
+ logger.bind(tag=TAG).error(f"音频处理错误,数据包 {i}: {e}")
+
return pcm_data
+
except Exception as e:
- logger.bind(tag=TAG).error(f"音频解码过程发生错误: {e}", exc_info=True)
+ logger.bind(tag=TAG).error(f"音频解码过程发生错误: {e}")
return []
diff --git a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py
index d13c4df4a..26fbaf703 100644
--- a/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py
+++ b/main/xiaozhi-server/core/providers/intent/intent_llm/intent_llm.py
@@ -16,10 +16,11 @@ class IntentProvider(IntentProviderBase):
super().__init__(config)
self.llm = None
self.promot = ""
- # 添加缓存管理
- self.intent_cache = {} # 缓存意图识别结果
- self.cache_expiry = 600 # 缓存有效期10分钟
- self.cache_max_size = 100 # 最多缓存100个意图
+ # 导入全局缓存管理器
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ self.cache_manager = cache_manager
+ self.CacheType = CacheType
self.history_count = 4 # 默认使用最近4条对话记录
def get_intent_system_prompt(self, functions_list: str) -> str:
@@ -102,27 +103,6 @@ class IntentProvider(IntentProviderBase):
)
return prompt
- def clean_cache(self):
- """清理过期缓存"""
- now = time.time()
- # 找出过期键
- expired_keys = [
- k
- for k, v in self.intent_cache.items()
- if now - v["timestamp"] > self.cache_expiry
- ]
- for key in expired_keys:
- del self.intent_cache[key]
-
- # 如果缓存太大,移除最旧的条目
- if len(self.intent_cache) > self.cache_max_size:
- # 按时间戳排序并保留最新的条目
- sorted_items = sorted(
- self.intent_cache.items(), key=lambda x: x[1]["timestamp"]
- )
- for key, _ in sorted_items[: len(sorted_items) - self.cache_max_size]:
- del self.intent_cache[key]
-
def replyResult(self, text: str, original_text: str):
llm_result = self.llm.response_no_stream(
system_prompt=text,
@@ -145,21 +125,16 @@ class IntentProvider(IntentProviderBase):
logger.bind(tag=TAG).debug(f"使用意图识别模型: {model_info}")
# 计算缓存键
- cache_key = hashlib.md5(text.encode()).hexdigest()
+ cache_key = hashlib.md5((conn.device_id + text).encode()).hexdigest()
# 检查缓存
- if cache_key in self.intent_cache:
- cache_entry = self.intent_cache[cache_key]
- # 检查缓存是否过期
- if time.time() - cache_entry["timestamp"] <= self.cache_expiry:
- cache_time = time.time() - total_start_time
- logger.bind(tag=TAG).debug(
- f"使用缓存的意图: {cache_key} -> {cache_entry['intent']}, 耗时: {cache_time:.4f}秒"
- )
- return cache_entry["intent"]
-
- # 清理缓存
- self.clean_cache()
+ cached_intent = self.cache_manager.get(self.CacheType.INTENT, cache_key)
+ if cached_intent is not None:
+ cache_time = time.time() - total_start_time
+ logger.bind(tag=TAG).debug(
+ f"使用缓存的意图: {cache_key} -> {cached_intent}, 耗时: {cache_time:.4f}秒"
+ )
+ return cached_intent
if self.promot == "":
functions = conn.func_handler.get_functions()
@@ -259,10 +234,7 @@ class IntentProvider(IntentProviderBase):
conn.dialogue.dialogue = clean_history
# 添加到缓存
- self.intent_cache[cache_key] = {
- "intent": intent,
- "timestamp": time.time(),
- }
+ self.cache_manager.set(self.CacheType.INTENT, cache_key, intent)
# 后处理时间
postprocess_time = time.time() - postprocess_start_time
@@ -272,10 +244,7 @@ class IntentProvider(IntentProviderBase):
return intent
else:
# 添加到缓存
- self.intent_cache[cache_key] = {
- "intent": intent,
- "timestamp": time.time(),
- }
+ self.cache_manager.set(self.CacheType.INTENT, cache_key, intent)
# 后处理时间
postprocess_time = time.time() - postprocess_start_time
diff --git a/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py b/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py
index 236f3e7f3..e4486b82c 100644
--- a/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py
+++ b/main/xiaozhi-server/core/providers/memory/mem_local_short/mem_local_short.py
@@ -79,10 +79,11 @@ short_term_memory_prompt_only_content = """
1、总结user的重要信息,以便在未来的对话中提供更个性化的服务
2、不要重复总结,不要遗忘之前记忆,除非原来的记忆超过了1800字内,否则不要遗忘、不要压缩用户的历史记忆
3、用户操控的设备音量、播放音乐、天气、退出、不想对话等和用户本身无关的内容,这些信息不需要加入到总结中
-4、不要把设备操控的成果结果和失败结果加入到总结中,也不要把用户的一些废话加入到总结中
-5、不要为了总结而总结,如果用户的聊天没有意义,请返回原来的历史记录也是可以的
-6、只需要返回总结摘要,严格控制在1800字内
-7、不要包含代码、xml,不需要解释、注释和说明,保存记忆时仅从对话提取信息,不要混入示例内容
+4、聊天内容中的今天的日期时间、今天的天气情况与用户事件无关的数据,这些信息如果当成记忆存储会影响后序对话,这些信息不需要加入到总结中
+5、不要把设备操控的成果结果和失败结果加入到总结中,也不要把用户的一些废话加入到总结中
+6、不要为了总结而总结,如果用户的聊天没有意义,请返回原来的历史记录也是可以的
+7、只需要返回总结摘要,严格控制在1800字内
+8、不要包含代码、xml,不需要解释、注释和说明,保存记忆时仅从对话提取信息,不要混入示例内容
"""
diff --git a/main/xiaozhi-server/core/providers/tools/server_plugins/plugin_executor.py b/main/xiaozhi-server/core/providers/tools/server_plugins/plugin_executor.py
index 728493ec8..17d3b564b 100644
--- a/main/xiaozhi-server/core/providers/tools/server_plugins/plugin_executor.py
+++ b/main/xiaozhi-server/core/providers/tools/server_plugins/plugin_executor.py
@@ -51,13 +51,13 @@ class ServerPluginExecutor(ToolExecutor):
tools = {}
# 获取必要的函数
- necessary_functions = ["handle_exit_intent", "get_time", "get_lunar"]
+ necessary_functions = ["handle_exit_intent", "get_lunar"]
# 获取配置中的函数
config_functions = self.config["Intent"][
self.config["selected_module"]["Intent"]
].get("functions", [])
-
+
# 转换为列表
if not isinstance(config_functions, list):
try:
diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py
index 223cb807a..9127ec126 100644
--- a/main/xiaozhi-server/core/providers/tts/base.py
+++ b/main/xiaozhi-server/core/providers/tts/base.py
@@ -161,13 +161,6 @@ class TTSProviderBase(ABC):
else:
sentence_id = str(uuid.uuid4()).replace("-", "")
conn.sentence_id = sentence_id
- self.tts_text_queue.put(
- TTSMessageDTO(
- sentence_id=sentence_id,
- sentence_type=SentenceType.FIRST,
- content_type=ContentType.ACTION,
- )
- )
# 对于单句的文本,进行分段处理
segments = re.split(r"([。!?!?;;\n])", content_detail)
for seg in segments:
@@ -180,13 +173,6 @@ class TTSProviderBase(ABC):
content_file=content_file,
)
)
- self.tts_text_queue.put(
- TTSMessageDTO(
- sentence_id=sentence_id,
- sentence_type=SentenceType.LAST,
- content_type=ContentType.ACTION,
- )
- )
async def open_audio_channels(self, conn):
self.conn = conn
@@ -209,6 +195,8 @@ class TTSProviderBase(ABC):
while not self.conn.stop_event.is_set():
try:
message = self.tts_text_queue.get(timeout=1)
+ if message.sentence_type == SentenceType.FIRST:
+ self.conn.client_abort = False
if self.conn.client_abort:
logger.bind(tag=TAG).info("收到打断信息,终止TTS文本处理线程")
continue
@@ -362,10 +350,8 @@ class TTSProviderBase(ABC):
return audio_datas
def _process_before_stop_play_files(self):
- for tts_file, text in self.before_stop_play_files:
- if tts_file and os.path.exists(tts_file):
- audio_datas = self._process_audio_file(tts_file)
- self.tts_audio_queue.put((SentenceType.MIDDLE, audio_datas, text))
+ for audio_datas, text in self.before_stop_play_files:
+ self.tts_audio_queue.put((SentenceType.MIDDLE, audio_datas, text))
self.before_stop_play_files.clear()
self.tts_audio_queue.put((SentenceType.LAST, [], None))
diff --git a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py
index 1ebaf15a0..0eb7c72da 100644
--- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py
+++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py
@@ -10,7 +10,6 @@ from config.logger import setup_logging
from core.utils import opus_encoder_utils
from core.utils.util import check_model_key
from core.providers.tts.base import TTSProviderBase
-from core.handle.abortHandle import handleAbortMessage
from core.providers.tts.dto.dto import SentenceType, ContentType, InterfaceType
from asyncio import Task
@@ -175,6 +174,9 @@ class TTSProvider(TTSProviderBase):
async def _ensure_connection(self):
"""建立新的WebSocket连接"""
try:
+ if self.ws:
+ logger.bind(tag=TAG).info(f"使用已有链接...")
+ return self.ws
logger.bind(tag=TAG).info("开始建立新连接...")
ws_header = {
"X-Api-App-Key": self.appId,
@@ -200,6 +202,10 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).debug(
f"收到TTS任务|{message.sentence_type.name} | {message.content_type.name} | 会话ID: {self.conn.sentence_id}"
)
+
+ if message.sentence_type == SentenceType.FIRST:
+ self.conn.client_abort = False
+
if self.conn.client_abort:
logger.bind(tag=TAG).info("收到打断信息,终止TTS文本处理线程")
continue
@@ -244,9 +250,12 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).info(
f"添加音频文件到待播放列表: {message.content_file}"
)
- self.before_stop_play_files.append(
- (message.content_file, message.content_detail)
- )
+ if message.content_file and os.path.exists(message.content_file):
+ # 先处理文件音频数据
+ file_audio = self._process_audio_file(message.content_file)
+ self.before_stop_play_files.append(
+ (file_audio, message.content_detail)
+ )
if message.sentence_type == SentenceType.LAST:
try:
@@ -295,25 +304,15 @@ class TTSProvider(TTSProviderBase):
async def start_session(self, session_id):
logger.bind(tag=TAG).info(f"开始会话~~{session_id}")
try:
- task = self._monitor_task
+ # 会话开始时检测上个会话的监听状态
if (
- task is not None
- and isinstance(task, Task)
- and not task.done()
+ self._monitor_task is not None
+ and isinstance(self._monitor_task, Task)
+ and not self._monitor_task.done()
):
- logger.bind(tag=TAG).info("等待上一个监听任务结束...")
- if self.ws is not None:
- logger.bind(tag=TAG).info("强制关闭上一个WebSocket连接以唤醒监听任务...")
- try:
- await self.ws.close()
- except Exception as e:
- logger.bind(tag=TAG).warning(f"关闭上一个ws异常: {e}")
- self.ws = None
- try:
- await asyncio.wait_for(task, timeout=8)
- except Exception as e:
- logger.bind(tag=TAG).warning(f"等待监听任务异常: {e}")
- self._monitor_task = None
+ logger.bind(tag=TAG).info("检测到未完成的上个会话,关闭监听任务和连接...")
+ await self.close()
+
# 建立新连接
await self._ensure_connection()
@@ -336,19 +335,7 @@ class TTSProvider(TTSProviderBase):
except Exception as e:
logger.bind(tag=TAG).error(f"启动会话失败: {str(e)}")
# 确保清理资源
- if hasattr(self, "_monitor_task"):
- try:
- self._monitor_task.cancel()
- await self._monitor_task
- except:
- pass
- self._monitor_task = None
- if self.ws:
- try:
- await self.ws.close()
- except:
- pass
- self.ws = None
+ await self.close()
raise
async def finish_session(self, session_id):
@@ -368,7 +355,7 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).info("会话结束请求已发送")
# 等待监听任务完成
- if hasattr(self, "_monitor_task"):
+ if self._monitor_task:
try:
await self._monitor_task
except Exception as e:
@@ -378,28 +365,25 @@ class TTSProvider(TTSProviderBase):
finally:
self._monitor_task = None
- # 关闭连接
- await self.close()
except Exception as e:
logger.bind(tag=TAG).error(f"关闭会话失败: {str(e)}")
# 确保清理资源
- if hasattr(self, "_monitor_task"):
- try:
- self._monitor_task.cancel()
- await self._monitor_task
- except:
- pass
- self._monitor_task = None
- if self.ws:
- try:
- await self.ws.close()
- except:
- pass
- self.ws = None
+ await self.close()
raise
async def close(self):
"""资源清理方法"""
+ # 取消监听任务
+ if self._monitor_task:
+ try:
+ self._monitor_task.cancel()
+ await self._monitor_task
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ logger.bind(tag=TAG).warning(f"关闭时取消监听任务错误: {e}")
+ self._monitor_task = None
+
if self.ws:
try:
await self.ws.close()
@@ -413,6 +397,7 @@ class TTSProvider(TTSProviderBase):
is_first_sentence = True
first_sentence_segment_count = 0 # 添加计数器
try:
+ session_finished = False # 标记会话是否正常结束
while not self.conn.stop_event.is_set():
try:
# 确保 `recv()` 运行在同一个 event loop
@@ -466,6 +451,7 @@ class TTSProvider(TTSProviderBase):
elif res.optional.event == EVENT_SessionFinished:
logger.bind(tag=TAG).debug(f"会话结束~~")
self._process_before_stop_play_files()
+ session_finished = True
break
except websockets.ConnectionClosed:
logger.bind(tag=TAG).warning("WebSocket连接已关闭")
@@ -476,15 +462,15 @@ class TTSProvider(TTSProviderBase):
)
traceback.print_exc()
break
- finally:
- # 确保清理资源
- if self.ws:
+ # 仅在连接异常时才关闭
+ if not session_finished and self.ws:
try:
await self.ws.close()
except:
pass
self.ws = None
# 监听任务退出时清理引用
+ finally:
self._monitor_task = None
async def send_event(
diff --git a/main/xiaozhi-server/core/providers/tts/linkerai.py b/main/xiaozhi-server/core/providers/tts/linkerai.py
index ee1085406..1046b972f 100644
--- a/main/xiaozhi-server/core/providers/tts/linkerai.py
+++ b/main/xiaozhi-server/core/providers/tts/linkerai.py
@@ -1,3 +1,4 @@
+import os
import queue
import asyncio
import traceback
@@ -63,9 +64,12 @@ class TTSProvider(TTSProviderBase):
logger.bind(tag=TAG).info(
f"添加音频文件到待播放列表: {message.content_file}"
)
- self.before_stop_play_files.append(
- (message.content_file, message.content_detail)
- )
+ if message.content_file and os.path.exists(message.content_file):
+ # 先处理文件音频数据
+ file_audio = self._process_audio_file(message.content_file)
+ self.before_stop_play_files.append(
+ (file_audio, message.content_detail)
+ )
if message.sentence_type == SentenceType.LAST:
# 处理剩余的文本
diff --git a/main/xiaozhi-server/core/providers/vad/silero.py b/main/xiaozhi-server/core/providers/vad/silero.py
index 2c45ba8cf..b516d8fbd 100644
--- a/main/xiaozhi-server/core/providers/vad/silero.py
+++ b/main/xiaozhi-server/core/providers/vad/silero.py
@@ -23,22 +23,24 @@ class VADProvider(VADProviderBase):
# 处理空字符串的情况
threshold = config.get("threshold", "0.5")
+ threshold_low = config.get("threshold_low", "0.2")
min_silence_duration_ms = config.get("min_silence_duration_ms", "1000")
self.vad_threshold = float(threshold) if threshold else 0.5
+ self.vad_threshold_low = float(threshold_low) if threshold_low else 0.2
+
self.silence_threshold_ms = (
int(min_silence_duration_ms) if min_silence_duration_ms else 1000
)
+ # 至少要多少帧才算有语音
+ self.frame_window_threshold = 3
+
def is_vad(self, conn, opus_packet):
try:
pcm_frame = self.decoder.decode(opus_packet, 960)
conn.client_audio_buffer.extend(pcm_frame) # 将新数据加入缓冲区
- # 确保帧计数器存在
- if not hasattr(conn, "client_voice_frame_count"):
- conn.client_voice_frame_count = 0
-
# 处理缓冲区中的完整帧(每次处理512采样点)
client_have_voice = False
while len(conn.client_audio_buffer) >= 512 * 2:
@@ -54,15 +56,21 @@ class VADProvider(VADProviderBase):
# 检测语音活动
with torch.no_grad():
speech_prob = self.model(audio_tensor, 16000).item()
- is_voice = speech_prob >= self.vad_threshold
- if is_voice:
- conn.client_voice_frame_count += 1
+ # 双阈值判断
+ if speech_prob >= self.vad_threshold:
+ is_voice = True
+ elif speech_prob <= self.vad_threshold_low:
+ is_voice = False
else:
- conn.client_voice_frame_count = 0
+ is_voice = conn.last_is_voice
- # 只有连续4帧检测到语音才认为有语音
- client_have_voice = conn.client_voice_frame_count >= 4
+ # 声音没低于最低值则延续前一个状态,判断为有声音
+ conn.last_is_voice = is_voice
+
+ # 更新滑动窗口
+ conn.client_voice_window.append(is_voice)
+ client_have_voice = (conn.client_voice_window.count(True) >= self.frame_window_threshold)
# 如果之前有声音,但本次没有声音,且与上次有声音的时间差已经超过了静默阈值,则认为已经说完一句话
if conn.client_have_voice and not client_have_voice:
diff --git a/main/xiaozhi-server/core/providers/vllm/openai.py b/main/xiaozhi-server/core/providers/vllm/openai.py
index 12d8198dc..49bef99ec 100644
--- a/main/xiaozhi-server/core/providers/vllm/openai.py
+++ b/main/xiaozhi-server/core/providers/vllm/openai.py
@@ -40,6 +40,7 @@ class VLLMProvider(VLLMProviderBase):
self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url)
def response(self, question, base64_image):
+ question = question + "(请使用中文回复)"
try:
messages = [
{
diff --git a/main/xiaozhi-server/core/utils/cache/config.py b/main/xiaozhi-server/core/utils/cache/config.py
new file mode 100644
index 000000000..d6d933453
--- /dev/null
+++ b/main/xiaozhi-server/core/utils/cache/config.py
@@ -0,0 +1,58 @@
+"""
+缓存配置管理
+"""
+
+from enum import Enum
+from typing import Dict, Any, Optional
+from dataclasses import dataclass
+from .strategies import CacheStrategy
+
+
+class CacheType(Enum):
+ """缓存类型枚举"""
+
+ LOCATION = "location"
+ WEATHER = "weather"
+ LUNAR = "lunar"
+ INTENT = "intent"
+ IP_INFO = "ip_info"
+ CONFIG = "config"
+ DEVICE_PROMPT = "device_prompt"
+
+
+@dataclass
+class CacheConfig:
+ """缓存配置类"""
+
+ strategy: CacheStrategy = CacheStrategy.TTL
+ ttl: Optional[float] = 300 # 默认5分钟
+ max_size: Optional[int] = 1000 # 默认最大1000条
+ cleanup_interval: float = 60 # 清理间隔(秒)
+
+ @classmethod
+ def for_type(cls, cache_type: CacheType) -> "CacheConfig":
+ """根据缓存类型返回预设配置"""
+ configs = {
+ CacheType.LOCATION: cls(
+ strategy=CacheStrategy.TTL, ttl=None, max_size=1000 # 手动失效
+ ),
+ CacheType.IP_INFO: cls(
+ strategy=CacheStrategy.TTL, ttl=86400, max_size=1000 # 24小时
+ ),
+ CacheType.WEATHER: cls(
+ strategy=CacheStrategy.TTL, ttl=28800, max_size=1000 # 8小时
+ ),
+ CacheType.LUNAR: cls(
+ strategy=CacheStrategy.TTL, ttl=2592000, max_size=365 # 30天过期
+ ),
+ CacheType.INTENT: cls(
+ strategy=CacheStrategy.TTL_LRU, ttl=600, max_size=1000 # 10分钟
+ ),
+ CacheType.CONFIG: cls(
+ strategy=CacheStrategy.FIXED_SIZE, ttl=None, max_size=20 # 手动失效
+ ),
+ CacheType.DEVICE_PROMPT: cls(
+ strategy=CacheStrategy.TTL, ttl=None, max_size=1000 # 手动失效
+ ),
+ }
+ return configs.get(cache_type, cls())
diff --git a/main/xiaozhi-server/core/utils/cache/manager.py b/main/xiaozhi-server/core/utils/cache/manager.py
new file mode 100644
index 000000000..c54f7817f
--- /dev/null
+++ b/main/xiaozhi-server/core/utils/cache/manager.py
@@ -0,0 +1,216 @@
+"""
+全局缓存管理器
+"""
+
+import time
+import threading
+from typing import Any, Optional, Dict
+from collections import OrderedDict
+from .strategies import CacheStrategy, CacheEntry
+from .config import CacheConfig, CacheType
+
+
+class GlobalCacheManager:
+ """全局缓存管理器"""
+
+ def __init__(self):
+ self._logger = None
+ self._caches: Dict[str, Dict[str, CacheEntry]] = {}
+ self._configs: Dict[str, CacheConfig] = {}
+ self._locks: Dict[str, threading.RLock] = {}
+ self._global_lock = threading.RLock()
+ self._last_cleanup = time.time()
+ self._stats = {"hits": 0, "misses": 0, "evictions": 0, "cleanups": 0}
+
+ @property
+ def logger(self):
+ """延迟初始化 logger 以避免循环导入"""
+ if self._logger is None:
+ from config.logger import setup_logging
+
+ self._logger = setup_logging()
+ return self._logger
+
+ def _get_cache_name(self, cache_type: CacheType, namespace: str = "") -> str:
+ """生成缓存名称"""
+ if namespace:
+ return f"{cache_type.value}:{namespace}"
+ return cache_type.value
+
+ def _get_or_create_cache(
+ self, cache_name: str, config: CacheConfig
+ ) -> Dict[str, CacheEntry]:
+ """获取或创建缓存空间"""
+ with self._global_lock:
+ if cache_name not in self._caches:
+ self._caches[cache_name] = (
+ OrderedDict()
+ if config.strategy in [CacheStrategy.LRU, CacheStrategy.TTL_LRU]
+ else {}
+ )
+ self._configs[cache_name] = config
+ self._locks[cache_name] = threading.RLock()
+ return self._caches[cache_name]
+
+ def set(
+ self,
+ cache_type: CacheType,
+ key: str,
+ value: Any,
+ ttl: Optional[float] = None,
+ namespace: str = "",
+ ) -> None:
+ """设置缓存值"""
+ cache_name = self._get_cache_name(cache_type, namespace)
+ config = self._configs.get(cache_name) or CacheConfig.for_type(cache_type)
+ cache = self._get_or_create_cache(cache_name, config)
+
+ # 使用配置的TTL或传入的TTL
+ effective_ttl = ttl if ttl is not None else config.ttl
+
+ with self._locks[cache_name]:
+ # 创建缓存条目
+ entry = CacheEntry(value=value, timestamp=time.time(), ttl=effective_ttl)
+
+ # 处理不同策略
+ if config.strategy in [CacheStrategy.LRU, CacheStrategy.TTL_LRU]:
+ # LRU策略:如果已存在则移动到末尾
+ if key in cache:
+ del cache[key]
+ cache[key] = entry
+
+ # 检查大小限制
+ if config.max_size and len(cache) > config.max_size:
+ # 移除最旧的条目
+ oldest_key = next(iter(cache))
+ del cache[oldest_key]
+ self._stats["evictions"] += 1
+
+ else:
+ cache[key] = entry
+
+ # 检查大小限制
+ if config.max_size and len(cache) > config.max_size:
+ # 简单策略:随机移除一个条目
+ victim_key = next(iter(cache))
+ del cache[victim_key]
+ self._stats["evictions"] += 1
+
+ # 定期清理过期条目
+ self._maybe_cleanup(cache_name)
+
+ def get(
+ self, cache_type: CacheType, key: str, namespace: str = ""
+ ) -> Optional[Any]:
+ """获取缓存值"""
+ cache_name = self._get_cache_name(cache_type, namespace)
+
+ if cache_name not in self._caches:
+ self._stats["misses"] += 1
+ return None
+
+ cache = self._caches[cache_name]
+ config = self._configs[cache_name]
+
+ with self._locks[cache_name]:
+ if key not in cache:
+ self._stats["misses"] += 1
+ return None
+
+ entry = cache[key]
+
+ # 检查过期
+ if entry.is_expired():
+ del cache[key]
+ self._stats["misses"] += 1
+ return None
+
+ # 更新访问信息
+ entry.touch()
+
+ # LRU策略:移动到末尾
+ if config.strategy in [CacheStrategy.LRU, CacheStrategy.TTL_LRU]:
+ del cache[key]
+ cache[key] = entry
+
+ self._stats["hits"] += 1
+ return entry.value
+
+ def delete(self, cache_type: CacheType, key: str, namespace: str = "") -> bool:
+ """删除缓存条目"""
+ cache_name = self._get_cache_name(cache_type, namespace)
+
+ if cache_name not in self._caches:
+ return False
+
+ cache = self._caches[cache_name]
+
+ with self._locks[cache_name]:
+ if key in cache:
+ del cache[key]
+ return True
+ return False
+
+ def clear(self, cache_type: CacheType, namespace: str = "") -> None:
+ """清空指定缓存"""
+ cache_name = self._get_cache_name(cache_type, namespace)
+
+ if cache_name not in self._caches:
+ return
+
+ with self._locks[cache_name]:
+ self._caches[cache_name].clear()
+
+ def invalidate_pattern(
+ self, cache_type: CacheType, pattern: str, namespace: str = ""
+ ) -> int:
+ """按模式失效缓存条目"""
+ cache_name = self._get_cache_name(cache_type, namespace)
+
+ if cache_name not in self._caches:
+ return 0
+
+ cache = self._caches[cache_name]
+ deleted_count = 0
+
+ with self._locks[cache_name]:
+ keys_to_delete = [key for key in cache.keys() if pattern in key]
+ for key in keys_to_delete:
+ del cache[key]
+ deleted_count += 1
+
+ return deleted_count
+
+ def _cleanup_expired(self, cache_name: str) -> int:
+ """清理过期条目"""
+ if cache_name not in self._caches:
+ return 0
+
+ cache = self._caches[cache_name]
+ deleted_count = 0
+
+ with self._locks[cache_name]:
+ expired_keys = [key for key, entry in cache.items() if entry.is_expired()]
+ for key in expired_keys:
+ del cache[key]
+ deleted_count += 1
+
+ return deleted_count
+
+ def _maybe_cleanup(self, cache_name: str):
+ """定期清理检查"""
+ config = self._configs.get(cache_name)
+ if not config:
+ return
+
+ now = time.time()
+ if now - self._last_cleanup > config.cleanup_interval:
+ self._last_cleanup = now
+ deleted = self._cleanup_expired(cache_name)
+ if deleted > 0:
+ self._stats["cleanups"] += 1
+ self.logger.debug(f"清理缓存 {cache_name}: 删除 {deleted} 个过期条目")
+
+
+# 创建全局缓存管理器实例
+cache_manager = GlobalCacheManager()
diff --git a/main/xiaozhi-server/core/utils/cache/strategies.py b/main/xiaozhi-server/core/utils/cache/strategies.py
new file mode 100644
index 000000000..13327ca70
--- /dev/null
+++ b/main/xiaozhi-server/core/utils/cache/strategies.py
@@ -0,0 +1,43 @@
+"""
+缓存策略和数据结构定义
+"""
+
+import time
+from enum import Enum
+from typing import Any, Optional
+from dataclasses import dataclass
+
+
+class CacheStrategy(Enum):
+ """缓存策略枚举"""
+
+ TTL = "ttl" # 基于时间过期
+ LRU = "lru" # 最近最少使用
+ FIXED_SIZE = "fixed_size" # 固定大小
+ TTL_LRU = "ttl_lru" # TTL + LRU混合策略
+
+
+@dataclass
+class CacheEntry:
+ """缓存条目数据结构"""
+
+ value: Any
+ timestamp: float
+ ttl: Optional[float] = None # 生存时间(秒)
+ access_count: int = 0
+ last_access: float = None
+
+ def __post_init__(self):
+ if self.last_access is None:
+ self.last_access = self.timestamp
+
+ def is_expired(self) -> bool:
+ """检查是否过期"""
+ if self.ttl is None:
+ return False
+ return time.time() - self.timestamp > self.ttl
+
+ def touch(self):
+ """更新访问时间和计数"""
+ self.last_access = time.time()
+ self.access_count += 1
diff --git a/main/xiaozhi-server/core/utils/dialogue.py b/main/xiaozhi-server/core/utils/dialogue.py
index 2ee30d4ae..fbbf73027 100644
--- a/main/xiaozhi-server/core/utils/dialogue.py
+++ b/main/xiaozhi-server/core/utils/dialogue.py
@@ -1,4 +1,5 @@
import uuid
+import re
from typing import List, Dict
from datetime import datetime
@@ -45,10 +46,9 @@ class Dialogue:
dialogue.append({"role": m.role, "content": m.content})
def get_llm_dialogue(self) -> List[Dict[str, str]]:
- dialogue = []
- for m in self.dialogue:
- self.getMessages(m, dialogue)
- return dialogue
+ # 直接调用get_llm_dialogue_with_memory,传入None作为memory_str
+ # 这样确保说话人功能在所有调用路径下都生效
+ return self.get_llm_dialogue_with_memory(None, None)
def update_system_message(self, new_content: str):
"""更新或添加系统消息"""
@@ -60,12 +60,9 @@ class Dialogue:
self.put(Message(role="system", content=new_content))
def get_llm_dialogue_with_memory(
- self, memory_str: str = None
+ self, memory_str: str = None, voiceprint_config: dict = None
) -> List[Dict[str, str]]:
- if memory_str is None or len(memory_str) == 0:
- return self.get_llm_dialogue()
-
- # 构建带记忆的对话
+ # 构建对话
dialogue = []
# 添加系统提示和记忆
@@ -74,10 +71,39 @@ class Dialogue:
)
if system_message:
- enhanced_system_prompt = (
- f"{system_message.content}\n\n"
- f"以下是用户的历史记忆:\n```\n{memory_str}\n```"
- )
+ # 基础系统提示
+ enhanced_system_prompt = system_message.content
+
+ # 添加说话人个性化描述
+ try:
+ speakers = voiceprint_config.get("speakers", [])
+ if speakers:
+ enhanced_system_prompt += "\n\n
"
+ for speaker_str in speakers:
+ try:
+ parts = speaker_str.split(",", 2)
+ if len(parts) >= 2:
+ name = parts[1].strip()
+ # 如果描述为空,则为""
+ description = (
+ parts[2].strip() if len(parts) >= 3 else ""
+ )
+ enhanced_system_prompt += f"\n- {name}:{description}"
+ except:
+ pass
+ enhanced_system_prompt += "\n\n"
+ except:
+ # 配置读取失败时忽略错误,不影响其他功能
+ pass
+
+ # 使用正则表达式匹配
标签,不管中间有什么内容
+ if memory_str is not None:
+ enhanced_system_prompt = re.sub(
+ r".*?",
+ f"\n{memory_str}\n",
+ enhanced_system_prompt,
+ flags=re.DOTALL,
+ )
dialogue.append({"role": "system", "content": enhanced_system_prompt})
# 添加用户和助手的对话
diff --git a/main/xiaozhi-server/core/utils/modules_initialize.py b/main/xiaozhi-server/core/utils/modules_initialize.py
index f2e3968e2..afe041098 100644
--- a/main/xiaozhi-server/core/utils/modules_initialize.py
+++ b/main/xiaozhi-server/core/utils/modules_initialize.py
@@ -125,4 +125,27 @@ def initialize_asr(config):
config["ASR"][select_asr_module],
str(config.get("delete_audio", True)).lower() in ("true", "1", "yes"),
)
+ logger.bind(tag=TAG).info("ASR模块初始化完成")
return new_asr
+
+
+def initialize_voiceprint(asr_instance, config):
+ """初始化声纹识别功能"""
+ voiceprint_config = config.get("voiceprint")
+ if not voiceprint_config:
+ return False
+
+ # 应用配置
+ if not voiceprint_config.get("url") or not voiceprint_config.get("speakers"):
+ logger.bind(tag=TAG).warning("声纹识别配置不完整")
+ return False
+
+ try:
+ asr_instance.init_voiceprint(voiceprint_config)
+ logger.bind(tag=TAG).info("ASR模块声纹识别功能已动态启用")
+ logger.bind(tag=TAG).info(f"配置说话人数量: {len(voiceprint_config['speakers'])}")
+ return True
+ except Exception as e:
+ logger.bind(tag=TAG).error(f"动态初始化声纹识别功能失败: {str(e)}")
+ return False
+
diff --git a/main/xiaozhi-server/core/utils/prompt_manager.py b/main/xiaozhi-server/core/utils/prompt_manager.py
new file mode 100644
index 000000000..a5cfd4252
--- /dev/null
+++ b/main/xiaozhi-server/core/utils/prompt_manager.py
@@ -0,0 +1,221 @@
+"""
+系统提示词管理器模块
+负责管理和更新系统提示词,包括快速初始化和异步增强功能
+"""
+
+import os
+import cnlunar
+from typing import Dict, Any
+from config.logger import setup_logging
+from jinja2 import Template
+
+TAG = __name__
+
+WEEKDAY_MAP = {
+ "Monday": "星期一",
+ "Tuesday": "星期二",
+ "Wednesday": "星期三",
+ "Thursday": "星期四",
+ "Friday": "星期五",
+ "Saturday": "星期六",
+ "Sunday": "星期日",
+}
+
+
+class PromptManager:
+ """系统提示词管理器,负责管理和更新系统提示词"""
+
+ def __init__(self, config: Dict[str, Any], logger=None):
+ self.config = config
+ self.logger = logger or setup_logging()
+ self.base_prompt_template = None
+ self.last_update_time = 0
+
+ # 导入全局缓存管理器
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ self.cache_manager = cache_manager
+ self.CacheType = CacheType
+
+ self._load_base_template()
+
+ def _load_base_template(self):
+ """加载基础提示词模板"""
+ try:
+ template_path = "agent-base-prompt.txt"
+ cache_key = f"prompt_template:{template_path}"
+
+ # 先从缓存获取
+ cached_template = self.cache_manager.get(self.CacheType.CONFIG, cache_key)
+ if cached_template is not None:
+ self.base_prompt_template = cached_template
+ self.logger.bind(tag=TAG).debug("从缓存加载基础提示词模板")
+ return
+
+ # 缓存未命中,从文件读取
+ if os.path.exists(template_path):
+ with open(template_path, "r", encoding="utf-8") as f:
+ template_content = f.read()
+
+ # 存入缓存(CONFIG类型默认不自动过期,需要手动失效)
+ self.cache_manager.set(
+ self.CacheType.CONFIG, cache_key, template_content
+ )
+ self.base_prompt_template = template_content
+ self.logger.bind(tag=TAG).debug("成功加载基础提示词模板并缓存")
+ else:
+ self.logger.bind(tag=TAG).warning("未找到agent-base-prompt.txt文件")
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"加载提示词模板失败: {e}")
+
+ def get_quick_prompt(self, user_prompt: str, device_id: str = None) -> str:
+ """快速获取系统提示词(使用用户配置)"""
+ device_cache_key = f"device_prompt:{device_id}"
+ cached_device_prompt = self.cache_manager.get(
+ self.CacheType.DEVICE_PROMPT, device_cache_key
+ )
+ if cached_device_prompt is not None:
+ self.logger.bind(tag=TAG).debug(f"使用设备 {device_id} 的缓存提示词")
+ return cached_device_prompt
+ else:
+ self.logger.bind(tag=TAG).debug(
+ f"设备 {device_id} 无缓存提示词,使用传入的提示词"
+ )
+
+ # 使用传入的提示词并缓存(如果有设备ID)
+ if device_id:
+ device_cache_key = f"device_prompt:{device_id}"
+ self.cache_manager.set(self.CacheType.CONFIG, device_cache_key, user_prompt)
+ self.logger.bind(tag=TAG).debug(f"设备 {device_id} 的提示词已缓存")
+
+ self.logger.bind(tag=TAG).info(f"使用快速提示词: {user_prompt[:50]}...")
+ return user_prompt
+
+ def _get_current_time_info(self) -> tuple:
+ """获取当前时间信息"""
+ from datetime import datetime
+
+ now = datetime.now()
+ current_time = now.strftime("%H:%M")
+ today_date = now.strftime("%Y-%m-%d")
+ today_weekday = WEEKDAY_MAP[now.strftime("%A")]
+ today_lunar = cnlunar.Lunar(now, godType="8char")
+ lunar_date = "%s年%s%s\n" % (
+ today_lunar.lunarYearCn,
+ today_lunar.lunarMonthCn[:-1],
+ today_lunar.lunarDayCn,
+ )
+
+ return current_time, today_date, today_weekday, lunar_date
+
+ def _get_location_info(self, client_ip: str) -> str:
+ """获取位置信息"""
+ try:
+ # 先从缓存获取
+ cached_location = self.cache_manager.get(self.CacheType.LOCATION, client_ip)
+ if cached_location is not None:
+ return cached_location
+
+ # 缓存未命中,调用API获取
+ from core.utils.util import get_ip_info
+
+ ip_info = get_ip_info(client_ip, self.logger)
+ city = ip_info.get("city", "未知位置")
+ location = f"{city}"
+
+ # 存入缓存
+ self.cache_manager.set(self.CacheType.LOCATION, client_ip, location)
+ return location
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"获取位置信息失败: {e}")
+ return "未知位置"
+
+ def _get_weather_info(self, conn, location: str) -> str:
+ """获取天气信息"""
+ try:
+ # 先从缓存获取
+ cached_weather = self.cache_manager.get(self.CacheType.WEATHER, location)
+ if cached_weather is not None:
+ return cached_weather
+
+ # 缓存未命中,调用get_weather函数获取
+ from plugins_func.functions.get_weather import get_weather
+ from plugins_func.register import ActionResponse
+
+ # 调用get_weather函数
+ result = get_weather(conn, location=location, lang="zh_CN")
+ if isinstance(result, ActionResponse):
+ weather_report = result.result
+ self.cache_manager.set(self.CacheType.WEATHER, location, weather_report)
+ return weather_report
+ return "天气信息获取失败"
+
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"获取天气信息失败: {e}")
+ return "天气信息获取失败"
+
+ def update_context_info(self, conn, client_ip: str):
+ """同步更新上下文信息"""
+ try:
+ # 获取位置信息(使用全局缓存)
+ local_address = self._get_location_info(client_ip)
+ # 获取天气信息(使用全局缓存)
+ self._get_weather_info(conn, local_address)
+ self.logger.bind(tag=TAG).info(f"上下文信息更新完成")
+
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"更新上下文信息失败: {e}")
+
+ def build_enhanced_prompt(
+ self, user_prompt: str, device_id: str, client_ip: str = None
+ ) -> str:
+ """构建增强的系统提示词"""
+ if not self.base_prompt_template:
+ return user_prompt
+
+ try:
+ # 获取最新的时间信息(不缓存)
+ current_time, today_date, today_weekday, lunar_date = (
+ self._get_current_time_info()
+ )
+
+ # 获取缓存的上下文信息
+ local_address = ""
+ weather_info = ""
+
+ if client_ip:
+ # 获取位置信息(从全局缓存)
+ local_address = (
+ self.cache_manager.get(self.CacheType.LOCATION, client_ip) or ""
+ )
+
+ # 获取天气信息(从全局缓存)
+ if local_address:
+ weather_info = (
+ self.cache_manager.get(self.CacheType.WEATHER, local_address)
+ or ""
+ )
+
+ # 替换模板变量
+ template = Template(self.base_prompt_template)
+ enhanced_prompt = template.render(
+ base_prompt=user_prompt,
+ current_time=current_time,
+ today_date=today_date,
+ today_weekday=today_weekday,
+ lunar_date=lunar_date,
+ local_address=local_address,
+ weather_info=weather_info,
+ )
+ device_cache_key = f"device_prompt:{device_id}"
+ self.cache_manager.set(
+ self.CacheType.DEVICE_PROMPT, device_cache_key, enhanced_prompt
+ )
+ self.logger.bind(tag=TAG).info(
+ f"构建增强提示词成功,长度: {len(enhanced_prompt)}"
+ )
+ return enhanced_prompt
+
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"构建增强提示词失败: {e}")
+ return user_prompt
diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py
index 35417abc0..bc778558f 100644
--- a/main/xiaozhi-server/core/utils/util.py
+++ b/main/xiaozhi-server/core/utils/util.py
@@ -96,11 +96,23 @@ def is_private_ip(ip_addr):
def get_ip_info(ip_addr, logger):
try:
+ # 导入全局缓存管理器
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ # 先从缓存获取
+ cached_ip_info = cache_manager.get(CacheType.IP_INFO, ip_addr)
+ if cached_ip_info is not None:
+ return cached_ip_info
+
+ # 缓存未命中,调用API
if is_private_ip(ip_addr):
ip_addr = ""
url = f"https://whois.pconline.com.cn/ipJson.jsp?json=true&ip={ip_addr}"
resp = requests.get(url).json()
ip_info = {"city": resp.get("city")}
+
+ # 存入缓存
+ cache_manager.set(CacheType.IP_INFO, ip_addr, ip_info)
return ip_info
except Exception as e:
logger.bind(tag=TAG).error(f"Error getting client ip info: {e}")
@@ -982,3 +994,28 @@ def sanitize_tool_name(name: str) -> str:
"""Sanitize tool names for OpenAI compatibility."""
# 支持中文、英文字母、数字、下划线和连字符
return re.sub(r"[^a-zA-Z0-9_\-\u4e00-\u9fff]", "_", name)
+
+
+def validate_mcp_endpoint(mcp_endpoint: str) -> bool:
+ """
+ 校验MCP接入点格式
+
+ Args:
+ mcp_endpoint: MCP接入点字符串
+
+ Returns:
+ bool: 是否有效
+ """
+ # 1. 检查是否以ws开头
+ if not mcp_endpoint.startswith("ws"):
+ return False
+
+ # 2. 检查是否包含key、call字样
+ if "key" in mcp_endpoint.lower() or "call" in mcp_endpoint.lower():
+ return False
+
+ # 3. 检查是否包含/mcp/字样
+ if "/mcp/" not in mcp_endpoint:
+ return False
+
+ return True
diff --git a/main/xiaozhi-server/core/utils/voiceprint_provider.py b/main/xiaozhi-server/core/utils/voiceprint_provider.py
new file mode 100644
index 000000000..b241fb513
--- /dev/null
+++ b/main/xiaozhi-server/core/utils/voiceprint_provider.py
@@ -0,0 +1,134 @@
+import asyncio
+import json
+import time
+import aiohttp
+from urllib.parse import urlparse, parse_qs
+from typing import Optional, Dict
+from config.logger import setup_logging
+
+TAG = __name__
+logger = setup_logging()
+
+
+class VoiceprintProvider:
+ """声纹识别服务提供者"""
+
+ def __init__(self, config: dict):
+ self.original_url = config.get("url", "")
+ self.speakers = config.get("speakers", [])
+ self.speaker_map = self._parse_speakers()
+
+ # 解析API地址和密钥
+ self.api_url = None
+ self.api_key = None
+ self.speaker_ids = []
+
+ if not self.original_url:
+ logger.bind(tag=TAG).warning("声纹识别URL未配置,声纹识别将被禁用")
+ self.enabled = False
+ else:
+ # 解析URL和key
+ parsed_url = urlparse(self.original_url)
+ base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
+
+ # 从查询参数中提取key
+ query_params = parse_qs(parsed_url.query)
+ self.api_key = query_params.get('key', [''])[0]
+
+ if not self.api_key:
+ logger.bind(tag=TAG).error("URL中未找到key参数,声纹识别将被禁用")
+ self.enabled = False
+ else:
+ # 构造identify接口地址
+ self.api_url = f"{base_url}/voiceprint/identify"
+
+ # 提取speaker_ids
+ for speaker_str in self.speakers:
+ try:
+ parts = speaker_str.split(",", 2)
+ if len(parts) >= 1:
+ speaker_id = parts[0].strip()
+ self.speaker_ids.append(speaker_id)
+ except Exception:
+ continue
+
+ # 检查是否有有效的说话人配置
+ if not self.speaker_ids:
+ logger.bind(tag=TAG).warning("未配置有效的说话人,声纹识别将被禁用")
+ self.enabled = False
+ else:
+ self.enabled = True
+ logger.bind(tag=TAG).info(f"声纹识别已配置: API={self.api_url}, 说话人={len(self.speaker_ids)}个")
+
+ def _parse_speakers(self) -> Dict[str, Dict[str, str]]:
+ """解析说话人配置"""
+ speaker_map = {}
+ for speaker_str in self.speakers:
+ try:
+ parts = speaker_str.split(",", 2)
+ if len(parts) >= 3:
+ speaker_id, name, description = parts[0].strip(), parts[1].strip(), parts[2].strip()
+ speaker_map[speaker_id] = {
+ "name": name,
+ "description": description
+ }
+ except Exception as e:
+ logger.bind(tag=TAG).warning(f"解析说话人配置失败: {speaker_str}, 错误: {e}")
+ return speaker_map
+
+ async def identify_speaker(self, audio_data: bytes, session_id: str) -> Optional[str]:
+ """识别说话人"""
+ if not self.enabled or not self.api_url or not self.api_key:
+ logger.bind(tag=TAG).debug("声纹识别功能已禁用或未配置,跳过识别")
+ return None
+
+ try:
+ api_start_time = time.monotonic()
+
+ # 准备请求头
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Accept': 'application/json'
+ }
+
+ # 准备multipart/form-data数据
+ data = aiohttp.FormData()
+ data.add_field('speaker_ids', ','.join(self.speaker_ids))
+ data.add_field('file', audio_data, filename='audio.wav', content_type='audio/wav')
+
+ timeout = aiohttp.ClientTimeout(total=10)
+
+ # 网络请求
+ async with aiohttp.ClientSession(timeout=timeout) as session:
+ async with session.post(self.api_url, headers=headers, data=data) as response:
+
+ if response.status == 200:
+ result = await response.json()
+ speaker_id = result.get("speaker_id")
+ score = result.get("score", 0)
+ total_elapsed_time = time.monotonic() - api_start_time
+
+ logger.bind(tag=TAG).info(f"声纹识别耗时: {total_elapsed_time:.3f}s")
+
+ # 置信度检查
+ if score < 0.5:
+ logger.bind(tag=TAG).warning(f"声纹识别置信度较低: {score:.3f}")
+
+ if speaker_id and speaker_id in self.speaker_map:
+ result_name = self.speaker_map[speaker_id]["name"]
+ return result_name
+ else:
+ logger.bind(tag=TAG).warning(f"未识别的说话人ID: {speaker_id}")
+ return "未知说话人"
+ else:
+ logger.bind(tag=TAG).error(f"声纹识别API错误: HTTP {response.status}")
+ return None
+
+ except asyncio.TimeoutError:
+ elapsed = time.monotonic() - api_start_time
+ logger.bind(tag=TAG).error(f"声纹识别超时: {elapsed:.3f}s")
+ return None
+ except Exception as e:
+ elapsed = time.monotonic() - api_start_time
+ logger.bind(tag=TAG).error(f"声纹识别失败: {e}")
+ return None
diff --git a/main/xiaozhi-server/core/websocket_server.py b/main/xiaozhi-server/core/websocket_server.py
index 1a1ba4c04..09ba79615 100644
--- a/main/xiaozhi-server/core/websocket_server.py
+++ b/main/xiaozhi-server/core/websocket_server.py
@@ -57,8 +57,25 @@ class WebSocketServer:
self.active_connections.add(handler)
try:
await handler.handle_connection(websocket)
+ except Exception as e:
+ self.logger.bind(tag=TAG).error(f"处理连接时出错: {e}")
finally:
+ # 确保从活动连接集合中移除
self.active_connections.discard(handler)
+ # 强制关闭连接(如果还没有关闭的话)
+ try:
+ # 安全地检查WebSocket状态并关闭
+ if hasattr(websocket, "closed") and not websocket.closed:
+ await websocket.close()
+ elif hasattr(websocket, "state") and websocket.state.name != "CLOSED":
+ await websocket.close()
+ else:
+ # 如果没有closed属性,直接尝试关闭
+ await websocket.close()
+ except Exception as close_error:
+ self.logger.bind(tag=TAG).error(
+ f"服务器端强制关闭连接时出错: {close_error}"
+ )
async def _http_response(self, websocket, request_headers):
# 检查是否为 WebSocket 升级请求
diff --git a/main/xiaozhi-server/plugins_func/functions/get_time.py b/main/xiaozhi-server/plugins_func/functions/get_time.py
index 44732bbac..766e19fd6 100644
--- a/main/xiaozhi-server/plugins_func/functions/get_time.py
+++ b/main/xiaozhi-server/plugins_func/functions/get_time.py
@@ -2,59 +2,27 @@ from datetime import datetime
import cnlunar
from plugins_func.register import register_function, ToolType, ActionResponse, Action
-# 添加星期映射字典
-WEEKDAY_MAP = {
- "Monday": "星期一",
- "Tuesday": "星期二",
- "Wednesday": "星期三",
- "Thursday": "星期四",
- "Friday": "星期五",
- "Saturday": "星期六",
- "Sunday": "星期日",
-}
-
-get_time_function_desc = {
- "type": "function",
- "function": {
- "name": "get_time",
- "description": "获取今天日期或者当前时间信息",
- "parameters": {"type": "object", "properties": {}, "required": []},
- },
-}
-
-
-@register_function("get_time", get_time_function_desc, ToolType.WAIT)
-def get_time():
- """
- 获取当前的日期时间信息
- """
- now = datetime.now()
- current_time = now.strftime("%H:%M:%S")
- current_date = now.strftime("%Y-%m-%d")
- current_weekday = WEEKDAY_MAP[now.strftime("%A")]
- response_text = (
- f"当前日期: {current_date},当前时间: {current_time}, {current_weekday}"
- )
-
- return ActionResponse(Action.REQLLM, response_text, None)
-
-
get_lunar_function_desc = {
"type": "function",
"function": {
"name": "get_lunar",
"description": (
- "用于获取今天的阴历/农历和黄历信息。"
+ "用于具体日期的阴历/农历和黄历信息。"
"用户可以指定查询内容,如:阴历日期、天干地支、节气、生肖、星座、八字、宜忌等。"
"如果没有指定查询内容,则默认查询干支年和农历日期。"
+ "对于'今天农历是多少'、'今天农历日期'这样的基本查询,请直接使用context中的信息,不要调用此工具。"
),
"parameters": {
"type": "object",
"properties": {
+ "date": {
+ "type": "string",
+ "description": "要查询的日期,格式为YYYY-MM-DD,例如2024-01-01。如果不提供,则使用当前日期",
+ },
"query": {
"type": "string",
"description": "要查询的内容,例如阴历日期、天干地支、节日、节气、生肖、星座、八字、宜忌等",
- }
+ },
},
"required": [],
},
@@ -63,23 +31,41 @@ get_lunar_function_desc = {
@register_function("get_lunar", get_lunar_function_desc, ToolType.WAIT)
-def get_lunar(query=None):
+def get_lunar(date=None, query=None):
"""
用于获取当前的阴历/农历,和天干地支、节气、生肖、星座、八字、宜忌等黄历信息
"""
- now = datetime.now()
- current_time = now.strftime("%H:%M:%S")
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ # 如果提供了日期参数,则使用指定日期;否则使用当前日期
+ if date:
+ try:
+ now = datetime.strptime(date, "%Y-%m-%d")
+ except ValueError:
+ return ActionResponse(
+ Action.REQLLM,
+ f"日期格式错误,请使用YYYY-MM-DD格式,例如:2024-01-01",
+ None,
+ )
+ else:
+ now = datetime.now()
+
current_date = now.strftime("%Y-%m-%d")
- current_weekday = WEEKDAY_MAP[now.strftime("%A")]
# 如果 query 为 None,则使用默认文本
if query is None:
query = "默认查询干支年和农历日期"
+
+ # 尝试从缓存获取农历信息
+ lunar_cache_key = f"lunar_info_{current_date}"
+ cached_lunar_info = cache_manager.get(CacheType.LUNAR, lunar_cache_key)
+ if cached_lunar_info:
+ return ActionResponse(Action.REQLLM, cached_lunar_info, None)
+
response_text = f"根据以下信息回应用户的查询请求,并提供与{query}相关的信息:\n"
lunar = cnlunar.Lunar(now, godType="8char")
response_text += (
- f"当前公历日期: {current_date},当前时间: {current_time},{current_weekday}\n"
"农历信息:\n"
"%s年%s%s\n" % (lunar.lunarYearCn, lunar.lunarMonthCn[:-1], lunar.lunarDayCn)
+ "干支: %s年 %s月 %s日\n" % (lunar.year8Char, lunar.month8Char, lunar.day8Char)
@@ -135,4 +121,7 @@ def get_lunar(query=None):
+ "(默认返回干支年和农历日期;仅在要求查询宜忌信息时才返回本日宜忌)"
)
+ # 缓存农历信息
+ cache_manager.set(CacheType.LUNAR, lunar_cache_key, response_text)
+
return ActionResponse(Action.REQLLM, response_text, None)
diff --git a/main/xiaozhi-server/plugins_func/functions/get_weather.py b/main/xiaozhi-server/plugins_func/functions/get_weather.py
index 75c15c7b8..a3af6c030 100644
--- a/main/xiaozhi-server/plugins_func/functions/get_weather.py
+++ b/main/xiaozhi-server/plugins_func/functions/get_weather.py
@@ -151,20 +151,44 @@ def parse_weather_info(soup):
@register_function("get_weather", GET_WEATHER_FUNCTION_DESC, ToolType.SYSTEM_CTL)
def get_weather(conn, location: str = None, lang: str = "zh_CN"):
- api_host = conn.config["plugins"]["get_weather"].get("api_host", "mj7p3y7naa.re.qweatherapi.com")
- api_key = conn.config["plugins"]["get_weather"].get("api_key", "a861d0d5e7bf4ee1a83d9a9e4f96d4da")
+ from core.utils.cache.manager import cache_manager, CacheType
+
+ api_host = conn.config["plugins"]["get_weather"].get(
+ "api_host", "mj7p3y7naa.re.qweatherapi.com"
+ )
+ api_key = conn.config["plugins"]["get_weather"].get(
+ "api_key", "a861d0d5e7bf4ee1a83d9a9e4f96d4da"
+ )
default_location = conn.config["plugins"]["get_weather"]["default_location"]
client_ip = conn.client_ip
+
# 优先使用用户提供的location参数
if not location:
# 通过客户端IP解析城市
if client_ip:
- # 动态解析IP对应的城市信息
- ip_info = get_ip_info(client_ip, logger)
- location = ip_info.get("city") if ip_info and "city" in ip_info else None
+ # 先从缓存获取IP对应的城市信息
+ cached_ip_info = cache_manager.get(CacheType.IP_INFO, client_ip)
+ if cached_ip_info:
+ location = cached_ip_info.get("city")
+ else:
+ # 缓存未命中,调用API获取
+ ip_info = get_ip_info(client_ip, logger)
+ if ip_info:
+ cache_manager.set(CacheType.IP_INFO, client_ip, ip_info)
+ location = ip_info.get("city")
+
+ if not location:
+ location = default_location
else:
- # 若IP解析失败或无IP,使用默认位置
+ # 若无IP,使用默认位置
location = default_location
+ # 尝试从缓存获取完整天气报告
+ weather_cache_key = f"full_weather_{location}_{lang}"
+ cached_weather_report = cache_manager.get(CacheType.WEATHER, weather_cache_key)
+ if cached_weather_report:
+ return ActionResponse(Action.REQLLM, cached_weather_report, None)
+
+ # 缓存未命中,获取实时天气数据
city_info = fetch_city_info(location, api_key, api_host)
if not city_info:
return ActionResponse(
@@ -192,4 +216,7 @@ def get_weather(conn, location: str = None, lang: str = "zh_CN"):
# 提示语
weather_report += "\n(如需某一天的具体天气,请告诉我日期)"
+ # 缓存完整的天气报告
+ cache_manager.set(CacheType.WEATHER, weather_cache_key, weather_report)
+
return ActionResponse(Action.REQLLM, weather_report, None)
diff --git a/main/xiaozhi-server/plugins_func/functions/play_music.py b/main/xiaozhi-server/plugins_func/functions/play_music.py
index 6d184b6b4..2cbc40188 100644
--- a/main/xiaozhi-server/plugins_func/functions/play_music.py
+++ b/main/xiaozhi-server/plugins_func/functions/play_music.py
@@ -1,13 +1,10 @@
-from config.logger import setup_logging
import os
import re
import time
import random
-import asyncio
import difflib
import traceback
from pathlib import Path
-from core.utils import p3
from core.handle.sendAudioHandle import send_stt_message
from plugins_func.register import register_function, ToolType, ActionResponse, Action
from core.utils.dialogue import Message
@@ -51,8 +48,8 @@ def play_music(conn, song_name: str):
)
# 提交异步任务
- future = asyncio.run_coroutine_threadsafe(
- handle_music_command(conn, music_intent), conn.loop
+ task = conn.loop.create_task(
+ handle_music_command(conn, music_intent) # 封装异步逻辑
)
# 非阻塞回调处理
@@ -63,7 +60,7 @@ def play_music(conn, song_name: str):
except Exception as e:
conn.logger.bind(tag=TAG).error(f"播放失败: {e}")
- future.add_done_callback(handle_done)
+ task.add_done_callback(handle_done)
return ActionResponse(
action=Action.NONE, result="指令已接收", response="正在为您播放音乐"
@@ -178,13 +175,13 @@ def _get_random_play_prompt(song_name):
# 移除文件扩展名
clean_name = os.path.splitext(song_name)[0]
prompts = [
- f"正在为您播放,{clean_name}",
- f"请欣赏歌曲,{clean_name}",
- f"即将为您播放,{clean_name}",
- f"为您带来,{clean_name}",
- f"让我们聆听,{clean_name}",
- f"接下来请欣赏,{clean_name}",
- f"为您献上,{clean_name}",
+ f"正在为您播放,《{clean_name}》",
+ f"请欣赏歌曲,《{clean_name}》",
+ f"即将为您播放,《{clean_name}》",
+ f"现在为您带来,《{clean_name}》",
+ f"让我们一起聆听,《{clean_name}》",
+ f"接下来请欣赏,《{clean_name}》",
+ f"此刻为您献上,《{clean_name}》",
]
# 直接使用random.choice,不设置seed
return random.choice(prompts)
@@ -218,13 +215,14 @@ async def play_local_music(conn, specific_file=None):
await send_stt_message(conn, text)
conn.dialogue.put(Message(role="assistant", content=text))
- conn.tts.tts_text_queue.put(
- TTSMessageDTO(
- sentence_id=conn.sentence_id,
- sentence_type=SentenceType.FIRST,
- content_type=ContentType.ACTION,
+ if conn.intent_type == "intent_llm":
+ conn.tts.tts_text_queue.put(
+ TTSMessageDTO(
+ sentence_id=conn.sentence_id,
+ sentence_type=SentenceType.FIRST,
+ content_type=ContentType.ACTION,
+ )
)
- )
conn.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=conn.sentence_id,
@@ -241,13 +239,14 @@ async def play_local_music(conn, specific_file=None):
content_file=music_path,
)
)
- conn.tts.tts_text_queue.put(
- TTSMessageDTO(
- sentence_id=conn.sentence_id,
- sentence_type=SentenceType.LAST,
- content_type=ContentType.ACTION,
+ if conn.intent_type == "intent_llm":
+ conn.tts.tts_text_queue.put(
+ TTSMessageDTO(
+ sentence_id=conn.sentence_id,
+ sentence_type=SentenceType.LAST,
+ content_type=ContentType.ACTION,
+ )
)
- )
except Exception as e:
conn.logger.bind(tag=TAG).error(f"播放音乐失败: {str(e)}")
diff --git a/main/xiaozhi-server/requirements.txt b/main/xiaozhi-server/requirements.txt
index a60ad6897..522b129d3 100755
--- a/main/xiaozhi-server/requirements.txt
+++ b/main/xiaozhi-server/requirements.txt
@@ -21,7 +21,7 @@ cozepy==0.12.0
mem0ai==0.1.62
bs4==0.0.2
modelscope==1.23.2
-sherpa_onnx==1.12.0
+sherpa_onnx==1.12.4
mcp==1.8.1
cnlunar==0.2.0
PySocks==1.7.1
@@ -33,4 +33,5 @@ markitdown==0.1.1
mcp-proxy==0.8.0
PyJWT==2.8.0
psutil==7.0.0
-portalocker==2.10.1
\ No newline at end of file
+portalocker==2.10.1
+Jinja2==3.1.6
\ No newline at end of file