# Xiaozhi-ESP32-Server: System Analysis ## 1. Project Overview Xiaozhi-ESP32-Server is a comprehensive backend system designed to provide intelligent voice interaction capabilities for ESP32-based smart hardware. Its primary purpose is to enable developers to quickly establish a robust server infrastructure capable of understanding natural language commands, interacting with various AI services (for speech recognition, language understanding, and speech synthesis), managing IoT devices, and offering a web-based interface for system configuration and administration. The project is composed of three main components: * **`xiaozhi-server`**: A Python-based core AI engine responsible for real-time voice processing and interaction logic with ESP32 devices. * **`manager-api`**: A Java Spring Boot application serving as the management backend, providing RESTful APIs for administration, configuration, and data persistence. * **`manager-web`**: A Vue.js web application (frontend) that provides a graphical user interface for administrators to manage and configure the system via `manager-api`. ## 2. Project Structure The project is organized into several key directories at the root level, with the core application logic residing within the `main/` directory. * **Root Level Directories:** * `.github/`: Contains GitHub-specific files like issue templates (`ISSUE_TEMPLATE/`) and workflow configurations (`workflows/`, e.g., `docker-image.yml`). * `docs/`: Houses all project documentation, including user guides (`Deployment.md`, `FAQ.md`), technical overviews (`TECHNICAL_OVERVIEW.md`), integration guides, and associated images. * `main/`: Contains the source code for the core application components. * `Dockerfile-server`, `Dockerfile-web`: Dockerfiles for building images for `xiaozhi-server` and `manager-web` (served by `manager-api`) respectively. * `docker-setup.sh`: A utility script to aid in setting up Docker deployments. * **Core Application Components (within `main/`):** * **`main/xiaozhi-server/` (Python AI Engine)** * **Purpose:** Handles real-time voice processing, AI service integration, and communication with ESP32 devices. * **Key Sub-directories:** * `app.py`: Main application entry point. * `config/`: Configuration management (`config.yaml`, `config_loader.py`, `manage_api_client.py`). Includes `assets/` for static audio files. * `core/`: Core operational logic, including `websocket_server.py`, `http_server.py`, `connection.py`, message handlers (`handle/`), and the crucial AI service provider pattern (`providers/` for ASR, LLM, TTS, VAD, VLLM, Intent, Memory). * `plugins_func/`: Plugin system for extensible "skills" (`functions/`, `loadplugins.py`). * `models/`: Directory for local AI model files (e.g., `SenseVoiceSmall`). * `requirements.txt`: Python dependencies. * **`main/manager-api/` (Java Management Backend)** * **Purpose:** Provides RESTful APIs for system administration, configuration (acting as a source for `xiaozhi-server`), and data persistence. * **Key Sub-directories (within `src/main/java/xiaozhi/`):** * `modules/`: Business logic organized by function (e.g., `sys`, `agent`, `device`, `config`, `timbre`, `ota`), typically following Controller, Service, DAO, Entity, DTO layers. * `common/`: Shared code, global configurations (Spring, MyBatis, Redis, Shiro, Knife4j), AOP aspects, exception handling, utilities. * `pom.xml`: Maven project configuration. * `src/main/resources/db/liquibase`: Database schema migrations. * `src/main/resources/application.yml`: Spring Boot configuration. * **`main/manager-web/` (Vue.js Management Frontend)** * **Purpose:** Provides a web-based control panel for system configuration and management via `manager-api`. * **Key Sub-directories (within `src/`):** * `main.js`: Application entry point. * `router/index.js`: Client-side routing (Vue Router). * `store/index.js`: State management (Vuex). * `apis/`: Modules for API communication with `manager-api`. * `views/`: Page-level Vue components. * `components/`: Reusable UI components. * `package.json`: NPM project configuration. * `.env.*`: Environment-specific configurations. ## 3. Core Functionality * **Voice Interaction:** * Supports wake-up word and manual activation. * Features real-time interruption of TTS playback. * Utilizes Voice Activity Detection (VAD) for accurate speech segmentation. * Multi-language support for ASR and TTS (Mandarin, Cantonese, English, etc., provider-dependent). * Handles real-time audio streaming via WebSockets. * **AI Service Integration:** * Highly modular provider pattern in `xiaozhi-server` for flexible integration of: * **ASR (Automatic Speech Recognition):** Local (FunASR, SherpaASR) and cloud services. * **LLM (Large Language Model):** Various models via OpenAI-compatible APIs (ChatGLM, Doubao, etc.) and direct integrations (Ollama). * **TTS (Text-to-Speech):** Cloud-based streaming (EdgeTTS, Lingxi) and local models (FishSpeech). * **VLLM (Vision Large Language Model):** For multi-modal interactions (e.g., ChatGLMVLLM). * **Intent Recognition:** LLM-based or function calling. * **Memory:** Local short-term memory and external services (mem0ai). * Allows easy switching between AI backends via configuration. * **IoT and Device Control:** * Manages registered ESP32 devices. * Interprets voice commands to control connected IoT devices, often via the plugin system. * Supports IOT and MCP (Module Communication Protocol). * **Web-Based Management (`manager-web`/`api`):** * Centralized web interface (智控台) for: * Configuring AI services (providers, API keys) for `xiaozhi-server`. * User and device management. * TTS voice timbre customization. * OTA (Over-The-Air) firmware updates for ESP32 devices. * `xiaozhi-server` dynamically fetches its configuration from `manager-api`. * **Plugin System and Extensibility:** * `xiaozhi-server` allows adding custom "skills" (e.g., weather, news, Home Assistant control) through a plugin system (`plugins_func/`). * LLMs can trigger these plugins using function calling capabilities. ## 4. Key Technologies * **`xiaozhi-server` (Python AI Engine):** * **Language:** Python 3 * **Core:** Asyncio, `websockets` library (for WebSocket server). * **Libraries:** `aiohttp`/`httpx` (HTTP client), `PyYAML` (config parsing), FFmpeg (audio processing), AI-specific libraries (FunASR, SileroVAD, OpenAI client). * **`manager-api` (Java Management Backend):** * **Language:** Java 21 * **Frameworks:** Spring Boot 3, Spring MVC. * **Libraries:** MyBatis-Plus (ORM), MySQL (database), Redis (caching via Spring Data Redis), Apache Shiro (security), Liquibase (DB migration), Knife4j (API docs), Maven (build). * **`manager-web` (Vue.js Management Frontend):** * **Languages:** JavaScript (ES6+), SCSS * **Frameworks:** Vue.js 2, Vue CLI. * **Libraries:** Vue Router, Vuex, Element UI (UI components), Flyio/Axios (HTTP client), Workbox (PWA). * **Overall:** Git (version control), Docker (containerization). ## 5. Inter-Component Communication * **ESP32 Devices <-> `xiaozhi-server`:** * **Protocol:** WebSocket (WS/WSS). * **Data:** Binary messages for audio streams (mic input to server, TTS output to device); JSON text messages for control, status, and metadata. * **Purpose:** Real-time voice interaction, device control, and status reporting. * **`manager-web` (Frontend) <-> `manager-api` (Backend):** * **Protocol:** HTTP/HTTPS (RESTful APIs). * **Data Format:** JSON. * **Purpose:** Enables administrators to manage and configure the system (users, devices, AI settings, OTA) via the web UI. * **`xiaozhi-server` <-> `manager-api`:** * **Protocol:** HTTP/HTTPS (RESTful APIs). * **Data Format:** JSON. * **Purpose:** `xiaozhi-server` pulls its operational configuration (AI providers, API keys, etc.) from `manager-api`, allowing dynamic updates. * **Interactions with External AI Services:** * **Protocol:** Primarily HTTP/HTTPS (RESTful APIs or service-specific SDKs); potentially WebSockets for streaming ASR/TTS. * **Data Format:** Mostly JSON; also audio formats. * **Purpose:** `xiaozhi-server` communicates with various cloud-based or local AI services for ASR, LLM, TTS, etc. * **`manager-api` <-> Databases (MySQL, Redis):** * **Protocol:** JDBC for MySQL, RESP for Redis. * **Purpose:** MySQL for persistent storage of configurations and management data; Redis for caching. ## 6. Deployment Options * **Docker-Based Deployment:** * **Simplified Installation (`xiaozhi-server` only):** * Deploys only the Python AI engine. Uses `docker-compose.yml`. * A `docker-setup.sh` script aids in initial setup (directories, config, model download). * **Full Module Installation (All Components):** * Deploys `xiaozhi-server`, `manager-api` (as `xiaozhi-esp32-server-web`), MySQL, and Redis. Uses `docker-compose_all.yml`. * Enables full web management capabilities. Requires configuration of `server.secret` from `manager-api` into `xiaozhi-server`'s config. * Pre-built images are available, and local builds are possible using provided Dockerfiles. * **Source Code Deployment:** * **`xiaozhi-server`:** Requires Python 3.10, Conda (recommended for `libopus`, `ffmpeg`), and `pip` dependencies. * **`manager-api`:** Requires JDK 21, Maven, and manual setup of MySQL/Redis. * **`manager-web`:** Requires Node.js and `npm` for dependencies and running the development server. * Suitable for development, customization, or specific environment needs. * **Configuration:** * `xiaozhi-server` relies on a `.config.yaml` file (in a `data/` directory) for its settings, including AI service selection and API keys. * In full deployments, `manager-api` settings are in `application.yml`, and `manager-web` settings in `.env` files. The web UI (智控台) becomes the primary interface for managing most operational configurations stored in the database.