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
|
2025-05-07 18:06:13 +08:00
|
|
|
from core.utils.util import initialize_modules, check_vad_update, check_asr_update
|
2025-05-07 09:11:59 +08:00
|
|
|
from config.config_loader import get_config_from_api
|
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-30 11:55:19 +08:00
|
|
|
self.config_lock = asyncio.Lock()
|
2025-04-12 17:36:04 +08:00
|
|
|
modules = initialize_modules(
|
2025-05-06 13:09:43 +08:00
|
|
|
self.logger,
|
|
|
|
|
self.config,
|
|
|
|
|
"VAD" in self.config["selected_module"],
|
|
|
|
|
"ASR" in self.config["selected_module"],
|
|
|
|
|
"LLM" in self.config["selected_module"],
|
|
|
|
|
"TTS" in self.config["selected_module"],
|
|
|
|
|
"Memory" in self.config["selected_module"],
|
|
|
|
|
"Intent" in self.config["selected_module"],
|
2025-02-02 23:01:14 +08:00
|
|
|
)
|
2025-05-06 13:09:43 +08:00
|
|
|
self._vad = modules["vad"] if "vad" in modules else None
|
|
|
|
|
self._asr = modules["asr"] if "asr" in modules else None
|
|
|
|
|
self._tts = modules["tts"] if "tts" in modules else None
|
|
|
|
|
self._llm = modules["llm"] if "llm" in modules else None
|
|
|
|
|
self._intent = modules["intent"] if "intent" in modules else None
|
|
|
|
|
self._memory = modules["memory"] if "memory" in modules else None
|
2025-04-12 17:36:04 +08:00
|
|
|
self.active_connections = set()
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
|
server_config = self.config["server"]
|
2025-04-22 23:36:23 +08:00
|
|
|
host = server_config.get("ip", "0.0.0.0")
|
2025-04-15 00:44:34 +08:00
|
|
|
port = int(server_config.get("port", 8000))
|
2025-02-02 23:01:14 +08:00
|
|
|
|
2025-04-28 12:01:50 +08:00
|
|
|
async with websockets.serve(
|
|
|
|
|
self._handle_connection, host, port, process_request=self._http_response
|
|
|
|
|
):
|
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-01 00:03:05 +08:00
|
|
|
handler = ConnectionHandler(
|
|
|
|
|
self.config,
|
|
|
|
|
self._vad,
|
|
|
|
|
self._asr,
|
|
|
|
|
self._llm,
|
|
|
|
|
self._tts,
|
|
|
|
|
self._memory,
|
2025-04-12 17:36:04 +08:00
|
|
|
self._intent,
|
2025-05-07 09:11:59 +08:00
|
|
|
self, # 传入server实例
|
2025-04-01 00:03:05 +08:00
|
|
|
)
|
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)
|
2025-04-28 12:01:50 +08:00
|
|
|
|
|
|
|
|
async def _http_response(self, websocket, request_headers):
|
|
|
|
|
# 检查是否为 WebSocket 升级请求
|
|
|
|
|
if request_headers.headers.get("connection", "").lower() == "upgrade":
|
|
|
|
|
# 如果是 WebSocket 请求,返回 None 允许握手继续
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
# 如果是普通 HTTP 请求,返回 "server is running"
|
|
|
|
|
return websocket.respond(200, "Server is running\n")
|
2025-05-07 09:11:59 +08:00
|
|
|
|
|
|
|
|
async def update_config(self) -> bool:
|
|
|
|
|
"""更新服务器配置并重新初始化组件
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: 更新是否成功
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
async with self.config_lock:
|
|
|
|
|
# 重新获取配置
|
|
|
|
|
new_config = get_config_from_api(self.config)
|
|
|
|
|
if new_config is None:
|
|
|
|
|
self.logger.bind(tag=TAG).error("获取新配置失败")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 检查 VAD 和 ASR 类型是否需要更新
|
2025-05-07 18:06:13 +08:00
|
|
|
update_vad = check_vad_update(self.config, new_config)
|
|
|
|
|
update_asr = check_asr_update(self.config, new_config)
|
2025-05-07 09:11:59 +08:00
|
|
|
|
|
|
|
|
# 更新配置
|
|
|
|
|
self.config = new_config
|
|
|
|
|
# 重新初始化组件
|
|
|
|
|
modules = initialize_modules(
|
|
|
|
|
self.logger,
|
|
|
|
|
new_config,
|
|
|
|
|
update_vad,
|
|
|
|
|
update_asr,
|
|
|
|
|
"LLM" in new_config["selected_module"],
|
|
|
|
|
"TTS" in new_config["selected_module"],
|
|
|
|
|
"Memory" in new_config["selected_module"],
|
|
|
|
|
"Intent" in new_config["selected_module"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 更新组件实例
|
2025-05-07 18:06:13 +08:00
|
|
|
if "vad" in modules:
|
|
|
|
|
self._vad = modules["vad"]
|
|
|
|
|
if "asr" in modules:
|
|
|
|
|
self._asr = modules["asr"]
|
|
|
|
|
if "tts" in modules:
|
|
|
|
|
self._tts = modules["tts"]
|
|
|
|
|
if "llm" in modules:
|
|
|
|
|
self._llm = modules["llm"]
|
|
|
|
|
if "intent" in modules:
|
|
|
|
|
self._intent = modules["intent"]
|
|
|
|
|
if "memory" in modules:
|
|
|
|
|
self._memory = modules["memory"]
|
2025-05-07 09:11:59 +08:00
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.logger.bind(tag=TAG).error(f"更新服务器配置失败: {str(e)}")
|
|
|
|
|
return False
|