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
546
547
548
549
550
551
552
# RustPBX: A Decoupled Architecture for AI Voice Agent Development

## System Architecture Overview

### Core Concepts of UA Mode

RustPBX's UserAgent (UA) mode introduces a decoupled architecture for AI voice agents, separating media processing from business logic through a WebSocket-based command/event interaction pattern.

This architectural approach allows developers to focus on AI logic implementation without requiring deep knowledge of audio processing complexities.

```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[Audio Processing Engine]
    E --> F[ASR Plugins]
    E --> G[TTS Plugins]
    E --> H[VAD Module]
    E --> I[Noise Reduction]
    
    subgraph "Decoupled Architecture"
        A
        B
    end
    
    subgraph "Media Processing Layer"
        E
        F
        G
        H
        I
    end
    
    subgraph "Communication Layer"
        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
```

### Key Architectural Features

1. **Separation of Concerns**: Complete isolation between AI logic and audio processing
2. **Flexible Extension**: Support for multiple ASR/TTS service providers
3. **Standard Protocols**: Built on SIP/WebRTC standards
4. **Real-time Interaction**: Low-latency bidirectional WebSocket communication


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

## UA Mode Workflow

### Call Establishment and Media Negotiation

UA mode supports two operational patterns: registering with a SIP proxy server to receive calls, or initiating outbound calls. Media channels can utilize either SIP/RTP or WebRTC, providing significant flexibility.

```mermaid
sequenceDiagram
    participant AI as AI Agent
    participant UA as RustPBX UA
    participant SIP as SIP Proxy
    participant Peer as Remote Device
    
    Note over AI,Peer: Outbound Call Scenario
    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 (User Voice)
    UA->>AI: ASR Events (Speech Recognition)
    AI->>UA: Response Commands
```

### Key Component Interactions

The UA manages the complete call lifecycle, including media streams, event processing, and command dispatch.


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

## Command/Event Interaction Mechanism

### WebSocket Protocol for Decoupled Architecture

RustPBX implements a decoupled architecture where AI Agents send commands via WebSocket to control UA behavior, while UA provides status feedback through events.

```mermaid
graph LR
    subgraph "AI Agent Process"
        A[Business Logic]
        B[LLM Integration]
        C[Decision Engine]
    end
    
    subgraph "RustPBX UA Process"
        D[Command Handler]
        E[Event Generator]
        F[Media Engine]
    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
```

### Core Command Types

```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,
}
```

### Primary Event Types

- **ASR Events**: Real-time speech recognition results
- **DTMF Events**: Touch-tone detection
- **Track Events**: Media track lifecycle changes
- **Silence Events**: Voice activity detection


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

## Plugin-based ASR/TTS Architecture

### Multi-Provider Support

RustPBX employs a plugin architecture supporting various ASR/TTS service providers, including cloud services like Alibaba Cloud and Tencent Cloud, enabling developers to choose the most suitable provider or implement custom solutions.

```mermaid
graph TD
    A[MediaStream] --> B[ASR Processor]
    A --> C[TTS Engine]
    
    B --> D[Alibaba Cloud ASR]
    B --> E[Tencent Cloud ASR]
    B --> F[Custom ASR]
    
    C --> G[Alibaba Cloud TTS]
    C --> H[Tencent Cloud TTS]
    C --> I[Custom TTS]
    
    subgraph "ASR Plugins"
        D
        E
        F
    end
    
    subgraph "TTS Plugins"
        G
        H
        I
    end
    
    J[Configuration] --> 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
```

### Extension Benefits

- **Unified Interface**: All ASR/TTS plugins implement consistent traits
- **Hot-swappable**: Runtime provider switching capability
- **Configuration-driven**: Easy switching through configuration files
- **Error Handling**: Built-in retry and fallback mechanisms


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

## Built-in Audio Processing Features

### Advanced Audio Processing Capabilities

RustPBX includes comprehensive audio processing features such as VAD (Voice Activity Detection), noise reduction, and background noise enhancement, significantly improving voice interaction quality.

```mermaid
graph TB
    A[Raw Audio Stream] --> B[VAD Detection]
    B --> C{Voice Detected?}
    C -->|Yes| D[Noise Reduction]
    C -->|No| E[Silence Processing]
    
    D --> F[Audio Enhancement]
    F --> G[ASR Processing]
    
    E --> H[Background Noise Detection]
    H --> I[Silence Events]
    
    subgraph "Audio Processing Pipeline"
        J[Jitter Buffer]
        K[Echo Cancellation]
        L[Automatic Gain Control]
    end
    
    G --> M[Recognition Results]
    I --> N[Silence Events]
    
    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
```

### Advanced TTS/Play Features

RustPBX implements various practical features for TTS and audio playback:

```rust
TtsCommand {
    streaming: Some(true),         // Enable streaming output
    end_of_stream: Some(false),    // Mark stream end
    auto_hangup: Some(true),       // Auto-hangup after playback
    wait_input_timeout: Some(30),  // Input timeout in seconds
}
```

### Feature Highlights

1. **Streaming TTS**: Real-time generation and playback for reduced latency
2. **Auto-hangup**: Automatic call termination after playback completion
3. **Input Waiting**: Post-playback waiting for voice or DTMF input
4. **Interruption Support**: User-initiated playback interruption
5. **Background Music**: Hold music during wait periods


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

## Architectural Comparison: RustPBX UA Mode vs Alternative Approaches

### Design Philosophy Differences

RustPBX UA mode represents a decoupled architectural approach for AI voice agents. While some frameworks utilize monolithic architectures with all components in a single process, RustPBX separates AI logic from media processing entirely.

```mermaid
graph TB
    subgraph "Monolithic Architecture"
        P1[AI Logic]
        P2[Audio Processing]
        P3[Protocol Stack]
        P4[Media Codecs]
        P5[ASR/TTS]
        
        P1 --> P2
        P2 --> P3
        P3 --> P4
        P4 --> P5
    end
    
    subgraph "RustPBX Decoupled Architecture"
        R1[AI Agent Process]
        R2[WebSocket Interface]
        R3[RustPBX UA Process]
        R4[Built-in Audio Processing]
        R5[Built-in Protocol Stack]
        
        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
```

### Development Approach Comparison

```mermaid
graph LR
    subgraph "Traditional Approach"
        A1[Single Language Stack]
        A2[Integrated Components]
        A3[Synchronous Processing]
        A4[Framework Coupling]
        
        A1 --> A2
        A2 --> A3
        A3 --> A4
    end
    
    subgraph "RustPBX Approach"
        B1[Multi-language Support]
        B2[Process Separation]
        B3[Event-driven Architecture]
        B4[Technology Freedom]
        
        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
```

### Key Architectural Differences

| Aspect                 | Monolithic Approach                    | RustPBX UA Mode                       |
| ---------------------- | -------------------------------------- | ------------------------------------- |
| **Architecture**       | Integrated components                  | Decoupled AI and media                |
| **Language Support**   | Framework-specific                     | Any language (Python/JS/Go/Java/etc.) |
| **Deployment**         | Single process                         | Distributed, independently scalable   |
| **Learning Curve**     | Requires audio processing knowledge    | Focus on AI logic only                |
| **Debugging**          | Coupled AI and media issues            | Isolated problem domains              |
| **Extensibility**      | Framework-bound extensions             | Plugin-based, hot-swappable           |
| **Technical Debt**     | Framework upgrades affect entire stack | Independent AI and media evolution    |
| **Team Collaboration** | Requires full-stack developers         | Enables specialized team roles        |

### Use Case Considerations

**Monolithic architectures may suit:**
- Rapid prototyping and validation
- Small-scale or personal projects
- Strong framework ecosystem dependencies
- Simple voice interaction requirements

**RustPBX UA mode excels for:**
- Enterprise applications and production environments
- High-scalability system requirements
- Multi-team collaborative development
- Complex voice interaction scenarios
- Performance and stability-critical applications

### Performance and Maintainability

```mermaid
graph TB
    subgraph "Performance Characteristics"
        PA[Traditional Performance]
        PB[Single Process Limits]
        PC[Runtime Constraints]
        
        RA[RustPBX Performance]
        RB[Multi-process Parallelism]
        RC[High-performance Runtime]
        
        PA --> PB
        PB --> PC
        RA --> RB
        RB --> RC
    end
    
    subgraph "Maintainability Aspects"
        PM[Traditional Maintenance]
        PN[High Coupling]
        PO[Complex Debugging]
        
        RM[RustPBX Maintenance]
        RN[Modular Decoupling]
        RO[Problem Isolation]
        
        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 Development Benefits

### Development Productivity Analysis

RustPBX's UA mode provides significant productivity benefits for Voice Agent development, enabling developers to concentrate on AI logic without handling complex audio and communication protocols.

```mermaid
graph TD
    subgraph "Traditional Development Model"
        A1[AI Logic] 
        A2[Audio Processing]
        A3[Protocol Handling]
        A4[Media Codecs]
        A1 -.-> A2
        A2 -.-> A3
        A3 -.-> A4
    end
    
    subgraph "RustPBX UA Mode"
        B1[AI Logic]
        B2[WebSocket Interface]
        B3[RustPBX UA]
        B4[Built-in Audio Processing]
        B5[Built-in Protocol Stack]
        
        B1 --> B2
        B2 --> B3
        B3 --> B4
        B3 --> B5
    end
    
    C[Developer Focus Area] --> 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
```

### Technology Stack Freedom

Developers can utilize any programming language and AI framework:

- **Python**: LangChain, OpenAI SDK, FastAPI
- **JavaScript**: Node.js, OpenAI API, Express
- **Java**: Spring Boot, AI frameworks
- **Go**: Custom AI logic, high-performance services
- **Rust**: Local AI inference, system-level optimization

### Deployment Architecture Flexibility

```mermaid
graph TB
    subgraph "AI Agent Server"
        A[Business Logic]
        B[LLM Services]
        C[Knowledge Base]
    end
    
    subgraph "RustPBX Server"
        D[UA Instance]
        E[Media Processing]
        F[Protocol Stack]
    end
    
    subgraph "External Services"
        G[ASR/TTS Services]
        H[SIP Carriers]
    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
```

### Development Advantages Summary

1. **Reduced Learning Curve**: No audio processing or SIP protocol expertise required
2. **Accelerated Development**: Focus on business logic enables rapid iteration
3. **Enhanced Testability**: WebSocket interface facilitates unit testing
4. **Horizontal Scalability**: Distributed deployment with support for WebRTC/SIP/WebSocket
5. **Technology Agnostic**: No constraints on AI development technology choices
6. **Production Ready**: Built-in monitoring, logging, and error handling


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

## Conclusion

RustPBX's UA mode delivers a robust and flexible platform for AI Voice Agent development through its decoupled architecture, WebSocket interaction mechanism, plugin-based design, and comprehensive audio processing capabilities. This approach enables developers to focus on AI logic implementation while delegating complex audio processing and communication protocols to RustPBX, significantly reducing development barriers and enhancing productivity.

The architecture's emphasis on separation of concerns, technology freedom, and operational flexibility makes it particularly well-suited for enterprise applications and production environments where scalability, maintainability, and team collaboration are paramount considerations.