mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
把 ConnectionHandler 中的 threading 相关代码改成 asyncio 相关代码
This commit is contained in:
parent
29bd266f97
commit
3faae85713
3 changed files with 74 additions and 72 deletions
|
|
@ -3,11 +3,9 @@ import json
|
||||||
import uuid
|
import uuid
|
||||||
import time
|
import time
|
||||||
import copy
|
import copy
|
||||||
import queue
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
import threading
|
|
||||||
import websockets
|
import websockets
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
import plugins_func.loadplugins
|
import plugins_func.loadplugins
|
||||||
|
|
@ -15,7 +13,6 @@ from config.logger import setup_logging
|
||||||
from core.utils.dialogue import Message, Dialogue
|
from core.utils.dialogue import Message, Dialogue
|
||||||
from core.handle.textHandle import handleTextMessage
|
from core.handle.textHandle import handleTextMessage
|
||||||
from core.utils.util import get_string_no_punctuation_or_emoji, extract_json_from_string, get_ip_info
|
from core.utils.util import get_string_no_punctuation_or_emoji, extract_json_from_string, get_ip_info
|
||||||
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
|
||||||
from core.handle.sendAudioHandle import sendAudioMessage
|
from core.handle.sendAudioHandle import sendAudioMessage
|
||||||
from core.handle.receiveAudioHandle import handleAudioMessage
|
from core.handle.receiveAudioHandle import handleAudioMessage
|
||||||
from core.handle.functionHandler import FunctionHandler
|
from core.handle.functionHandler import FunctionHandler
|
||||||
|
|
@ -49,12 +46,12 @@ class ConnectionHandler:
|
||||||
self.client_abort = False
|
self.client_abort = False
|
||||||
self.client_listen_mode = "auto"
|
self.client_listen_mode = "auto"
|
||||||
|
|
||||||
# 线程任务相关
|
# 异步任务相关
|
||||||
self.loop = asyncio.get_event_loop()
|
self.loop = asyncio.get_running_loop()
|
||||||
self.stop_event = threading.Event()
|
self.stop_event = asyncio.Event()
|
||||||
self.tts_queue = queue.Queue()
|
self.tts_queue = asyncio.Queue()
|
||||||
self.audio_play_queue = queue.Queue()
|
self.audio_play_queue = asyncio.Queue()
|
||||||
self.executor = ThreadPoolExecutor(max_workers=10)
|
self.background_tasks = set()
|
||||||
|
|
||||||
# 依赖的组件
|
# 依赖的组件
|
||||||
self.vad = _vad
|
self.vad = _vad
|
||||||
|
|
@ -153,13 +150,15 @@ class ConnectionHandler:
|
||||||
# 异步初始化
|
# 异步初始化
|
||||||
await self.loop.run_in_executor(None, self._initialize_components)
|
await self.loop.run_in_executor(None, self._initialize_components)
|
||||||
|
|
||||||
# tts 消化线程
|
# 启动TTS任务
|
||||||
tts_priority = threading.Thread(target=self._tts_priority_thread, daemon=True)
|
tts_task = asyncio.create_task(self._tts_priority_task())
|
||||||
tts_priority.start()
|
self.background_tasks.add(tts_task)
|
||||||
|
tts_task.add_done_callback(self.background_tasks.discard)
|
||||||
|
|
||||||
# 音频播放 消化线程
|
# 启动音频播放任务
|
||||||
audio_play_priority = threading.Thread(target=self._audio_play_priority_thread, daemon=True)
|
audio_play_task = asyncio.create_task(self._audio_play_priority_task())
|
||||||
audio_play_priority.start()
|
self.background_tasks.add(audio_play_task)
|
||||||
|
audio_play_task.add_done_callback(self.background_tasks.discard)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async for message in self.websocket:
|
async for message in self.websocket:
|
||||||
|
|
@ -214,8 +213,7 @@ class ConnectionHandler:
|
||||||
# 发送验证码语音提示
|
# 发送验证码语音提示
|
||||||
text = f"请在后台输入验证码:{' '.join(auth_code)}"
|
text = f"请在后台输入验证码:{' '.join(auth_code)}"
|
||||||
self.recode_first_last_text(text)
|
self.recode_first_last_text(text)
|
||||||
future = self.executor.submit(self.speak_and_play, text)
|
await self.tts_queue.put((text, 0))
|
||||||
self.tts_queue.put(future)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -226,11 +224,10 @@ class ConnectionHandler:
|
||||||
return False
|
return False
|
||||||
return not self.is_device_verified
|
return not self.is_device_verified
|
||||||
|
|
||||||
def chat(self, query):
|
async def chat(self, query):
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
future = asyncio.run_coroutine_threadsafe(self._check_and_broadcast_auth_code(), self.loop)
|
await self._check_and_broadcast_auth_code()
|
||||||
future.result()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.dialogue.put(Message(role="user", content=query))
|
self.dialogue.put(Message(role="user", content=query))
|
||||||
|
|
@ -240,8 +237,7 @@ class ConnectionHandler:
|
||||||
try:
|
try:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
memory_str = await self.memory.query_memory(query)
|
||||||
memory_str = future.result()
|
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
||||||
llm_responses = self.llm.response(
|
llm_responses = self.llm.response(
|
||||||
|
|
@ -284,8 +280,7 @@ class ConnectionHandler:
|
||||||
# segment_text = " "
|
# segment_text = " "
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
await self.tts_queue.put((segment_text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
||||||
|
|
||||||
# 处理最后剩余的文本
|
# 处理最后剩余的文本
|
||||||
|
|
@ -296,21 +291,24 @@ class ConnectionHandler:
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
await self.tts_queue.put((segment_text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
|
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
||||||
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def chat_with_function_calling(self, query, tool_call=False):
|
def create_chat_task(self, query):
|
||||||
|
task = asyncio.create_task(self.chat(query))
|
||||||
|
self.background_tasks.add(task)
|
||||||
|
task.add_done_callback(self.background_tasks.discard)
|
||||||
|
|
||||||
|
async def chat_with_function_calling(self, query, tool_call=False):
|
||||||
self.logger.bind(tag=TAG).debug(f"Chat with function calling start: {query}")
|
self.logger.bind(tag=TAG).debug(f"Chat with function calling start: {query}")
|
||||||
"""Chat with function calling for intent detection using streaming"""
|
"""Chat with function calling for intent detection using streaming"""
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
future = asyncio.run_coroutine_threadsafe(self._check_and_broadcast_auth_code(), self.loop)
|
await self._check_and_broadcast_auth_code()
|
||||||
future.result()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if not tool_call:
|
if not tool_call:
|
||||||
|
|
@ -326,8 +324,7 @@ class ConnectionHandler:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
memory_str = await self.memory.query_memory(query)
|
||||||
memory_str = future.result()
|
|
||||||
|
|
||||||
# self.logger.bind(tag=TAG).info(f"对话记录: {self.dialogue.get_llm_dialogue_with_memory(memory_str)}")
|
# self.logger.bind(tag=TAG).info(f"对话记录: {self.dialogue.get_llm_dialogue_with_memory(memory_str)}")
|
||||||
|
|
||||||
|
|
@ -397,8 +394,7 @@ class ConnectionHandler:
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
await self.tts_queue.put((segment_text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
||||||
|
|
||||||
# 处理function call
|
# 处理function call
|
||||||
|
|
@ -431,7 +427,7 @@ class ConnectionHandler:
|
||||||
"arguments": function_arguments
|
"arguments": function_arguments
|
||||||
}
|
}
|
||||||
result = self.func_handler.handle_llm_function_call(self, function_call_data)
|
result = self.func_handler.handle_llm_function_call(self, function_call_data)
|
||||||
self._handle_function_result(result, function_call_data, text_index + 1)
|
await self._handle_function_result(result, function_call_data, text_index + 1)
|
||||||
|
|
||||||
# 处理最后剩余的文本
|
# 处理最后剩余的文本
|
||||||
full_text = "".join(response_message)
|
full_text = "".join(response_message)
|
||||||
|
|
@ -441,8 +437,7 @@ class ConnectionHandler:
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
await self.tts_queue.put((segment_text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
|
|
||||||
# 存储对话内容
|
# 存储对话内容
|
||||||
if len(response_message) > 0:
|
if len(response_message) > 0:
|
||||||
|
|
@ -453,12 +448,16 @@ class ConnectionHandler:
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _handle_function_result(self, result, function_call_data, text_index):
|
def create_chat_with_function_calling_task(self, query, tool_call=False):
|
||||||
|
task = asyncio.create_task(self.chat_with_function_calling(query, tool_call))
|
||||||
|
self.background_tasks.add(task)
|
||||||
|
task.add_done_callback(self.background_tasks.discard)
|
||||||
|
|
||||||
|
async def _handle_function_result(self, result, function_call_data, text_index):
|
||||||
if result.action == Action.RESPONSE: # 直接回复前端
|
if result.action == Action.RESPONSE: # 直接回复前端
|
||||||
text = result.response
|
text = result.response
|
||||||
self.recode_first_last_text(text, text_index)
|
self.recode_first_last_text(text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, text, text_index)
|
await self.tts_queue.put((text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
self.dialogue.put(Message(role="assistant", content=text))
|
self.dialogue.put(Message(role="assistant", content=text))
|
||||||
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
|
elif result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
|
||||||
|
|
||||||
|
|
@ -475,33 +474,32 @@ class ConnectionHandler:
|
||||||
"index": 0}]))
|
"index": 0}]))
|
||||||
|
|
||||||
self.dialogue.put(Message(role="tool", tool_call_id=function_id, content=text))
|
self.dialogue.put(Message(role="tool", tool_call_id=function_id, content=text))
|
||||||
self.chat_with_function_calling(text, tool_call=True)
|
await self.chat_with_function_calling(text, tool_call=True)
|
||||||
elif result.action == Action.NOTFOUND:
|
elif result.action == Action.NOTFOUND:
|
||||||
text = result.result
|
text = result.result
|
||||||
self.recode_first_last_text(text, text_index)
|
self.recode_first_last_text(text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, text, text_index)
|
await self.tts_queue.put((text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
self.dialogue.put(Message(role="assistant", content=text))
|
self.dialogue.put(Message(role="assistant", content=text))
|
||||||
else:
|
else:
|
||||||
text = result.result
|
text = result.result
|
||||||
self.recode_first_last_text(text, text_index)
|
self.recode_first_last_text(text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, text, text_index)
|
await self.tts_queue.put((text, text_index))
|
||||||
self.tts_queue.put(future)
|
|
||||||
self.dialogue.put(Message(role="assistant", content=text))
|
self.dialogue.put(Message(role="assistant", content=text))
|
||||||
|
|
||||||
def _tts_priority_thread(self):
|
async def _tts_priority_task(self):
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
text = None
|
text = None
|
||||||
try:
|
try:
|
||||||
future = self.tts_queue.get()
|
text, text_index = await self.tts_queue.get()
|
||||||
if future is None:
|
if text is None:
|
||||||
continue
|
continue
|
||||||
text = None
|
|
||||||
opus_datas, text_index, tts_file = [], 0, None
|
opus_datas, tts_file = [], None
|
||||||
try:
|
try:
|
||||||
self.logger.bind(tag=TAG).debug("正在处理TTS任务...")
|
self.logger.bind(tag=TAG).debug("正在处理TTS任务...")
|
||||||
tts_timeout = self.config.get("tts_timeout", 10)
|
tts_timeout = self.config.get("tts_timeout", 10)
|
||||||
tts_file, text, text_index = future.result(timeout=tts_timeout)
|
tts_file, text, text_index = await asyncio.wait_for(self.speak_and_play(text, text_index), timeout=tts_timeout)
|
||||||
|
|
||||||
if text is None or len(text) <= 0:
|
if text is None or len(text) <= 0:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty")
|
self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty")
|
||||||
elif tts_file is None:
|
elif tts_file is None:
|
||||||
|
|
@ -512,40 +510,40 @@ class ConnectionHandler:
|
||||||
opus_datas, duration = self.tts.audio_to_opus_data(tts_file)
|
opus_datas, duration = self.tts.audio_to_opus_data(tts_file)
|
||||||
else:
|
else:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}")
|
self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}")
|
||||||
except TimeoutError:
|
except asyncio.TimeoutError | TimeoutError:
|
||||||
self.logger.bind(tag=TAG).error("TTS超时")
|
self.logger.bind(tag=TAG).error("TTS超时")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错: {e}")
|
self.logger.bind(tag=TAG).error(f"TTS出错: {e}")
|
||||||
|
|
||||||
if not self.client_abort:
|
if not self.client_abort:
|
||||||
# 如果没有中途打断就发送语音
|
# 如果没有中途打断就发送语音
|
||||||
self.audio_play_queue.put((opus_datas, text, text_index))
|
await self.audio_play_queue.put((opus_datas, text, text_index))
|
||||||
|
|
||||||
if self.tts.delete_audio_file and tts_file is not None and os.path.exists(tts_file):
|
if self.tts.delete_audio_file and tts_file is not None and os.path.exists(tts_file):
|
||||||
os.remove(tts_file)
|
os.remove(tts_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
|
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
|
||||||
self.clearSpeakStatus()
|
self.clearSpeakStatus()
|
||||||
asyncio.run_coroutine_threadsafe(
|
await self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}))
|
||||||
self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})),
|
self.logger.bind(tag=TAG).error(f"tts_priority task: {text} {e}")
|
||||||
self.loop
|
|
||||||
)
|
|
||||||
self.logger.bind(tag=TAG).error(f"tts_priority priority_thread: {text} {e}")
|
|
||||||
|
|
||||||
def _audio_play_priority_thread(self):
|
async def _audio_play_priority_task(self):
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
text = None
|
text = None
|
||||||
try:
|
try:
|
||||||
opus_datas, text, text_index = self.audio_play_queue.get()
|
opus_datas, text, text_index = await self.audio_play_queue.get()
|
||||||
future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index),
|
await sendAudioMessage(self, opus_datas, text, text_index)
|
||||||
self.loop)
|
|
||||||
future.result()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}")
|
self.logger.bind(tag=TAG).error(f"audio_play_priority task: {text} {e}")
|
||||||
|
|
||||||
def speak_and_play(self, text, text_index=0):
|
async def speak_and_play(self, text, text_index=0):
|
||||||
if text is None or len(text) <= 0:
|
if text is None or len(text) <= 0:
|
||||||
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
|
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
|
||||||
return None, text, text_index
|
return None, text, text_index
|
||||||
tts_file = self.tts.to_tts(text)
|
|
||||||
|
# 使用事件循环运行同步的TTS方法
|
||||||
|
tts_file = await self.loop.run_in_executor(None, self.tts.to_tts, text)
|
||||||
|
|
||||||
if tts_file is None:
|
if tts_file is None:
|
||||||
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
||||||
return None, text, text_index
|
return None, text, text_index
|
||||||
|
|
@ -569,7 +567,6 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# 清理其他资源
|
# 清理其他资源
|
||||||
self.stop_event.set()
|
self.stop_event.set()
|
||||||
self.executor.shutdown(wait=False)
|
|
||||||
if self.websocket:
|
if self.websocket:
|
||||||
await self.websocket.close()
|
await self.websocket.close()
|
||||||
self.logger.bind(tag=TAG).info("连接资源已释放")
|
self.logger.bind(tag=TAG).info("连接资源已释放")
|
||||||
|
|
@ -581,13 +578,18 @@ class ConnectionHandler:
|
||||||
self.client_voice_stop = False
|
self.client_voice_stop = False
|
||||||
self.logger.bind(tag=TAG).debug("VAD states reset.")
|
self.logger.bind(tag=TAG).debug("VAD states reset.")
|
||||||
|
|
||||||
def chat_and_close(self, text):
|
async def chat_and_close(self, text):
|
||||||
"""Chat with the user and then close the connection"""
|
"""Chat with the user and then close the connection"""
|
||||||
try:
|
try:
|
||||||
# Use the existing chat method
|
# Use the existing chat method
|
||||||
self.chat(text)
|
await self.chat(text)
|
||||||
|
|
||||||
# After chat is complete, close the connection
|
# After chat is complete, close the connection
|
||||||
self.close_after_chat = True
|
self.close_after_chat = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"Chat and close error: {str(e)}")
|
self.logger.bind(tag=TAG).error(f"Chat and close error: {str(e)}")
|
||||||
|
|
||||||
|
def create_chat_and_close_task(self, text):
|
||||||
|
task = asyncio.create_task(self.chat_and_close(text))
|
||||||
|
self.background_tasks.add(task)
|
||||||
|
task.add_done_callback(self.background_tasks.discard)
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ async def process_intent_result(conn, intent, original_text):
|
||||||
logger.bind(tag=TAG).info(f"识别到退出意图: {intent}")
|
logger.bind(tag=TAG).info(f"识别到退出意图: {intent}")
|
||||||
# 如果是明确的离别意图,发送告别语并关闭连接
|
# 如果是明确的离别意图,发送告别语并关闭连接
|
||||||
await send_stt_message(conn, original_text)
|
await send_stt_message(conn, original_text)
|
||||||
conn.executor.submit(conn.chat_and_close, original_text)
|
conn.create_chat_and_close_task(original_text)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 处理播放音乐意图
|
# 处理播放音乐意图
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,9 @@ async def startToChat(conn, text):
|
||||||
await send_stt_message(conn, text)
|
await send_stt_message(conn, text)
|
||||||
if conn.use_function_call_mode:
|
if conn.use_function_call_mode:
|
||||||
# 使用支持function calling的聊天方法
|
# 使用支持function calling的聊天方法
|
||||||
conn.executor.submit(conn.chat_with_function_calling, text)
|
conn.create_chat_with_function_calling_task(text)
|
||||||
else:
|
else:
|
||||||
conn.executor.submit(conn.chat, text)
|
conn.create_chat_task(text)
|
||||||
|
|
||||||
|
|
||||||
async def no_voice_close_connect(conn):
|
async def no_voice_close_connect(conn):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue