Skip to main content

active_call/playbook/
mod.rs

1use crate::media::agc::AGCOption;
2use crate::media::monitor::MonitorOption;
3use crate::media::recorder::RecorderOption;
4use crate::media::vad::VADOption;
5use crate::synthesis::SynthesisOption;
6use crate::transcription::TranscriptionOption;
7use crate::{
8    EouOption, RealtimeOption, RingbackDetectionOption, SipOption, media::ambiance::AmbianceOption,
9};
10use anyhow::{Result, anyhow};
11use minijinja::Environment;
12use serde::{Deserialize, Serialize};
13use serde_json::Value;
14use std::{collections::HashMap, path::Path};
15use tokio::fs;
16
17/// Expand environment variables in the format ${VAR_NAME}
18fn expand_env_vars(input: &str) -> String {
19    let re = regex::Regex::new(r"\$\{([^}]+)\}").unwrap();
20    re.replace_all(input, |caps: &regex::Captures| {
21        let var_name = &caps[1];
22        std::env::var(var_name).unwrap_or_else(|_| format!("${{{}}}", var_name))
23    })
24    .to_string()
25}
26
27#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default, PartialEq)]
28#[serde(rename_all = "lowercase")]
29pub enum InterruptionStrategy {
30    #[default]
31    Both,
32    Vad,
33    Asr,
34    None,
35}
36
37#[derive(Debug, Deserialize, Serialize, Clone, Default)]
38#[serde(rename_all = "camelCase")]
39pub struct InterruptionConfig {
40    pub strategy: InterruptionStrategy,
41    pub min_speech_ms: Option<u32>,
42    pub filler_word_filter: Option<bool>,
43    pub volume_fade_ms: Option<u32>,
44    pub ignore_first_ms: Option<u32>,
45}
46
47#[derive(Debug, Deserialize, Serialize, Clone, Default)]
48#[serde(rename_all = "camelCase")]
49pub struct PlaybookConfig {
50    pub asr: Option<TranscriptionOption>,
51    pub tts: Option<SynthesisOption>,
52    pub llm: Option<LlmConfig>,
53    pub vad: Option<VADOption>,
54    pub denoise: Option<bool>,
55    pub agc: Option<AGCOption>,
56    pub ambiance: Option<AmbianceOption>,
57    pub recorder: Option<RecorderOption>,
58    pub extra: Option<HashMap<String, String>>,
59    pub eou: Option<EouOption>,
60    pub greeting: Option<String>,
61    pub interruption: Option<InterruptionConfig>,
62    pub dtmf: Option<HashMap<String, DtmfAction>>,
63    pub dtmf_collectors: Option<HashMap<String, DtmfCollectorConfig>>,
64    pub realtime: Option<RealtimeOption>,
65    pub ringback_detection: Option<RingbackDetectionOption>,
66    /// Side-path audio distribution (see `media::monitor`).
67    pub monitor: Option<MonitorOption>,
68    pub posthook: Option<PostHookConfig>,
69    pub follow_up: Option<FollowUpConfig>,
70    pub sip: Option<SipOption>,
71}
72
73#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)]
74#[serde(rename_all = "camelCase")]
75pub struct FollowUpConfig {
76    pub timeout: u64,
77    pub max_count: u32,
78}
79
80#[derive(Debug, Deserialize, Serialize, Clone)]
81#[serde(rename_all = "lowercase")]
82pub enum SummaryType {
83    Short,
84    Detailed,
85    Intent,
86    Json,
87    #[serde(untagged)]
88    Custom(String),
89}
90
91impl SummaryType {
92    pub fn prompt(&self) -> &str {
93        match self {
94            Self::Short => "summarize the conversation in one or two sentences.",
95            Self::Detailed => {
96                "summarize the conversation in detail, including key points, decisions, and action items."
97            }
98            Self::Intent => "identify and summarize the user's main intent and needs.",
99            Self::Json => {
100                "output the conversation summary in JSON format with fields: intent, key_points, sentiment."
101            }
102            Self::Custom(p) => p,
103        }
104    }
105}
106
107#[derive(Debug, Deserialize, Serialize, Clone, Default)]
108#[serde(rename_all = "camelCase")]
109pub struct PostHookConfig {
110    pub url: String,
111    pub summary: Option<SummaryType>,
112    pub method: Option<String>,
113    pub headers: Option<HashMap<String, String>>,
114    pub include_history: Option<bool>,
115    pub timeout: Option<u32>,
116}
117
118#[derive(Debug, Deserialize, Serialize, Clone)]
119#[serde(tag = "action", rename_all = "lowercase")]
120pub enum DtmfAction {
121    Goto { scene: String },
122    Transfer { target: String },
123    Hangup,
124}
125
126/// Validation rule for DTMF digit collection
127#[derive(Debug, Deserialize, Serialize, Clone, Default)]
128#[serde(rename_all = "camelCase")]
129pub struct DtmfValidation {
130    /// Regex pattern for validation, e.g. "^1[3-9]\\d{9}$" for Chinese phone numbers
131    pub pattern: String,
132    /// Error message shown when validation fails
133    pub error_message: Option<String>,
134}
135
136/// Configuration for a DTMF digit collector template
137#[derive(Debug, Deserialize, Serialize, Clone, Default)]
138#[serde(rename_all = "camelCase")]
139pub struct DtmfCollectorConfig {
140    /// Human-readable description of this collector (used in LLM prompt generation)
141    pub description: Option<String>,
142    /// Exact expected digit count (shorthand for min_digits == max_digits)
143    pub digits: Option<u32>,
144    /// Minimum digits required
145    pub min_digits: Option<u32>,
146    /// Maximum digits allowed
147    pub max_digits: Option<u32>,
148    /// Key that terminates collection: "#" or "*"
149    pub finish_key: Option<String>,
150    /// Overall timeout in seconds (default: 15)
151    pub timeout: Option<u32>,
152    /// Max seconds between consecutive key presses (default: 5)
153    pub inter_digit_timeout: Option<u32>,
154    /// Validation rule (regex + error message)
155    pub validation: Option<DtmfValidation>,
156    /// Max retry attempts when validation fails (default: 3)
157    pub retry_times: Option<u32>,
158    /// Whether voice input (ASR) can interrupt collection (default: false)
159    pub interruptible: Option<bool>,
160}
161
162#[derive(Debug, Deserialize, Serialize, Clone, Default)]
163#[serde(rename_all = "camelCase")]
164pub struct LlmConfig {
165    pub provider: String,
166    pub model: Option<String>,
167    pub base_url: Option<String>,
168    pub api_key: Option<String>,
169    pub prompt: Option<String>,
170    pub greeting: Option<String>,
171    pub language: Option<String>,
172    pub features: Option<Vec<String>>,
173    pub repair_window_ms: Option<u64>,
174    pub summary_limit: Option<usize>,
175    /// Custom tool instructions. If not set, default tool instructions based on language will be used.
176    /// Set this to override the built-in tool usage instructions completely.
177    pub tool_instructions: Option<String>,
178}
179
180#[derive(Serialize, Deserialize, Clone, Debug)]
181pub struct ChatMessage {
182    pub role: String,
183    pub content: String,
184}
185
186#[derive(Debug, Clone, Default)]
187pub struct Scene {
188    pub id: String,
189    pub prompt: String,
190    /// The original unrendered prompt template, preserved for dynamic re-rendering
191    /// with updated variables (e.g., after set_var during conversation).
192    pub raw_prompt: Option<String>,
193    pub dtmf: Option<HashMap<String, DtmfAction>>,
194    pub play: Option<String>,
195    pub follow_up: Option<FollowUpConfig>,
196}
197
198/// Built-in session variable key constants.
199/// These are automatically injected into `extras` so they can be referenced
200/// in playbook templates using `{{ session_id }}`, `{{ call_type }}`, etc.
201pub const BUILTIN_SESSION_ID: &str = "session_id";
202pub const BUILTIN_CALL_TYPE: &str = "call_type";
203pub const BUILTIN_CALLER: &str = "caller";
204pub const BUILTIN_CALLEE: &str = "callee";
205pub const BUILTIN_START_TIME: &str = "start_time";
206
207/// Render a scene prompt template dynamically using the current variables.
208/// This allows `set_var` values set during conversation to be used in scene prompts.
209///
210/// If `raw_prompt` is `None` or rendering fails, falls back to the pre-rendered `prompt`.
211pub fn render_scene_prompt(scene: &Scene, vars: &HashMap<String, serde_json::Value>) -> String {
212    let template = match &scene.raw_prompt {
213        Some(t) if t.contains("{{") => t,
214        _ => return scene.prompt.clone(),
215    };
216
217    let env = Environment::new();
218    let mut context = vars.clone();
219
220    // Build sip dictionary from _sip_header_keys (same logic as Playbook::render)
221    let sip_header_keys: Vec<String> = vars
222        .get("_sip_header_keys")
223        .and_then(|v| serde_json::from_value(v.clone()).ok())
224        .unwrap_or_default();
225
226    let mut sip_headers = HashMap::new();
227    for key in &sip_header_keys {
228        if let Some(value) = vars.get(key) {
229            sip_headers.insert(key.clone(), value.clone());
230        }
231    }
232    context.insert(
233        "sip".to_string(),
234        serde_json::to_value(&sip_headers).unwrap_or(Value::Null),
235    );
236
237    // Remove internal keys from context
238    context.retain(|k, _| !k.starts_with('_'));
239
240    match env.render_str(template, &context) {
241        Ok(rendered) => rendered,
242        Err(_) => scene.prompt.clone(),
243    }
244}
245
246#[derive(Debug, Clone)]
247pub struct Playbook {
248    pub raw_content: String,
249    pub config: PlaybookConfig,
250    pub scenes: HashMap<String, Scene>,
251    pub initial_scene_id: Option<String>,
252}
253
254impl Playbook {
255    pub async fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
256        let content = fs::read_to_string(path).await?;
257        Self::parse(&content)
258    }
259
260    pub fn render(&self, vars: &HashMap<String, serde_json::Value>) -> Result<Self> {
261        let env = Environment::new();
262        let mut context = vars.clone();
263
264        // Get the list of SIP header keys stored by extract_headers processing
265        // If not present, sip dict will be empty (no headers were configured for extraction)
266        let sip_header_keys: Vec<String> = vars
267            .get("_sip_header_keys")
268            .and_then(|v| serde_json::from_value(v.clone()).ok())
269            .unwrap_or_default();
270
271        // Separate SIP headers into sip dictionary based on stored keys
272        let mut sip_headers = HashMap::new();
273        for key in &sip_header_keys {
274            if let Some(value) = vars.get(key) {
275                sip_headers.insert(key.clone(), value.clone());
276            }
277        }
278        context.insert(
279            "sip".to_string(),
280            serde_json::to_value(&sip_headers).unwrap_or(Value::Null),
281        );
282
283        let rendered = env.render_str(&self.raw_content, &context)?;
284        let mut res = Self::parse(&rendered)?;
285        // Preserve the original raw_content (with templates) for dynamic re-rendering
286        res.raw_content = self.raw_content.clone();
287        // Preserve original raw_prompts from the unrendered playbook for dynamic re-rendering
288        for (scene_id, scene) in &self.scenes {
289            if let Some(res_scene) = res.scenes.get_mut(scene_id) {
290                res_scene.raw_prompt = scene.raw_prompt.clone();
291            }
292        }
293        res.config.sip.as_mut().map(|sip| {
294            sip.hangup_headers = self
295                .config
296                .sip
297                .as_ref()
298                .and_then(|sip| sip.hangup_headers.clone());
299        });
300        Ok(res)
301    }
302
303    pub fn parse(content: &str) -> Result<Self> {
304        if !content.starts_with("---") {
305            return Err(anyhow!("Missing front matter"));
306        }
307
308        let parts: Vec<&str> = content.splitn(3, "---").collect();
309        if parts.len() < 3 {
310            return Err(anyhow!("Invalid front matter format"));
311        }
312
313        let yaml_str = parts[1];
314        let prompt_section = parts[2].trim();
315
316        // Expand environment variables in YAML configuration
317        // This allows ALL fields to use ${VAR_NAME} syntax
318        let expanded_yaml = expand_env_vars(yaml_str);
319        let mut config: PlaybookConfig = serde_yaml::from_str(&expanded_yaml)?;
320
321        let mut scenes = HashMap::new();
322        let mut first_scene_id: Option<String> = None;
323
324        let dtmf_regex =
325            regex::Regex::new(r#"<dtmf\s+digit="([^"]+)"\s+action="([^"]+)"(?:\s+scene="([^"]+)")?(?:\s+target="([^"]+)")?\s*/>"#).unwrap();
326        let play_regex = regex::Regex::new(r#"<play\s+file="([^"]+)"\s*/>"#).unwrap();
327        let followup_regex =
328            regex::Regex::new(r#"<followup\s+timeout="(\d+)"\s+max="(\d+)"\s*/>"#).unwrap();
329
330        let parse_scene = |id: String, content: String| -> Scene {
331            let mut dtmf_map = HashMap::new();
332            let mut play = None;
333            let mut follow_up = None;
334            let mut final_content = content.clone();
335
336            for cap in dtmf_regex.captures_iter(&content) {
337                let digit = cap.get(1).unwrap().as_str().to_string();
338                let action_type = cap.get(2).unwrap().as_str();
339
340                let action = match action_type {
341                    "goto" => {
342                        let scene = cap
343                            .get(3)
344                            .map(|m| m.as_str().to_string())
345                            .unwrap_or_default();
346                        DtmfAction::Goto { scene }
347                    }
348                    "transfer" => {
349                        let target = cap
350                            .get(4)
351                            .map(|m| m.as_str().to_string())
352                            .unwrap_or_default();
353                        DtmfAction::Transfer { target }
354                    }
355                    "hangup" => DtmfAction::Hangup,
356                    _ => continue,
357                };
358                dtmf_map.insert(digit, action);
359            }
360
361            if let Some(cap) = play_regex.captures(&content) {
362                play = Some(cap.get(1).unwrap().as_str().to_string());
363            }
364
365            if let Some(cap) = followup_regex.captures(&content) {
366                let timeout = cap.get(1).unwrap().as_str().parse().unwrap_or(0);
367                let max_count = cap.get(2).unwrap().as_str().parse().unwrap_or(0);
368                follow_up = Some(FollowUpConfig { timeout, max_count });
369            }
370
371            // Remove dtmf and play tags from the content
372            final_content = dtmf_regex.replace_all(&final_content, "").to_string();
373            final_content = play_regex.replace_all(&final_content, "").to_string();
374            final_content = followup_regex.replace_all(&final_content, "").to_string();
375            final_content = final_content.trim().to_string();
376
377            Scene {
378                id,
379                raw_prompt: Some(final_content.clone()),
380                prompt: final_content,
381                dtmf: if dtmf_map.is_empty() {
382                    None
383                } else {
384                    Some(dtmf_map)
385                },
386                play,
387                follow_up,
388            }
389        };
390
391        // Parse scenes from markdown. Look for headers like "# Scene: <id>"
392        let scene_regex = regex::Regex::new(r"(?m)^# Scene:\s*(.+)$").unwrap();
393        let mut last_match_end = 0;
394        let mut last_scene_id: Option<String> = None;
395
396        for cap in scene_regex.captures_iter(prompt_section) {
397            let m = cap.get(0).unwrap();
398            let scene_id = cap.get(1).unwrap().as_str().trim().to_string();
399
400            if first_scene_id.is_none() {
401                first_scene_id = Some(scene_id.clone());
402            }
403
404            if let Some(id) = last_scene_id {
405                let scene_content = prompt_section[last_match_end..m.start()].trim().to_string();
406                scenes.insert(id.clone(), parse_scene(id, scene_content));
407            } else {
408                // Content before the first scene header
409                let pre_content = prompt_section[..m.start()].trim();
410                if !pre_content.is_empty() {
411                    let id = "default".to_string();
412                    first_scene_id = Some(id.clone());
413                    scenes.insert(id.clone(), parse_scene(id, pre_content.to_string()));
414                }
415            }
416
417            last_scene_id = Some(scene_id);
418            last_match_end = m.end();
419        }
420
421        if let Some(id) = last_scene_id {
422            let scene_content = prompt_section[last_match_end..].trim().to_string();
423            scenes.insert(id.clone(), parse_scene(id, scene_content));
424        } else if !prompt_section.is_empty() {
425            // No scene headers found, treat the whole prompt as "default"
426            let id = "default".to_string();
427            first_scene_id = Some(id.clone());
428            scenes.insert(id.clone(), parse_scene(id, prompt_section.to_string()));
429        }
430
431        if let Some(llm) = config.llm.as_mut() {
432            // Fallback to direct env var if not set
433            if llm.api_key.is_none() {
434                if let Ok(key) = std::env::var("OPENAI_API_KEY") {
435                    llm.api_key = Some(key);
436                }
437            }
438            if llm.base_url.is_none() {
439                if let Ok(url) = std::env::var("OPENAI_BASE_URL") {
440                    llm.base_url = Some(url);
441                }
442            }
443            if llm.model.is_none() {
444                if let Ok(model) = std::env::var("OPENAI_MODEL") {
445                    llm.model = Some(model);
446                }
447            }
448
449            // Use the first scene found as the initial prompt
450            if let Some(initial_id) = first_scene_id.clone() {
451                if let Some(scene) = scenes.get(&initial_id) {
452                    llm.prompt = Some(scene.prompt.clone());
453                }
454            }
455        }
456
457        Ok(Self {
458            raw_content: content.to_string(),
459            config,
460            scenes,
461            initial_scene_id: first_scene_id,
462        })
463    }
464}
465
466pub mod dialogue;
467pub mod handler;
468pub mod runner;
469
470pub use dialogue::DialogueHandler;
471pub use handler::{LlmHandler, RagRetriever};
472pub use runner::PlaybookRunner;
473
474#[cfg(test)]
475mod tests {
476    use super::*;
477    use serde_json::json;
478
479    #[test]
480    fn test_playbook_parsing_with_variables() {
481        let content = r#"---
482llm:
483  provider: openai
484  model: |-
485    {{ model_name }}
486  greeting: |-
487    Hello, {{ user_name }}!
488---
489# Scene: main
490You are an assistant for {{ company }}.
491"#;
492        let mut variables = HashMap::new();
493        variables.insert("model_name".to_string(), json!("gpt-4"));
494        variables.insert("user_name".to_string(), json!("Alice"));
495        variables.insert("company".to_string(), json!("RestSend"));
496
497        let playbook = Playbook::parse(content)
498            .unwrap()
499            .render(&variables)
500            .unwrap();
501        assert_eq!(
502            playbook.config.llm.as_ref().unwrap().model,
503            Some("gpt-4".to_string())
504        );
505        assert_eq!(
506            playbook.config.llm.as_ref().unwrap().greeting,
507            Some("Hello, Alice!".to_string())
508        );
509
510        let scene = playbook.scenes.get("main").unwrap();
511        assert_eq!(scene.prompt, "You are an assistant for RestSend.");
512    }
513
514    #[test]
515    fn test_playbook_scene_dtmf_parsing() {
516        let content = r#"---
517llm:
518  provider: openai
519---
520# Scene: main
521<dtmf digit="1" action="goto" scene="product" />
522<dtmf digit="2" action="transfer" target="sip:123@domain" />
523<dtmf digit="0" action="hangup" />
524Welcome to our service.
525"#;
526        let playbook = Playbook::parse(content).unwrap();
527
528        let scene = playbook.scenes.get("main").unwrap();
529        assert_eq!(scene.prompt, "Welcome to our service.");
530
531        let dtmf = scene.dtmf.as_ref().unwrap();
532        assert_eq!(dtmf.len(), 3);
533
534        match dtmf.get("1").unwrap() {
535            DtmfAction::Goto { scene } => assert_eq!(scene, "product"),
536            _ => panic!("Expected Goto action"),
537        }
538
539        match dtmf.get("2").unwrap() {
540            DtmfAction::Transfer { target } => assert_eq!(target, "sip:123@domain"),
541            _ => panic!("Expected Transfer action"),
542        }
543
544        match dtmf.get("0").unwrap() {
545            DtmfAction::Hangup => {}
546            _ => panic!("Expected Hangup action"),
547        }
548    }
549
550    #[test]
551    fn test_playbook_dtmf_priority() {
552        let content = r#"---
553llm:
554  provider: openai
555dtmf:
556  "1": { action: "goto", scene: "global_dest" }
557  "9": { action: "hangup" }
558---
559# Scene: main
560<dtmf digit="1" action="goto" scene="local_dest" />
561Welcome.
562"#;
563        let playbook = Playbook::parse(content).unwrap();
564
565        // Check global config
566        let global_dtmf = playbook.config.dtmf.as_ref().unwrap();
567        assert_eq!(global_dtmf.len(), 2);
568
569        // Check scene config
570        let scene = playbook.scenes.get("main").unwrap();
571        let scene_dtmf = scene.dtmf.as_ref().unwrap();
572        assert_eq!(scene_dtmf.len(), 1);
573
574        // Verify scene has local_dest for "1"
575        match scene_dtmf.get("1").unwrap() {
576            DtmfAction::Goto { scene } => assert_eq!(scene, "local_dest"),
577            _ => panic!("Expected Local Goto action"),
578        }
579    }
580
581    #[test]
582    fn test_posthook_config_parsing() {
583        let content = r#"---
584posthook:
585  url: "http://test.com"
586  summary: "json"
587  includeHistory: true
588  headers:
589    X-API-Key: "secret"
590llm:
591  provider: openai
592---
593# Scene: main
594Hello
595"#;
596        let playbook = Playbook::parse(content).unwrap();
597        let posthook = playbook.config.posthook.unwrap();
598        assert_eq!(posthook.url, "http://test.com");
599        match posthook.summary.unwrap() {
600            SummaryType::Json => {}
601            _ => panic!("Expected Json summary type"),
602        }
603        assert_eq!(posthook.include_history, Some(true));
604        assert_eq!(
605            posthook.headers.unwrap().get("X-API-Key").unwrap(),
606            "secret"
607        );
608    }
609
610    #[test]
611    fn test_env_var_expansion() {
612        // Set test env vars
613        unsafe {
614            std::env::set_var("TEST_API_KEY", "sk-test-12345");
615            std::env::set_var("TEST_BASE_URL", "https://api.test.com");
616        }
617
618        let content = r#"---
619llm:
620  provider: openai
621  apiKey: "${TEST_API_KEY}"
622  baseUrl: "${TEST_BASE_URL}"
623  model: gpt-4
624---
625# Scene: main
626Test
627"#;
628        let playbook = Playbook::parse(content).unwrap();
629        let llm = playbook.config.llm.unwrap();
630
631        assert_eq!(llm.api_key.unwrap(), "sk-test-12345");
632        assert_eq!(llm.base_url.unwrap(), "https://api.test.com");
633        assert_eq!(llm.model.unwrap(), "gpt-4");
634
635        // Clean up
636        unsafe {
637            std::env::remove_var("TEST_API_KEY");
638            std::env::remove_var("TEST_BASE_URL");
639        }
640    }
641
642    #[test]
643    fn test_env_var_expansion_missing() {
644        // Test with undefined var
645        let content = r#"---
646llm:
647  provider: openai
648  apiKey: "${UNDEFINED_VAR}"
649---
650# Scene: main
651Test
652"#;
653        let playbook = Playbook::parse(content).unwrap();
654        let llm = playbook.config.llm.unwrap();
655
656        // Should keep the placeholder if env var not found
657        assert_eq!(llm.api_key.unwrap(), "${UNDEFINED_VAR}");
658    }
659
660    #[test]
661    fn test_custom_summary_parsing() {
662        let content = r#"---
663posthook:
664  url: "http://test.com"
665  summary: "Please summarize customly"
666llm:
667  provider: openai
668---
669# Scene: main
670Hello
671"#;
672        let playbook = Playbook::parse(content).unwrap();
673        let posthook = playbook.config.posthook.unwrap();
674        match posthook.summary.unwrap() {
675            SummaryType::Custom(s) => assert_eq!(s, "Please summarize customly"),
676            _ => panic!("Expected Custom summary type"),
677        }
678    }
679
680    #[test]
681    fn test_sip_dict_access_with_hyphens() {
682        // Test accessing SIP headers with hyphens via sip dictionary
683        let content = r#"---
684llm:
685  provider: openai
686  greeting: Hello {{ sip["X-Customer-Name"] }}!
687---
688# Scene: main
689Your ID is {{ sip["X-Customer-ID"] }}.
690Session type: {{ sip["X-Session-Type"] }}.
691"#;
692        let mut variables = HashMap::new();
693        variables.insert("X-Customer-Name".to_string(), json!("Alice"));
694        variables.insert("X-Customer-ID".to_string(), json!("CID-12345"));
695        variables.insert("X-Session-Type".to_string(), json!("inbound"));
696        // Simulate extract_headers processing
697        variables.insert(
698            "_sip_header_keys".to_string(),
699            json!(["X-Customer-Name", "X-Customer-ID", "X-Session-Type"]),
700        );
701
702        let playbook = Playbook::parse(content)
703            .unwrap()
704            .render(&variables)
705            .unwrap();
706        assert_eq!(
707            playbook.config.llm.as_ref().unwrap().greeting,
708            Some("Hello Alice!".to_string())
709        );
710
711        let scene = playbook.scenes.get("main").unwrap();
712        assert_eq!(
713            scene.prompt,
714            "Your ID is CID-12345.\nSession type: inbound."
715        );
716    }
717
718    #[test]
719    fn test_sip_dict_only_contains_sip_headers() {
720        // Test that sip dict only contains SIP headers from extract_headers, not other variables
721        let content = r#"---
722llm:
723  provider: openai
724---
725# Scene: main
726SIP Header: {{ sip["X-Custom-Header"] }}
727Regular var: {{ regular_var }}
728"#;
729        let mut variables = HashMap::new();
730        variables.insert("X-Custom-Header".to_string(), json!("header_value"));
731        variables.insert("regular_var".to_string(), json!("regular_value"));
732        variables.insert("another_var".to_string(), json!("another"));
733        // Only X-Custom-Header is extracted
734        variables.insert("_sip_header_keys".to_string(), json!(["X-Custom-Header"]));
735
736        let playbook = Playbook::parse(content)
737            .unwrap()
738            .render(&variables)
739            .unwrap();
740        let scene = playbook.scenes.get("main").unwrap();
741
742        // Both should work - SIP header via sip dict, regular var via direct access
743        assert!(scene.prompt.contains("SIP Header: header_value"));
744        assert!(scene.prompt.contains("Regular var: regular_value"));
745    }
746
747    #[test]
748    fn test_sip_dict_mixed_access() {
749        // Test that both direct access and sip dict access work together
750        let content = r#"---
751llm:
752  provider: openai
753---
754# Scene: main
755Direct: {{ simple_var }}
756SIP Header: {{ sip["X-Custom-Header"] }}
757SIP via Direct: {{ X_Custom_Header2 }}
758"#;
759        let mut variables = HashMap::new();
760        variables.insert("simple_var".to_string(), json!("direct_value"));
761        variables.insert("X-Custom-Header".to_string(), json!("header_value"));
762        variables.insert("X_Custom_Header2".to_string(), json!("header2_value"));
763        // Only X-Custom-Header is in extract_headers
764        variables.insert("_sip_header_keys".to_string(), json!(["X-Custom-Header"]));
765
766        let playbook = Playbook::parse(content)
767            .unwrap()
768            .render(&variables)
769            .unwrap();
770        let scene = playbook.scenes.get("main").unwrap();
771
772        assert!(scene.prompt.contains("Direct: direct_value"));
773        assert!(scene.prompt.contains("SIP Header: header_value"));
774        // X_Custom_Header2 doesn't start with X-, so won't be in sip dict
775        assert!(scene.prompt.contains("SIP via Direct: header2_value"));
776    }
777
778    #[test]
779    fn test_sip_dict_empty_context() {
780        // Test that sip dict works with no variables
781        let content = r#"---
782llm:
783  provider: openai
784---
785# Scene: main
786No variables here.
787"#;
788        let playbook = Playbook::parse(content).unwrap();
789        let scene = playbook.scenes.get("main").unwrap();
790        assert_eq!(scene.prompt, "No variables here.");
791    }
792
793    #[test]
794    fn test_sip_dict_case_insensitive() {
795        // Test that extract_headers can include headers with different cases
796        let content = r#"---
797llm:
798  provider: openai
799---
800# Scene: main
801Upper: {{ sip["X-Header-Upper"] }}
802Lower: {{ sip["x-header-lower"] }}
803"#;
804        let mut variables = HashMap::new();
805        variables.insert("X-Header-Upper".to_string(), json!("UPPER"));
806        variables.insert("x-header-lower".to_string(), json!("lower"));
807        variables.insert(
808            "_sip_header_keys".to_string(),
809            json!(["X-Header-Upper", "x-header-lower"]),
810        );
811
812        let playbook = Playbook::parse(content)
813            .unwrap()
814            .render(&variables)
815            .unwrap();
816        let scene = playbook.scenes.get("main").unwrap();
817
818        assert!(scene.prompt.contains("Upper: UPPER"));
819        assert!(scene.prompt.contains("Lower: lower"));
820    }
821
822    #[test]
823    fn test_env_vars_in_all_fields() {
824        // Test that ${VAR} works in all configuration fields
825        unsafe {
826            std::env::set_var("TEST_MODEL_ALL", "gpt-4o");
827            std::env::set_var("TEST_API_KEY_ALL", "sk-test-12345");
828            std::env::set_var("TEST_BASE_URL_ALL", "https://api.example.com");
829            std::env::set_var("TEST_SPEAKER_ALL", "F1");
830            std::env::set_var("TEST_LANGUAGE_ALL", "zh");
831            std::env::set_var("TEST_SPEED_ALL", "1.2");
832        }
833
834        let content = r#"---
835asr:
836  provider: "sensevoice"
837  language: "${TEST_LANGUAGE_ALL}"
838tts:
839  provider: "supertonic"
840  speaker: "${TEST_SPEAKER_ALL}"
841  speed: ${TEST_SPEED_ALL}
842llm:
843  provider: "openai"
844  model: "${TEST_MODEL_ALL}"
845  apiKey: "${TEST_API_KEY_ALL}"
846  baseUrl: "${TEST_BASE_URL_ALL}"
847---
848# Scene: main
849Test content
850"#;
851
852        let playbook = Playbook::parse(content).unwrap();
853
854        // Verify ASR fields
855        let asr = playbook.config.asr.unwrap();
856        assert_eq!(asr.language.unwrap(), "zh");
857
858        // Verify TTS fields
859        let tts = playbook.config.tts.unwrap();
860        assert_eq!(tts.speaker.unwrap(), "F1");
861        assert_eq!(tts.speed, Some(1.2));
862
863        // Verify LLM fields
864        let llm = playbook.config.llm.unwrap();
865        assert_eq!(llm.model.unwrap(), "gpt-4o");
866        assert_eq!(llm.api_key.unwrap(), "sk-test-12345");
867        assert_eq!(llm.base_url.unwrap(), "https://api.example.com");
868
869        unsafe {
870            std::env::remove_var("TEST_MODEL_ALL");
871            std::env::remove_var("TEST_API_KEY_ALL");
872            std::env::remove_var("TEST_BASE_URL_ALL");
873            std::env::remove_var("TEST_SPEAKER_ALL");
874            std::env::remove_var("TEST_LANGUAGE_ALL");
875            std::env::remove_var("TEST_SPEED_ALL");
876        }
877    }
878
879    #[test]
880    fn test_sip_dict_with_http_command() {
881        // Test that SIP headers work correctly in HTTP command URLs
882        let content = r#"---
883llm:
884  provider: openai
885---
886# Scene: main
887Querying API: <http url='https://api.example.com/customers/{{ sip["X-Customer-ID"] }}' method="GET" />
888"#;
889        let mut variables = HashMap::new();
890        variables.insert("X-Customer-ID".to_string(), json!("CUST12345"));
891        variables.insert("_sip_header_keys".to_string(), json!(["X-Customer-ID"]));
892
893        let playbook = Playbook::parse(content)
894            .unwrap()
895            .render(&variables)
896            .unwrap();
897        let scene = playbook.scenes.get("main").unwrap();
898
899        // The HTTP tag should be preserved in the prompt with the variable expanded
900        assert!(
901            scene
902                .prompt
903                .contains("https://api.example.com/customers/CUST12345")
904        );
905    }
906
907    #[test]
908    fn test_sip_dict_without_extract_config() {
909        // Test that sip dict is empty when no _sip_header_keys is present
910        let content = r#"---
911llm:
912  provider: openai
913---
914# Scene: main
915Regular var: {{ regular_var }}
916SIP dict should be empty.
917"#;
918        let mut variables = HashMap::new();
919        variables.insert("regular_var".to_string(), json!("regular_value"));
920        // No _sip_header_keys, so sip dict should be empty
921
922        let playbook = Playbook::parse(content)
923            .unwrap()
924            .render(&variables)
925            .unwrap();
926        let scene = playbook.scenes.get("main").unwrap();
927
928        assert!(scene.prompt.contains("Regular var: regular_value"));
929    }
930
931    #[test]
932    fn test_sip_dict_with_multiple_headers_in_yaml() {
933        // Test SIP headers used in YAML configuration section
934        let content = r#"---
935llm:
936  provider: openai
937  greeting: 'Welcome {{ sip["X-Customer-Name"] }}! Your ID is {{ sip["X-Customer-ID"] }}.'
938---
939# Scene: main
940How can I help you today?
941"#;
942        let mut variables = HashMap::new();
943        variables.insert("X-Customer-Name".to_string(), json!("Alice"));
944        variables.insert("X-Customer-ID".to_string(), json!("CUST789"));
945        variables.insert(
946            "_sip_header_keys".to_string(),
947            json!(["X-Customer-Name", "X-Customer-ID"]),
948        );
949
950        let playbook = Playbook::parse(content).unwrap();
951        let playbook = playbook.render(&variables).unwrap();
952
953        assert_eq!(
954            playbook.config.llm.as_ref().unwrap().greeting,
955            Some("Welcome Alice! Your ID is CUST789.".to_string())
956        );
957    }
958
959    #[test]
960    fn test_wrong_syntax_should_fail() {
961        // Test that using {{ X-Header }} (without sip dict) should fail
962        let content = r#"---
963llm:
964  provider: openai
965---
966# Scene: main
967This will fail: {{ X-Customer-ID }}
968"#;
969        let mut variables = HashMap::new();
970        variables.insert("X-Customer-ID".to_string(), json!("CUST123"));
971        variables.insert("_sip_header_keys".to_string(), json!(["X-Customer-ID"]));
972
973        // This should fail because X-Customer-ID is not in the direct context
974        // It's only in sip dict
975        let playbook = Playbook::parse(content).unwrap();
976        let result = playbook.render(&variables);
977        // Templates are not checked during parsing anymore
978        assert!(result.is_err());
979    }
980
981    #[test]
982    fn test_sip_dict_with_set_var() {
983        // Test that SIP headers work in set_var commands
984        let content = r#"---
985llm:
986  provider: openai
987---
988# Scene: main
989<set_var key="X-Call-Status" value="active" />
990Customer: {{ sip["X-Customer-ID"] }}
991Status set successfully.
992"#;
993        let mut variables = HashMap::new();
994        variables.insert("X-Customer-ID".to_string(), json!("CUST456"));
995        variables.insert("_sip_header_keys".to_string(), json!(["X-Customer-ID"]));
996
997        let playbook = Playbook::parse(content)
998            .unwrap()
999            .render(&variables)
1000            .unwrap();
1001        let scene = playbook.scenes.get("main").unwrap();
1002
1003        assert!(scene.prompt.contains("Customer: CUST456"));
1004        assert!(scene.prompt.contains("<set_var"));
1005    }
1006
1007    #[test]
1008    fn test_sip_dict_mixed_with_regular_vars_in_complex_scenario() {
1009        // Test complex scenario with both SIP headers and regular variables
1010        let content = r#"---
1011llm:
1012  provider: openai
1013  greeting: 'Hello {{ sip["X-Customer-Name"] }}, member level: {{ member_level }}'
1014---
1015# Scene: main
1016Your ID: {{ sip["X-Customer-ID"] }}
1017Your status: {{ account_status }}
1018Your priority: {{ sip["X-Priority"] }}
1019Order count: {{ order_count }}
1020"#;
1021        let mut variables = HashMap::new();
1022        // SIP headers
1023        variables.insert("X-Customer-Name".to_string(), json!("Bob"));
1024        variables.insert("X-Customer-ID".to_string(), json!("CUST999"));
1025        variables.insert("X-Priority".to_string(), json!("VIP"));
1026        // Regular variables
1027        variables.insert("member_level".to_string(), json!("Gold"));
1028        variables.insert("account_status".to_string(), json!("Active"));
1029        variables.insert("order_count".to_string(), json!(5));
1030        // Mark SIP headers
1031        variables.insert(
1032            "_sip_header_keys".to_string(),
1033            json!(["X-Customer-Name", "X-Customer-ID", "X-Priority"]),
1034        );
1035
1036        let playbook = Playbook::parse(content)
1037            .unwrap()
1038            .render(&variables)
1039            .unwrap();
1040
1041        // Check greeting has both types
1042        assert_eq!(
1043            playbook.config.llm.as_ref().unwrap().greeting,
1044            Some("Hello Bob, member level: Gold".to_string())
1045        );
1046
1047        // Check scene has all variables correctly rendered
1048        let scene = playbook.scenes.get("main").unwrap();
1049        assert!(scene.prompt.contains("Your ID: CUST999"));
1050        assert!(scene.prompt.contains("Your status: Active"));
1051        assert!(scene.prompt.contains("Your priority: VIP"));
1052        assert!(scene.prompt.contains("Order count: 5"));
1053    }
1054
1055    #[test]
1056    fn test_raw_prompt_preserved_after_render() {
1057        // Test that raw_prompt is preserved after rendering so it can be re-rendered later
1058        let content = r#"---
1059llm:
1060  provider: openai
1061---
1062# Scene: greeting
1063您好,{{ customer_name }}!您的意图是:{{ intent }}
1064# Scene: detail
1065客户意图:{{ intent }}
1066详细信息在此。
1067"#;
1068        let mut variables = HashMap::new();
1069        variables.insert("customer_name".to_string(), json!("张三"));
1070        variables.insert("intent".to_string(), json!("咨询"));
1071
1072        let playbook = Playbook::parse(content)
1073            .unwrap()
1074            .render(&variables)
1075            .unwrap();
1076
1077        // Verify rendered prompts
1078        let greeting = playbook.scenes.get("greeting").unwrap();
1079        assert!(greeting.prompt.contains("您好,张三"));
1080        assert!(greeting.prompt.contains("您的意图是:咨询"));
1081
1082        // Verify raw_prompt still has the template
1083        assert!(greeting.raw_prompt.is_some());
1084        let raw = greeting.raw_prompt.as_ref().unwrap();
1085        assert!(raw.contains("{{ customer_name }}"));
1086        assert!(raw.contains("{{ intent }}"));
1087
1088        // Verify detail scene too
1089        let detail = playbook.scenes.get("detail").unwrap();
1090        assert!(detail.raw_prompt.is_some());
1091        assert!(detail.raw_prompt.as_ref().unwrap().contains("{{ intent }}"));
1092    }
1093
1094    #[test]
1095    fn test_render_scene_prompt_with_dynamic_vars() {
1096        // Test render_scene_prompt: simulates set_var updating variables mid-conversation
1097        let scene = Scene {
1098            id: "main".to_string(),
1099            raw_prompt: Some("客户意图:{{ intent }}\n客户ID:{{ sip[\"X-Jobid\"] }}".to_string()),
1100            prompt: "客户意图:\n客户ID:JOB123".to_string(), // initially rendered
1101            ..Default::default()
1102        };
1103
1104        // Simulate variables after set_var has been called
1105        let mut vars = HashMap::new();
1106        vars.insert("intent".to_string(), json!("买零食"));
1107        vars.insert("X-Jobid".to_string(), json!("JOB123"));
1108        vars.insert("_sip_header_keys".to_string(), json!(["X-Jobid"]));
1109
1110        let rendered = render_scene_prompt(&scene, &vars);
1111        assert!(rendered.contains("客户意图:买零食"));
1112        assert!(rendered.contains("客户ID:JOB123"));
1113    }
1114
1115    #[test]
1116    fn test_render_scene_prompt_fallback_without_template() {
1117        // When raw_prompt has no template markers, should return prompt as-is
1118        let scene = Scene {
1119            id: "simple".to_string(),
1120            raw_prompt: Some("你好,欢迎光临".to_string()),
1121            prompt: "你好,欢迎光临".to_string(),
1122            ..Default::default()
1123        };
1124
1125        let vars = HashMap::new();
1126        let rendered = render_scene_prompt(&scene, &vars);
1127        assert_eq!(rendered, "你好,欢迎光临");
1128    }
1129
1130    #[test]
1131    fn test_render_scene_prompt_fallback_no_raw_prompt() {
1132        // When raw_prompt is None, should return prompt
1133        let scene = Scene {
1134            id: "legacy".to_string(),
1135            prompt: "Hello world".to_string(),
1136            ..Default::default()
1137        };
1138
1139        let vars = HashMap::new();
1140        let rendered = render_scene_prompt(&scene, &vars);
1141        assert_eq!(rendered, "Hello world");
1142    }
1143
1144    #[test]
1145    fn test_render_scene_prompt_with_builtin_vars() {
1146        // Test that built-in session variables work in scene prompts
1147        let scene = Scene {
1148            id: "main".to_string(),
1149            raw_prompt: Some(
1150                "会话ID:{{ session_id }}\n呼叫类型:{{ call_type }}\n主叫:{{ caller }}\n被叫:{{ callee }}\n开始时间:{{ start_time }}"
1151                    .to_string(),
1152            ),
1153            prompt: String::new(),
1154            ..Default::default()
1155        };
1156
1157        let mut vars = HashMap::new();
1158        vars.insert(BUILTIN_SESSION_ID.to_string(), json!("sess-12345"));
1159        vars.insert(BUILTIN_CALL_TYPE.to_string(), json!("sip"));
1160        vars.insert(BUILTIN_CALLER.to_string(), json!("sip:alice@example.com"));
1161        vars.insert(BUILTIN_CALLEE.to_string(), json!("sip:bob@example.com"));
1162        vars.insert(
1163            BUILTIN_START_TIME.to_string(),
1164            json!("2026-02-14T10:00:00Z"),
1165        );
1166
1167        let rendered = render_scene_prompt(&scene, &vars);
1168        assert!(rendered.contains("会话ID:sess-12345"));
1169        assert!(rendered.contains("呼叫类型:sip"));
1170        assert!(rendered.contains("主叫:sip:alice@example.com"));
1171        assert!(rendered.contains("被叫:sip:bob@example.com"));
1172        assert!(rendered.contains("开始时间:2026-02-14T10:00:00Z"));
1173    }
1174
1175    #[test]
1176    fn test_render_scene_prompt_mixed_sip_and_set_var() {
1177        // Test mixed SIP headers and set_var variables in dynamic rendering
1178        let scene = Scene {
1179            id: "main".to_string(),
1180            raw_prompt: Some(
1181                "客户:{{ sip[\"X-Customer-Name\"] }}\n意图:{{ intent }}\n会话:{{ session_id }}"
1182                    .to_string(),
1183            ),
1184            prompt: String::new(),
1185            ..Default::default()
1186        };
1187
1188        let mut vars = HashMap::new();
1189        // SIP header
1190        vars.insert("X-Customer-Name".to_string(), json!("王五"));
1191        vars.insert("_sip_header_keys".to_string(), json!(["X-Customer-Name"]));
1192        // set_var variable
1193        vars.insert("intent".to_string(), json!("退货"));
1194        // Built-in variable
1195        vars.insert(BUILTIN_SESSION_ID.to_string(), json!("sess-99"));
1196
1197        let rendered = render_scene_prompt(&scene, &vars);
1198        assert!(rendered.contains("客户:王五"));
1199        assert!(rendered.contains("意图:退货"));
1200        assert!(rendered.contains("会话:sess-99"));
1201    }
1202
1203    #[test]
1204    fn test_render_scene_prompt_graceful_on_missing_vars() {
1205        // When a referenced variable is missing, MiniJinja renders it as empty string
1206        // The render still succeeds but with empty values
1207        let scene = Scene {
1208            id: "main".to_string(),
1209            raw_prompt: Some("意图:{{ intent }}".to_string()),
1210            prompt: "意图:(未知)".to_string(), // fallback (not used since rendering succeeds)
1211            ..Default::default()
1212        };
1213
1214        let vars = HashMap::new(); // no intent variable
1215        let rendered = render_scene_prompt(&scene, &vars);
1216        // MiniJinja renders missing vars as empty string
1217        assert_eq!(rendered, "意图:");
1218    }
1219
1220    #[test]
1221    fn test_raw_prompt_set_on_parse() {
1222        // Verify that raw_prompt is set during initial parsing
1223        let content = r#"---
1224llm:
1225  provider: openai
1226---
1227# Scene: main
1228Hello {{ name }}!
1229"#;
1230        let playbook = Playbook::parse(content).unwrap();
1231        let scene = playbook.scenes.get("main").unwrap();
1232        assert!(scene.raw_prompt.is_some());
1233        assert!(scene.raw_prompt.as_ref().unwrap().contains("{{ name }}"));
1234    }
1235
1236    #[test]
1237    fn test_builtin_var_constants() {
1238        // Verify the built-in variable constant values
1239        assert_eq!(BUILTIN_SESSION_ID, "session_id");
1240        assert_eq!(BUILTIN_CALL_TYPE, "call_type");
1241        assert_eq!(BUILTIN_CALLER, "caller");
1242        assert_eq!(BUILTIN_CALLEE, "callee");
1243        assert_eq!(BUILTIN_START_TIME, "start_time");
1244    }
1245}