rustpbx 0.4.9

A SIP PBX implementation in Rust
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
use crate::call::app::ivr::common::SessionData;
use crate::call::app::ivr::config::{ActionNode, EntryAction, IvrDefinition, MenuNode};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tracing::{info, warn};

// ── Provider Trait ───────────────────────────────────────────────────────────

#[async_trait]
pub trait ActionProvider: Send + Sync {
    /// Provider identifier for debug tracing
    fn name(&self) -> &str {
        "unknown"
    }

    async fn next_action(&self, ctx: ProviderContext) -> anyhow::Result<ActionNode>;

    async fn on_session_start(&self, ctx: &SessionContext) -> anyhow::Result<()> {
        let _ = ctx;
        Ok(())
    }

    async fn on_session_end(&self, reason: &EndReason, _session_id: &str) -> anyhow::Result<()> {
        let _ = reason;
        Ok(())
    }

    /// Called when a DtmfMenu resolves a DTMF key locally (no round‑trip to
    /// the provider).  Fire‑and‑forget notification so the provider stays
    /// informed about which keys were pressed and what action was taken.
    async fn on_local_dtmf_match(&self, _digit: &str, _action: &ActionNode) {}
}

// ── Context ──────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionContext {
    pub session_id: String,
    pub caller: String,
    pub callee: String,
    pub direction: String,
    pub tenant_id: Option<String>,
    pub ivr_id: Option<String>,
    /// All SIP headers from the original INVITE request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sip_headers: Option<HashMap<String, String>>,
    /// Name of the matched route that sent this call into the IVR.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub route_name: Option<String>,
    /// Extra headers configured on the route (from routing rule `option.headers`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub route_headers: Option<HashMap<String, String>>,
    /// Arbitrary passthrough data set by the caller / external system.
    /// The provider receives this and can use it for correlation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_data: Option<serde_json::Value>,
    /// Whether this session was re-entered from agent/queue (transfer-back).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transferred_from: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderContext {
    pub session_id: String,
    pub caller: String,
    pub callee: String,
    pub direction: String,
    pub tenant_id: Option<String>,
    pub ivr_id: Option<String>,
    pub variables: HashMap<String, String>,
    /// All SIP headers from the original INVITE request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sip_headers: Option<HashMap<String, String>>,
    pub event: Option<ProviderEvent>,
    /// Name of the matched route that sent this call into the IVR.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub route_name: Option<String>,
    /// Extra headers configured on the route.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub route_headers: Option<HashMap<String, String>>,
    /// Passthrough data — the provider can set `custom_data` in its response
    /// and it will be echoed back in every subsequent ProviderContext.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_data: Option<serde_json::Value>,
    /// Step timing: ISO-8601 timestamp when this step started.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_start_time: Option<String>,
    /// Step timing: ISO-8601 timestamp when this step ended (set before sending).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_end_time: Option<String>,
    /// Step timing: wall-clock duration of the previous step in milliseconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_duration_ms: Option<u64>,
    /// Monotonic step index (0 for SessionStart, incremented thereafter).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_index: Option<u32>,
    /// Whether this session was re-entered from agent/queue.
    /// Values: `"agent"`, `"queue"`, or `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transferred_from: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ProviderEvent {
    SessionStart,
    AudioComplete {
        interrupted: bool,
    },
    Dtmf {
        digit: String,
    },
    DtmfTimeout,
    ApiResponse {
        status: u16,
        body: serde_json::Value,
    },
    PhoneCollected {
        number: String,
    },
    RecordingComplete {
        url: String,
        duration_secs: u64,
    },
    InputVoice {
        text: String,
        confidence: f32,
    },
    Error {
        reason: String,
    },
    DtmfMenuInvalid {
        digit: String,
    },
    DtmfMenuTimeout,
}

#[derive(Debug, Clone)]
pub enum EndReason {
    /// IVR completed normally (played all nodes, no transfer).
    Normal,
    /// IVR exited because the call was transferred to an agent or extension.
    Transfer(String),
    /// IVR exited because the call was sent to a queue.
    TransferToQueue(String),
    /// IVR exited because the call jumped to another IVR.
    TransferToIvr(String),
    /// System (PBX) initiated the hangup.
    Hangup,
    /// User / remote party hung up.
    UserHangup,
    /// Error during IVR execution.
    Error(String),
}

impl From<&str> for EndReason {
    fn from(s: &str) -> Self {
        match s {
            "hangup" => EndReason::Hangup,
            "normal" => EndReason::Normal,
            other => EndReason::Error(other.to_string()),
        }
    }
}

// ── RetryConfig ────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct RetryConfig {
    pub max_retries: u32,
    pub timeout_ms: u64,
    pub fallback_action: Option<ActionNode>,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            timeout_ms: 1000,
            fallback_action: Some(ActionNode {
                action: EntryAction::Hangup {
                    prompt: Some("sounds/error.wav".into()),
                    prompt_text: None,
                    prompt_voice: None,
                },
                next: None,
                step_id: None,
                step_name: None,
                extra: None,
            }),
        }
    }
}

// ── TreeProvider ─────────────────────────────────────────────────────────────

#[allow(dead_code)]
pub struct TreeProvider {
    definition: IvrDefinition,
    sess: SessionData,
    menu_stack: Vec<String>,
    state: TreeState,
    retry_count: u32,
}

#[allow(dead_code)]
enum TreeState {
    Idle,
    WaitingDtmf { menu_key: String },
}

impl TreeProvider {
    pub fn new(definition: IvrDefinition) -> Self {
        Self {
            definition,
            sess: SessionData::default(),
            menu_stack: vec!["root".to_string()],
            state: TreeState::Idle,
            retry_count: 0,
        }
    }

    fn current_menu(&self) -> Option<&MenuNode> {
        let key = self.menu_stack.last()?;
        self.definition.get_menu(key)
    }

    fn build_dtmf_menu_action(&self) -> ActionNode {
        let menu = self.current_menu().cloned().unwrap_or_default();
        let mut entries = HashMap::new();
        for entry in &menu.entries {
            // Convert TOML EntryAction to ActionNode
            entries.insert(
                entry.key.clone(),
                ActionNode {
                    action: entry.action.clone(),
                    next: None,
                    step_id: None,
                    step_name: None,
                    extra: None,
                },
            );
        }
        let timeout_action = menu.timeout_action.clone().map(|a| {
            Box::new(ActionNode {
                action: a,
                next: None,
                step_id: None,
                step_name: None,
                extra: None,
            })
        });
        let invalid_action = if menu.invalid_prompt.is_some() || menu.invalid_text.is_some() {
            let prompt = menu
                .invalid_prompt
                .clone()
                .or_else(|| menu.invalid_text.clone());
            Some(Box::new(ActionNode::with_next(
                EntryAction::Prompt {
                    file: prompt,
                    tts_text: None,
                    tts_voice: menu.invalid_voice.clone(),
                    record_name_list: None,
                    interruptible: false,
                    tts_api_url: None,
                },
                ActionNode::new(EntryAction::Repeat),
            )))
        } else {
            None
        };
        ActionNode {
            action: EntryAction::DtmfMenu {
                greeting: Some(menu.greeting).filter(|g| !g.is_empty()),
                greeting_text: menu.greeting_text,
                greeting_record_list: None,
                greeting_voice: menu.greeting_voice,
                timeout_ms: menu.timeout_ms,
                max_retries: menu.max_retries,
                entries,
                timeout_action,
                invalid_action,
                greeting_api_url: None,
            },
            next: None,
            step_id: None,
            step_name: None,
            extra: None,
        }
    }
}

#[async_trait]
impl ActionProvider for TreeProvider {
    fn name(&self) -> &str {
        "tree"
    }

    async fn next_action(&self, ctx: ProviderContext) -> anyhow::Result<ActionNode> {
        match ctx.event.as_ref() {
            Some(ProviderEvent::SessionStart) => Ok(self.build_dtmf_menu_action()),
            Some(ProviderEvent::Dtmf { digit }) => {
                let menu = self.current_menu().cloned().unwrap_or_default();
                if let Some(entry) = menu.entries.iter().find(|e| e.key == digit.as_str()) {
                    return Ok(ActionNode {
                        action: entry.action.clone(),
                        next: None,
                        step_id: None,
                        step_name: None,
                        extra: None,
                    });
                }
                if let Some(ref unknown) = menu.unknown_key_action {
                    return Ok(ActionNode {
                        action: unknown.clone(),
                        next: None,
                        step_id: None,
                        step_name: None,
                        extra: None,
                    });
                }
                Ok(ActionNode::new(EntryAction::Repeat))
            }
            Some(ProviderEvent::DtmfMenuTimeout) | Some(ProviderEvent::DtmfTimeout) => {
                let menu = self.current_menu().cloned().unwrap_or_default();
                if let Some(ta) = menu.timeout_action {
                    Ok(ActionNode {
                        action: ta,
                        next: None,
                        step_id: None,
                        step_name: None,
                        extra: None,
                    })
                } else {
                    Ok(ActionNode::new(EntryAction::Repeat))
                }
            }
            _ => Ok(ActionNode::new(EntryAction::Repeat)),
        }
    }
}

// ── StepProvider (HTTP) ──────────────────────────────────────────────────────

pub struct StepProvider {
    url: String,
    headers: HashMap<String, String>,
    http_client: reqwest::Client,
    retry: RetryConfig,
}

impl StepProvider {
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            headers: HashMap::new(),
            http_client: reqwest::Client::new(),
            retry: RetryConfig::default(),
        }
    }

    pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
        self.headers = headers;
        self
    }

    pub fn with_retry(mut self, retry: RetryConfig) -> Self {
        self.retry = retry;
        self
    }

    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.http_client = client;
        self
    }

    /// Add a single HTTP header.
    pub fn add_header(&mut self, key: &str, value: &str) {
        self.headers.insert(key.to_string(), value.to_string());
    }
}

#[async_trait]
impl ActionProvider for StepProvider {
    fn name(&self) -> &str {
        "step"
    }

    async fn next_action(&self, ctx: ProviderContext) -> anyhow::Result<ActionNode> {
        let mut last_err = anyhow::anyhow!("no retry attempted");
        let body_str = serde_json::to_string(&ctx).unwrap_or_default();
        for attempt in 0..self.retry.max_retries {
            let start = std::time::Instant::now();
            info!(
                url = %self.url,
                method = "POST",
                headers = ?self.headers,
                body = %body_str,
                attempt = attempt,
                "StepProvider next_action request"
            );
            let req = self.http_client.post(&self.url).json(&ctx);
            match crate::http_util::execute_request(
                req,
                &self.headers,
                Some(Duration::from_millis(self.retry.timeout_ms)),
            )
            .await
            {
                Ok(resp) => {
                    let status = resp.status();
                    let elapsed = start.elapsed();
                    let body = resp.text().await.unwrap_or_default();
                    info!(
                        url = %self.url,
                        status = %status,
                        duration_ms = %elapsed.as_millis(),
                        response_body = %body,
                        "StepProvider next_action response"
                    );
                    return serde_json::from_str(&body)
                        .map_err(|e| anyhow::anyhow!("failed to parse ActionNode: {}", e));
                }
                Err(e) => {
                    let elapsed = start.elapsed();
                    last_err = e;
                    info!(
                        url = %self.url,
                        error = %last_err,
                        duration_ms = %elapsed.as_millis(),
                        "StepProvider next_action error"
                    );
                }
            }
            if attempt < self.retry.max_retries - 1 {
                tokio::time::sleep(Duration::from_millis(100)).await;
            }
        }
        // All retries exhausted → fallback
        match &self.retry.fallback_action {
            Some(node) => Ok(node.clone()),
            None => Err(last_err),
        }
    }

    async fn on_session_start(&self, ctx: &SessionContext) -> anyhow::Result<()> {
        let url = format!("{}/start", self.url);
        let body_str = serde_json::to_string(ctx).unwrap_or_default();
        info!(
            url = %url,
            method = "POST",
            headers = ?self.headers,
            body = %body_str,
            "StepProvider on_session_start request"
        );
        let start = std::time::Instant::now();
        let req = self.http_client.post(&url).json(ctx);
        if let Err(e) = crate::http_util::execute_request(req, &self.headers, None).await {
            warn!(
                url = %url,
                error = %e,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_session_start failed"
            );
        } else {
            info!(
                url = %url,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_session_start response"
            );
        }
        Ok(())
    }

    async fn on_session_end(&self, reason: &EndReason, session_id: &str) -> anyhow::Result<()> {
        let url = format!("{}/end", self.url);
        let body = serde_json::json!({
            "session_id": session_id,
            "reason": format!("{:?}", reason),
        });
        let body_str = serde_json::to_string(&body).unwrap_or_default();
        info!(
            url = %url,
            method = "POST",
            headers = ?self.headers,
            body = %body_str,
            "StepProvider on_session_end request"
        );
        let start = std::time::Instant::now();
        let req = self.http_client.post(&url).json(&body);
        if let Err(e) = crate::http_util::execute_request(req, &self.headers, None).await {
            warn!(
                url = %url,
                error = %e,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_session_end failed"
            );
        } else {
            info!(
                url = %url,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_session_end response"
            );
        }
        Ok(())
    }

    async fn on_local_dtmf_match(&self, digit: &str, action: &ActionNode) {
        let url = format!("{}/dtmf-match", self.url);
        let body = serde_json::json!({ "digit": digit, "action": action });
        let body_str = serde_json::to_string(&body).unwrap_or_default();
        info!(
            url = %url,
            method = "POST",
            headers = ?self.headers,
            body = %body_str,
            "StepProvider on_local_dtmf_match request"
        );
        let start = std::time::Instant::now();
        let req = self.http_client.post(&url).json(&body);
        if let Err(e) = crate::http_util::execute_request(req, &self.headers, None).await {
            warn!(
                url = %url,
                error = %e,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_local_dtmf_match failed"
            );
        } else {
            info!(
                url = %url,
                duration_ms = %start.elapsed().as_millis(),
                "StepProvider on_local_dtmf_match response"
            );
        }
    }
}