Files
xiaozhi-esp32-server/main/xiaozhi-server/core/connection.py
T

992 lines
38 KiB
Python
Raw Normal View History

2025-02-02 23:01:14 +08:00
import os
2025-05-27 18:06:44 +08:00
import sys
2025-04-12 17:36:04 +08:00
import copy
2025-02-02 23:01:14 +08:00
import json
import uuid
import time
import queue
import asyncio
import threading
2025-05-27 18:06:44 +08:00
import traceback
import subprocess
2025-02-02 23:01:14 +08:00
import websockets
2025-05-29 13:42:31 +08:00
from core.handle.mcpHandle import call_mcp_tool
2025-03-31 21:40:48 +08:00
from core.utils.util import (
extract_json_from_string,
2025-05-07 18:06:13 +08:00
check_vad_update,
check_asr_update,
2025-05-20 22:58:42 +08:00
filter_sensitive_info,
2025-03-31 21:40:48 +08:00
)
2025-05-27 18:06:44 +08:00
from typing import Dict, Any
from core.mcp.manager import MCPManager
2025-05-29 23:56:34 +08:00
from core.utils.modules_initialize import (
initialize_modules,
initialize_tts,
initialize_asr,
)
2025-05-27 18:06:44 +08:00
from core.handle.reportHandle import report
from core.providers.tts.default import DefaultTTS
2025-05-24 12:11:13 +08:00
from concurrent.futures import ThreadPoolExecutor
2025-05-27 18:06:44 +08:00
from core.utils.dialogue import Message, Dialogue
2025-05-29 23:56:34 +08:00
from core.providers.asr.dto.dto import InterfaceType
2025-05-27 18:06:44 +08:00
from core.handle.textHandle import handleTextMessage
2025-03-15 11:48:14 +08:00
from core.handle.functionHandler import FunctionHandler
2025-05-27 18:06:44 +08:00
from plugins_func.loadplugins import auto_import_modules
2025-03-20 11:52:37 +08:00
from plugins_func.register import Action, ActionResponse
from core.auth import AuthMiddleware, AuthenticationError
from config.config_loader import get_private_config_from_api
2025-05-27 18:06:44 +08:00
from core.providers.tts.dto.dto import ContentType, TTSMessageDTO, SentenceType
2025-05-28 01:13:25 +08:00
from config.logger import setup_logging, build_module_string, update_module_string
from config.manage_api_client import DeviceNotFoundException, DeviceBindException
2025-05-27 18:06:44 +08:00
2025-02-26 01:33:05 +08:00
2025-02-18 00:07:19 +08:00
TAG = __name__
2025-03-31 21:40:48 +08:00
auto_import_modules("plugins_func.functions")
2025-02-26 01:33:05 +08:00
class TTSException(RuntimeError):
pass
2025-02-02 23:01:14 +08:00
class ConnectionHandler:
2025-03-31 21:40:48 +08:00
def __init__(
2025-05-02 00:07:36 +08:00
self,
config: Dict[str, Any],
_vad,
_asr,
_llm,
_memory,
_intent,
server=None,
2025-03-31 21:40:48 +08:00
):
2025-05-07 18:06:13 +08:00
self.common_config = config
self.config = copy.deepcopy(config)
self.session_id = str(uuid.uuid4())
2025-02-18 00:07:19 +08:00
self.logger = setup_logging()
self.server = server # 保存server实例的引用
2025-02-02 23:01:14 +08:00
2025-05-09 11:39:32 +08:00
self.auth = AuthMiddleware(config)
2025-04-15 18:30:12 +08:00
self.need_bind = False
self.bind_code = None
2025-05-02 00:07:36 +08:00
self.read_config_from_api = self.config.get("read_config_from_api", False)
2025-04-15 18:30:12 +08:00
2025-02-02 23:01:14 +08:00
self.websocket = None
self.headers = None
2025-04-30 17:29:27 +08:00
self.device_id = None
2025-03-17 14:20:40 +08:00
self.client_ip = None
self.client_ip_info = {}
2025-02-02 23:01:14 +08:00
self.prompt = None
self.welcome_msg = None
self.max_output_size = 0
self.chat_history_conf = 0
2025-06-04 11:41:04 +08:00
self.audio_format = "opus"
2025-02-04 13:57:05 +08:00
# 客户端状态相关
2025-02-04 12:20:10 +08:00
self.client_abort = False
2025-05-30 15:47:32 +08:00
self.client_is_speaking = False
2025-02-04 13:57:05 +08:00
self.client_listen_mode = "auto"
2025-02-02 23:01:14 +08:00
# 线程任务相关
self.loop = asyncio.get_event_loop()
self.stop_event = threading.Event()
self.executor = ThreadPoolExecutor(max_workers=5)
2025-02-02 23:01:14 +08:00
2025-05-13 15:54:00 +08:00
# 添加上报线程池
self.report_queue = queue.Queue()
self.report_thread = None
# 未来可以通过修改此处,调节asr的上报和tts的上报,目前默认都开启
self.report_asr_enable = self.read_config_from_api
self.report_tts_enable = self.read_config_from_api
2025-04-30 17:29:27 +08:00
2025-02-02 23:01:14 +08:00
# 依赖的组件
2025-05-07 18:06:13 +08:00
self.vad = None
self.asr = None
2025-05-26 22:30:45 +08:00
self.tts = None
2025-05-07 18:06:13 +08:00
self._asr = _asr
self._vad = _vad
2025-02-02 23:01:14 +08:00
self.llm = _llm
2025-03-03 15:00:04 +08:00
self.memory = _memory
2025-03-09 21:33:45 +08:00
self.intent = _intent
2025-02-02 23:01:14 +08:00
# vad相关变量
self.client_audio_buffer = bytearray()
2025-02-02 23:01:14 +08:00
self.client_have_voice = False
self.client_have_voice_last_time = 0.0
self.client_no_voice_last_time = 0.0
2025-02-02 23:01:14 +08:00
self.client_voice_stop = False
# asr相关变量
2025-06-04 11:41:04 +08:00
# 因为实际部署时可能会用到公共的本地ASR,不能把变量暴露给公共ASR
# 所以涉及到ASR的变量,需要在这里定义,属于connection的私有变量
2025-02-02 23:01:14 +08:00
self.asr_audio = []
2025-06-04 11:41:04 +08:00
self.asr_audio_queue = queue.Queue()
2025-02-02 23:01:14 +08:00
# llm相关变量
2025-05-26 02:20:38 +08:00
self.llm_finish_task = True
2025-02-02 23:01:14 +08:00
self.dialogue = Dialogue()
# tts相关变量
2025-05-26 02:20:38 +08:00
self.sentence_id = None
2025-02-02 23:01:14 +08:00
2025-02-24 18:06:13 +08:00
# iot相关变量
self.iot_descriptors = {}
2025-03-31 22:54:37 +08:00
self.func_handler = None
2025-02-24 18:06:13 +08:00
2025-04-12 17:36:04 +08:00
self.cmd_exit = self.config["exit_commands"]
2025-02-14 23:09:12 +08:00
self.max_cmd_length = 0
for cmd in self.cmd_exit:
if len(cmd) > self.max_cmd_length:
self.max_cmd_length = len(cmd)
2025-02-26 01:33:05 +08:00
2025-05-09 11:39:32 +08:00
# 是否在聊天结束后关闭连接
self.close_after_chat = False
self.load_function_plugin = False
self.intent_type = "nointent"
2025-03-09 21:33:45 +08:00
self.timeout_task = None
self.timeout_seconds = (
int(self.config.get("close_connection_no_voice_time", 120)) + 60
) # 在原来第一道关闭的基础上加60秒,进行二道关闭
2025-05-28 11:49:18 +08:00
# {"mcp":true} 表示启用MCP功能
self.features = None
2025-05-06 14:57:29 +08:00
2025-02-02 23:01:14 +08:00
async def handle_connection(self, ws):
try:
2025-02-13 17:06:48 +08:00
# 获取并验证headers
self.headers = dict(ws.request.headers)
2025-04-16 16:44:44 +08:00
if self.headers.get("device-id", None) is None:
# 尝试从 URL 的查询参数中获取 device-id
from urllib.parse import parse_qs, urlparse
# 从 WebSocket 请求中获取路径
request_path = ws.request.path
if not request_path:
self.logger.bind(tag=TAG).error("无法获取请求路径")
return
parsed_url = urlparse(request_path)
query_params = parse_qs(parsed_url.query)
if "device-id" in query_params:
self.headers["device-id"] = query_params["device-id"][0]
self.headers["client-id"] = query_params["client-id"][0]
else:
2025-05-06 13:10:26 +08:00
await ws.send("端口正常,如需测试连接,请使用test_page.html")
await self.close(ws)
2025-04-16 16:44:44 +08:00
return
2025-02-26 01:33:05 +08:00
# 获取客户端ip地址
2025-03-17 14:20:40 +08:00
self.client_ip = ws.remote_address[0]
2025-03-31 21:40:48 +08:00
self.logger.bind(tag=TAG).info(
f"{self.client_ip} conn - Headers: {self.headers}"
)
2025-02-14 23:09:12 +08:00
2025-02-13 17:06:48 +08:00
# 进行认证
await self.auth.authenticate(self.headers)
2025-02-26 01:33:05 +08:00
# 认证通过,继续处理
self.websocket = ws
2025-04-30 17:29:27 +08:00
self.device_id = self.headers.get("device-id", None)
# 启动超时检查任务
self.timeout_task = asyncio.create_task(self._check_timeout())
self.welcome_msg = self.config["xiaozhi"]
self.welcome_msg["session_id"] = self.session_id
await self.websocket.send(json.dumps(self.welcome_msg))
# 获取差异化配置
2025-05-07 18:06:13 +08:00
self._initialize_private_config()
2025-03-17 14:20:40 +08:00
# 异步初始化
2025-05-07 18:06:13 +08:00
self.executor.submit(self._initialize_components)
2025-03-01 01:54:55 +08:00
2025-02-13 17:06:48 +08:00
try:
async for message in self.websocket:
await self._route_message(message)
except websockets.exceptions.ConnectionClosed:
2025-02-18 00:07:19 +08:00
self.logger.bind(tag=TAG).info("客户端断开连接")
2025-02-14 23:09:12 +08:00
2025-02-13 17:06:48 +08:00
except AuthenticationError as e:
2025-02-18 00:07:19 +08:00
self.logger.bind(tag=TAG).error(f"Authentication failed: {str(e)}")
2025-02-13 17:06:48 +08:00
return
except Exception as e:
2025-02-24 22:04:48 +08:00
stack_trace = traceback.format_exc()
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
2025-02-13 17:06:48 +08:00
return
2025-03-03 15:00:04 +08:00
finally:
2025-04-01 00:03:05 +08:00
await self._save_and_close(ws)
async def _save_and_close(self, ws):
"""保存记忆并关闭连接"""
try:
2025-05-06 13:10:26 +08:00
if self.memory:
2025-05-14 11:22:09 +08:00
# 使用线程池异步保存记忆
def save_memory_task():
try:
# 创建新事件循环(避免与主循环冲突)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
2025-05-15 16:13:32 +08:00
loop.run_until_complete(
self.memory.save_memory(self.dialogue.dialogue)
)
2025-05-14 11:22:09 +08:00
except Exception as e:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
loop.close()
# 启动线程保存记忆,不等待完成
threading.Thread(target=save_memory_task, daemon=True).start()
except Exception as e:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
2025-05-14 11:22:09 +08:00
# 立即关闭连接,不等待记忆保存完成
await self.close(ws)
2025-02-02 23:01:14 +08:00
async def reset_timeout(self):
"""重置超时计时器"""
if self.timeout_task:
self.timeout_task.cancel()
self.timeout_task = asyncio.create_task(self._check_timeout())
2025-02-02 23:01:14 +08:00
async def _route_message(self, message):
"""消息路由"""
# 重置超时计时器
await self.reset_timeout()
2025-02-02 23:01:14 +08:00
if isinstance(message, str):
2025-02-04 13:57:05 +08:00
await handleTextMessage(self, message)
2025-02-02 23:01:14 +08:00
elif isinstance(message, bytes):
2025-06-04 11:41:04 +08:00
if self.vad is None:
return
if self.asr is None:
return
self.asr_audio_queue.put(message)
2025-02-02 23:01:14 +08:00
2025-05-07 14:36:08 +08:00
async def handle_restart(self, message):
"""处理服务器重启请求"""
try:
self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...")
# 发送确认响应
await self.websocket.send(
json.dumps(
{
"type": "server",
"status": "success",
"message": "服务器重启中...",
2025-05-20 22:58:42 +08:00
"content": {"action": "restart"},
}
)
)
2025-05-07 14:36:08 +08:00
# 异步执行重启操作
def restart_server():
"""实际执行重启的方法"""
time.sleep(1)
self.logger.bind(tag=TAG).info("执行服务器重启...")
2025-05-07 16:17:50 +08:00
subprocess.Popen(
[sys.executable, "app.py"],
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
start_new_session=True,
2025-05-07 16:17:50 +08:00
)
os._exit(0)
2025-05-07 14:36:08 +08:00
# 使用线程执行重启避免阻塞事件循环
threading.Thread(target=restart_server, daemon=True).start()
except Exception as e:
self.logger.bind(tag=TAG).error(f"重启失败: {str(e)}")
await self.websocket.send(
json.dumps(
{
"type": "server",
"status": "error",
"message": f"Restart failed: {str(e)}",
2025-05-20 22:58:42 +08:00
"content": {"action": "restart"},
}
)
)
2025-05-07 14:36:08 +08:00
2025-05-07 18:06:13 +08:00
def _initialize_components(self):
2025-05-27 18:51:08 +08:00
try:
2025-05-28 01:13:25 +08:00
self.selected_module_str = build_module_string(
self.config.get("selected_module", {})
)
update_module_string(self.selected_module_str)
2025-05-27 18:51:08 +08:00
"""初始化组件"""
if self.config.get("prompt") is not None:
self.prompt = self.config["prompt"]
self.change_system_prompt(self.prompt)
self.logger.bind(tag=TAG).info(
f"初始化组件: prompt成功 {self.prompt[:50]}..."
)
"""初始化本地组件"""
if self.vad is None:
self.vad = self._vad
if self.asr is None:
2025-05-29 23:56:34 +08:00
self.asr = self._initialize_asr()
# 打开语音识别通道
asyncio.run_coroutine_threadsafe(
self.asr.open_audio_channels(self), self.loop
)
2025-05-27 18:51:08 +08:00
if self.tts is None:
self.tts = self._initialize_tts()
2025-05-29 23:56:34 +08:00
# 打开语音合成通道
2025-05-27 18:51:08 +08:00
asyncio.run_coroutine_threadsafe(
self.tts.open_audio_channels(self), self.loop
)
2025-05-07 18:06:13 +08:00
2025-05-27 18:51:08 +08:00
"""加载记忆"""
self._initialize_memory()
"""加载意图识别"""
self._initialize_intent()
"""初始化上报线程"""
self._init_report_threads()
except Exception as e:
self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}")
2025-04-30 17:29:27 +08:00
def _init_report_threads(self):
"""初始化ASR和TTS上报线程"""
2025-05-06 13:10:26 +08:00
if not self.read_config_from_api or self.need_bind:
2025-05-02 00:07:36 +08:00
return
if self.chat_history_conf == 0:
return
if self.report_thread is None or not self.report_thread.is_alive():
self.report_thread = threading.Thread(
target=self._report_worker, daemon=True
2025-04-30 17:29:27 +08:00
)
self.report_thread.start()
2025-04-30 17:29:27 +08:00
self.logger.bind(tag=TAG).info("TTS上报线程已启动")
2025-03-18 14:26:27 +08:00
2025-05-26 22:30:45 +08:00
def _initialize_tts(self):
"""初始化TTS"""
tts = None
if not self.need_bind:
tts = initialize_tts(self.config)
if tts is None:
tts = DefaultTTS(self.config, delete_audio_file=True)
return tts
2025-05-29 23:56:34 +08:00
def _initialize_asr(self):
"""初始化ASR"""
if self._asr.interface_type == InterfaceType.LOCAL:
# 如果公共ASR是本地服务,则直接返回
# 因为本地一个实例ASR,可以被多个连接共享
asr = self._asr
else:
# 如果公共ASR是远程服务,则初始化一个新实例
# 因为远程ASR,涉及到websocket连接和接收线程,需要每个连接一个实例
asr = initialize_asr(self.config)
return asr
def _initialize_private_config(self):
2025-04-12 17:36:04 +08:00
"""如果是从配置文件获取,则进行二次实例化"""
2025-05-02 00:07:36 +08:00
if not self.read_config_from_api:
2025-04-12 17:36:04 +08:00
return
"""从接口获取差异化的配置进行二次实例化,非全量重新实例化"""
try:
begin_time = time.time()
2025-04-12 17:36:04 +08:00
private_config = get_private_config_from_api(
self.config,
self.headers.get("device-id"),
self.headers.get("client-id", self.headers.get("device-id")),
2025-04-12 17:36:04 +08:00
)
2025-04-15 00:44:34 +08:00
private_config["delete_audio"] = bool(self.config.get("delete_audio", True))
self.logger.bind(tag=TAG).info(
f"{time.time() - begin_time} 秒,获取差异化配置成功: {json.dumps(filter_sensitive_info(private_config), ensure_ascii=False)}"
)
2025-04-15 18:30:12 +08:00
except DeviceNotFoundException as e:
self.need_bind = True
private_config = {}
except DeviceBindException as e:
self.need_bind = True
self.bind_code = e.bind_code
private_config = {}
2025-04-12 17:36:04 +08:00
except Exception as e:
2025-04-15 18:30:12 +08:00
self.need_bind = True
2025-04-12 17:36:04 +08:00
self.logger.bind(tag=TAG).error(f"获取差异化配置失败: {e}")
private_config = {}
2025-05-07 18:06:13 +08:00
init_llm, init_tts, init_memory, init_intent = (
2025-04-12 17:36:04 +08:00
False,
False,
False,
False,
)
2025-05-07 18:06:13 +08:00
init_vad = check_vad_update(self.common_config, private_config)
init_asr = check_asr_update(self.common_config, private_config)
if private_config.get("TTS", None) is not None:
init_tts = True
self.config["TTS"] = private_config["TTS"]
self.config["selected_module"]["TTS"] = private_config["selected_module"][
"TTS"
2025-04-12 17:36:04 +08:00
]
if private_config.get("LLM", None) is not None:
init_llm = True
2025-04-13 18:10:17 +08:00
self.config["LLM"] = private_config["LLM"]
2025-04-12 17:36:04 +08:00
self.config["selected_module"]["LLM"] = private_config["selected_module"][
"LLM"
]
if private_config.get("Memory", None) is not None:
init_memory = True
2025-04-13 18:10:17 +08:00
self.config["Memory"] = private_config["Memory"]
2025-04-12 17:36:04 +08:00
self.config["selected_module"]["Memory"] = private_config[
"selected_module"
]["Memory"]
if private_config.get("Intent", None) is not None:
init_intent = True
2025-04-13 18:10:17 +08:00
self.config["Intent"] = private_config["Intent"]
model_intent = private_config.get('selected_module', {}).get('Intent', {})
self.config["selected_module"]["Intent"] = model_intent
# 加载插件配置
if model_intent != 'Intent_nointent':
plugin_from_server = private_config.get("plugins", {})
for plugin, config_str in plugin_from_server.items():
plugin_from_server[plugin] = json.loads(config_str)
self.config['plugins'] = plugin_from_server
2025-05-07 18:06:13 +08:00
if private_config.get("prompt", None) is not None:
self.config["prompt"] = private_config["prompt"]
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:
self.max_output_size = int(private_config["device_max_output_size"])
if private_config.get("chat_history_conf", None) is not None:
self.chat_history_conf = int(private_config["chat_history_conf"])
2025-04-12 17:36:04 +08:00
try:
modules = initialize_modules(
self.logger,
private_config,
init_vad,
init_asr,
init_llm,
2025-05-07 18:06:13 +08:00
init_tts,
2025-04-12 17:36:04 +08:00
init_memory,
init_intent,
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"初始化组件失败: {e}")
modules = {}
2025-05-07 18:06:13 +08:00
if modules.get("tts", None) is not None:
self.tts = modules["tts"]
if modules.get("vad", None) is not None:
self.vad = modules["vad"]
if modules.get("asr", None) is not None:
self.asr = modules["asr"]
2025-04-12 17:36:04 +08:00
if modules.get("llm", None) is not None:
self.llm = modules["llm"]
if modules.get("intent", None) is not None:
self.intent = modules["intent"]
if modules.get("memory", None) is not None:
self.memory = modules["memory"]
def _initialize_memory(self):
2025-06-01 13:34:32 +08:00
if self.memory is None:
return
"""初始化记忆模块"""
self.memory.init_memory(
2025-05-15 16:13:32 +08:00
role_id=self.device_id,
llm=self.llm,
summary_memory=self.config.get("summaryMemory", None),
save_to_file=not self.read_config_from_api,
)
# 获取记忆总结配置
memory_config = self.config["Memory"]
memory_type = self.config["Memory"][self.config["selected_module"]["Memory"]][
"type"
]
# 如果使用 nomen,直接返回
if memory_type == "nomem":
return
# 使用 mem_local_short 模式
elif memory_type == "mem_local_short":
memory_llm_name = memory_config[self.config["selected_module"]["Memory"]][
"llm"
]
if memory_llm_name and memory_llm_name in self.config["LLM"]:
# 如果配置了专用LLM,则创建独立的LLM实例
from core.utils import llm as llm_utils
memory_llm_config = self.config["LLM"][memory_llm_name]
memory_llm_type = memory_llm_config.get("type", memory_llm_name)
memory_llm = llm_utils.create_instance(
memory_llm_type, memory_llm_config
)
self.logger.bind(tag=TAG).info(
f"为记忆总结创建了专用LLM: {memory_llm_name}, 类型: {memory_llm_type}"
)
self.memory.set_llm(memory_llm)
else:
# 否则使用主LLM
self.memory.set_llm(self.llm)
self.logger.bind(tag=TAG).info("使用主LLM作为意图识别模型")
def _initialize_intent(self):
2025-06-01 13:34:32 +08:00
if self.intent is None:
return
2025-05-09 11:39:32 +08:00
self.intent_type = self.config["Intent"][
self.config["selected_module"]["Intent"]
]["type"]
if self.intent_type == "function_call" or self.intent_type == "intent_llm":
self.load_function_plugin = True
"""初始化意图识别模块"""
# 获取意图识别配置
intent_config = self.config["Intent"]
2025-04-13 18:10:17 +08:00
intent_type = self.config["Intent"][self.config["selected_module"]["Intent"]][
"type"
]
# 如果使用 nointent,直接返回
if intent_type == "nointent":
return
# 使用 intent_llm 模式
elif intent_type == "intent_llm":
2025-04-13 18:10:17 +08:00
intent_llm_name = intent_config[self.config["selected_module"]["Intent"]][
"llm"
]
if intent_llm_name and intent_llm_name in self.config["LLM"]:
# 如果配置了专用LLM,则创建独立的LLM实例
from core.utils import llm as llm_utils
intent_llm_config = self.config["LLM"][intent_llm_name]
intent_llm_type = intent_llm_config.get("type", intent_llm_name)
intent_llm = llm_utils.create_instance(
intent_llm_type, intent_llm_config
)
self.logger.bind(tag=TAG).info(
f"为意图识别创建了专用LLM: {intent_llm_name}, 类型: {intent_llm_type}"
)
self.intent.set_llm(intent_llm)
else:
# 否则使用主LLM
self.intent.set_llm(self.llm)
self.logger.bind(tag=TAG).info("使用主LLM作为意图识别模型")
"""加载插件"""
self.func_handler = FunctionHandler(self)
self.mcp_manager = MCPManager(self)
2025-03-24 10:28:48 +08:00
"""加载MCP工具"""
2025-03-31 21:40:48 +08:00
asyncio.run_coroutine_threadsafe(
self.mcp_manager.initialize_servers(), self.loop
)
2025-03-24 10:28:48 +08:00
2025-03-15 11:48:14 +08:00
def change_system_prompt(self, prompt):
self.prompt = prompt
2025-04-13 18:46:04 +08:00
# 更新系统prompt至上下文
self.dialogue.update_system_message(self.prompt)
2025-02-26 01:33:05 +08:00
def chat(self, query, tool_call=False):
2025-05-26 22:30:45 +08:00
self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}")
2025-02-02 23:01:14 +08:00
self.llm_finish_task = False
2025-03-18 14:26:27 +08:00
2025-03-15 11:48:14 +08:00
if not tool_call:
self.dialogue.put(Message(role="user", content=query))
2025-03-09 21:33:45 +08:00
# Define intent functions
functions = None
if self.intent_type == "function_call" and hasattr(self, "func_handler"):
functions = self.func_handler.get_functions()
2025-05-30 09:20:40 +08:00
if hasattr(self, "mcp_client"):
2025-05-29 13:42:31 +08:00
mcp_tools = self.mcp_client.get_available_tools()
if mcp_tools is not None and len(mcp_tools) > 0:
if functions is None:
functions = []
functions.extend(mcp_tools)
2025-03-09 21:33:45 +08:00
response_message = []
2025-03-18 14:26:27 +08:00
2025-03-09 21:33:45 +08:00
try:
# 使用带记忆的对话
2025-05-07 18:06:13 +08:00
memory_str = None
if self.memory is not None:
future = asyncio.run_coroutine_threadsafe(
self.memory.query_memory(query), self.loop
)
memory_str = future.result()
2025-03-09 21:33:45 +08:00
2025-05-25 08:56:58 +08:00
uuid_str = str(uuid.uuid4()).replace("-", "")
2025-05-26 02:20:38 +08:00
self.sentence_id = uuid_str
if 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),
functions=functions,
)
else:
llm_responses = self.llm.response(
self.session_id,
self.dialogue.get_llm_dialogue_with_memory(memory_str),
)
2025-03-09 21:33:45 +08:00
except Exception as e:
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
return None
# 处理流式响应
2025-03-11 00:25:33 +08:00
tool_call_flag = False
function_name = None
function_id = None
function_arguments = ""
content_arguments = ""
2025-05-26 02:20:38 +08:00
text_index = 0
2025-05-30 15:47:32 +08:00
self.client_abort = False
2025-03-09 21:33:45 +08:00
for response in llm_responses:
2025-05-29 23:56:34 +08:00
if self.client_abort:
break
2025-05-28 21:34:21 +08:00
if functions is not None:
content, tools_call = response
if "content" in response:
content = response["content"]
tools_call = None
if content is not None and len(content) > 0:
content_arguments += content
if not tool_call_flag and content_arguments.startswith("<tool_call>"):
# print("content_arguments", content_arguments)
tool_call_flag = True
2025-03-09 21:33:45 +08:00
2025-06-05 17:07:29 +08:00
if tools_call is not None and len(tools_call) > 0:
tool_call_flag = True
if tools_call[0].id is not None:
function_id = tools_call[0].id
if tools_call[0].function.name is not None:
function_name = tools_call[0].function.name
if tools_call[0].function.arguments is not None:
function_arguments += tools_call[0].function.arguments
else:
content = response
2025-03-11 00:25:33 +08:00
if content is not None and len(content) > 0:
if not tool_call_flag:
2025-03-11 00:25:33 +08:00
response_message.append(content)
2025-05-26 02:20:38 +08:00
if text_index == 0:
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
sentence_type=SentenceType.FIRST,
content_type=ContentType.ACTION,
2025-03-31 21:40:48 +08:00
)
2025-05-26 02:20:38 +08:00
)
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
sentence_type=SentenceType.MIDDLE,
content_type=ContentType.TEXT,
content_detail=content,
)
)
text_index += 1
2025-03-15 11:48:14 +08:00
# 处理function call
if tool_call_flag:
bHasError = False
if function_id is None:
a = extract_json_from_string(content_arguments)
if a is not None:
try:
content_arguments_json = json.loads(a)
function_name = content_arguments_json["name"]
2025-03-31 21:40:48 +08:00
function_arguments = json.dumps(
content_arguments_json["arguments"], ensure_ascii=False
)
2025-03-15 11:48:14 +08:00
function_id = str(uuid.uuid4().hex)
except Exception as e:
bHasError = True
response_message.append(a)
else:
bHasError = True
response_message.append(content_arguments)
if bHasError:
2025-03-31 21:40:48 +08:00
self.logger.bind(tag=TAG).error(
f"function call error: {content_arguments}"
)
2025-03-15 11:48:14 +08:00
if not bHasError:
2025-04-16 11:48:40 +08:00
response_message.clear()
self.logger.bind(tag=TAG).debug(
2025-03-31 21:40:48 +08:00
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}"
)
2025-03-15 11:48:14 +08:00
function_call_data = {
"name": function_name,
"id": function_id,
2025-03-31 21:40:48 +08:00
"arguments": function_arguments,
2025-03-15 11:48:14 +08:00
}
2025-03-20 08:59:45 +08:00
# 处理Server端MCP工具调用
2025-03-20 08:59:45 +08:00
if self.mcp_manager.is_mcp_tool(function_name):
2025-03-20 11:52:37 +08:00
result = self._handle_mcp_tool_call(function_call_data)
elif hasattr(self, "mcp_client") and self.mcp_client.has_tool(
function_name
):
# 如果是小智端MCP工具调用
2025-05-29 13:42:31 +08:00
self.logger.bind(tag=TAG).debug(
f"调用小智端MCP工具: {function_name}, 参数: {function_arguments}"
2025-05-29 13:42:31 +08:00
)
try:
result = asyncio.run_coroutine_threadsafe(
call_mcp_tool(
self, self.mcp_client, function_name, function_arguments
),
self.loop,
).result()
self.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}")
result = ActionResponse(
action=Action.REQLLM, result=result, response=""
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"MCP工具调用失败: {e}")
result = ActionResponse(
action=Action.REQLLM, result="MCP工具调用失败", response=""
)
2025-03-20 08:59:45 +08:00
else:
# 处理系统函数
2025-03-31 21:40:48 +08:00
result = self.func_handler.handle_llm_function_call(
self, function_call_data
)
2025-05-26 02:20:38 +08:00
self._handle_function_result(result, function_call_data)
2025-03-09 21:33:45 +08:00
# 存储对话内容
2025-03-18 14:26:27 +08:00
if len(response_message) > 0:
2025-03-31 21:40:48 +08:00
self.dialogue.put(
Message(role="assistant", content="".join(response_message))
)
2025-05-26 02:20:38 +08:00
if text_index > 0:
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
sentence_type=SentenceType.LAST,
content_type=ContentType.ACTION,
)
)
2025-03-09 21:33:45 +08:00
self.llm_finish_task = True
2025-03-31 21:40:48 +08:00
self.logger.bind(tag=TAG).debug(
json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)
)
2025-03-09 21:33:45 +08:00
return True
2025-03-20 11:52:37 +08:00
def _handle_mcp_tool_call(self, function_call_data):
function_arguments = function_call_data["arguments"]
function_name = function_call_data["name"]
try:
args_dict = function_arguments
if isinstance(function_arguments, str):
try:
args_dict = json.loads(function_arguments)
except json.JSONDecodeError:
2025-03-31 21:40:48 +08:00
self.logger.bind(tag=TAG).error(
f"无法解析 function_arguments: {function_arguments}"
)
return ActionResponse(
action=Action.REQLLM, result="参数解析失败", response=""
)
tool_result = asyncio.run_coroutine_threadsafe(
self.mcp_manager.execute_tool(function_name, args_dict), self.loop
).result()
2025-03-20 11:52:37 +08:00
# meta=None content=[TextContent(type='text', text='北京当前天气:\n温度: 21°C\n天气: 晴\n湿度: 6%\n风向: 西北 风\n风力等级: 5级', annotations=None)] isError=False
2025-03-20 18:20:09 +08:00
content_text = ""
if tool_result is not None and tool_result.content is not None:
for content in tool_result.content:
content_type = content.type
if content_type == "text":
content_text = content.text
elif content_type == "image":
pass
2025-03-31 21:40:48 +08:00
2025-03-20 18:20:09 +08:00
if len(content_text) > 0:
2025-03-31 21:40:48 +08:00
return ActionResponse(
action=Action.REQLLM, result=content_text, response=""
)
2025-03-20 11:52:37 +08:00
except Exception as e:
self.logger.bind(tag=TAG).error(f"MCP工具调用错误: {e}")
2025-03-31 21:40:48 +08:00
return ActionResponse(
action=Action.REQLLM, result="工具调用出错", response=""
)
2025-03-20 11:52:37 +08:00
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
2025-05-26 02:20:38 +08:00
def _handle_function_result(self, result, function_call_data):
2025-03-18 14:26:27 +08:00
if result.action == Action.RESPONSE: # 直接回复前端
2025-03-11 00:25:33 +08:00
text = result.response
2025-05-26 02:20:38 +08:00
self.tts.tts_one_sentence(self, ContentType.TEXT, content_detail=text)
2025-03-11 00:25:33 +08:00
self.dialogue.put(Message(role="assistant", content=text))
2025-03-18 14:26:27 +08:00
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
text = result.result
if text is not None and len(text) > 0:
function_id = function_call_data["id"]
function_name = function_call_data["name"]
function_arguments = function_call_data["arguments"]
2025-03-31 21:40:48 +08:00
self.dialogue.put(
Message(
role="assistant",
tool_calls=[
{
"id": function_id,
"function": {
"arguments": function_arguments,
"name": function_name,
},
"type": "function",
"index": 0,
}
],
)
)
2025-03-31 21:40:48 +08:00
self.dialogue.put(
2025-05-09 11:39:32 +08:00
Message(
role="tool",
tool_call_id=(
str(uuid.uuid4()) if function_id is None else function_id
),
content=text,
)
2025-03-31 21:40:48 +08:00
)
self.chat(text, tool_call=True)
elif result.action == Action.NOTFOUND or result.action == Action.ERROR:
2025-03-18 14:26:27 +08:00
text = result.result
2025-05-26 02:20:38 +08:00
self.tts.tts_one_sentence(self, ContentType.TEXT, content_detail=text)
2025-03-18 14:26:27 +08:00
self.dialogue.put(Message(role="assistant", content=text))
2025-03-18 00:34:16 +08:00
else:
pass
2025-03-11 00:25:33 +08:00
def _report_worker(self):
"""聊天记录上报工作线程"""
2025-04-30 17:29:27 +08:00
while not self.stop_event.is_set():
try:
# 从队列获取数据,设置超时以便定期检查停止事件
item = self.report_queue.get(timeout=1)
2025-04-30 17:29:27 +08:00
if item is None: # 检测毒丸对象
break
2025-05-26 20:27:22 +08:00
type, text, audio_data, report_time = item
2025-04-30 17:29:27 +08:00
try:
2025-05-28 18:22:31 +08:00
# 检查线程池状态
if self.executor is None:
continue
2025-05-13 15:54:00 +08:00
# 提交任务到线程池
self.executor.submit(
self._process_report, type, text, audio_data, report_time
)
2025-04-30 17:29:27 +08:00
except Exception as e:
self.logger.bind(tag=TAG).error(f"聊天记录上报线程异常: {e}")
2025-04-30 17:29:27 +08:00
except queue.Empty:
continue
except Exception as e:
self.logger.bind(tag=TAG).error(f"聊天记录上报工作线程异常: {e}")
2025-04-30 17:29:27 +08:00
self.logger.bind(tag=TAG).info("聊天记录上报线程已退出")
2025-04-30 17:29:27 +08:00
2025-05-26 20:27:22 +08:00
def _process_report(self, type, text, audio_data, report_time):
2025-05-13 15:54:00 +08:00
"""处理上报任务"""
try:
# 执行上报(传入二进制数据)
2025-05-26 20:27:22 +08:00
report(self, type, text, audio_data, report_time)
2025-05-13 15:54:00 +08:00
except Exception as e:
self.logger.bind(tag=TAG).error(f"上报处理异常: {e}")
finally:
# 标记任务完成
self.report_queue.task_done()
2025-02-02 23:01:14 +08:00
def clearSpeakStatus(self):
2025-05-30 15:47:32 +08:00
self.client_is_speaking = False
2025-02-18 00:07:19 +08:00
self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态")
2025-02-02 23:01:14 +08:00
async def close(self, ws=None):
2025-02-02 23:01:14 +08:00
"""资源清理方法"""
2025-05-28 18:22:31 +08:00
try:
# 取消超时任务
if self.timeout_task:
self.timeout_task.cancel()
self.timeout_task = None
2025-05-14 11:22:09 +08:00
2025-05-28 18:22:31 +08:00
# 清理MCP资源
if hasattr(self, "mcp_manager") and self.mcp_manager:
await self.mcp_manager.cleanup_all()
2025-05-28 18:22:31 +08:00
# 触发停止事件
if self.stop_event:
self.stop_event.set()
2025-02-26 01:33:05 +08:00
2025-05-28 18:22:31 +08:00
# 清空任务队列
self.clear_queues()
2025-03-31 21:40:48 +08:00
2025-05-28 18:22:31 +08:00
# 关闭WebSocket连接
if ws:
await ws.close()
elif self.websocket:
await self.websocket.close()
2025-03-31 21:40:48 +08:00
2025-05-28 18:22:31 +08:00
# 最后关闭线程池(避免阻塞)
if self.executor:
self.executor.shutdown(wait=False)
self.executor = None
2025-05-14 11:22:09 +08:00
2025-05-28 18:22:31 +08:00
self.logger.bind(tag=TAG).info("连接资源已释放")
except Exception as e:
self.logger.bind(tag=TAG).error(f"关闭连接时出错: {e}")
2025-02-02 23:01:14 +08:00
2025-04-25 09:34:22 +08:00
def clear_queues(self):
2025-05-14 11:22:09 +08:00
"""清空所有任务队列"""
2025-05-27 18:06:44 +08:00
if self.tts:
self.logger.bind(tag=TAG).debug(
f"开始清理: TTS队列大小={self.tts.tts_text_queue.qsize()}, 音频队列大小={self.tts.tts_audio_queue.qsize()}"
)
2025-05-14 11:22:09 +08:00
2025-05-27 18:06:44 +08:00
# 使用非阻塞方式清空队列
2025-05-28 21:34:21 +08:00
for q in [
self.tts.tts_text_queue,
self.tts.tts_audio_queue,
self.report_queue,
]:
2025-05-27 18:06:44 +08:00
if not q:
continue
while True:
try:
q.get_nowait()
except queue.Empty:
break
2025-05-14 11:22:09 +08:00
2025-05-27 18:06:44 +08:00
self.logger.bind(tag=TAG).debug(
f"清理结束: TTS队列大小={self.tts.tts_text_queue.qsize()}, 音频队列大小={self.tts.tts_audio_queue.qsize()}"
)
2025-02-02 23:01:14 +08:00
def reset_vad_states(self):
self.client_audio_buffer = bytearray()
2025-02-02 23:01:14 +08:00
self.client_have_voice = False
self.client_have_voice_last_time = 0
self.client_voice_stop = False
2025-02-18 00:07:19 +08:00
self.logger.bind(tag=TAG).debug("VAD states reset.")
2025-03-09 21:33:45 +08:00
def chat_and_close(self, text):
"""Chat with the user and then close the connection"""
try:
# Use the existing chat method
self.chat(text)
# After chat is complete, close the connection
self.close_after_chat = True
except Exception as e:
self.logger.bind(tag=TAG).error(f"Chat and close error: {str(e)}")
async def _check_timeout(self):
"""检查连接超时"""
try:
while not self.stop_event.is_set():
await asyncio.sleep(self.timeout_seconds)
if not self.stop_event.is_set():
self.logger.bind(tag=TAG).info("连接超时,准备关闭")
await self.close(self.websocket)
break
except Exception as e:
self.logger.bind(tag=TAG).error(f"超时检查任务出错: {e}")