- Curated guides under docs/; trim China-only integration docs and screenshots - Root and main README set aligned with westernized product positioning - LICENSE text update where applicable
64 KiB
Technical Documentation: xiaozhi-esp32-server
Table of Contents:
- Introduction
- Overall Architecture
- Core Components Deep Analysis
- Data Flow and Interaction Mechanisms
- Core Functionality Overview
- Deployment and Configuration Overview
1. Introduction
The xiaozhi-esp32-server project is a comprehensive backend system specifically designed to support ESP32-based intelligent hardware. Its core objective is to enable developers to quickly build a powerful server infrastructure that not only understands natural language instructions but also efficiently interacts with various AI services (for speech recognition, natural language understanding, and speech synthesis), manages Internet of Things (IoT) devices, and provides a web-based user interface for system configuration and management. By integrating multiple cutting-edge technologies into a highly cohesive and extensible platform, this project aims to simplify and accelerate the development process of customizable voice assistants and intelligent control systems. It is not just a simple server, but a bridge connecting hardware, AI capabilities, and user management.
2. Overall Architecture
The xiaozhi-esp32-server system adopts a distributed, multi-component collaborative architectural design, ensuring modularity, maintainability, and scalability of the system. Each core component has its own responsibilities and works collaboratively. The main components include:
-
ESP32 Hardware (Client Device): This is the physical intelligent hardware device that end users directly interact with. Its main responsibilities include:
- Capturing user voice commands.
- Securely sending captured raw audio data to
xiaozhi-serverfor processing. - Receiving synthesized voice responses from
xiaozhi-serverand playing them to users through speakers. - Controlling other peripheral devices or IoT devices connected to it (such as smart bulbs, sensors, etc.) based on instructions received from
xiaozhi-server.
-
xiaozhi-server(Core AI Engine - Python Implementation): This Python-based server is the "brain" of the entire system, responsible for handling all voice-related logic and AI interactions. Its key responsibilities are detailed as follows:- Establishing stable, low-latency real-time bidirectional communication links with ESP32 devices through the WebSocket protocol.
- Receiving audio streams from ESP32 and using Voice Activity Detection (VAD) technology to precisely segment effective voice segments.
- Integrating and calling Automatic Speech Recognition (ASR) services (configurable for local or cloud), converting voice segments to text.
- Parsing user intent and generating intelligent responses through interaction with Large Language Models (LLM), supporting complex natural language understanding tasks.
- Managing context information and user memory in multi-turn conversations to provide coherent interaction experiences.
- Calling Text-to-Speech (TTS) services to synthesize LLM-generated text responses into natural and fluent speech.
- Executing custom commands through a flexible plugin system, including control logic for IoT devices.
- Obtaining detailed runtime operation configuration from the
manager-apiservice.
-
manager-api(Management Backend - Java Implementation): This is an application built on the Java Spring Boot framework that provides a secure RESTful API for the management and configuration of the entire system. It serves not only as the backend support for themanager-webconsole but also as the configuration data source forxiaozhi-server. Its core functions include:- Providing user authentication (login, permission verification) and user account management functions for the web console.
- ESP32 device registration, information management, and maintenance of device-specific configurations.
- Persistently storing system configurations in MySQL database, such as user-selected AI service providers, API keys, device parameters, plugin settings, etc.
- Providing specific API endpoints for
xiaozhi-serverto pull the latest configuration it needs. - Managing TTS voice options, handling OTA (Over-The-Air) firmware update processes and related metadata.
- Using Redis as high-speed cache to store hot data (such as session information, frequently accessed configurations) to improve API response speed and overall system performance.
-
manager-web(Web Control Panel - Vue.js Implementation): This is a Single Page Application (SPA) built on Vue.js that provides system administrators with a graphical, user-friendly operation interface. Its main capabilities include:- Conveniently configuring various AI services used by
xiaozhi-server(such as ASR, LLM, TTS provider switching, parameter adjustment). - Managing platform user accounts, role assignment, and permission control.
- Managing registered ESP32 devices and their related settings.
- (Potential functionality) Monitoring system operation status, viewing logs, troubleshooting, etc.
- Comprehensive interaction with all backend management functions provided by
manager-api.
- Conveniently configuring various AI services used by
-
manager-mobile(Smart Control Console Mobile Version - uni-app Implementation): This is a cross-platform mobile management terminal based on uni-app v3 + Vue 3 + Vite, supporting App (Android & iOS) and WeChat Mini Program. Its main capabilities include:- Providing convenient management interface on mobile devices, similar to manager-web functionality but optimized for mobile.
- Supporting core functions such as user login, device management, AI service configuration, etc.
- Cross-platform adaptation, one set of code can run simultaneously on iOS, Android, and WeChat Mini Program.
- Implementing network requests based on alova + @alova/adapter-uniapp, seamlessly integrated with manager-api.
- Using pinia for state management to ensure data consistency.
High-Level Interaction Flow Overview:
- Voice Interaction Main Line: After ESP32 device captures user voice, it transmits audio data in real-time to
xiaozhi-serverthrough WebSocket. Afterxiaozhi-servercompletes a series of AI processing (VAD, ASR, LLM interaction, TTS), it sends the synthesized voice response back to ESP32 device for playback through WebSocket. All real-time interactions directly related to voice are completed in this link. - Management Configuration Main Line: Administrators access the
manager-webconsole through browsers.manager-webexecutes various management operations (such as modifying configurations, managing users or devices) by calling RESTful HTTP interfaces provided bymanager-api. Data is passed between the two in JSON format. - Configuration Synchronization: When
xiaozhi-serverstarts or specific update mechanisms are triggered, it actively pulls its latest operation configuration frommanager-apithrough HTTP requests. This ensures that configuration changes made by administrators on the web interface can be timely and effectively applied to the operation of the core AI engine.
This frontend-backend separation, core service and management service separation architectural design enables xiaozhi-server to focus on efficient real-time AI processing tasks, while manager-api and manager-web together provide a powerful and easy-to-use management and configuration platform. Each component has clear responsibilities, which is conducive to independent development, testing, deployment, and expansion.
xiaozhi-esp32-server
├─ xiaozhi-server Port 8000 Python development Responsible for ESP32 communication
├─ manager-web Port 8001 Node.js+Vue development Responsible for providing web interface for console
├─ manager-api Port 8002 Java development Responsible for providing console API
└─ manager-mobile Cross-platform mobile app uni-app+Vue3 development Responsible for providing mobile smart console management
3. Core Components Deep Analysis
3.1. xiaozhi-server (Core AI Engine - Python Implementation)
xiaozhi-server serves as the intelligent core of the system, fully responsible for handling voice interactions, interfacing with various AI services, and managing communication with ESP32 devices. Its design goal is to achieve efficient, flexible, and extensible voice AI processing capabilities.
-
Core Objectives:
- Providing real-time voice command processing services for ESP32 devices.
- Deep integration of various AI services, including: Automatic Speech Recognition (ASR), Large Language Models (LLM) for Natural Language Understanding (NLU), Text-to-Speech (TTS), Voice Activity Detection (VAD), Intent Recognition, and Conversation Memory.
- Fine-grained management of conversation flow and context state between users and devices.
- Based on user instructions, executing custom functions and controlling Internet of Things (IoT) devices through a plugin mechanism.
- Supporting dynamic configuration loading and updates through
manager-api.
-
Core Technology Stack:
- Python 3: As the main programming language, Python is chosen for its rich AI/ML ecosystem libraries and rapid development characteristics.
- Asyncio: Python's asynchronous programming framework, which is key to
xiaozhi-server's high performance. It is widely used for efficiently handling concurrent WebSocket connections from multiple ESP32 devices, as well as performing non-blocking I/O operations when communicating with external AI service APIs, ensuring server responsiveness under high concurrency. websocketslibrary: Provides specific implementation of WebSocket server, supporting full-duplex real-time communication with ESP32 clients.- HTTP clients (such as
aiohttp,httpx): Used for asynchronous HTTP requests, mainly to obtain configuration information frommanager-apiand interact with cloud AI service APIs. - YAML (usually through PyYAML library): Used to parse local
config.yamlconfiguration files. - FFmpeg (external dependency): Checked during
app.pystartup (check_ffmpeg_installed()). FFmpeg is typically used for audio processing and format conversion, for example, ensuring audio data meets specific AI service requirements or for internal processing.
-
Key Implementation Details:
-
AI Service Provider Pattern (Provider Pattern -
core/providers/):- Design Philosophy: This is the core design pattern for
xiaozhi-serverto integrate different AI services, greatly enhancing the system's flexibility and extensibility. For each AI service type (ASR, TTS, LLM, VAD, Intent, Memory, VLLM), an abstract base class (ABC, Abstract Base Class) is defined in its corresponding subdirectory, such ascore/providers/asr/base.py. This base class specifies the common interface methods that services of this type must implement (such as ASR'sasync def transcribe(self, audio_chunk: bytes) -> str: pass). - Specific Implementation: Various specific AI service providers or local model implementations exist as independent Python classes (for example,
core/providers/asr/openai.pyimplements OpenAI ASR logic,core/providers/llm/openai.pyimplements integration with OpenAI GPT models). These specific classes inherit from the corresponding abstract base class and implement the interfaces they define. Some providers also use DTOs (Data Transfer Objects, existing in their respectivedto/directories) to structure data exchanged with external services. - Advantages: Enables core business logic to call different AI services in a unified manner without caring about their underlying specific implementations. Users can easily switch AI service backends through configuration files. Adding support for new AI services also becomes relatively simple, requiring only implementation of the corresponding Provider interface.
- Dynamic Loading and Initialization: The
core/utils/modules_initialize.pyscript plays the role of a factory. When the server starts or receives configuration update instructions, it dynamically imports and instantiates the corresponding Provider classes based on theselected_modulein the configuration file and the specific provider settings for each service.
- Design Philosophy: This is the core design pattern for
-
WebSocket Communication and Connection Handling (
app.py,core/websocket_server.py,core/connection.py):- Server Startup and Entry (
app.py):app.pyserves as the main entry point, responsible for initializing the application environment (such as checking FFmpeg, loading configuration, setting up logging).- It generates or loads an
auth_key(JWT key) to protect specific HTTP interfaces (such as the vision analysis interface/mcp/vision/explain). Ifmanager-api.secretin the configuration is empty, it generates a UUID asauth_key. - Uses
asyncio.create_task()to concurrently startWebSocketServer(listening onws://0.0.0.0:8000/xiaozhi/v1/) andSimpleHttpServer(listening onhttp://0.0.0.0:8003/xiaozhi/ota/). - Contains a
monitor_stdin()coroutine for keeping the application alive or handling terminal input in certain environments.
- WebSocket Server Core (
core/websocket_server.py):- The
WebSocketServerclass uses thewebsocketslibrary to listen for connection requests from ESP32 devices. - For each successful WebSocket connection, it creates an independent
ConnectionHandlerinstance (presumably defined incore/connection.py). This design pattern of one handler instance per connection is key to achieving multi-device state isolation and concurrent processing, ensuring that conversation flows and context information for each device do not interfere with each other. - The server also provides an
_http_responsemethod that allows simple responses to non-WebSocket upgrade HTTP GET requests on the same port (such as returning "Server is running"), facilitating health checks.
- The
- Dynamic Configuration Updates:
WebSocketServercontains anupdate_config()asynchronous method. This method usesconfig_lock(anasyncio.Lock) to ensure atomicity of configuration updates. It callsget_config_from_api()(possibly implemented inconfig_loader.py, communicating withmanager-apithroughmanage_api_client.py) to obtain new configuration. Through helper functions likecheck_vad_update()andcheck_asr_update(), it determines whether specific AI modules need to be reinitialized, avoiding unnecessary overhead. The updated configuration is used to re-callinitialize_modules(), thus achieving hot-switching of AI service providers.
- Server Startup and Entry (
-
Message Processing and Conversation Flow Control (
core/handle/andConnectionHandler):ConnectionHandler(presumably) serves as the control center for each connection, responsible for receiving messages from ESP32 and distributing them to corresponding processing modules in thecore/handle/directory based on message type or current conversation state. This modular processor design makesConnectionHandlerlogic clearer and easier to extend.- Main Processing Modules and Their Responsibilities:
helloHandle.py: Handles handshake protocols, device authentication, or initialization information exchange when initially connecting with ESP32.receiveAudioHandle.py: Receives audio stream data, calls VAD Provider for voice activity detection, and passes effective audio segments to ASR Provider for recognition.textHandle.py/intentHandler.py: After obtaining text recognized by ASR, interacts with Intent Provider (possibly using LLM for intent recognition) and LLM Provider to understand user intent and generate preliminary responses or decisions.functionHandler.py: When LLM responses contain instructions to execute specific "function calls", this module is responsible for finding and executing corresponding plugin functions from the plugin registry.sendAudioHandle.py: Delivers LLM-generated final text responses to TTS Provider for speech synthesis and sends audio streams back to ESP32 through WebSocket.abortHandle.py: Handles interrupt requests from ESP32, such as stopping current TTS playback.iotHandle.py,mcpHandle.py: Handle specific instructions related to IoT device control or more complex module communication protocols (MCP).
-
Plugin-based Function Extension System (
plugins_func/):- Design Purpose: Provides a standardized way to extend voice assistant functionality and "skills" without modifying core code.
- Implementation Mechanism:
- Various specific functions exist as independent Python scripts in the
plugins_func/functions/directory (for example,get_weather.py,hass_set_state.pyfor Home Assistant integration). loadplugins.pyis responsible for scanning and loading these plugin modules when the server starts.register.py(or specific decorators/functions within plugin modules) may be used to define metadata for each plugin function, including:- Function Name: Identifier used when LLM calls the function.
- Function Description: For LLM to understand the purpose of this function.
- Parameters Schema: Usually a JSON Schema that defines in detail the parameters required by the function, their types, whether they are required, and descriptions. This is key for LLM to correctly generate function call parameters.
- Various specific functions exist as independent Python scripts in the
- Execution Flow: When LLM decides during its thinking process that it needs to call some external tool or function to obtain information or perform operations, it generates a structured "function call" request based on the pre-provided function schema.
functionHandler.pyinxiaozhi-servercaptures this request, finds the corresponding Python function from the plugin registry and executes it, then returns the execution result to LLM, which generates the final natural language response to the user based on this result.
-
Configuration Management (
config/):- Loading Mechanism:
config_loader.py(called throughsettings.py) is responsible for loading basic configuration from theconfig.yamlfile in the root directory. - Remote Configuration and Merging: Through
manage_api_client.py(using libraries likeaiohttpto communicate withmanager-api), configuration can be pulled from themanager-apiservice. Remote configuration usually overrides settings with the same name in localconfig.yaml, thus enabling dynamic adjustment of server behavior through the web interface. - Logging System:
logger.pyinitializes the application logging system (possibly usingloguruor wrapping the standardloggingmodule, supporting adding tags throughlogger.bind(tag=TAG)for easy tracking and filtering). - Static Resources: The
config/assets/directory contains static audio files used for system prompts (such as device binding prompt soundbind_code.wav, error prompt sounds, etc.).
- Loading Mechanism:
-
Auxiliary HTTP Service (
core/http_server.py):- Runs a simple HTTP server in parallel with the WebSocket service to handle specific HTTP requests. The main function is to provide OTA (Over-The-Air) firmware update download services for ESP32 devices (through the
/xiaozhi/ota/endpoint). Additionally, it may also host other utility HTTP interfaces such as/mcp/vision/explain(vision analysis).
- Runs a simple HTTP server in parallel with the WebSocket service to handle specific HTTP requests. The main function is to provide OTA (Over-The-Air) firmware update download services for ESP32 devices (through the
-
In summary, xiaozhi-server is a highly modular, configuration-driven AI application server built using modern Python asynchronous programming models. Its carefully designed Provider pattern and plugin architecture give it powerful adaptability and extensibility, enabling flexible integration of different AI capabilities and supporting growing functional requirements.
3.2. manager-api (Management Backend - Java Spring Boot Implementation)
The manager-api component is a powerful backend service built using Java and Spring Boot framework, serving as the central administrative management and configuration hub for the entire xiaozhi-esp32-server ecosystem.
-
Core Objectives:
- Providing a secure, stable, RESTful-compliant API interface for
manager-web(Vue.js frontend), enabling administrators to conveniently manage users, devices, system configurations, and other related resources. - Acting as a centralized configuration data provider for
xiaozhi-server(Python core AI engine), allowingxiaozhi-serverinstances to obtain their latest operation parameters during startup or runtime. - Persistently storing critical data, such as: user account information, device registration details, AI service provider configurations (including API keys, selected service models, etc.), TTS voice parameters, and OTA firmware version information.
- Providing a secure, stable, RESTful-compliant API interface for
-
Core Technology Stack:
- Java 21: The JDK version adopted by the project, ensuring support for modern Java features.
- Spring Boot 3: As the core development framework, it greatly simplifies the creation and deployment of independent, production-level Spring applications. It provides key features such as auto-configuration, embedded web server (default Tomcat), dependency management, etc.
- Spring MVC: Module in the Spring framework for building web applications and RESTful APIs.
- MyBatis-Plus: An ORM (Object-Relational Mapping) framework that enhances MyBatis functionality. It simplifies database operations, provides powerful CRUD (Create, Read, Update, Delete) functionality, conditional constructors, code generators, etc., and integrates well with Spring Boot.
- MySQL: As the main backend relational database, used for storing all management data and configuration information that need to be persisted.
- Druid: A powerful JDBC connection pool implementation that provides rich monitoring capabilities and excellent performance for efficient database connection management.
- Redis (through Spring Data Redis): A high-performance in-memory data structure store, commonly used for implementing data caching (such as caching hot configuration data, user session information) to significantly improve API response speed.
- Apache Shiro: A mature and easy-to-use Java security framework responsible for handling application authentication (user identity verification) and authorization (API access permission control) requirements.
- Liquibase: An open-source tool for tracking, managing, and applying database schema changes. It allows developers to define and version database structure changes in a database-independent way.
- Knife4j: An API documentation generation tool that integrates Swagger and enhances UI, designed specifically for Java MVC frameworks (especially Spring Boot). It can generate beautiful and interactive API documentation interfaces (usually accessible through
/xiaozhi/doc.html). - Maven: Used for project build automation and dependency management.
- Lombok: A Java library that automatically generates constructors, getters/setters, equals/hashCode, toString, and other boilerplate code through annotations, reducing redundancy.
- HuTool / Google Guava: Provides a large number of utility classes to simplify common programming tasks.
- Twilio SDK: Twilio SMS service SDK for integrating SMS sending functionality (such as verification codes, notifications).
-
Key Implementation Details:
-
Modular Project Structure (
modules/package):- The core business logic of
manager-apiis clearly divided into different modules under thesrc/main/java/xiaozhi/modules/directory. This approach of dividing modules by functional domain (for example,sysresponsible for system management,agentresponsible for agent configuration,deviceresponsible for device management,configresponsible for providing configuration forxiaozhi-server,securityresponsible for security,timbreresponsible for voice management,otaresponsible for firmware upgrades) greatly improves code maintainability and extensibility. - Internal Structure of Each Module: Each business module typically follows the classic three-layer architecture or its variants:
- Controller (Control Layer): Located at
xiaozhi.modules.[module_name].controller. - Service (Service Layer): Located at
xiaozhi.modules.[module_name].service. - DAO/Mapper (Data Access Layer): Located at
xiaozhi.modules.[module_name].dao. - Entity (Entity Class): Located at
xiaozhi.modules.[module_name].entity. - DTO (Data Transfer Object): Located at
xiaozhi.modules.[module_name].dto.
- Controller (Control Layer): Located at
- The core business logic of
-
Layered Architecture Implementation:
- Controller Layer (
@RestController): These classes use Spring MVC annotations (such as@GetMapping,@PostMapping, etc.) to define API endpoints. They are responsible for receiving HTTP requests, deserializing JSON data from the request body into DTO objects, calling corresponding Service layer methods to handle business logic, and finally serializing the Service layer's return results into JSON and returning them as HTTP responses to clients. - Service Layer (
@Service): These classes (usually combinations of interfaces and their implementation classes) encapsulate core business rules and operation flows. They may call one or more DAO/Mapper objects to interact with the database and often use@Transactionalannotations to manage database transaction atomicity. - Data Access (DAO/Mapper) Layer (MyBatis-Plus Mappers): These are Java interfaces that inherit from the
BaseMapper<Entity>interface provided by MyBatis-Plus. MyBatis-Plus automatically provides standard CRUD methods for these interfaces. For more complex database queries, developers can implement them by defining methods in Mapper interfaces and using annotations (such as@Select,@Update) or writing corresponding XML mapping files. For example,UserMapper.selectById(userId)will be automatically implemented by MyBatis-Plus. - Entity Layer (
@TableName,@TableIdand other MyBatis-Plus annotations): These POJO (Plain Old Java Objects) classes directly map to table structures in the database. Lombok's@Dataannotation is commonly used to automatically generate getters/setters, etc. - DTO Layer: Used for data transfer between layers, especially between Controller layer and Service layer, and in API request/response bodies. Using DTOs helps decouple API interface data structures from database entity data structures, making APIs more stable.
- Controller Layer (
-
Common Functionality and Configuration (
common/package):- The
src/main/java/xiaozhi/common/package provides a series of common components and configurations shared across modules:- Base Classes: Such as
BaseDao,BaseEntity,BaseService,CrudService, providing common properties or methods for corresponding components in each module. - Global Configuration: Including
MybatisPlusConfig(MyBatis-Plus configuration, such as pagination plugins, data permission plugins, etc.),RedisConfig(Redis connection and serialization configuration),SwaggerConfig(Knife4j configuration),AsyncConfig(asynchronous task executor configuration). - Custom Annotations: For example,
@LogOperationfor recording operation logs through AOP,@DataFiltermay be used to implement data scope filtering. - AOP Aspects: Such as
RedisAspectmay be used to implement method-level caching logic. - Global Exception Handling:
RenExceptionHandler(using@ControllerAdviceannotation) catches specific or all exceptions thrown in the application (such as customRenException) and returns unified format JSON error responses to clients.ErrorCodedefines standardized error codes. - Utility Classes: Provides various practical tools such as date conversion, JSON processing (Jackson), IP address acquisition, HTTP context operations, unified result encapsulation (
Resultclass), etc. - Validation Tools:
ValidatorUtilsandAssertUtilsare used to simplify parameter validation logic. - XSS Protection: Components like
XssFilterare used to prevent cross-site scripting attacks. - MyBatis-Plus Auto-fill:
FieldMetaObjectHandleris used to automatically fill common fields likecreateTime,updateTimewhen executing insert or update database operations.
- Base Classes: Such as
- The
-
Security Mechanism (Apache Shiro):
- Shiro's configuration (usually under
modules/security/config/orcommon/config/) defines how user authentication and authorization are performed. - Realms: Custom Shiro Realm classes are responsible for querying user information (username, password, salt) from the database for identity verification, as well as obtaining user role and permission information for authorization decisions.
- Filters: Shiro filter chains are applied to protect API endpoints, ensuring that only authenticated users with sufficient permissions can access specific resources.
- Session/Token Management: Shiro manages user sessions. For RESTful APIs, it may combine with token mechanisms like OAuth2 or JWT to implement stateless authentication.
- Shiro's configuration (usually under
-
Database Version Control (Liquibase):
- Changes to database table structures, indexes, initial data, etc., are defined and version-managed through Liquibase's
changelogfiles (usually in XML format). When the application starts, Liquibase automatically checks and applies necessary database structure updates, ensuring consistency of database structures across development, testing, and production environments.
- Changes to database table structures, indexes, initial data, etc., are defined and version-managed through Liquibase's
-
API Documentation:
- Complete API interface documentation can be accessed at: https://2662r3426b.vicp.fun/xiaozhi/doc.html
- This documentation is generated using Knife4j, providing detailed descriptions of all RESTful API endpoints, request/response examples, and online testing functionality.
-
manager-api builds a comprehensive, well-structured, secure, reliable, and easy-to-maintain and extend Java backend service through these carefully selected technologies and design patterns. Its modular design is particularly suitable for handling complex systems with multiple management functional requirements.
3.3. manager-web (Web Management Frontend - Vue.js Implementation)
The manager-web component is a Single Page Application (SPA) built using the Vue.js 2 framework. It provides system administrators with a feature-rich, user-friendly graphical user interface for comprehensive management and configuration of the xiaozhi-esp32-server ecosystem.
-
Core Objectives:
- Providing a web-based centralized control panel for administrators to perform system operations and monitoring.
- Implementing convenient management of AI service providers (ASR, LLM, TTS, etc.) in
xiaozhi-serverand their related API keys or license configurations. - Supporting fine-grained management of user accounts, roles, and permissions.
- Providing ESP32 device registration, configuration, and status viewing functionality.
- Allowing administrators to customize TTS voices, manage OTA firmware update processes, adjust system-level parameters and dictionary data, etc.
- Serving as a graphical interactive frontend for all functions exposed by
manager-api.
-
Core Technology Stack:
- Vue.js 2: A progressive JavaScript framework for building user interfaces. Its core features include declarative rendering, component-based systems, data binding, etc., making it very suitable for building complex SPAs.
- Vue CLI (
@vue/cli-service): Vue.js's official command-line tool for rapid project setup, development server operation (supporting Hot Module Replacement HMR), and production environment build packaging (internally integrated and configured with Webpack). - Vue Router (
vue-router): Vue.js's official route manager. It is responsible for implementing navigation switching between different "pages" or view components within the SPA without reloading the entire HTML page, providing a smooth user experience. - Vuex (
vuex): Vue.js's official state management pattern and library. It serves as a "central data store" for all components in the application, used for managing globally shared state (such as current logged-in user information, device lists, application configuration, etc.), particularly suitable for large and complex applications. - Element UI (
element-ui): A popular desktop UI component library based on Vue 2.0. It provides a large number of pre-designed and implemented components (such as forms, tables, dialogs, navigation menus, buttons, prompts, etc.), helping developers quickly build professional and consistent user interfaces. - JavaScript (ES6+): The main programming language for frontend logic implementation, utilizing its modern features for development.
- SCSS (Sassy CSS): A CSS preprocessor that adds advanced features to CSS such as variables, nested rules, mixins, inheritance, etc., making CSS code easier to organize, maintain, and reuse.
- HTTP Client (Flyio or Axios through
vue-axios): Used to initiate asynchronous HTTP (AJAX) requests from the browser to themanager-apibackend to obtain data or submit operations. - Webpack: A powerful module bundler (managed and configured by Vue CLI at the bottom layer). It treats various resources in the project (JavaScript files, CSS, images, fonts, etc.) as modules and bundles them into browser-recognizable static files.
- Workbox (through
workbox-webpack-plugin): A library developed by Google to simplify Service Worker writing and PWA (Progressive Web App) implementation. It can help generate Service Worker scripts, implement resource caching, offline access, and other functions. - Opus Libraries (
opus-decoder,opus-recorder): These audio processing libraries indicate that the frontend may have some ability to directly process Opus format audio in the browser, for example: for testing microphone input, allowing administrators to record custom audio segments (possibly for TTS voice samples or voice command testing), or playing Opus-encoded audio previewed in the management interface.
-
Key Implementation Details:
-
Single Page Application (SPA) Structure:
- The entire frontend application loads one main HTML file (
public/index.html). All subsequent page switching and content updates are dynamically completed on the client side by Vue Router, without needing to request new HTML pages from the server each time. This pattern provides faster page loading speeds and smoother interaction experiences.
- The entire frontend application loads one main HTML file (
-
Component-Based Architecture:
- The user interface consists of a series of reusable Vue components (
.vuesingle-file components), forming a component tree. This approach improves code modularity, maintainability, and reusability. src/main.js: The application's entry JS file. It is responsible for creating and initializing the root Vue instance, registering global plugins (such as Vue Router, Vuex, Element UI), and mounting the root Vue instance to a DOM element inpublic/index.html(usually#app).src/App.vue: The application's root component. It typically defines the application's basic layout structure (such as including navigation bar, sidebar, main content area) and displays the view component matched by the current route through the<router-view></router-view>tag.- View Components (
src/views/): These components represent various "pages" or main functional areas in the application (for example,Login.vuelogin page,DeviceManagement.vuedevice management page,UserManagement.vueuser management page,ModelConfig.vuemodel configuration page). They are usually directly mapped by Vue Router. - Reusable UI Components (
src/components/): Contains smaller-grained UI components shared between different views (for example,HeaderBar.vuetop navigation bar,AddDeviceDialog.vueadd device dialog,AudioPlayer.vueaudio player component).
- The user interface consists of a series of reusable Vue components (
-
Client-Side Routing (
src/router/index.js):- Vue Router is configured in this file, defining the application's routing table. Each routing rule maps a specific URL path to a view component.
- Often includes Navigation Guards, such as
beforeEachguards, used to execute logic before route transitions, such as checking whether the user is logged in, and redirecting to the login page if not logged in, thus protecting pages that require authentication to access.
-
State Management (
src/store/index.js):- Vuex is used to build a centralized state management center (Store). This Store includes:
- State: Stores application-level shared data (for example, detailed information of the current logged-in user, device lists obtained from APIs, system configuration, etc.).
- Getters: Similar to computed properties in Vue components, used to derive some state values from State for convenient component use.
- Mutations: The only methods that can synchronously modify data in State. They must be synchronous functions.
- Actions: Used to handle asynchronous operations (such as API calls) or encapsulate multiple Mutation submissions. Actions call APIs, and after obtaining data, update State by
committing one or more Mutations.
- For example, when a user logs in, an Action named
loginmight be called, which sends a login request to the backend API, obtains user information and token after success, thencommits a Mutation namedSET_USER_INFOto update user information and token in State.
- Vuex is used to build a centralized state management center (Store). This Store includes:
-
API Communication (
src/apis/):- All HTTP communication logic with the
manager-apibackend is encapsulated in thesrc/apis/directory, usually organized according to backend API modules (for example,src/apis/module/agent.js,src/apis/module/device.js). - Each module exports a series of functions, each function corresponding to a specific API request. These functions internally use configured HTTP client instances (for example, uniformly configuring Axios or Flyio instances in
src/apis/api.jsorsrc/apis/httpRequest.js, possibly including setting request base addresses, request/response interceptors, etc.). - Interceptors: HTTP client request interceptors are commonly used to automatically add authentication tokens (such as JWT) before each request is sent; response interceptors can be used for global handling of API errors (such as insufficient permissions, server errors) or preprocessing response data.
- All HTTP communication logic with the
-
Styles and Resources (
src/styles/,src/assets/):Element UIprovides basic component styles.- The
src/styles/global.scssfile is used to define globally shared SCSS styles, variables, mixins, etc. - The
<style scoped>tag inside Vue single-file components allows writing local styles that only affect the current component. - The
src/assets/directory stores static resources such as images and fonts.
-
Build and PWA Features:
- Vue CLI packages all code and resources into optimized static files through Webpack for production deployment.
- The use of
workbox-webpack-plugin(reflected inservice-worker.jsandregisterServiceWorker.jsfiles) indicates that the project integrates Service Worker technology. Service Workers can intercept network requests, implement intelligent caching of frontend resources (thus speeding up subsequent access), and even provide certain offline access capabilities when the network is disconnected, which is one of the core technologies of PWA.
-
Environment Configuration (
.envseries files):- The
.envfiles (as well as.env.development,.env.production, etc.) in the project root directory are used to define environment variables. These variables (for example,VUE_APP_API_BASE_URLto specify the base URL ofmanager-api) can be accessed in application code through the formprocess.env.VUE_APP_XXX, thus allowing different parameters to be configured for different build environments (development, testing, production).
- The
-
manager-web builds a powerful, maintainable, and user-friendly management interface through the comprehensive application of these technologies, providing solid frontend support for the configuration and monitoring of the xiaozhi-esp32-server system.
3.4. manager-mobile (Smart Control Console Mobile Version - uni-app Implementation)
The manager-mobile component is a cross-platform mobile management terminal based on uni-app v3 + Vue 3 + Vite, supporting App (Android & iOS) and WeChat Mini Program. It provides system administrators with a mobile management interface, making management operations more convenient.
-
Core Objectives:
- Providing convenient management interface on mobile devices, similar to manager-web functionality but optimized for mobile.
- Supporting core functions such as user login, device management, AI service configuration, etc.
- Cross-platform adaptation, one set of code can run simultaneously on iOS, Android, and WeChat Mini Program.
- Providing smooth and efficient management experience for mobile users.
-
Platform Compatibility:
| H5 | iOS | Android | WeChat Mini Program |
|---|---|---|---|
| √ | √ | √ | √ |
-
Core Technology Stack:
- uni-app v3: A framework for developing all frontend applications using Vue.js, supporting iOS, Android, H5, and various mini programs.
- Vue 3: A progressive framework for building user interfaces, providing better performance and new features.
- Vite: Next-generation frontend development and build tool, providing extremely fast development experience.
- pnpm: Fast, disk-space-saving package manager.
- alova: Lightweight, flexible request strategy library, paired with @alova/adapter-uniapp to adapt to uni-app environment.
- pinia: Vue's state management library, replacing Vuex, providing more concise API and better TypeScript support.
- UnoCSS: High-performance and highly flexible instant atomic CSS engine.
- TypeScript: Provides type-safe development experience.
-
Key Implementation Details:
-
Cross-Platform Architecture:
- Based on uni-app framework, achieving the goal of one set of code running on multiple platforms, greatly reducing development and maintenance costs.
- For different platform characteristics and limitations, platform-specific code processing is done through conditional compilation.
-
Project Structure:
src/App.vue: The application's root component, defining global styles and configuration.src/main.ts: The application's entry file, responsible for initializing Vue instance, registering plugins and route interceptors.src/pages/: Stores application page components, such as login page, device management page, etc.src/layouts/: Defines application layout components, such as default layout, layout with tabbar, etc.src/api/: Encapsulates communication logic with backend APIs.src/store/: Uses pinia for state management.src/components/: Stores reusable components.src/utils/: Provides common utility functions.
-
Network Requests:
- Implements network requests based on alova + @alova/adapter-uniapp, uniformly handling request headers, authentication, errors, etc.
- Request addresses and environment configuration are managed through .env files, supporting switching between different environments.
-
Routing and Authentication:
- Uses uni-app's routing system, combined with route interceptors to implement page login verification and permission control.
- When unauthenticated users access pages requiring authentication, they are redirected to the login page.
-
State Management:
- Uses pinia to manage application state, such as user information, device lists, etc.
- Implements persistent storage of state through pinia-plugin-persistedstate plugin.
-
Build and Release:
- Supports multiple build commands, such as building WeChat Mini Program, Android and iOS App, etc.
- Uses HBuilderX for cloud packaging of Apps, simplifying the packaging process.
-
manager-mobile provides users with a fully functional, smooth mobile management tool through the application of these technologies, enabling administrators to perform system management and configuration anytime, anywhere.
4. Data Flow and Interaction Mechanisms
The xiaozhi-esp32-server system works collaboratively through clearly defined data flows and interaction protocols between components. The main communication methods rely on WebSocket protocol optimized for real-time interaction and RESTful API suitable for client-server requests.
4.1. Core Voice Interaction Flow (ESP32 Device <-> xiaozhi-server)
This flow is real-time, mainly through WebSocket for low-latency, bidirectional data exchange.
-
Communication Protocol Documentation:
- Detailed communication protocol documentation can be accessed at: https://github.com/lapy/xiaozhi-esp32-server/wiki
- This document details the WebSocket communication protocol between ESP32 devices and
xiaozhi-server, including:- Connection establishment and handshake process
- Audio data transmission format
- Control command format
- Status report format
- Error handling mechanism
-
Connection Establishment and Handshake:
- ESP32 device acts as a client, actively initiating WebSocket connection requests to the specified endpoint of
xiaozhi-server(for example,ws://<server IP>:<WebSocket port>/xiaozhi/v1/). xiaozhi-server(core/websocket_server.py) receives the connection and instantiates an independentConnectionHandlerobject for each successfully connected ESP32 device to manage the entire lifecycle of that session.- After connection establishment, an initial handshake process may be executed (handled by
core/handle/helloHandle.py) for exchanging device identification, authentication information, protocol version, or basic status.
- ESP32 device acts as a client, actively initiating WebSocket connection requests to the specified endpoint of
-
Audio Uplink Transmission (ESP32 ->
xiaozhi-server):- After a user speaks to the ESP32 device, the device's microphone captures raw audio data (usually PCM or compressed formats like Opus).
- ESP32 pushes these audio data chunks as WebSocket binary messages in real-time to the corresponding
ConnectionHandlerinxiaozhi-server. - The server-side
core/handle/receiveAudioHandle.pymodule is responsible for receiving, buffering, and processing this audio data.
-
AI Core Processing (within
xiaozhi-server):- VAD (Voice Activity Detection):
receiveAudioHandle.pyuses configured VAD providers (such as SileroVAD) to analyze audio streams, accurately identifying speech start and end points, filtering out silent or noise segments. - ASR (Automatic Speech Recognition): Detected effective speech segments are sent to configured ASR providers (local or cloud-based, depending on your deployment). ASR engines convert audio signals to text strings.
- NLU/LLM (Natural Language Understanding/Large Language Model): ASR output text, along with current conversation context history obtained from Memory providers, and description schemas of available functions (tools) loaded from
plugins_func/, are passed together to configured LLM providers. - Function Call Execution (if LLM decision requires): If LLM analysis determines that external functions need to be called (such as querying weather, controlling home appliances), it generates a structured function call request.
core/handle/functionHandler.pyreceives this request, finds and executes corresponding Python functions defined inplugins_func/, and returns the function execution results to LLM. LLM then generates the final natural language response based on this result. - Response Generation: LLM synthesizes all information (user input, context, function call results, etc.) to generate the final text response.
- Memory Update: Current round interactions (user questions, LLM responses, possible function calls) are processed by Memory providers to update conversation history for subsequent interactions.
- TTS (Text-to-Speech): LLM-generated final text responses are sent to configured TTS providers, which synthesize text into speech data streams (such as MP3 or WAV format).
- VAD (Voice Activity Detection):
-
Audio Downlink Response (
xiaozhi-server-> ESP32):- Speech data streams synthesized by TTS providers are sent back to ESP32 devices in real-time as WebSocket binary messages through the
core/handle/sendAudioHandle.pymodule. - ESP32 devices receive these audio data chunks and immediately play them to users through speakers.
- Speech data streams synthesized by TTS providers are sent back to ESP32 devices in real-time as WebSocket binary messages through the
-
Control and Status Messages (Bidirectional):
- In addition to audio streams, ESP32 and
xiaozhi-serveralso exchange text messages through WebSocket, which are usually encapsulated in JSON format. - ESP32 -> Server: Devices may send status reports (such as network status, microphone status), error codes, or specific control commands (such as "stop TTS playback" triggered by user button presses).
- Server -> ESP32: The server may send control instructions to devices (such as "start listening", "stop listening", adjust sensitivity, send specific configuration parameters).
- Modules like
core/handle/abortHandle.py(handling interrupt requests) andcore/handle/reportHandle.py(handling device reports) are responsible for parsing and responding to these control/status messages.
- In addition to audio streams, ESP32 and
4.2. Management and Configuration Flow (manager-web <-> manager-api <-> xiaozhi-server)
This flow mainly relies on HTTP/HTTPS-based RESTful API for request-response interactions.
-
Administrator UI Backend Interaction (
manager-web->manager-api):- When administrators perform operations in the
manager-webinterface (for example, saving a configuration, adding a new user, registering an ESP32 device):- The Vue.js frontend application (
manager-web) initiates asynchronous HTTP requests (usually GET, POST, PUT, DELETE) to corresponding REST API endpoints ofmanager-apithrough its API encapsulation modules (located insrc/apis/module/). - Request and response bodies usually use JSON format.
@RestControllerclasses inmanager-apireceive these requests. The Apache Shiro framework first performs authentication and authorization checks on the requests.- After verification, the Controller distributes requests to corresponding Service layers to handle business logic. Service layers may interact with MySQL database (through MyBatis-Plus) and may utilize Redis for data caching.
- After processing is complete,
manager-apireturns a JSON-formatted HTTP response tomanager-web. manager-webupdates its Vuex state storage and user interface display based on response results.
- The Vue.js frontend application (
- When administrators perform operations in the
-
Configuration Synchronization (
manager-api->xiaozhi-server):- The operation of
xiaozhi-serverdepends on dynamic configuration obtained frommanager-api(such as currently selected AI service providers and their API keys). - Pull Mechanism: The
config/manage_api_client.pymodule withinxiaozhi-server, during server startup or through specific update triggers (such as whenWebSocketServer.update_config()is called), initiates HTTP GET requests to a specified endpoint ofmanager-api(for example, provided by some Controller inmodules/config/controller/). manager-apiresponds to this request, returning configuration data required byxiaozhi-server(in JSON format).- After
xiaozhi-serverreceives the configuration, it updates its internal state and may reinitialize related AI service modules to make the new configuration effective.
- The operation of
-
OTA Firmware Update Flow (Conceptual Description):
- Administrators upload new ESP32 firmware packages to specific endpoints of
manager-apithrough themanager-webinterface. manager-apistores the firmware files and records related metadata (version number, applicable device models, etc.).- When administrators trigger OTA updates for specific devices:
manager-apimay notifyxiaozhi-server(specific notification mechanisms may be polling checkpoints, orxiaozhi-serverexposing an API to receive update notifications, or more loosely coupled mechanisms like message queues).xiaozhi-servercan then send an instruction message containing the firmware download URL to the target ESP32 device through WebSocket.- After ESP32 devices receive the instruction, they download firmware through HTTP GET requests from that URL. This URL may point to the path served by
xiaozhi-server's ownSimpleHttpServer(such as/xiaozhi/ota/), or in some architectures, it may directly point tomanager-apior dedicated file servers.
- Administrators upload new ESP32 firmware packages to specific endpoints of
4.3. Main Protocol Summary:
- WebSocket: Selected for communication links between ESP32 and
xiaozhi-serverbecause it is very suitable for real-time, low-latency, bidirectional data stream transmission (especially audio) and asynchronous control message delivery. - RESTful APIs (based on HTTP/HTTPS, usually using JSON as data exchange format): This is the standard way for web service communication. Used for request-response interactions between
manager-web(client) andmanager-api(server), and also forxiaozhi-server(as client) to pull configuration information frommanager-api(as server). Its stateless nature, extensive library support, and easy-to-understand semantics make it an ideal choice for such interactions.
This multi-protocol communication strategy ensures that different types of interaction requirements within the system can be handled efficiently and appropriately, balancing real-time performance and standardized request-response patterns.
5. Core Functionality Overview
The xiaozhi-esp32-server system provides a rich set of features designed to support developers in building advanced voice control applications:
- Comprehensive Voice Interaction Backend: Provides end-to-end solutions from voice capture guidance to response generation and action execution.
- Modular and Pluggable AI Services:
- Supports a wide range of ASR (Automatic Speech Recognition), LLM (Large Language Model), TTS (Text-to-Speech), VAD (Voice Activity Detection), intent recognition, and memory providers.
- Allows dynamic selection and configuration of these services (including cloud-based APIs and local models) to balance cost, performance, privacy, and language requirements.
- Advanced Conversation Management:
- Supports natural interaction with wake word-initiated conversations, manual (push-to-talk) conversations, and real-time interruption of system responses.
- Includes contextual memory to maintain coherence in multi-turn conversations.
- Has automatic sleep mode after a period of inactivity.
- Multi-language Capabilities:
- Supports recognition and synthesis of multiple languages, including English, Spanish, French, German, Japanese, and Korean (depending on the selected ASR/LLM/TTS provider).
- Extensible Functionality through Plugins:
- Powerful plugin system allows developers to add custom "skills" or functions (for example, getting weather, controlling smart home devices, accessing news).
- These functions can be triggered by LLM using its function calling capabilities based on provided schemas.
- Built-in support for Home Assistant integration.
- IoT Device Control:
- Designed to manage and control smart home devices and other IoT hardware through voice commands, utilizing the plugin system.
- Web-based Management Console (
manager-web&manager-api):- Provides comprehensive graphical interface for:
- System configuration (AI service selection, API keys, operation parameters).
- Role-based access control user management.
- ESP32 device registration and management.
- Voice timbre/TTS voice customization.
- ESP32 device OTA (Over-The-Air) firmware update management.
- System parameter and dictionary management.
- Provides comprehensive graphical interface for:
- Flexible Deployment Options:
- Supports deployment through Docker containers (for simplified server-only or full-stack setups) and direct deployment from source code to adapt to various environments and user expertise levels.
- Dynamic Remote Configuration:
xiaozhi-servercan obtain its configuration frommanager-api, allowing real-time updates of AI providers and settings without server restart.
- Open Source and Community-Driven:
- Licensed under MIT license, encouraging transparency, collaboration, and community contributions.
- Cost-Effective Solutions:
- Provides "entry-level free setup" path, utilizing free tiers of AI services or local models, making it easy for experimentation and personal projects.
- Progressive Web App (PWA) Features:
manager-webcontrol panel includes Service Worker integration for enhanced caching and potential offline access capabilities.
- Detailed API Documentation:
manager-apiprovides OpenAPI (Swagger) documentation through Knife4j for clear understanding and testing of its RESTful endpoints.
These features together make xiaozhi-esp32-server a powerful, adaptable, and user-friendly platform for building complex voice interaction applications.
6. Deployment and Configuration Overview
The xiaozhi-esp32-server system is designed with full consideration of flexibility, providing multiple deployment methods and comprehensive configuration options to adapt to different usage scenarios and requirements.
Deployment Options:
The project can be deployed in multiple ways, mainly including using Docker to simplify the installation process, or deploying directly from source code for greater control and development.
-
Docker-based Deployment:
- Simplified Installation (only
xiaozhi-server): This option only deploys the core Python-basedxiaozhi-server. It is suitable for users who mainly need voice AI processing capabilities and IoT control, without requiring complete web management interface and database support functions (such as OTA). In this mode, configuration is usually managed through local files (config.yaml), but if needed, it can still point to an existingmanager-apiinstance. - Full Module Installation (all components): This solution deploys all core components:
xiaozhi-server, Java-basedmanager-api, and Vue.js-basedmanager-web, along with required database services (MySQL and Redis). This provides a complete system experience, including web control panels for comprehensive configuration and management. - The project provides
Dockerfiledefinitions for each service and usesdocker-compose.ymlfiles (for example,docker-compose.ymlfor basic version,docker-compose_all.ymlfor full-featured version) to orchestrate and manage multi-container deployments. Additionally, adocker-setup.shscript may be provided to assist in automating part of the Docker environment setup work.
- Simplified Installation (only
-
Source Code Deployment:
- This method requires manually setting up corresponding development environments for each component: Python environment for
xiaozhi-server, Java/Maven environment formanager-api, Node.js/Vue CLI environment formanager-web. - For full module installation, MySQL and Redis database services also need to be manually installed and configured.
- This approach is typically used for project development, deep customization, debugging, or in production scenarios with special environmental requirements.
- This method requires manually setting up corresponding development environments for each component: Python environment for
Configuration Management:
Configuration is key to customizing system behavior, especially in selecting AI service providers and managing API keys.
-
xiaozhi-serverConfiguration:- Local
config.yaml: A main YAML format configuration file located in the root directory ofxiaozhi-server. It defines server ports, selected AI service providers (ASR, LLM, TTS, VAD, intent recognition, memory modules, etc.), their respective API keys or model paths, plugin configurations, and log levels. - Remote Configuration through
manager-api:xiaozhi-serveris designed to obtain its runtime configuration frommanager-api. Settings obtained frommanager-apiusually override settings with the same name in localconfig.yaml. This brings two major benefits:- Centralized Management: All configurations can be managed uniformly through the
manager-webinterface. - Dynamic Updates:
xiaozhi-servercan refresh its configuration and reinitialize AI modules without completely restarting the service.
- Centralized Management: All configurations can be managed uniformly through the
config/config_loader.pyandconfig/manage_api_client.pyinxiaozhi-serverare responsible for handling configuration loading, merging, and pulling logic frommanager-api.
- Local
-
manager-apiConfiguration:- As a Spring Boot application, its configuration is mainly managed through
application.propertiesorapplication.ymlfiles located in thesrc/main/resourcesdirectory. - Key configuration items include: database connection information (MySQL URL, username, password), Redis server address and port, application service port (default 8002), Apache Shiro security-related settings, and configuration parameters for any integrated third-party services (such as Twilio SMS).
- As a Spring Boot application, its configuration is mainly managed through
-
manager-webConfiguration:- Environment-specific settings for the Vue.js frontend application are managed through
.envseries files (such as.env,.env.development,.env.production) in the project root directory. - The most critical configuration here is usually the API base URL address of the
manager-apibackend (for example,VUE_APP_API_BASE_URL), to which the frontend application will send all API requests.
- Environment-specific settings for the Vue.js frontend application are managed through
-
Predefined Configuration Schemes:
- Project documentation (usually README) will recommend some common configuration combinations, for example:
- "Entry-level Free Setup": This scheme aims to utilize free tier quotas of cloud AI services or completely free local models to minimize users' initial usage costs and operational expenses.
- "Full Streaming Configuration": This scheme prioritizes system response speed and interaction smoothness, usually selecting AI services that support streaming processing (possibly paid).
- These predefined schemes provide guidance for users to configure AI service providers in
xiaozhi-server(throughmanager-webinterface or directly modifyingconfig.yaml).
- Project documentation (usually README) will recommend some common configuration combinations, for example:
In the case of full module deployment, it is recommended to use the manager-web control panel as the main operation interface for most configuration tasks, as it provides a user-friendly way to manage various settings that are persisted by manager-api and ultimately used by xiaozhi-server.