mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
- Defaults and config aligned with English-first product surface - Adds/removes ASR/TTS paths; drops China-only provider implementations and bundled SenseVoice assets - Westernizes prompts, plugin/tool copy, connection and handler behavior on upstream fixes - Powermem max_tokens and OpenAI no-thinking guard preserved on downstream path
48 lines
1.8 KiB
Python
48 lines
1.8 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:
|
|
"""Main text message processor."""
|
|
|
|
def __init__(self, registry: TextMessageHandlerRegistry):
|
|
self.registry = registry
|
|
|
|
async def process_message(self, conn: "ConnectionHandler", message: str) -> None:
|
|
"""Primary entry point for processing inbound text messages."""
|
|
try:
|
|
# Parse the message as JSON first.
|
|
msg_json = json.loads(message)
|
|
|
|
# Handle structured JSON messages.
|
|
if isinstance(msg_json, dict):
|
|
message_type = msg_json.get("type")
|
|
|
|
# Log the received message before dispatch.
|
|
conn.logger.bind(tag=TAG).info(
|
|
f"Received {message_type} message: {message}"
|
|
)
|
|
|
|
# Look up and execute the registered handler.
|
|
handler = self.registry.get_handler(message_type)
|
|
if handler:
|
|
await handler.handle(conn, msg_json)
|
|
else:
|
|
conn.logger.bind(tag=TAG).error(
|
|
f"Received unknown message type: {message}"
|
|
)
|
|
# Handle plain numeric messages.
|
|
elif isinstance(msg_json, int):
|
|
conn.logger.bind(tag=TAG).info(f"Received numeric message: {message}")
|
|
await conn.websocket.send(message)
|
|
|
|
except json.JSONDecodeError:
|
|
# Forward non-JSON payloads directly after logging them.
|
|
conn.logger.bind(tag=TAG).error(f"Parsed invalid message payload: {message}")
|
|
await conn.websocket.send(message)
|