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

121 lines
4.5 KiB
Python
Raw Normal View History

2025-02-02 23:01:14 +08:00
import asyncio
import websockets
2025-02-18 00:07:19 +08:00
from config.logger import setup_logging
2025-02-02 23:01:14 +08:00
from core.connection import ConnectionHandler
from core.utils.util import get_local_ip
2025-03-09 21:33:45 +08:00
from core.utils import asr, vad, llm, tts, memory, intent
2025-02-02 23:01:14 +08:00
2025-02-18 00:07:19 +08:00
TAG = __name__
2025-02-02 23:01:14 +08:00
2025-02-23 14:38:21 +08:00
2025-02-02 23:01:14 +08:00
class WebSocketServer:
def __init__(self, config: dict):
self.config = config
2025-02-18 00:07:19 +08:00
self.logger = setup_logging()
2025-04-09 14:32:32 +08:00
self._vad, self._asr, self._llm, self._memory, self.intent = (
2025-04-01 00:03:05 +08:00
self._create_processing_instances()
)
2025-02-26 01:33:05 +08:00
self.active_connections = set() # 添加全局连接记录
2025-02-02 23:01:14 +08:00
def _create_processing_instances(self):
2025-04-01 00:03:05 +08:00
memory_cls_name = self.config["selected_module"].get(
"Memory", "nomem"
) # 默认使用nomem
has_memory_cfg = (
2025-04-09 14:32:32 +08:00
self.config.get("Memory") and memory_cls_name in self.config["Memory"]
2025-04-01 00:03:05 +08:00
)
2025-03-03 15:00:04 +08:00
memory_cfg = self.config["Memory"][memory_cls_name] if has_memory_cfg else {}
2025-02-02 23:01:14 +08:00
"""创建处理模块实例"""
return (
vad.create_instance(
self.config["selected_module"]["VAD"],
2025-04-01 00:03:05 +08:00
self.config["VAD"][self.config["selected_module"]["VAD"]],
2025-02-02 23:01:14 +08:00
),
asr.create_instance(
2025-04-01 00:03:05 +08:00
(
self.config["selected_module"]["ASR"]
if not "type"
2025-04-09 14:32:32 +08:00
in self.config["ASR"][self.config["selected_module"]["ASR"]]
2025-04-01 00:03:05 +08:00
else self.config["ASR"][self.config["selected_module"]["ASR"]][
"type"
]
),
2025-02-02 23:01:14 +08:00
self.config["ASR"][self.config["selected_module"]["ASR"]],
2025-04-01 00:03:05 +08:00
self.config["delete_audio"],
2025-02-02 23:01:14 +08:00
),
llm.create_instance(
2025-04-01 00:03:05 +08:00
(
self.config["selected_module"]["LLM"]
if not "type"
2025-04-09 14:32:32 +08:00
in self.config["LLM"][self.config["selected_module"]["LLM"]]
2025-04-01 00:03:05 +08:00
else self.config["LLM"][self.config["selected_module"]["LLM"]][
"type"
]
),
2025-02-02 23:01:14 +08:00
self.config["LLM"][self.config["selected_module"]["LLM"]],
),
2025-03-03 15:00:04 +08:00
memory.create_instance(memory_cls_name, memory_cfg),
2025-03-09 21:33:45 +08:00
intent.create_instance(
2025-04-01 00:03:05 +08:00
(
self.config["selected_module"]["Intent"]
if not "type"
2025-04-09 14:32:32 +08:00
in self.config["Intent"][self.config["selected_module"]["Intent"]]
2025-04-01 00:03:05 +08:00
else self.config["Intent"][
self.config["selected_module"]["Intent"]
]["type"]
),
self.config["Intent"][self.config["selected_module"]["Intent"]],
2025-03-09 21:33:45 +08:00
),
2025-02-02 23:01:14 +08:00
)
async def start(self):
server_config = self.config["server"]
host = server_config["ip"]
port = server_config["port"]
2025-04-01 00:03:05 +08:00
self.logger.bind(tag=TAG).info(
2025-04-04 00:27:04 +08:00
"Server is running at ws://{}:{}/xiaozhi/v1/", get_local_ip(), port
2025-04-01 00:03:05 +08:00
)
self.logger.bind(tag=TAG).info(
"=======上面的地址是websocket协议地址,请勿用浏览器访问======="
)
2025-04-04 00:27:04 +08:00
self.logger.bind(tag=TAG).info(
"如想测试websocket请用谷歌浏览器打开test目录下的test_page.html"
)
self.logger.bind(tag=TAG).info(
"=============================================================\n"
)
2025-04-01 00:03:05 +08:00
async with websockets.serve(self._handle_connection, host, port):
2025-02-02 23:01:14 +08:00
await asyncio.Future()
async def _handle_connection(self, websocket):
"""处理新连接,每次创建独立的ConnectionHandler"""
2025-02-26 01:33:05 +08:00
# 创建ConnectionHandler时传入当前server实例
2025-04-09 14:32:32 +08:00
_tts = tts.create_instance(
(
self.config["selected_module"]["TTS"]
if not "type"
in self.config["TTS"][self.config["selected_module"]["TTS"]]
else self.config["TTS"][self.config["selected_module"]["TTS"]][
"type"
]
),
self.config["TTS"][self.config["selected_module"]["TTS"]],
self.config["delete_audio"],
)
2025-04-01 00:03:05 +08:00
handler = ConnectionHandler(
self.config,
self._vad,
self._asr,
self._llm,
2025-04-09 14:32:32 +08:00
_tts,
2025-04-01 00:03:05 +08:00
self._memory,
self.intent,
)
2025-02-26 01:33:05 +08:00
self.active_connections.add(handler)
try:
await handler.handle_connection(websocket)
finally:
self.active_connections.discard(handler)