mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +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
93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
import asyncio
|
|
from aiohttp import web
|
|
from config.logger import setup_logging
|
|
from core.api.ota_handler import OTAHandler
|
|
from core.api.vision_handler import VisionHandler
|
|
|
|
TAG = __name__
|
|
|
|
|
|
class SimpleHttpServer:
|
|
def __init__(self, config: dict):
|
|
self.config = config
|
|
self.logger = setup_logging()
|
|
self.ota_handler = OTAHandler(config)
|
|
self.vision_handler = VisionHandler(config)
|
|
|
|
def _get_websocket_url(self, local_ip: str, port: int) -> str:
|
|
"""Get WebSocket address.
|
|
|
|
Args:
|
|
local_ip: Local IP address
|
|
port: Port number
|
|
|
|
Returns:
|
|
str: WebSocket address
|
|
"""
|
|
server_config = self.config["server"]
|
|
websocket_config = server_config.get("websocket")
|
|
|
|
if websocket_config and "your_" not in websocket_config.lower():
|
|
return websocket_config
|
|
else:
|
|
return f"ws://{local_ip}:{port}/xiaozhi/v1/"
|
|
|
|
async def start(self):
|
|
try:
|
|
server_config = self.config["server"]
|
|
read_config_from_api = self.config.get("read_config_from_api", False)
|
|
host = server_config.get("ip", "0.0.0.0")
|
|
port = int(server_config.get("http_port", 8003))
|
|
|
|
if port:
|
|
app = web.Application()
|
|
|
|
if not read_config_from_api:
|
|
# If the admin console is disabled (single-module mode),
|
|
# expose the simple OTA endpoints to distribute websocket URL.
|
|
app.add_routes(
|
|
[
|
|
web.get("/xiaozhi/ota/", self.ota_handler.handle_get),
|
|
web.post("/xiaozhi/ota/", self.ota_handler.handle_post),
|
|
web.options(
|
|
"/xiaozhi/ota/", self.ota_handler.handle_options
|
|
),
|
|
# Download endpoint (data/bin/*.bin only)
|
|
web.get(
|
|
"/xiaozhi/ota/download/{filename}",
|
|
self.ota_handler.handle_download,
|
|
),
|
|
web.options(
|
|
"/xiaozhi/ota/download/{filename}",
|
|
self.ota_handler.handle_options,
|
|
),
|
|
]
|
|
)
|
|
# Add routes
|
|
app.add_routes(
|
|
[
|
|
web.get("/mcp/vision/explain", self.vision_handler.handle_get),
|
|
web.post(
|
|
"/mcp/vision/explain", self.vision_handler.handle_post
|
|
),
|
|
web.options(
|
|
"/mcp/vision/explain", self.vision_handler.handle_options
|
|
),
|
|
]
|
|
)
|
|
|
|
# Run server
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, host, port)
|
|
await site.start()
|
|
|
|
# Keep server running
|
|
while True:
|
|
await asyncio.sleep(3600) # Periodic keep-alive
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"HTTP server failed to start: {e}")
|
|
import traceback
|
|
|
|
self.logger.bind(tag=TAG).error(f"Traceback: {traceback.format_exc()}")
|
|
raise
|