RustPBX: 分体式VoiceAgent架构
March 1, 2026 · View on GitHub
系统架构概览
UA模式核心概念
RustPBX的UserAgent(UA)模式是一种创新的AI语音代理架构,它将媒体处理与业务逻辑完全分离,通过WebSocket协议实现command/event交互模式。
分体式架构使得开发者可以专注于AI逻辑开发,而无需深入了解音频处理的复杂细节。
graph TD
A[AI Agent] -->|WebSocket Commands| B[RustPBX UA]
B -->|WebSocket Events| A
B --> C[SIP Proxy/Registry]
B --> D[WebRTC Peer]
B --> E[音频处理引擎]
E --> F[ASR插件]
E --> G[TTS插件]
E --> H[VAD模块]
E --> I[降噪模块]
subgraph "分体式架构"
A
B
end
subgraph "媒体处理层"
E
F
G
H
I
end
subgraph "通信层"
C
D
end
style A fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style C fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style D fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style E fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style F fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style G fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style H fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style I fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
架构特点
- 分离关注点:AI逻辑与音频处理完全分离
- 灵活扩展:支持多种ASR/TTS服务商
- 标准协议:基于SIP/WebRTC标准
- 实时交互:WebSocket双向通信保证低延迟
UA模式工作流程
呼叫建立与媒体协商
UA模式支持两种工作方式:注册到SIP代理服务器接收呼叫,或者主动发起呼叫。媒体通道可以是SIP/RTP或者WebRTC,提供了极大的灵活性。
sequenceDiagram
participant AI as AI Agent
participant UA as RustPBX UA
participant SIP as SIP Proxy
participant Peer as 对方设备
Note over AI,Peer: 外呼场景
AI->>UA: Invite Command (SIP/WebRTC)
UA->>SIP: INVITE Request
SIP->>Peer: INVITE Forward
Peer->>SIP: 200 OK (SDP Answer)
SIP->>UA: 200 OK Forward
UA->>AI: Answer Event (SDP)
UA->>AI: TrackStart Event
AI->>UA: TTS Command
UA->>Peer: Audio Stream (TTS)
Peer->>UA: Audio Stream (用户语音)
UA->>AI: ASR Events (语音识别结果)
AI->>UA: Response Commands
关键组件交互
UA管理着整个呼叫的生命周期,包括媒体流、事件处理和命令分发。
Command/Event交互机制
WebSocket协议实现分体式架构
与Pipecat等单体架构不同,RustPBX采用分体式架构,AI Agent通过WebSocket发送命令控制UA行为,UA通过事件反馈状态和结果。
graph LR
subgraph "AI Agent进程"
A[业务逻辑]
B[LLM集成]
C[决策引擎]
end
subgraph "RustPBX UA进程"
D[命令处理器]
E[事件生成器]
F[媒体引擎]
end
A -->|TTS Command| D
A -->|Play Command| D
A -->|Hangup Command| D
E -->|ASR Events| A
E -->|DTMF Events| A
E -->|TrackStart/End Events| A
D --> F
F --> E
style A fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style C fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style D fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style E fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style F fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
主要Commands类型
// 从代码中提取的核心命令类型
enum Command {
Tts { text, speaker, auto_hangup, streaming, ... },
Play { url, auto_hangup, wait_input_timeout },
Hangup { reason, initiator },
Refer { caller, callee, options },
Mute/Unmute { track_id },
Pause/Resume,
Interrupt,
}
主要Events类型
- ASR Events: 实时语音识别结果
- DTMF Events: 按键检测
- Track Events: 媒体轨道状态变化
- Silence Events: 静音检测
插件化ASR/TTS架构
多服务商支持
RustPBX通过插件化架构支持多种ASR/TTS服务商,包括阿里云、腾讯云等,开发者可以根据需求选择最适合的服务商或者自定义实现。
graph TD
A[MediaStream] --> B[ASR Processor]
A --> C[TTS Engine]
B --> D[阿里云ASR]
B --> E[腾讯云ASR]
B --> F[自定义ASR]
C --> G[阿里云TTS]
C --> H[腾讯云TTS]
C --> I[自定义TTS]
subgraph "ASR插件"
D
E
F
end
subgraph "TTS插件"
G
H
I
end
J[配置文件] --> B
J --> C
style A fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style C fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style D fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style E fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style F fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style G fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style H fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style I fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style J fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
扩展便利性
- 统一接口:所有ASR/TTS插件实现相同的trait
- 热插拔:支持运行时切换服务商
- 配置驱动:通过配置文件轻松切换
- 错误处理:内置重试和降级机制
内置音频处理特性
高级音频处理能力
RustPBX内置了多种音频处理特性,包括VAD(语音活动检测)、降噪、背景噪音增强等,这些特性大大提升了语音交互的质量。
graph TB
A[原始音频流] --> B[VAD检测]
B --> C{是否有语音?}
C -->|是| D[降噪处理]
C -->|否| E[静音处理]
D --> F[音频增强]
F --> G[ASR处理]
E --> H[背景噪音检测]
H --> I[静音事件]
subgraph "音频处理管道"
J[Jitter Buffer]
K[回声消除]
L[自动增益控制]
end
G --> M[识别结果]
I --> N[Silence Event]
style A fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style C fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style D fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style E fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style F fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style G fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style H fill:#e94560,stroke:#ff6b6b,stroke-width:2px,color:#fff
style I fill:#ff6b6b,stroke:#4ecdc4,stroke-width:2px,color:#fff
style J fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style K fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style L fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style M fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
style N fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
TTS/Play高级特性
RustPBX在TTS和播放功能上实现了多种实用特性:
// 流式TTS支持
TtsCommand {
streaming: Some(true), // 启用流式输出
end_of_stream: Some(false), // 标记流结束
auto_hangup: Some(true), // 播放完自动挂机
wait_input_timeout: Some(30), // 等待输入超时
}
特性详解
- 流式TTS:支持边生成边播放,降低延迟
- 自动挂机:播放完成后自动结束通话
- 等待输入:播放后等待用户语音或DTMF输入
- 打断支持:用户可以随时打断播放
- 背景音乐:支持在等待期间播放背景音乐
RustPBX UA模式 vs Pipecat框架对比
架构模式差异
RustPBX UA模式与Pipecat框架代表了两种不同的AI语音代理架构理念。Pipecat采用单体架构,所有组件运行在同一进程中;而RustPBX UA模式采用分体式架构,AI逻辑与媒体处理完全分离。
graph TB
subgraph "Pipecat单体架构"
P1[AI逻辑]
P2[音频处理]
P3[协议栈]
P4[媒体编解码]
P5[ASR/TTS]
P1 --> P2
P2 --> P3
P3 --> P4
P4 --> P5
end
subgraph "RustPBX分体式架构"
R1[AI Agent进程]
R2[WebSocket接口]
R3[RustPBX UA进程]
R4[内置音频处理]
R5[内置协议栈]
R1 --> R2
R2 --> R3
R3 --> R4
R3 --> R5
end
style P1 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style P2 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style P3 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style P4 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style P5 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style R1 fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style R2 fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style R3 fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style R4 fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style R5 fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
开发模式对比
graph LR
subgraph "Pipecat开发模式"
A1[Python单进程]
A2[集成所有组件]
A3[同步处理流程]
A4[框架绑定强]
A1 --> A2
A2 --> A3
A3 --> A4
end
subgraph "RustPBX开发模式"
B1[多语言支持]
B2[进程分离]
B3[异步事件驱动]
B4[技术栈自由]
B1 --> B2
B2 --> B3
B3 --> B4
end
style A1 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A2 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A3 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A4 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style B1 fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B2 fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style B3 fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style B4 fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
关键差异对比
| 维度 | Pipecat | RustPBX UA模式 |
|---|---|---|
| 架构模式 | 单体架构,所有组件集成 | 分体式架构,AI与媒体分离 |
| 开发语言 | 仅支持Python | 支持任意语言(Python/JS/Go/Java等) |
| 部署方式 | 单进程部署 | 分布式部署,可独立扩展 |
| 学习成本 | 需要了解音频处理细节 | 只需关注AI逻辑 |
| 调试难度 | AI和媒体问题耦合 | 问题域隔离,易于定位 |
| 扩展性 | 框架内扩展 | 插件化扩展,热插拔 |
| 技术债务 | 框架升级影响全栈 | AI与媒体独立迭代 |
| 团队协作 | 需要全栈开发者 | 可分工协作开发 |
适用场景分析
Pipecat适合:
- 快速原型验证
- 小型项目或个人项目
- 对Python生态依赖较强的场景
- 简单的语音交互需求
RustPBX UA模式适合:
- 企业级应用和生产环境
- 需要高可扩展性的系统
- 多团队协作开发
- 复杂的语音交互场景
- 对性能和稳定性要求较高的场景
性能与可维护性
graph TB
subgraph "性能对比"
PA[Pipecat性能]
PB[单进程限制]
PC[GIL约束]
RA[RustPBX性能]
RB[多进程并行]
RC[Rust高性能]
PA --> PB
PB --> PC
RA --> RB
RB --> RC
end
subgraph "可维护性对比"
PM[Pipecat维护]
PN[耦合度高]
PO[调试复杂]
RM[RustPBX维护]
RN[模块解耦]
RO[问题隔离]
PM --> PN
PN --> PO
RM --> RN
RN --> RO
end
style PA fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style PB fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style PC fill:#ee5a52,stroke:#c44569,stroke-width:2px,color:#fff
style PM fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style PN fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style PO fill:#ee5a52,stroke:#c44569,stroke-width:2px,color:#fff
style RA fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style RB fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style RC fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
style RM fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style RN fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style RO fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
Voice Agent开发优势
开发便利性分析
RustPBX的UA模式为Voice Agent开发提供了极大的便利性,开发者可以专注于AI逻辑而不需要处理复杂的音频和通信协议。
graph TD
subgraph "传统开发模式"
A1[AI逻辑]
A2[音频处理]
A3[协议处理]
A4[媒体编解码]
A1 -.-> A2
A2 -.-> A3
A3 -.-> A4
end
subgraph "RustPBX UA模式"
B1[AI逻辑]
B2[WebSocket接口]
B3[RustPBX UA]
B4[内置音频处理]
B5[内置协议栈]
B1 --> B2
B2 --> B3
B3 --> B4
B3 --> B5
end
C[开发者只需关注] --> B1
style A1 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A2 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A3 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style A4 fill:#ff6b6b,stroke:#ee5a52,stroke-width:2px,color:#fff
style B1 fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B2 fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style B3 fill:#0f3460,stroke:#533483,stroke-width:2px,color:#eee
style B4 fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style B5 fill:#533483,stroke:#e94560,stroke-width:2px,color:#eee
style C fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
技术栈选择自由
开发者可以使用任何编程语言和AI框架:
- Python: LangChain, OpenAI SDK
- JavaScript: Node.js, OpenAI API
- Java: Spring Boot, AI框架
- Go: 自定义AI逻辑
- Rust: 本地AI推理
部署架构灵活性
graph TB
subgraph "AI Agent服务器"
A[业务逻辑]
B[LLM服务]
C[知识库]
end
subgraph "RustPBX服务器"
D[UA实例]
E[媒体处理]
F[协议栈]
end
subgraph "外部服务"
G[ASR/TTS服务]
H[SIP运营商]
end
A -->|WebSocket| D
D -->|API| G
D -->|SIP| H
style A fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style B fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style C fill:#1a1a2e,stroke:#16213e,stroke-width:2px,color:#00d4aa
style D fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style E fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style F fill:#16213e,stroke:#0f3460,stroke-width:2px,color:#00d4aa
style G fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
style H fill:#4ecdc4,stroke:#45b7b8,stroke-width:2px,color:#fff
开发优势总结
- 低学习成本:无需了解音频处理和SIP协议
- 高开发效率:专注业务逻辑,快速迭代
- 易于测试:WebSocket接口便于单元测试
- 可扩展性:支持分布式部署,支持WebRTC/Sip/Websocket等三种音频交互方式
- 技术栈自由:不限制AI开发技术选择
- 生产就绪:内置监控、日志、错误处理
总结
RustPBX的UA模式通过分体式架构、WebSocket交互机制、插件化设计和内置音频处理特性,为AI Voice Agent的开发提供了一个强大而灵活的平台。开发者可以专注于AI逻辑的实现,而将复杂的音频处理和通信协议交给RustPBX处理,大大降低了开发门槛,提高了开发效率。