Skip to content

Conversation

Copy link

Copilot AI commented Dec 12, 2025

Implements XiaoZhi WebSocket protocol server as a BotSharp plugin to enable realtime voice conversations with xiaozhi-esp32 and compatible clients.

Implementation

Plugin Architecture

  • XiaoZhiPlugin implements IBotSharpAppPlugin for automatic middleware registration
  • XiaoZhiStreamMiddleware handles WebSocket connections at /xiaozhi/stream/{agentId}/{conversationId}
  • Protocol models for client/server handshake and binary audio packets (v1/v2/v3)

Protocol Handling

  • Client hello negotiation with version detection (1-3)
  • Binary audio streaming: V1 (raw OPUS), V2 (16-byte header), V3 (4-byte header)
  • JSON control messages (wake_word_detected, start_listening, stop_listening, abort_speaking)
  • Direct WebSocket message handling—bypasses BotSharpRealtimeSession which only supports text

Integration

  • Connects to IRealtimeHub for LLM realtime API conversations
  • Base64 audio encoding for completer compatibility
  • Conversation state persistence and routing

Configuration

{
  "XiaoZhi": {
    "EndpointPath": "/xiaozhi/stream",
    "DefaultProtocolVersion": 3,
    "AudioFormat": "opus",
    "SampleRate": 24000,
    "Channels": 1,
    "FrameDuration": 60
  }
}

Protocol Example

Client connects and sends hello:

{
  "type": "hello",
  "version": 3,
  "transport": "websocket",
  "audio_params": {
    "format": "opus",
    "sample_rate": 16000,
    "channels": 1,
    "frame_duration": 20
  }
}

Server responds with session:

{
  "type": "hello",
  "transport": "websocket",
  "session_id": "uuid",
  "audio_params": {
    "format": "opus",
    "sample_rate": 24000,
    "channels": 1,
    "frame_duration": 60
  }
}

Binary audio then flows bidirectionally using protocol-specific packet headers.

Compatibility

Works with xiaozhi-esp32, Verdure.Assistant, and py-xiaozhi clients. Reference implementation: xiaozhi-esp32-server.

Original prompt

This section details on the original issue you should resolve

<issue_title>使用botsharp实现一个小智服务端</issue_title>
<issue_description>请你分析下面的.net的小智客户端
https://github.com/maker-community/Verdure.Assistant
和和官方的esp32版本的小智客户端
https://github.com/78/xiaozhi-esp32
以及第三方的python小智客户端
https://github.com/huangjunsen0406/py-xiaozhi
主要分析小智的通讯协议的逻辑,以websocket为准

再结合第三方实现的python服务端的实现
https://github.com/xinnan-tech/xiaozhi-esp32-server

结合botsharp的realtime的功能的实现
https://github.com/maker-community/BotSharp
在botsharp中实现一个插件,用来使用大模型实时api进行小智协议的适配,做到可以使用botsharp作为小智服务端的提供者。

注意事项:

  1. 只新增插件单独实现小智服务端的功能
  2. 功能尽量和botsharp的功能进行兼容
  3. 以核心的对话作为实现目标,做到更换小智客户端ota地址就能够进行通话</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits December 12, 2025 06:54
Co-authored-by: GreenShadeZhang <24240675+GreenShadeZhang@users.noreply.github.com>
Co-authored-by: GreenShadeZhang <24240675+GreenShadeZhang@users.noreply.github.com>
Co-authored-by: GreenShadeZhang <24240675+GreenShadeZhang@users.noreply.github.com>
Co-authored-by: GreenShadeZhang <24240675+GreenShadeZhang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Xiaozhi server plugin to BotSharp Add XiaoZhi WebSocket server plugin for realtime voice conversations Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用botsharp实现一个小智服务端

2 participants