active_call/playbook/
mod.rs

1use crate::media::recorder::RecorderOption;
2use crate::media::vad::VADOption;
3use crate::synthesis::SynthesisOption;
4use crate::transcription::TranscriptionOption;
5use crate::{EouOption, RealtimeOption, media::ambiance::AmbianceOption};
6use anyhow::{Result, anyhow};
7use minijinja::Environment;
8use serde::{Deserialize, Serialize};
9use std::{collections::HashMap, path::Path};
10use tokio::fs;
11
12#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default, PartialEq)]
13#[serde(rename_all = "lowercase")]
14pub enum InterruptionStrategy {
15    #[default]
16    Both,
17    Vad,
18    Asr,
19    None,
20}
21
22#[derive(Debug, Deserialize, Serialize, Clone, Default)]
23#[serde(rename_all = "camelCase")]
24pub struct InterruptionConfig {
25    pub strategy: InterruptionStrategy,
26    pub min_speech_ms: Option<u32>,
27    pub filler_word_filter: Option<bool>,
28    pub volume_fade_ms: Option<u32>,
29    pub ignore_first_ms: Option<u32>,
30}
31
32#[derive(Debug, Deserialize, Serialize, Clone, Default)]
33#[serde(rename_all = "camelCase")]
34pub struct PlaybookConfig {
35    pub asr: Option<TranscriptionOption>,
36    pub tts: Option<SynthesisOption>,
37    pub llm: Option<LlmConfig>,
38    pub vad: Option<VADOption>,
39    pub denoise: Option<bool>,
40    pub ambiance: Option<AmbianceOption>,
41    pub recorder: Option<RecorderOption>,
42    pub extra: Option<HashMap<String, String>>,
43    pub eou: Option<EouOption>,
44    pub greeting: Option<String>,
45    pub interruption: Option<InterruptionConfig>,
46    pub dtmf: Option<HashMap<String, DtmfAction>>,
47    pub realtime: Option<RealtimeOption>,
48    pub posthook: Option<PostHookConfig>,
49    pub follow_up: Option<FollowUpConfig>,
50}
51
52#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)]
53#[serde(rename_all = "camelCase")]
54pub struct FollowUpConfig {
55    pub timeout: u64,
56    pub max_count: u32,
57}
58
59#[derive(Debug, Deserialize, Serialize, Clone)]
60#[serde(rename_all = "lowercase")]
61pub enum SummaryType {
62    Short,
63    Detailed,
64    Intent,
65    Json,
66    #[serde(untagged)]
67    Custom(String),
68}
69
70impl SummaryType {
71    pub fn prompt(&self) -> &str {
72        match self {
73            Self::Short => "summarize the conversation in one or two sentences.",
74            Self::Detailed => {
75                "summarize the conversation in detail, including key points, decisions, and action items."
76            }
77            Self::Intent => "identify and summarize the user's main intent and needs.",
78            Self::Json => {
79                "output the conversation summary in JSON format with fields: intent, key_points, sentiment."
80            }
81            Self::Custom(p) => p,
82        }
83    }
84}
85
86#[derive(Debug, Deserialize, Serialize, Clone, Default)]
87#[serde(rename_all = "camelCase")]
88pub struct PostHookConfig {
89    pub url: String,
90    pub summary: Option<SummaryType>,
91    pub method: Option<String>,
92    pub headers: Option<HashMap<String, String>>,
93    pub include_history: Option<bool>,
94}
95
96#[derive(Debug, Deserialize, Serialize, Clone)]
97#[serde(tag = "action", rename_all = "lowercase")]
98pub enum DtmfAction {
99    Goto { scene: String },
100    Transfer { target: String },
101    Hangup,
102}
103
104#[derive(Debug, Deserialize, Serialize, Clone, Default)]
105#[serde(rename_all = "camelCase")]
106pub struct LlmConfig {
107    pub provider: String,
108    pub model: Option<String>,
109    pub base_url: Option<String>,
110    pub api_key: Option<String>,
111    pub prompt: Option<String>,
112    pub greeting: Option<String>,
113}
114
115#[derive(Serialize, Deserialize, Clone, Debug)]
116pub struct ChatMessage {
117    pub role: String,
118    pub content: String,
119}
120
121#[derive(Debug, Clone, Default)]
122pub struct Scene {
123    pub id: String,
124    pub prompt: String,
125    pub dtmf: Option<HashMap<String, DtmfAction>>,
126    pub play: Option<String>,
127    pub follow_up: Option<FollowUpConfig>,
128}
129
130#[derive(Debug, Clone)]
131pub struct Playbook {
132    pub config: PlaybookConfig,
133    pub scenes: HashMap<String, Scene>,
134    pub initial_scene_id: Option<String>,
135}
136
137impl Playbook {
138    pub async fn load<P: AsRef<Path>>(
139        path: P,
140        variables: Option<&HashMap<String, serde_json::Value>>,
141    ) -> Result<Self> {
142        let content = fs::read_to_string(path).await?;
143        Self::parse(&content, variables)
144    }
145
146    pub fn parse(
147        content: &str,
148        variables: Option<&HashMap<String, serde_json::Value>>,
149    ) -> Result<Self> {
150        let rendered_content = if let Some(vars) = variables {
151            let env = Environment::new();
152            env.render_str(content, vars)?
153        } else {
154            content.to_string()
155        };
156
157        if !rendered_content.starts_with("---") {
158            return Err(anyhow!("Missing front matter"));
159        }
160
161        let parts: Vec<&str> = rendered_content.splitn(3, "---").collect();
162        if parts.len() < 3 {
163            return Err(anyhow!("Invalid front matter format"));
164        }
165
166        let yaml_str = parts[1];
167        let prompt_section = parts[2].trim();
168
169        let mut config: PlaybookConfig = serde_yaml::from_str(yaml_str)?;
170
171        let mut scenes = HashMap::new();
172        let mut first_scene_id: Option<String> = None;
173
174        let dtmf_regex =
175            regex::Regex::new(r#"<dtmf\s+digit="([^"]+)"\s+action="([^"]+)"(?:\s+scene="([^"]+)")?(?:\s+target="([^"]+)")?\s*/>"#).unwrap();
176        let play_regex = regex::Regex::new(r#"<play\s+file="([^"]+)"\s*/>"#).unwrap();
177        let followup_regex = regex::Regex::new(r#"<followup\s+timeout="(\d+)"\s+max="(\d+)"\s*/>"#).unwrap();
178
179        let parse_scene = |id: String, content: String| -> Scene {
180            let mut dtmf_map = HashMap::new();
181            let mut play = None;
182            let mut follow_up = None;
183            let mut final_content = content.clone();
184
185            for cap in dtmf_regex.captures_iter(&content) {
186                let digit = cap.get(1).unwrap().as_str().to_string();
187                let action_type = cap.get(2).unwrap().as_str();
188
189                let action = match action_type {
190                    "goto" => {
191                        let scene = cap
192                            .get(3)
193                            .map(|m| m.as_str().to_string())
194                            .unwrap_or_default();
195                        DtmfAction::Goto { scene }
196                    }
197                    "transfer" => {
198                        let target = cap
199                            .get(4)
200                            .map(|m| m.as_str().to_string())
201                            .unwrap_or_default();
202                        DtmfAction::Transfer { target }
203                    }
204                    "hangup" => DtmfAction::Hangup,
205                    _ => continue,
206                };
207                dtmf_map.insert(digit, action);
208            }
209
210            if let Some(cap) = play_regex.captures(&content) {
211                play = Some(cap.get(1).unwrap().as_str().to_string());
212            }
213
214            if let Some(cap) = followup_regex.captures(&content) {
215                let timeout = cap.get(1).unwrap().as_str().parse().unwrap_or(0);
216                let max_count = cap.get(2).unwrap().as_str().parse().unwrap_or(0);
217                follow_up = Some(FollowUpConfig { timeout, max_count });
218            }
219
220            // Remove dtmf and play tags from the content
221            final_content = dtmf_regex.replace_all(&final_content, "").to_string();
222            final_content = play_regex.replace_all(&final_content, "").to_string();
223            final_content = followup_regex.replace_all(&final_content, "").to_string();
224            final_content = final_content.trim().to_string();
225
226            Scene {
227                id,
228                prompt: final_content,
229                dtmf: if dtmf_map.is_empty() {
230                    None
231                } else {
232                    Some(dtmf_map)
233                },
234                play,
235                follow_up,
236            }
237        };
238
239        // Parse scenes from markdown. Look for headers like "# Scene: <id>"
240        let scene_regex = regex::Regex::new(r"(?m)^# Scene:\s*(.+)$").unwrap();
241        let mut last_match_end = 0;
242        let mut last_scene_id: Option<String> = None;
243
244        for cap in scene_regex.captures_iter(prompt_section) {
245            let m = cap.get(0).unwrap();
246            let scene_id = cap.get(1).unwrap().as_str().trim().to_string();
247
248            if first_scene_id.is_none() {
249                first_scene_id = Some(scene_id.clone());
250            }
251
252            if let Some(id) = last_scene_id {
253                let scene_content = prompt_section[last_match_end..m.start()].trim().to_string();
254                scenes.insert(id.clone(), parse_scene(id, scene_content));
255            } else {
256                // Content before the first scene header
257                let pre_content = prompt_section[..m.start()].trim();
258                if !pre_content.is_empty() {
259                    let id = "default".to_string();
260                    first_scene_id = Some(id.clone());
261                    scenes.insert(id.clone(), parse_scene(id, pre_content.to_string()));
262                }
263            }
264
265            last_scene_id = Some(scene_id);
266            last_match_end = m.end();
267        }
268
269        if let Some(id) = last_scene_id {
270            let scene_content = prompt_section[last_match_end..].trim().to_string();
271            scenes.insert(id.clone(), parse_scene(id, scene_content));
272        } else if !prompt_section.is_empty() {
273            // No scene headers found, treat the whole prompt as "default"
274            let id = "default".to_string();
275            first_scene_id = Some(id.clone());
276            scenes.insert(id.clone(), parse_scene(id, prompt_section.to_string()));
277        }
278
279        if let Some(llm) = config.llm.as_mut() {
280            if llm.api_key.is_none() {
281                if let Ok(key) = std::env::var("OPENAI_API_KEY") {
282                    llm.api_key = Some(key);
283                }
284            }
285            if llm.base_url.is_none() {
286                if let Ok(url) = std::env::var("OPENAI_BASE_URL") {
287                    llm.base_url = Some(url);
288                }
289            }
290            if llm.model.is_none() {
291                if let Ok(model) = std::env::var("OPENAI_MODEL") {
292                    llm.model = Some(model);
293                }
294            }
295
296            // Use the first scene found as the initial prompt
297            if let Some(initial_id) = first_scene_id.clone() {
298                if let Some(scene) = scenes.get(&initial_id) {
299                    llm.prompt = Some(scene.prompt.clone());
300                }
301            }
302        }
303
304        Ok(Self {
305            config,
306            scenes,
307            initial_scene_id: first_scene_id,
308        })
309    }
310}
311
312pub mod dialogue;
313pub mod handler;
314pub mod runner;
315
316pub use dialogue::DialogueHandler;
317pub use handler::{LlmHandler, RagRetriever};
318pub use runner::PlaybookRunner;
319
320#[cfg(test)]
321mod tests {
322    use super::*;
323    use serde_json::json;
324
325    #[test]
326    fn test_playbook_parsing_with_variables() {
327        let content = r#"---
328llm:
329  provider: openai
330  model: {{ model_name }}
331  greeting: Hello, {{ user_name }}!
332---
333# Scene: main
334You are an assistant for {{ company }}.
335"#;
336        let mut variables = HashMap::new();
337        variables.insert("model_name".to_string(), json!("gpt-4"));
338        variables.insert("user_name".to_string(), json!("Alice"));
339        variables.insert("company".to_string(), json!("RestSend"));
340
341        let playbook = Playbook::parse(content, Some(&variables)).unwrap();
342
343        assert_eq!(
344            playbook.config.llm.as_ref().unwrap().model,
345            Some("gpt-4".to_string())
346        );
347        assert_eq!(
348            playbook.config.llm.as_ref().unwrap().greeting,
349            Some("Hello, Alice!".to_string())
350        );
351
352        let scene = playbook.scenes.get("main").unwrap();
353        assert_eq!(scene.prompt, "You are an assistant for RestSend.");
354    }
355
356    #[test]
357    fn test_playbook_scene_dtmf_parsing() {
358        let content = r#"---
359llm:
360  provider: openai
361---
362# Scene: main
363<dtmf digit="1" action="goto" scene="product" />
364<dtmf digit="2" action="transfer" target="sip:123@domain" />
365<dtmf digit="0" action="hangup" />
366Welcome to our service.
367"#;
368        let playbook = Playbook::parse(content, None).unwrap();
369
370        let scene = playbook.scenes.get("main").unwrap();
371        assert_eq!(scene.prompt, "Welcome to our service.");
372
373        let dtmf = scene.dtmf.as_ref().unwrap();
374        assert_eq!(dtmf.len(), 3);
375
376        match dtmf.get("1").unwrap() {
377            DtmfAction::Goto { scene } => assert_eq!(scene, "product"),
378            _ => panic!("Expected Goto action"),
379        }
380
381        match dtmf.get("2").unwrap() {
382            DtmfAction::Transfer { target } => assert_eq!(target, "sip:123@domain"),
383            _ => panic!("Expected Transfer action"),
384        }
385
386        match dtmf.get("0").unwrap() {
387            DtmfAction::Hangup => {}
388            _ => panic!("Expected Hangup action"),
389        }
390    }
391
392    #[test]
393    fn test_playbook_dtmf_priority() {
394        let content = r#"---
395llm:
396  provider: openai
397dtmf:
398  "1": { action: "goto", scene: "global_dest" }
399  "9": { action: "hangup" }
400---
401# Scene: main
402<dtmf digit="1" action="goto" scene="local_dest" />
403Welcome.
404"#;
405        let playbook = Playbook::parse(content, None).unwrap();
406
407        // Check global config
408        let global_dtmf = playbook.config.dtmf.as_ref().unwrap();
409        assert_eq!(global_dtmf.len(), 2);
410
411        // Check scene config
412        let scene = playbook.scenes.get("main").unwrap();
413        let scene_dtmf = scene.dtmf.as_ref().unwrap();
414        assert_eq!(scene_dtmf.len(), 1);
415
416        // Verify scene has local_dest for "1"
417        match scene_dtmf.get("1").unwrap() {
418            DtmfAction::Goto { scene } => assert_eq!(scene, "local_dest"),
419            _ => panic!("Expected Local Goto action"),
420        }
421    }
422
423    #[test]
424    fn test_posthook_config_parsing() {
425        let content = r#"---
426posthook:
427  url: "http://test.com"
428  summary: "json"
429  includeHistory: true
430  headers:
431    X-API-Key: "secret"
432llm:
433  provider: openai
434---
435# Scene: main
436Hello
437"#;
438        let playbook = Playbook::parse(content, None).unwrap();
439        let posthook = playbook.config.posthook.unwrap();
440        assert_eq!(posthook.url, "http://test.com");
441        match posthook.summary.unwrap() {
442            SummaryType::Json => {}
443            _ => panic!("Expected Json summary type"),
444        }
445        assert_eq!(posthook.include_history, Some(true));
446        assert_eq!(
447            posthook.headers.unwrap().get("X-API-Key").unwrap(),
448            "secret"
449        );
450    }
451
452    #[test]
453    fn test_custom_summary_parsing() {
454        let content = r#"---
455posthook:
456  url: "http://test.com"
457  summary: "Please summarize customly"
458llm:
459  provider: openai
460---
461# Scene: main
462Hello
463"#;
464        let playbook = Playbook::parse(content, None).unwrap();
465        let posthook = playbook.config.posthook.unwrap();
466        match posthook.summary.unwrap() {
467            SummaryType::Custom(s) => assert_eq!(s, "Please summarize customly"),
468            _ => panic!("Expected Custom summary type"),
469        }
470    }
471}