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

1540 lines
62 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import copy
import json
import uuid
import time
import queue
import asyncio
import threading
import traceback
import subprocess
import websockets
from core.utils.util import (
extract_json_from_string,
check_vad_update,
check_asr_update,
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.providers.tts.default import DefaultTTS
from concurrent.futures import ThreadPoolExecutor
from core.utils.dialogue import Message, Dialogue
from core.providers.asr.dto.dto import InterfaceType
from core.providers.tts.dto.dto import MessageTag
from core.providers.llm.base import LLMProviderBase
from core.handle.textHandle import handleTextMessage
from core.providers.tools.unified_tool_handler import UnifiedToolHandler
from plugins_func.loadplugins import auto_import_modules
from plugins_func.register import Action
from core.auth import 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, create_connection_logger
from config.manage_api_client import DeviceNotFoundException, DeviceBindException
from core.utils.prompt_manager import PromptManager
from core.utils.voiceprint_provider import VoiceprintProvider
from core.utils import textUtils
from config.live_agent_api_client import (
get_agent_config_from_api,
get_agent_by_wake_from_api,
extract_user_id_from_jwt,
)
TAG = __name__
auto_import_modules("plugins_func.functions")
class TTSException(RuntimeError):
pass
class ConnectionHandler:
def __init__(
self,
config: Dict[str, Any],
_vad,
_asr,
_llm,
_memory,
_intent,
server=None,
):
self.common_config = config
self.config = copy.deepcopy(config)
self.session_id = str(uuid.uuid4())
self.logger = setup_logging()
self.server = server # 保存server实例的引用
self.need_bind = False
self.bind_code = None
self.read_config_from_api = self.config.get("read_config_from_api", False)
self.read_config_from_live_agent_api = self.config.get("read_config_from_live_agent_api", False)
self.websocket = None
self.headers = None
self.device_id = None
self.owner_id = None # Device owner's user_id for memory storage
self.client_ip = None
self.prompt = None
self.welcome_msg = None
self.max_output_size = 0
self.chat_history_conf = 0
self.audio_format = "opus"
self.defer_agent_init = False
# 客户端状态相关
self.client_abort = False
self.client_is_speaking = False
self.client_listen_mode = "auto"
# 线程任务相关
self.loop = asyncio.get_event_loop()
self.stop_event = threading.Event()
self.executor = ThreadPoolExecutor(max_workers=5)
# 添加上报线程池
self.report_queue = queue.Queue()
self.report_thread = None
# Enable report for both manager-api and live-agent-api modes
self._report_enabled = self.read_config_from_api or self.read_config_from_live_agent_api
self.report_asr_enable = self._report_enabled
self.report_tts_enable = self._report_enabled
# 依赖的组件
self.vad = None
self.asr = None
self.tts = None
self._asr = _asr
self._vad = _vad
self.llm = _llm
self.memory = _memory
self.intent = _intent
# 为每个连接单独管理声纹识别
self.voiceprint_provider = None
# vad相关变量
self.client_audio_buffer = bytearray()
self.client_have_voice = False
self.client_voice_window = deque(maxlen=5)
self.last_activity_time = 0.0 # 统一的活动时间戳(毫秒)
self.client_voice_stop = False
self.last_is_voice = False
# asr相关变量
# 因为实际部署时可能会用到公共的本地ASR,不能把变量暴露给公共ASR
# 所以涉及到ASR的变量,需要在这里定义,属于connection的私有变量
self.asr_audio = []
self.asr_audio_queue = queue.Queue()
# llm相关变量
self.llm_finish_task = True
self.dialogue = Dialogue()
# tts相关变量
self.sentence_id = None
# 处理TTS响应没有文本返回
self.tts_MessageText = ""
# iot相关变量
self.iot_descriptors = {}
self.func_handler = None
self.cmd_exit = self.config["exit_commands"]
# 是否在聊天结束后关闭连接
self.close_after_chat = False
self.load_function_plugin = False
self.intent_type = "nointent"
self.timeout_seconds = (
int(self.config.get("close_connection_no_voice_time", 120)) + 60
) # 在原来第一道关闭的基础上加60秒,进行二道关闭
self.timeout_task = None
# {"mcp":true} 表示启用MCP功能
self.features = None
# 标记连接是否来自MQTT
self.conn_from_mqtt_gateway = False
# 初始化提示词管理器
self.prompt_manager = PromptManager(config, self.logger)
# agent-related configs
self._instruction = None
self._voice_opening = None
self._voice_closing = None
self._language = None
# reconnected flag
self.reconnected: bool = False
async def handle_connection(self, ws):
try:
# 获取并验证headers
self.headers = dict(ws.request.headers)
real_ip = self.headers.get("x-real-ip") or self.headers.get(
"x-forwarded-for"
)
if real_ip:
self.client_ip = real_ip.split(",")[0].strip()
else:
self.client_ip = ws.remote_address[0]
self.logger.bind(tag=TAG).info(
f"{self.client_ip} conn - Headers: {self.headers}"
)
self.device_id = self.headers.get("device-id", None)
self.agent_id = self.headers.get("agent-id", None)
# Extract user_id from JWT token (if live-agent-api secret_key configured)
# This enables proper memory initialization with the real user identity
auth_header = self.headers.get("authorization", "")
if auth_header and self.read_config_from_live_agent_api:
jwt_user_id = extract_user_id_from_jwt(auth_header, self.config)
if jwt_user_id:
self.owner_id = jwt_user_id
self.logger.bind(tag=TAG).info(
f"Extracted owner_id from JWT: {jwt_user_id[:20]}..."
)
# 认证通过,继续处理
self.websocket = ws
# check if the connection is reconnected by the mobile-end
self.reconnected = self.headers.get("reconnected", "0") == "1"
self.logger.bind(tag=TAG).info(f"reconnected: {self.reconnected}")
# 检查是否来自MQTT连接
request_path = ws.request.path
self.conn_from_mqtt_gateway = request_path.endswith("?from=mqtt_gateway")
if self.conn_from_mqtt_gateway:
self.logger.bind(tag=TAG).info("连接来自:MQTT网关")
# 初始化活动时间戳
self.last_activity_time = time.time() * 1000
# 启动超时检查任务
self.timeout_task = asyncio.create_task(self._check_timeout())
# todo: welcome message need to be set after private config is loaded
self.welcome_msg = self.config["xiaozhi"]
self.welcome_msg["session_id"] = self.session_id
# 获取差异化配置
self._initialize_agent_config()
# 异步初始化
self.executor.submit(self._initialize_components)
try:
async for message in self.websocket:
await self._route_message(message)
except websockets.exceptions.ConnectionClosed:
self.logger.bind(tag=TAG).info("客户端断开连接")
except AuthenticationError as e:
self.logger.bind(tag=TAG).error(f"Authentication failed: {str(e)}")
return
except Exception as e:
stack_trace = traceback.format_exc()
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
return
finally:
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):
"""保存记忆并关闭连接"""
try:
if self.memory:
# 准备上下文信息
context = {
"session_id": self.session_id,
"device_id": self.device_id,
"user_id": self.owner_id, # 设备所有者的 user_id
"mac_address": getattr(self, 'mac_address', None),
"agent_id": getattr(self, 'agent_id', None),
}
# 使用线程池异步保存记忆
def save_memory_task():
try:
# 创建新事件循环(避免与主循环冲突)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
self.memory.save_memory(self.dialogue.dialogue, context)
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
try:
loop.close()
except Exception:
pass
# 启动线程保存记忆,不等待完成
threading.Thread(target=save_memory_task, daemon=True).start()
except Exception as e:
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
finally:
# 立即关闭连接,不等待记忆保存完成
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):
"""消息路由"""
if isinstance(message, str):
await handleTextMessage(self, message)
elif isinstance(message, bytes):
if self.vad is None or self.asr is None:
return
# 处理来自MQTT网关的音频包
if self.conn_from_mqtt_gateway and len(message) >= 16:
handled = await self._process_mqtt_audio_message(message)
if handled:
return
# 不需要头部处理或没有头部时,直接处理原始消息
self.asr_audio_queue.put(message)
async def _process_mqtt_audio_message(self, message):
"""
处理来自MQTT网关的音频消息,解析16字节头部并提取音频数据
Args:
message: 包含头部的音频消息
Returns:
bool: 是否成功处理了消息
"""
try:
# 提取头部信息
timestamp = int.from_bytes(message[8:12], "big")
audio_length = int.from_bytes(message[12:16], "big")
# 提取音频数据
if audio_length > 0 and len(message) >= 16 + audio_length:
# 有指定长度,提取精确的音频数据
audio_data = message[16 : 16 + audio_length]
# 基于时间戳进行排序处理
self._process_websocket_audio(audio_data, timestamp)
return True
elif len(message) > 16:
# 没有指定长度或长度无效,去掉头部后处理剩余数据
audio_data = message[16:]
self.asr_audio_queue.put(audio_data)
return True
except Exception as e:
self.logger.bind(tag=TAG).error(f"解析WebSocket音频包失败: {e}")
# 处理失败,返回False表示需要继续处理
return False
def _process_websocket_audio(self, audio_data, timestamp):
"""处理WebSocket格式的音频包"""
# 初始化时间戳序列管理
if not hasattr(self, "audio_timestamp_buffer"):
self.audio_timestamp_buffer = {}
self.last_processed_timestamp = 0
self.max_timestamp_buffer_size = 20
# 如果时间戳是递增的,直接处理
if timestamp >= self.last_processed_timestamp:
self.asr_audio_queue.put(audio_data)
self.last_processed_timestamp = timestamp
# 处理缓冲区中的后续包
processed_any = True
while processed_any:
processed_any = False
for ts in sorted(self.audio_timestamp_buffer.keys()):
if ts > self.last_processed_timestamp:
buffered_audio = self.audio_timestamp_buffer.pop(ts)
self.asr_audio_queue.put(buffered_audio)
self.last_processed_timestamp = ts
processed_any = True
break
else:
# 乱序包,暂存
if len(self.audio_timestamp_buffer) < self.max_timestamp_buffer_size:
self.audio_timestamp_buffer[timestamp] = audio_data
else:
self.asr_audio_queue.put(audio_data)
async def handle_restart(self, message):
"""处理服务器重启请求"""
try:
self.logger.bind(tag=TAG).info("收到服务器重启指令,准备执行...")
# 发送确认响应
await self.websocket.send(
json.dumps(
{
"type": "server",
"status": "success",
"message": "服务器重启中...",
"content": {"action": "restart"},
}
)
)
# 异步执行重启操作
def restart_server():
"""实际执行重启的方法"""
time.sleep(1)
self.logger.bind(tag=TAG).info("执行服务器重启...")
subprocess.Popen(
[sys.executable, "app.py"],
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
start_new_session=True,
)
os._exit(0)
# 使用线程执行重启避免阻塞事件循环
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)}",
"content": {"action": "restart"},
}
)
)
def _initialize_components(self):
try:
self.selected_module_str = build_module_string(
self.config.get("selected_module", {})
)
self.logger = create_connection_logger(self.selected_module_str)
# 缺少 agent-id 时,延后 TTS/LLM 初始化,待唤醒词解析后再加载
if self.read_config_from_live_agent_api and not self.agent_id:
self.defer_agent_init = True
self.logger.bind(tag=TAG).info(
"agent-id missing, defer LLM/TTS init until wake word resolves agent"
)
else:
self._initialize_agent_config()
"""初始化本地组件"""
if self.vad is None:
self.vad = self._vad
if self.asr is None:
self.asr = self._initialize_asr()
# 初始化声纹识别
self._initialize_voiceprint()
# 打开语音识别通道
asyncio.run_coroutine_threadsafe(
self.asr.open_audio_channels(self), self.loop
)
init_llm = not self.defer_agent_init
init_tts = not self.defer_agent_init
init_memory = not self.defer_agent_init
init_intent = not self.defer_agent_init
# prewarm LLM first connection
if init_llm and isinstance(self.llm, LLMProviderBase):
self.llm.prewarm()
"""加载记忆"""
if init_memory:
self._initialize_memory()
"""加载意图识别"""
if init_intent:
self._initialize_intent()
"""更新系统提示词(必须在 TTS 初始化前,以便加载 role 的 TTS 配置)"""
if init_tts or init_llm:
self._init_prompt_enhancement()
# 初始化 TTS(在 prompt 初始化之后,以便使用 role 的 TTS 配置)
if init_tts and self.tts is None:
self.tts = self._initialize_tts()
# 打开语音合成通道
if init_tts and self.tts:
asyncio.run_coroutine_threadsafe(
self.tts.open_audio_channels(self), self.loop
)
"""初始化上报线程"""
if init_tts or init_llm:
self._init_report_threads()
if self._voice_opening and self.tts:
self.logger.bind(tag=TAG).info(f"send the opening message: {self._voice_opening}")
opening_sentence_id = str(uuid.uuid4().hex)
message_tag = MessageTag.OPENING
# FIRST: Start session
self.tts.tts_text_queue.put(TTSMessageDTO(
sentence_id=opening_sentence_id,
sentence_type=SentenceType.FIRST,
content_type=ContentType.ACTION,
message_tag=message_tag,
))
self.tts.tts_text_queue.put(TTSMessageDTO(
sentence_id=str(uuid.uuid4().hex),
sentence_type=SentenceType.MIDDLE,
content_type=ContentType.TEXT,
content_detail=self._voice_opening,
message_tag=message_tag,
)
)
self.tts.tts_text_queue.put(TTSMessageDTO(
sentence_id=opening_sentence_id,
sentence_type=SentenceType.LAST,
content_type=ContentType.ACTION,
message_tag=message_tag,
))
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)
# 获取用户画像(如果 Memory 模块已初始化)
user_persona = None
if self.memory and hasattr(self.memory, 'get_user_persona'):
try:
user_persona = self.memory.get_user_persona()
if user_persona:
self.logger.bind(tag=TAG).debug(f"获取到用户画像,长度: {len(user_persona)}")
except Exception as e:
self.logger.bind(tag=TAG).warning(f"获取用户画像失败: {e}")
# 构建增强的系统提示词(返回 (enhanced_prompt, role_tts_config))
result = self.prompt_manager.build_enhanced_prompt(
user_prompt=self._instruction,
device_id=self.device_id,
client_ip=self.client_ip,
language=self._language,
user_persona=user_persona,
)
# 解包返回值
if isinstance(result, tuple):
enhanced_prompt, role_tts_config = result
# 保存 role 的 TTS 配置到 self.config(供 TTS 初始化使用)
if role_tts_config:
self.config["_role_tts_config"] = role_tts_config
self.logger.bind(tag=TAG).info(
f"保存 Role TTS 配置到 config: {role_tts_config}"
)
else:
# 兼容旧版本返回值(仅返回 prompt 字符串)
enhanced_prompt = result
if enhanced_prompt:
self.change_system_prompt(enhanced_prompt)
self.logger.bind(tag=TAG).info("系统提示词已增强更新")
def _init_report_threads(self):
"""Initialize chat message report thread for live-agent-api"""
# Only enable for live-agent-api mode
if not self.read_config_from_live_agent_api or self.need_bind:
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
)
self.report_thread.start()
self.logger.bind(tag=TAG).info("Chat report thread started")
def _initialize_tts(self):
"""
初始化TTS(支持三级优先级配置)
优先级:
1. API 下发的 TTS 配置(在 _initialize_private_config 中已应用)
2. Role 中的 TTS 配置
3. selected_module.TTS(兜底配置)
"""
tts = None
if not self.need_bind:
# 检查是否有 role 的 TTS 配置(优先级2)
role_tts_config = self.config.get("_role_tts_config")
self.logger.bind(tag=TAG).info(f"🔍 检查 _role_tts_config: {role_tts_config}")
if role_tts_config:
self.logger.bind(tag=TAG).info("✅ 发现 Role TTS 配置,准备应用")
self._apply_role_tts_config(role_tts_config)
else:
self.logger.bind(tag=TAG).info("ℹ️ 没有 Role TTS 配置,使用默认配置")
# 初始化 TTS(优先级1和3在这里统一处理)
tts = initialize_tts(self.config)
if tts is None:
tts = DefaultTTS(self.config, delete_audio_file=True)
return tts
def _apply_role_tts_config(self, role_tts_config: dict):
"""应用 role 中的 TTS 配置"""
self.logger.bind(tag=TAG).debug(f"开始应用 Role TTS 配置: {role_tts_config}")
provider = role_tts_config.get("provider")
voice_id = role_tts_config.get("voice_id")
if not provider or not voice_id:
self.logger.bind(tag=TAG).warning("Role TTS 配置不完整,跳过应用")
return
# 检查当前 selected_module.TTS 是否已经被 API 覆盖
# 如果 API 已经设置了 TTS,则不应用 role 配置(API 优先级更高)
current_tts = self.config["selected_module"]["TTS"]
if self.config.get("_api_tts_applied"):
self.logger.bind(tag=TAG).info(
f"API 已设置 TTS 配置(优先级1),跳过 Role TTS 配置: {provider}"
)
return
# 应用 role 的 TTS 配置
# 根据 provider 映射到实际的 TTS 模块名
tts_module_map = {
"elevenlabs": "ElevenLabsSDK",
"cartesia": "CartesiaSDK",
"edge": "EdgeTTS",
"doubao": "VolcanoStreamTTS",
# 可以继续添加更多映射...
}
tts_module = tts_module_map.get(provider.lower())
if not tts_module:
self.logger.bind(tag=TAG).warning(
f"未知的 TTS provider: {provider},使用默认配置"
)
return
# 检查该 TTS 模块是否在配置中存在
if tts_module not in self.config.get("TTS", {}):
self.logger.bind(tag=TAG).warning(
f"TTS 模块 {tts_module} 未在配置中定义,跳过应用"
)
return
# 更新 selected_module.TTS
self.config["selected_module"]["TTS"] = tts_module
# 更新 voice_id(如果该 TTS 模块支持)
if "voice_id" in self.config["TTS"][tts_module]:
self.config["TTS"][tts_module]["voice_id"] = voice_id
self.logger.bind(tag=TAG).info(
f"✅ 应用 Role TTS 配置: provider={provider}, "
f"module={tts_module}, voice_id={voice_id[:16]}..."
)
else:
self.logger.bind(tag=TAG).warning(
f"TTS 模块 {tts_module} 不支持 voice_id 配置"
)
def _initialize_asr(self):
"""初始化ASR"""
# 检查 _asr 是否为 None
if self._asr is None:
return initialize_asr(self.config)
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_voiceprint(self):
"""为当前连接初始化声纹识别"""
try:
voiceprint_config = self.config.get("voiceprint", {})
if voiceprint_config:
voiceprint_provider = VoiceprintProvider(voiceprint_config)
if voiceprint_provider is not None and voiceprint_provider.enabled:
self.voiceprint_provider = voiceprint_provider
self.logger.bind(tag=TAG).info("声纹识别功能已在连接时动态启用")
else:
self.logger.bind(tag=TAG).warning("声纹识别功能启用但配置不完整")
else:
self.logger.bind(tag=TAG).info("声纹识别功能未启用")
except Exception as e:
self.logger.bind(tag=TAG).warning(f"声纹识别初始化失败: {str(e)}")
def _initialize_private_config(self):
"""如果是从配置文件获取,则进行二次实例化"""
if not self.read_config_from_api:
return
"""从接口获取差异化的配置进行二次实例化,非全量重新实例化"""
try:
begin_time = time.time()
private_config = get_private_config_from_api(
self.config,
self.headers.get("device-id"),
self.headers.get("client-id", self.headers.get("device-id")),
)
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)}"
)
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 = {}
except Exception as e:
self.need_bind = True
self.logger.bind(tag=TAG).error(f"获取差异化配置失败: {e}")
private_config = {}
init_llm, init_tts, init_memory, init_intent = (
False,
False,
False,
False,
)
init_vad = check_vad_update(self.common_config, private_config)
init_asr = check_asr_update(self.common_config, private_config)
if private_config.get("Memory", None) is not None:
init_memory = True
self.config["Memory"] = private_config["Memory"]
self.config["selected_module"]["Memory"] = private_config[
"selected_module"
]["Memory"]
if private_config.get("Intent", None) is not None:
init_intent = True
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
self.config["Intent"][self.config["selected_module"]["Intent"]][
"functions"
] = 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:
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"])
if private_config.get("mcp_endpoint", None) is not None:
self.config["mcp_endpoint"] = private_config["mcp_endpoint"]
try:
modules = initialize_modules(
self.logger,
private_config,
init_vad,
init_asr,
init_llm,
init_tts,
init_memory,
init_intent,
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"初始化组件失败: {e}")
modules = {}
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"]
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):
if self.memory is None:
return
"""初始化记忆模块"""
# Use owner_id (real user_id) for memory storage, fallback to device_id if not available
memory_user_id = self.owner_id if self.owner_id else self.device_id
self.memory.init_memory(
role_id=memory_user_id,
llm=self.llm,
agent_id=self.agent_id,
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):
if self.intent is None:
return
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"]
intent_type = self.config["Intent"][self.config["selected_module"]["Intent"]][
"type"
]
# 如果使用 nointent,直接返回
if intent_type == "nointent":
return
# 使用 intent_llm 模式
elif intent_type == "intent_llm":
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 = UnifiedToolHandler(self)
# 异步初始化工具处理器
if hasattr(self, "loop") and self.loop:
asyncio.run_coroutine_threadsafe(self.func_handler._initialize(), self.loop)
def _initialize_agent_config(self):
"""initialize agent config from live-agent-api"""
if not self.read_config_from_live_agent_api:
return
private_config = get_agent_config_from_api(self.agent_id, self.config)
if not private_config:
self.logger.bind(tag=TAG).error(f"Failed to get agent config for {self.agent_id}")
return
self._apply_agent_runtime_config(private_config)
init_llm, init_tts, init_memory, init_intent = (
True,
True,
False,
False,
)
init_vad = True
init_asr = True
try:
modules = initialize_modules(
self.logger,
self.config,
init_vad,
init_asr,
init_llm,
init_tts,
init_memory,
init_intent,
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"初始化组件失败: {e}")
modules = {}
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"]
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 _apply_agent_runtime_config(self, private_config: dict):
"""Apply agent-specific runtime config to connection"""
if not private_config:
return
voice_id = private_config.get("voice_id")
if voice_id:
if "TTS" in self.config and "FishSpeech" in self.config.get("TTS", {}):
self.config["TTS"]["FishSpeech"]["reference_id"] = voice_id
if "TTS" in self.config and "FishDualStreamTTS" in self.config.get("TTS", {}):
self.config["TTS"]["FishDualStreamTTS"]["reference_id"] = voice_id
self._instruction = private_config.get("instruction", self._instruction)
self._voice_opening = private_config.get("voice_opening", self._voice_opening)
self._voice_closing = private_config.get("voice_closing", self._voice_closing)
self._language = private_config.get("language", self._language)
# Set chat history config for live-agent-api mode
# 0: disable, 1: text only, 2: text + audio
live_api_config = self.config.get("live-agent-api", {})
self.chat_history_conf = live_api_config.get("chat_history_conf", 2)
async def ensure_agent_ready(self, wake_word: str | None = None) -> bool:
"""
Resolve agent when missing and initialize LLM/TTS lazily.
"""
if not self.read_config_from_live_agent_api:
return True
if not self.defer_agent_init and self.tts and self.llm:
return True
private_config = None
if not self.agent_id:
resolved = get_agent_by_wake_from_api(
self.device_id, wake_word=wake_word, config=self.config
)
if not resolved:
self.logger.bind(tag=TAG).error(
f"Failed to resolve agent by wake_word for device {self.device_id}"
)
self.need_bind = True
return False
self.agent_id = resolved.get("agent_id")
# Only set owner_id from API if not already extracted from JWT
if not self.owner_id:
self.owner_id = resolved.get("owner_id") # Device owner's user_id
private_config = resolved.get("agent_config")
if private_config is None:
private_config = get_agent_config_from_api(self.agent_id, self.config)
if not private_config:
self.logger.bind(tag=TAG).error(
f"Failed to get agent config for {self.agent_id}"
)
return False
self._apply_agent_runtime_config(private_config)
self.defer_agent_init = False
try:
modules = initialize_modules(
self.logger,
self.config,
False,
False,
True,
True,
False,
False,
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"初始化组件失败: {e}")
modules = {}
if modules.get("llm", None) is not None:
self.llm = modules["llm"]
if isinstance(self.llm, LLMProviderBase):
self.llm.prewarm()
if modules.get("tts", None) is not None:
self.tts = modules["tts"]
asyncio.run_coroutine_threadsafe(
self.tts.open_audio_channels(self), self.loop
)
if modules.get("intent", None) is not None:
self.intent = modules["intent"]
if modules.get("memory", None) is not None:
self.memory = modules["memory"]
# 初始化 prompt 与上报线程
self._init_prompt_enhancement()
self._init_report_threads()
return True
def change_system_prompt(self, prompt):
self.prompt = prompt
# 更新系统prompt至上下文
self.dialogue.update_system_message(self.prompt)
def chat(self, query, depth=0):
"""
Process user message and generate response
Args:
query: User message, can be:
- str: Text content
- List[Dict]: Multimodal content
depth: Recursive depth, for function calling
"""
self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}")
# 记录 LLM 开始处理时间
llm_start_time = time.time() * 1000
llm_first_token_time = None
# 检查 TTS 是否已初始化
if self.tts is None:
self.logger.bind(tag=TAG).error("TTS 未初始化,无法处理聊天请求")
return False
self.llm_finish_task = False
# extract text content for memory query
if isinstance(query, list):
# multimodal content: extract text part
text_parts = [item.get("text", "") for item in query if item.get("type") == "text"]
query_text = " ".join(text_parts)
else:
query_text = query
# 为最顶层时新建会话ID和发送FIRST请求
if depth == 0:
self.sentence_id = str(uuid.uuid4().hex)
self.dialogue.put(Message(role="user", content=query))
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"):
functions = self.func_handler.get_functions()
response_message = []
try:
# use dialogue with memory (use text for memory query)
memory_str = None
# if self.memory is not None:
# memory_start_time = time.time() * 1000
# future = asyncio.run_coroutine_threadsafe(
# self.memory.query_memory(query_text), self.loop
# )
# memory_str = future.result()
# memory_duration = (time.time() * 1000 - memory_start_time) / 1000
# self.logger.bind(tag=TAG).info(f"[Latency] Memory查询完成, 耗时: {memory_duration:.3f}s")
# 获取对话历史
dialogue_history = self.dialogue.get_llm_dialogue_with_memory(
memory_str, self.config.get("voiceprint", {})
)
if self.intent_type == "function_call" and functions is not None:
# 直接使用同步生成器(response_with_functions 是同步方法)
llm_responses = self.llm.response_with_functions(
self.session_id,
dialogue_history,
functions=functions,
)
else:
# 直接使用同步生成器(response 是同步方法)
llm_responses = self.llm.response(
self.session_id,
dialogue_history,
)
except Exception as e:
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}", exc_info=True)
return None
# 处理流式响应
tool_call_flag = False
function_name = None
function_id = None
function_arguments = ""
content_arguments = ""
self.client_abort = False
emotion_flag = True
for response in llm_responses:
if self.client_abort:
break
if self.intent_type == "function_call" and 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
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
# 记录首个 token 时间(首字延迟)
if llm_first_token_time is None and content is not None and len(content) > 0:
llm_first_token_time = time.time() * 1000
first_token_delay = llm_first_token_time - llm_start_time
# 计算从用户说完到首 token 的延迟
e2e_first_token = 0
if hasattr(self, '_latency_voice_end_time'):
e2e_first_token = llm_first_token_time - self._latency_voice_end_time
self.logger.bind(tag=TAG).info(
f"🤖 [延迟追踪] LLM首token: {first_token_delay:.0f}ms | "
f"用户说完→首token: {e2e_first_token:.0f}ms"
)
# 在llm回复中获取情绪表情,一轮对话只在开头获取一次
# if emotion_flag and content is not None and content.strip():
# asyncio.run_coroutine_threadsafe(
# textUtils.get_emotion(self, content),
# self.loop,
# )
# emotion_flag = False
if content is not None and len(content) > 0:
if not tool_call_flag:
response_message.append(content)
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
sentence_type=SentenceType.MIDDLE,
content_type=ContentType.TEXT,
content_detail=content,
)
)
# 处理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"]
function_arguments = json.dumps(
content_arguments_json["arguments"], ensure_ascii=False
)
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:
self.logger.bind(tag=TAG).error(
f"function call error: {content_arguments}"
)
if not bHasError:
# 如需要大模型先处理一轮,添加相关处理后的日志情况
if len(response_message) > 0:
text_buff = "".join(response_message)
self.tts_MessageText = text_buff
self.dialogue.put(Message(role="assistant", content=text_buff))
response_message.clear()
self.logger.bind(tag=TAG).debug(
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}"
)
function_call_data = {
"name": function_name,
"id": function_id,
"arguments": function_arguments,
}
# 使用统一工具处理器处理所有工具调用
result = asyncio.run_coroutine_threadsafe(
self.func_handler.handle_llm_function_call(
self, function_call_data
),
self.loop,
).result()
self._handle_function_result(result, function_call_data, depth=depth)
# 记录 LLM 完成时间
llm_end_time = time.time() * 1000
llm_total_delay = llm_end_time - llm_start_time
# 计算从用户说完到 LLM 完成的延迟
e2e_llm_complete = 0
if hasattr(self, '_latency_voice_end_time'):
e2e_llm_complete = llm_end_time - self._latency_voice_end_time
self.logger.bind(tag=TAG).info(
f"🤖 [延迟追踪] LLM完成: {llm_total_delay:.0f}ms | "
f"用户说完→LLM完成: {e2e_llm_complete:.0f}ms"
)
# 存储对话内容
if len(response_message) > 0:
text_buff = "".join(response_message)
self.tts_MessageText = text_buff
self.dialogue.put(Message(role="assistant", content=text_buff))
if depth == 0:
self.tts.tts_text_queue.put(
TTSMessageDTO(
sentence_id=self.sentence_id,
sentence_type=SentenceType.LAST,
content_type=ContentType.ACTION,
)
)
self.llm_finish_task = True
# 使用lambda延迟计算,只有在DEBUG级别时才执行get_llm_dialogue()
self.logger.bind(tag=TAG).debug(
lambda: json.dumps(
self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False
)
)
return True
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)
self.dialogue.put(Message(role="assistant", content=text))
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"]
self.dialogue.put(
Message(
role="assistant",
tool_calls=[
{
"id": function_id,
"function": {
"arguments": (
"{}"
if function_arguments == ""
else function_arguments
),
"name": function_name,
},
"type": "function",
"index": 0,
}
],
)
)
self.dialogue.put(
Message(
role="tool",
tool_call_id=(
str(uuid.uuid4()) if function_id is None else function_id
),
content=text,
)
)
self.chat(text, 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)
self.dialogue.put(Message(role="assistant", content=text))
else:
pass
def _report_worker(self):
"""聊天记录上报工作线程"""
while not self.stop_event.is_set():
try:
# 从队列获取数据,设置超时以便定期检查停止事件
item = self.report_queue.get(timeout=1)
try:
self._process_report(*item)
except Exception as e:
self.logger.bind(tag=TAG).error(f"聊天记录上报线程异常: {e}")
except queue.Empty:
continue
except Exception as e:
self.logger.bind(tag=TAG).error(f"聊天记录上报工作线程异常: {e}")
# stop_event is set, continue processing remaining messages in report_queue
self.logger.bind(tag=TAG).info("processing remaining report messages...")
while not self.report_queue.empty():
try:
item = self.report_queue.get(timeout=0.1)
try:
self._process_report(*item)
except Exception as e:
self.logger.bind(tag=TAG).error(f"processing remaining report messages failed: {e}")
except queue.Empty:
break # Queue is empty, exit
self.logger.bind(tag=TAG).info("聊天记录上报线程已退出")
def _process_report(self, role, text, audio_data, report_time, attachments=None):
"""处理上报任务"""
try:
# 执行上报(传入二进制数据和附件)
report(self, role, text, audio_data, report_time, attachments)
except Exception as e:
self.logger.bind(tag=TAG).error(f"上报处理异常: {e}")
finally:
# 标记任务完成
self.report_queue.task_done()
def clearSpeakStatus(self):
self.client_is_speaking = False
self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态")
async def close(self, ws=None):
"""资源清理方法"""
try:
# 清理音频缓冲区
if hasattr(self, "audio_buffer"):
self.audio_buffer.clear()
# 取消超时任务
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:
try:
await self.func_handler.cleanup()
except Exception as cleanup_error:
self.logger.bind(tag=TAG).error(
f"清理工具处理器时出错: {cleanup_error}"
)
# 触发停止事件
if self.stop_event:
self.stop_event.set()
# clear TTS text queue and audio queue, except report_queue
self.clear_queues()
# process remaining messages in report_queue
if self._report_enabled and self.report_queue:
try:
self.logger.bind(tag=TAG).info("waiting for report queue to be processed...")
# wait for all messages to be processed
self.report_queue.join()
self.logger.bind(tag=TAG).info("report queue processed")
except Exception as e:
self.logger.bind(tag=TAG).warning(f"waiting for report queue timeout or failed: {e}")
# 关闭WebSocket连接
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:
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):
"""clear TTS task queues (except report_queue, which is handled by close method)"""
if self.tts:
self.logger.bind(tag=TAG).debug(
f"开始清理: TTS队列大小={self.tts.tts_text_queue.qsize()}, 音频队列大小={self.tts.tts_audio_queue.qsize()}"
)
# use non-blocking way to clear TTS queues
for q in [
self.tts.tts_text_queue,
self.tts.tts_audio_queue,
]:
if not q:
continue
while True:
try:
q.get_nowait()
except queue.Empty:
break
self.logger.bind(tag=TAG).debug(
f"清理结束: TTS队列大小={self.tts.tts_text_queue.qsize()}, 音频队列大小={self.tts.tts_audio_queue.qsize()}"
)
def reset_vad_states(self):
self.client_audio_buffer = bytearray()
self.client_have_voice = False
self.client_voice_stop = False
# reset VAD exponential filter
# if self.vad:
# self.vad.reset_filter()
self.logger.bind(tag=TAG).debug("VAD states reset.")
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():
# 检查是否超时(只有在时间戳已初始化的情况下)
if self.last_activity_time > 0.0:
current_time = time.time() * 1000
if (
current_time - self.last_activity_time
> self.timeout_seconds * 1000
):
if not self.stop_event.is_set():
self.logger.bind(tag=TAG).info("连接超时,准备关闭")
# 设置停止事件,防止重复处理
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("超时检查任务已退出")