mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
为 ConnectionHandler 相关的函数参数添加类型注解,使用 TYPE_CHECKING 避免循环导入。主要修改包括: - 在 abortHandle、textHandle 等处理模块中为 conn 参数添加 ConnectionHandler 类型注解 - 在 websocket_server、connection 等核心模块中为方法参数添加类型注解 - 在 plugins_func 下的多个功能模块中为函数参数添加类型注解 - 在 providers 相关模块中为工具执行器和方法添加类型注解 - 统一代码格式,如将单引号字符串改为双引号 Fixes #2034
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import json
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from core.connection import ConnectionHandler
|
|
from core.handle.textMessageHandlerRegistry import TextMessageHandlerRegistry
|
|
|
|
TAG = __name__
|
|
|
|
|
|
class TextMessageProcessor:
|
|
"""消息处理器主类"""
|
|
|
|
def __init__(self, registry: TextMessageHandlerRegistry):
|
|
self.registry = registry
|
|
|
|
async def process_message(self, conn: "ConnectionHandler", message: str) -> None:
|
|
"""处理消息的主入口"""
|
|
try:
|
|
# 解析JSON消息
|
|
msg_json = json.loads(message)
|
|
|
|
# 处理JSON消息
|
|
if isinstance(msg_json, dict):
|
|
message_type = msg_json.get("type")
|
|
|
|
# 记录日志
|
|
conn.logger.bind(tag=TAG).info(f"收到{message_type}消息:{message}")
|
|
|
|
# 获取并执行处理器
|
|
handler = self.registry.get_handler(message_type)
|
|
if handler:
|
|
await handler.handle(conn, msg_json)
|
|
else:
|
|
conn.logger.bind(tag=TAG).error(f"收到未知类型消息:{message}")
|
|
# 处理纯数字消息
|
|
elif isinstance(msg_json, int):
|
|
conn.logger.bind(tag=TAG).info(f"收到数字消息:{message}")
|
|
await conn.websocket.send(message)
|
|
|
|
except json.JSONDecodeError:
|
|
# 非JSON消息直接转发
|
|
conn.logger.bind(tag=TAG).error(f"解析到错误的消息:{message}")
|
|
await conn.websocket.send(message)
|