active-call 0.3.78

A SIP/WebRTC voice agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# RustPBX: 分体式VoiceAgent架构

## 系统架构概览

### UA模式核心概念

RustPBX的UserAgent(UA)模式是一种创新的AI语音代理架构,它将媒体处理与业务逻辑完全分离,通过WebSocket协议实现command/event交互模式。

分体式架构使得开发者可以专注于AI逻辑开发,而无需深入了解音频处理的复杂细节。

```mermaid
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
```

### 架构特点

1. **分离关注点**:AI逻辑与音频处理完全分离
2. **灵活扩展**:支持多种ASR/TTS服务商
3. **标准协议**:基于SIP/WebRTC标准
4. **实时交互**:WebSocket双向通信保证低延迟

<div style="page-break-after: always;"></div>

## UA模式工作流程

### 呼叫建立与媒体协商

UA模式支持两种工作方式:注册到SIP代理服务器接收呼叫,或者主动发起呼叫。媒体通道可以是SIP/RTP或者WebRTC,提供了极大的灵活性。

```mermaid
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管理着整个呼叫的生命周期,包括媒体流、事件处理和命令分发。

<div style="page-break-after: always;"></div>

## Command/Event交互机制

### WebSocket协议实现分体式架构

与Pipecat等单体架构不同,RustPBX采用分体式架构,AI Agent通过WebSocket发送命令控制UA行为,UA通过事件反馈状态和结果。

```mermaid
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类型

```rust
// 从代码中提取的核心命令类型
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**: 静音检测

<div style="page-break-after: always;"></div>

## 插件化ASR/TTS架构

### 多服务商支持

RustPBX通过插件化架构支持多种ASR/TTS服务商,包括阿里云、腾讯云等,开发者可以根据需求选择最适合的服务商或者自定义实现。

```mermaid
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
- **热插拔**:支持运行时切换服务商
- **配置驱动**:通过配置文件轻松切换
- **错误处理**:内置重试和降级机制

<div style="page-break-after: always;"></div>

## 内置音频处理特性

### 高级音频处理能力

RustPBX内置了多种音频处理特性,包括VAD(语音活动检测)、降噪、背景噪音增强等,这些特性大大提升了语音交互的质量。

```mermaid
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和播放功能上实现了多种实用特性:

```rust
// 流式TTS支持
TtsCommand {
    streaming: Some(true),     // 启用流式输出
    end_of_stream: Some(false), // 标记流结束
    auto_hangup: Some(true),   // 播放完自动挂机
    wait_input_timeout: Some(30), // 等待输入超时
}
```

### 特性详解

1. **流式TTS**:支持边生成边播放,降低延迟
2. **自动挂机**:播放完成后自动结束通话
3. **等待输入**:播放后等待用户语音或DTMF输入
4. **打断支持**:用户可以随时打断播放
5. **背景音乐**:支持在等待期间播放背景音乐

<div style="page-break-after: always;"></div>

## RustPBX UA模式 vs Pipecat框架对比

### 架构模式差异

RustPBX UA模式与Pipecat框架代表了两种不同的AI语音代理架构理念。Pipecat采用单体架构,所有组件运行在同一进程中;而RustPBX UA模式采用分体式架构,AI逻辑与媒体处理完全分离。

```mermaid
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
```

### 开发模式对比

```mermaid
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模式适合:**
- 企业级应用和生产环境
- 需要高可扩展性的系统
- 多团队协作开发
- 复杂的语音交互场景
- 对性能和稳定性要求较高的场景

### 性能与可维护性

```mermaid
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
```

<div style="page-break-after: always;"></div>

## Voice Agent开发优势

### 开发便利性分析

RustPBX的UA模式为Voice Agent开发提供了极大的便利性,开发者可以专注于AI逻辑而不需要处理复杂的音频和通信协议。

```mermaid
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推理

### 部署架构灵活性

```mermaid
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
```

### 开发优势总结

1. **低学习成本**:无需了解音频处理和SIP协议
2. **高开发效率**:专注业务逻辑,快速迭代
3. **易于测试**:WebSocket接口便于单元测试
4. **可扩展性**:支持分布式部署,支持WebRTC/Sip/Websocket等三种音频交互方式
5. **技术栈自由**:不限制AI开发技术选择
6. **生产就绪**:内置监控、日志、错误处理

<div style="page-break-after: always;"></div>

## 总结

RustPBX的UA模式通过分体式架构、WebSocket交互机制、插件化设计和内置音频处理特性,为AI Voice Agent的开发提供了一个强大而灵活的平台。开发者可以专注于AI逻辑的实现,而将复杂的音频处理和通信协议交给RustPBX处理,大大降低了开发门槛,提高了开发效率。