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
151 lines
5 KiB
Python
151 lines
5 KiB
Python
import sys
|
||
import uuid
|
||
import signal
|
||
import asyncio
|
||
from aioconsole import ainput
|
||
from config.settings import load_config
|
||
from config.logger import setup_logging
|
||
from core.utils.util import get_local_ip, validate_mcp_endpoint
|
||
from core.http_server import SimpleHttpServer
|
||
from core.websocket_server import WebSocketServer
|
||
from core.utils.util import check_ffmpeg_installed
|
||
from core.utils.gc_manager import get_gc_manager
|
||
|
||
TAG = __name__
|
||
logger = setup_logging()
|
||
|
||
|
||
async def wait_for_exit() -> None:
|
||
"""
|
||
Block until Ctrl-C / SIGTERM is received.
|
||
- Unix: uses add_signal_handler
|
||
- Windows: relies on KeyboardInterrupt
|
||
"""
|
||
loop = asyncio.get_running_loop()
|
||
stop_event = asyncio.Event()
|
||
|
||
if sys.platform != "win32": # Unix / macOS
|
||
for sig in (signal.SIGINT, signal.SIGTERM):
|
||
loop.add_signal_handler(sig, stop_event.set)
|
||
await stop_event.wait()
|
||
else:
|
||
# Windows: await an always-pending future so KeyboardInterrupt
|
||
# can bubble to asyncio.run, avoiding shutdown hangs from legacy threads.
|
||
try:
|
||
await asyncio.Future()
|
||
except KeyboardInterrupt: # Ctrl‑C
|
||
pass
|
||
|
||
|
||
async def monitor_stdin():
|
||
"""Monitor stdin and consume Enter key presses."""
|
||
while True:
|
||
await ainput() # Async wait for input, consume Enter
|
||
|
||
|
||
async def main():
|
||
check_ffmpeg_installed()
|
||
config = load_config()
|
||
|
||
# auth_key priority: config server.auth_key > manager-api.secret > auto-generated
|
||
# auth_key is used for JWT auth (vision API, OTA tokens, websocket auth)
|
||
auth_key = config["server"].get("auth_key", "")
|
||
|
||
# Validate auth_key, fall back to manager-api.secret if invalid
|
||
if not auth_key or len(auth_key) == 0 or "your_" in auth_key.lower():
|
||
auth_key = config.get("manager-api", {}).get("secret", "")
|
||
# Validate secret, generate random key if invalid
|
||
if not auth_key or len(auth_key) == 0 or "your_" in auth_key.lower():
|
||
auth_key = str(uuid.uuid4().hex)
|
||
|
||
config["server"]["auth_key"] = auth_key
|
||
|
||
# Add stdin monitor task
|
||
stdin_task = asyncio.create_task(monitor_stdin())
|
||
|
||
# Start global GC manager (cleanup every 5 minutes)
|
||
gc_manager = get_gc_manager(interval_seconds=300)
|
||
await gc_manager.start()
|
||
|
||
# Start WebSocket server
|
||
ws_server = WebSocketServer(config)
|
||
ws_task = asyncio.create_task(ws_server.start())
|
||
# Start simple HTTP server
|
||
ota_server = SimpleHttpServer(config)
|
||
ota_task = asyncio.create_task(ota_server.start())
|
||
|
||
read_config_from_api = config.get("read_config_from_api", False)
|
||
port = int(config["server"].get("http_port", 8003))
|
||
if not read_config_from_api:
|
||
logger.bind(tag=TAG).info(
|
||
"OTA endpoint:\t\thttp://{}:{}/xiaozhi/ota/",
|
||
get_local_ip(),
|
||
port,
|
||
)
|
||
logger.bind(tag=TAG).info(
|
||
"Vision endpoint:\thttp://{}:{}/mcp/vision/explain",
|
||
get_local_ip(),
|
||
port,
|
||
)
|
||
mcp_endpoint = config.get("mcp_endpoint", None)
|
||
if mcp_endpoint is not None and "your_" not in mcp_endpoint.lower():
|
||
# Validate MCP endpoint format
|
||
if validate_mcp_endpoint(mcp_endpoint):
|
||
logger.bind(tag=TAG).info("MCP endpoint:\t{}", mcp_endpoint)
|
||
# Convert MCP endpoint to call endpoint
|
||
mcp_endpoint = mcp_endpoint.replace("/mcp/", "/call/")
|
||
config["mcp_endpoint"] = mcp_endpoint
|
||
else:
|
||
logger.bind(tag=TAG).error("MCP endpoint format is invalid")
|
||
config["mcp_endpoint"] = "your MCP endpoint websocket address"
|
||
|
||
# Get WebSocket config, using safe defaults
|
||
websocket_port = 8000
|
||
server_config = config.get("server", {})
|
||
if isinstance(server_config, dict):
|
||
websocket_port = int(server_config.get("port", 8000))
|
||
|
||
logger.bind(tag=TAG).info(
|
||
"WebSocket address:\tws://{}:{}/xiaozhi/v1/",
|
||
get_local_ip(),
|
||
websocket_port,
|
||
)
|
||
|
||
logger.bind(tag=TAG).info(
|
||
"======= The address above is the WebSocket endpoint. Do not open it in a browser. ======="
|
||
)
|
||
logger.bind(tag=TAG).info(
|
||
"To test WebSocket, open test/test_page.html in Chrome."
|
||
)
|
||
logger.bind(tag=TAG).info(
|
||
"=============================================================\n"
|
||
)
|
||
|
||
try:
|
||
await wait_for_exit() # Block until exit signal
|
||
except asyncio.CancelledError:
|
||
print("Tasks cancelled, cleaning up resources...")
|
||
finally:
|
||
# Stop global GC manager
|
||
await gc_manager.stop()
|
||
|
||
# Cancel all tasks
|
||
stdin_task.cancel()
|
||
ws_task.cancel()
|
||
if ota_task:
|
||
ota_task.cancel()
|
||
|
||
# Wait for tasks to finish (with timeout)
|
||
await asyncio.wait(
|
||
[stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task],
|
||
timeout=3.0,
|
||
return_when=asyncio.ALL_COMPLETED,
|
||
)
|
||
print("Server shut down. Exiting.")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
asyncio.run(main())
|
||
except KeyboardInterrupt:
|
||
print("Interrupted by user. Exiting.")
|