Skip to main content

active_call/handler/
handler.rs

1use crate::media::cache;
2use crate::synthesis::{SynthesisEvent, SynthesisOption};
3use crate::{
4    app::AppState,
5    call::{
6        ActiveCall, ActiveCallType, Command,
7        active_call::{ActiveCallGuard, CallParams, CallSpec},
8    },
9    handler::playbook,
10    playbook::{Playbook, PlaybookRunner},
11};
12use crate::{event::SessionEvent, media::track::TrackConfig};
13use axum::{
14    Json, Router,
15    extract::{Path, Query, State, WebSocketUpgrade, ws::Message},
16    response::sse::{Event, KeepAlive, Sse},
17    response::{IntoResponse, Response},
18    routing::{get, post},
19};
20use bytes::Bytes;
21use chrono::Utc;
22use futures::{SinkExt, StreamExt};
23use rustrtc::IceServer;
24use serde_json::json;
25use std::collections::HashMap;
26use std::{path::PathBuf, sync::Arc, time::Duration};
27use tokio::{join, select};
28use tokio_util::sync::CancellationToken;
29use tracing::{debug, info, trace, warn};
30use uuid::Uuid;
31
32fn filter_headers(
33    extras: &mut std::collections::HashMap<String, serde_json::Value>,
34    allowed_headers: &[String],
35) {
36    extras.retain(|k, _| allowed_headers.iter().any(|h| h.eq_ignore_ascii_case(k)));
37}
38
39pub fn call_router() -> Router<AppState> {
40    let r = Router::new()
41        .route("/call", get(ws_handler))
42        .route("/call/webrtc", get(webrtc_handler))
43        .route("/call/sip", get(sip_handler))
44        .route("/list", get(list_active_calls))
45        .route("/kill/{id}", get(kill_active_call))
46        .route("/events/{id}", get(stream_events))
47        .route("/command/{id}", post(send_command))
48        .route("/precache", post(precache));
49    r
50}
51
52pub fn iceservers_router() -> Router<AppState> {
53    let r = Router::new();
54    r.route("/iceservers", get(get_iceservers))
55}
56
57pub fn playbook_router() -> Router<AppState> {
58    Router::new()
59        .route("/api/playbooks", get(playbook::list_playbooks))
60        .route(
61            "/api/playbooks/{name}",
62            get(playbook::get_playbook).post(playbook::save_playbook),
63        )
64        .route(
65            "/api/playbook/run",
66            axum::routing::post(playbook::run_playbook),
67        )
68        .route("/api/records", get(playbook::list_records))
69}
70
71pub async fn ws_handler(
72    ws: WebSocketUpgrade,
73    State(state): State<AppState>,
74    Query(params): Query<CallParams>,
75) -> Response {
76    call_handler(ActiveCallType::WebSocket, ws, state, params).await
77}
78
79pub async fn sip_handler(
80    ws: WebSocketUpgrade,
81    State(state): State<AppState>,
82    Query(params): Query<CallParams>,
83) -> Response {
84    call_handler(ActiveCallType::Sip, ws, state, params).await
85}
86
87pub async fn webrtc_handler(
88    ws: WebSocketUpgrade,
89    State(state): State<AppState>,
90    Query(params): Query<CallParams>,
91) -> Response {
92    call_handler(ActiveCallType::Webrtc, ws, state, params).await
93}
94
95/// Core call handling logic that works with either WebSocket or mpsc channels
96///
97/// `extras` and `playbook_name` are session-scoped parameters passed directly
98/// by the caller (SIP handler, CLI, etc.) instead of through global maps.
99/// Returns the final call extras (including `_hangup_headers` if set) so the
100/// caller can use them for SIP BYE or other post-call processing.
101pub async fn call_handler_core(
102    call_type: ActiveCallType,
103    session_id: String,
104    app_state: AppState,
105    cancel_token: CancellationToken,
106    audio_receiver: tokio::sync::mpsc::UnboundedReceiver<Bytes>,
107    server_side_track: Option<String>,
108    dump_events: bool,
109    ping_interval: u64,
110    mut command_receiver: tokio::sync::mpsc::UnboundedReceiver<Command>,
111    event_sender_to_client: tokio::sync::mpsc::UnboundedSender<crate::event::SessionEvent>,
112    extras: Option<HashMap<String, serde_json::Value>>,
113    playbook_name: Option<String>,
114) -> Option<HashMap<String, serde_json::Value>> {
115    let _cancel_guard = cancel_token.clone().drop_guard();
116    let track_config = TrackConfig::default();
117
118    let active_call = Arc::new(ActiveCall::new(CallSpec {
119        call_type: call_type.clone(),
120        cancel_token: cancel_token.clone(),
121        session_id: session_id.clone(),
122        invitation: app_state.invitation.clone(),
123        app_state: app_state.clone(),
124        track_config,
125        audio_receiver: Some(audio_receiver),
126        dump_events,
127        server_side_track_id: server_side_track.clone(),
128        extras,
129    }));
130
131    // Load playbook: prefer direct parameter, fall back to pending_playbooks
132    // (pending_playbooks is used by the run_playbook HTTP endpoint)
133    {
134        let name_or_content = playbook_name.or_else(|| {
135            app_state
136                .pending_playbooks
137                .try_lock()
138                .ok()
139                .and_then(|mut pending| pending.remove(&session_id).map(|(val, _)| val))
140        });
141        if let Some(name_or_content) = name_or_content {
142            let playbook_result = if name_or_content.trim().starts_with("---") {
143                Playbook::parse(&name_or_content)
144            } else {
145                // If path already contains config/playbook, use it as-is; otherwise prepend it
146                let path = if name_or_content.starts_with("config/playbook/") {
147                    PathBuf::from(&name_or_content)
148                } else {
149                    PathBuf::from("config/playbook").join(&name_or_content)
150                };
151                Playbook::load(path).await
152            };
153
154            match playbook_result {
155                Ok(mut playbook) => {
156                    // Filter extracted headers if configured (only for SIP calls)
157                    if call_type == ActiveCallType::Sip {
158                        if let Some(sip_config) = &playbook.config.sip {
159                            if let Some(allowed_headers) = &sip_config.extract_headers {
160                                let extras = active_call.extras.rcu(|e| {
161                                    let mut e = HashMap::clone(e);
162                                    filter_headers(&mut e, allowed_headers);
163                                    e
164                                });
165                                // Store the list of SIP header keys for later template rendering
166                                let header_keys: Vec<String> = extras
167                                    .keys()
168                                    .filter(|k| !k.starts_with('_'))
169                                    .cloned()
170                                    .collect();
171                                active_call.set_extra(
172                                    "_sip_header_keys",
173                                    serde_json::to_value(&header_keys).unwrap_or_default(),
174                                );
175                                if let Ok(result) = playbook.render(&active_call.extras.load_full())
176                                {
177                                    playbook = result;
178                                }
179                            }
180                        }
181                    }
182
183                    match PlaybookRunner::new(playbook, active_call.clone()) {
184                        Ok(runner) => {
185                            crate::spawn(async move {
186                                runner.run().await;
187                            });
188                            let display_name = if name_or_content.trim().starts_with("---") {
189                                "custom content"
190                            } else {
191                                &name_or_content
192                            };
193                            info!(session_id, "Playbook runner started for {}", display_name);
194                        }
195                        Err(e) => {
196                            let display_name = if name_or_content.trim().starts_with("---") {
197                                "custom content"
198                            } else {
199                                &name_or_content
200                            };
201                            warn!(
202                                session_id,
203                                "Failed to create runner {}: {}", display_name, e
204                            )
205                        }
206                    }
207                }
208                Err(e) => {
209                    let display_name = if name_or_content.trim().starts_with("---") {
210                        "custom content"
211                    } else {
212                        &name_or_content
213                    };
214                    warn!(
215                        session_id,
216                        "Failed to load playbook {}: {}", display_name, e
217                    );
218                    let event = SessionEvent::Error {
219                        timestamp: crate::media::get_timestamp(),
220                        track_id: session_id.clone(),
221                        sender: "playbook".to_string(),
222                        error: format!("{}", e),
223                        code: None,
224                    };
225                    event_sender_to_client.send(event).ok();
226                    return None;
227                }
228            }
229        }
230    }
231
232    let recv_commands_loop = async {
233        while let Some(command) = command_receiver.recv().await {
234            if let Err(_) = active_call.enqueue_command(command).await {
235                break;
236            }
237        }
238    };
239
240    let mut event_receiver = active_call.event_sender.subscribe();
241    let send_events_loop = async {
242        loop {
243            match event_receiver.recv().await {
244                Ok(event) => {
245                    if let Err(_) = event_sender_to_client.send(event) {
246                        break;
247                    }
248                }
249                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
250                Err(_) => break,
251            }
252        }
253    };
254
255    let send_ping_loop = async {
256        if ping_interval == 0 {
257            active_call.cancel_token.cancelled().await;
258            return;
259        }
260        let mut ticker = tokio::time::interval(Duration::from_secs(ping_interval));
261        loop {
262            ticker.tick().await;
263            let payload = Utc::now().to_rfc3339();
264            let event = SessionEvent::Ping {
265                timestamp: crate::media::get_timestamp(),
266                payload: Some(payload),
267            };
268            if let Err(_) = active_call.event_sender.send(event) {
269                break;
270            }
271        }
272    };
273
274    let guard = ActiveCallGuard::new(active_call.clone());
275    info!(
276        session_id,
277        active_calls = guard.active_calls,
278        ?call_type,
279        "new call started"
280    );
281    let receiver = active_call.new_receiver();
282
283    let (r, _) = join! {
284        active_call.clone().serve(receiver),
285        async {
286            select!{
287                _ = send_ping_loop => {},
288                _ = cancel_token.cancelled() => {},
289                _ = send_events_loop => { },
290                _ = recv_commands_loop => {
291                    info!(session_id, "Command receiver closed");
292                },
293            }
294            cancel_token.cancel();
295        }
296    };
297    // drain events
298    while let Ok(event) = event_receiver.try_recv() {
299        if let Err(_) = event_sender_to_client.send(event) {
300            break;
301        }
302    }
303    match r {
304        Ok(_) => info!(session_id, "call ended successfully"),
305        Err(e) => warn!(session_id, "call ended with error: {}", e),
306    }
307
308    // Capture final extras (including _hangup_headers) before cleanup
309    let final_extras = Some(active_call.extras.load_full().as_ref().clone());
310
311    active_call.cleanup().await.ok();
312    debug!(session_id, "Call handler core completed");
313
314    final_extras
315}
316
317pub async fn call_handler(
318    call_type: ActiveCallType,
319    ws: WebSocketUpgrade,
320    app_state: AppState,
321    params: CallParams,
322) -> Response {
323    let has_explicit_id = params.id.is_some();
324    let session_id = params
325        .id
326        .clone()
327        .unwrap_or_else(|| format!("s.{}", Uuid::new_v4().to_string()));
328    let server_side_track = params.server_side_track.clone();
329    let dump_events = params.dump_events.unwrap_or(true);
330    let ping_interval = params.ping_interval.unwrap_or(20);
331
332    // Only an empty `forward` may hop to peers. Any present value is local-only.
333    // `forward=true` probes 404 if the call is absent so the originator can try
334    // the next peer instead of creating a new call.
335    //
336    // A ringing inbound SIP call only exists as a pending dialog (registered by
337    // the INVITE handler), not yet in `active_calls` — the ActiveCall is
338    // created by the websocket attach below. Treat pending dialogs as hosted
339    // locally so the attach neither probes peers nor 404s (same predicate the
340    // ActiveCall itself uses to resolve its SIP dialog).
341    if has_explicit_id {
342        let found_locally = {
343            let active_calls = app_state.active_calls.lock().unwrap();
344            active_calls.contains_key(&session_id)
345        } || app_state
346            .invitation
347            .find_dialog_id_by_session_id(&session_id)
348            .is_some();
349        if !found_locally {
350            if params.forward.is_none() {
351                if let Some(peer_ws) =
352                    crate::handler::peer::try_forward(&app_state, &session_id, &params).await
353                {
354                    info!(session_id, "forwarding websocket to peer hosting the call");
355                    return ws.on_upgrade(move |socket| async move {
356                        crate::handler::peer::tunnel(socket, peer_ws).await;
357                    });
358                }
359            }
360            if params.forward == Some(true) {
361                warn!(
362                    session_id,
363                    "call not found on this node, rejecting forwarded request"
364                );
365                return (
366                    axum::http::StatusCode::NOT_FOUND,
367                    "call not found on this node",
368                )
369                    .into_response();
370            }
371        }
372    }
373
374    let resp = ws.on_upgrade(move |socket| async move {
375        let (mut ws_sender, mut ws_receiver) = socket.split();
376        let (audio_sender, audio_receiver) = tokio::sync::mpsc::unbounded_channel::<Bytes>();
377        let (command_sender, command_receiver) = tokio::sync::mpsc::unbounded_channel::<Command>();
378        let (event_sender_to_client, mut event_receiver_from_core) =
379            tokio::sync::mpsc::unbounded_channel::<crate::event::SessionEvent>();
380        let cancel_token = CancellationToken::new();
381
382        // Start core handler in background
383        let session_id_clone = session_id.clone();
384        let app_state_clone = app_state.clone();
385        let cancel_token_clone = cancel_token.clone();
386        crate::spawn(async move {
387            call_handler_core(
388                call_type,
389                session_id_clone,
390                app_state_clone,
391                cancel_token_clone,
392                audio_receiver,
393                server_side_track,
394                dump_events,
395                ping_interval.into(),
396                command_receiver,
397                event_sender_to_client,
398                None, // extras — not used for WebSocket calls
399                None, // playbook_name — falls back to pending_playbooks
400            )
401            .await;
402        });
403
404        // Handle WebSocket I/O
405        let recv_from_ws_loop = async {
406            while let Some(Ok(message)) = ws_receiver.next().await {
407                match message {
408                    Message::Text(text) => {
409                        let command = match serde_json::from_str::<Command>(&text) {
410                            Ok(cmd) => cmd,
411                            Err(e) => {
412                                warn!(session_id, %text, "Failed to parse command {}", e);
413                                continue;
414                            }
415                        };
416                        if let Err(_) = command_sender.send(command) {
417                            break;
418                        }
419                    }
420                    Message::Binary(bin) => {
421                        audio_sender.send(bin.into()).ok();
422                    }
423                    Message::Close(_) => {
424                        info!(session_id, "WebSocket closed by client");
425                        break;
426                    }
427                    _ => {}
428                }
429            }
430        };
431
432        let send_to_ws_loop = async {
433            while let Some(event) = event_receiver_from_core.recv().await {
434                trace!(session_id, %event, "Sending WS message");
435                let message = match event.into_ws_message() {
436                    Ok(msg) => msg,
437                    Err(e) => {
438                        warn!(session_id, error=%e, "Failed to serialize event to WS message");
439                        continue;
440                    }
441                };
442                if let Err(_) = ws_sender.send(message).await {
443                    info!(session_id, "WebSocket send failed, closing");
444                    break;
445                }
446            }
447        };
448
449        select! {
450            _ = recv_from_ws_loop => {
451                info!(session_id, "WebSocket receive loop ended");
452            },
453            _ = send_to_ws_loop => {
454                info!(session_id, "WebSocket send loop ended");
455            },
456        }
457
458        // drain events queued after the losing side of select! above
459        while let Ok(event) = event_receiver_from_core.try_recv() {
460            if let Ok(message) = event.into_ws_message() {
461                if ws_sender.send(message).await.is_err() {
462                    break;
463                }
464            }
465        }
466
467        cancel_token.cancel();
468        ws_sender.flush().await.ok();
469        ws_sender.close().await.ok();
470        debug!(session_id, "WebSocket connection closed");
471    });
472    resp
473}
474
475pub(crate) async fn get_iceservers(State(state): State<AppState>) -> Response {
476    if let Some(ice_servers) = state.config.ice_servers.as_ref() {
477        return Json(ice_servers).into_response();
478    }
479    Json(vec![IceServer {
480        urls: vec!["stun:stun.l.google.com:19302".to_string()],
481        ..Default::default()
482    }])
483    .into_response()
484}
485
486pub(crate) async fn list_active_calls(State(state): State<AppState>) -> Response {
487    // Clone the call handles out of the registry lock first, then read each
488    // call's lock-free progress snapshot without holding the registry lock.
489    let calls = state
490        .active_calls
491        .lock()
492        .unwrap()
493        .iter()
494        .map(|(_, c)| c.clone())
495        .collect::<Vec<_>>();
496    let list = calls
497        .iter()
498        .map(|c| {
499            let progress = c.progress.load_full();
500            json!({
501                "id": c.session_id,
502                "callType": c.call_type,
503                "cs.option": progress.option,
504                "ringTime": progress.ring_time,
505                "startTime": progress.answer_time,
506            })
507        })
508        .collect::<Vec<_>>();
509    Json(serde_json::json!({ "active_calls": list })).into_response()
510}
511
512pub(crate) async fn kill_active_call(
513    Path(id): Path<String>,
514    State(state): State<AppState>,
515) -> Response {
516    let active_calls = state.active_calls.lock().unwrap();
517    if let Some(call) = active_calls.get(&id) {
518        call.cancel_token.cancel();
519        Json(serde_json::json!({ "status": "killed", "id": id })).into_response()
520    } else {
521        (
522            axum::http::StatusCode::NOT_FOUND,
523            Json(serde_json::json!({ "status": "not_found", "id": id })),
524        )
525            .into_response()
526    }
527}
528
529pub(crate) async fn stream_events(
530    Path(id): Path<String>,
531    State(state): State<AppState>,
532) -> Response {
533    let mut rx_events;
534    let mut rx_commands;
535    {
536        let active_calls = state.active_calls.lock().unwrap();
537        if let Some(call) = active_calls.get(&id) {
538            rx_events = call.event_sender.subscribe();
539            rx_commands = call.cmd_sender.subscribe();
540        } else {
541            return (axum::http::StatusCode::NOT_FOUND, "track not active").into_response();
542        }
543    }
544
545    let stream = async_stream::stream! {
546        loop {
547            let result = tokio::select! {
548                r = rx_events.recv() => r.map(|e| serde_json::to_string(&e).map(|json| Event::default().event("event").data(json))),
549                r = rx_commands.recv() => r.map(|c| serde_json::to_string(&c).map(|json| Event::default().event("command").data(json))),
550            };
551            match result {
552                Ok(Ok(sse_event)) => yield Ok::<Event, serde_json::Error>(sse_event),
553                Ok(Err(e)) => yield Err(e.into()),
554                Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
555                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
556            }
557        }
558    };
559
560    let mut response = Sse::new(stream)
561        .keep_alive(KeepAlive::default())
562        .into_response();
563    response.headers_mut().insert(
564        axum::http::header::CONTENT_TYPE,
565        "text/event-stream;charset=utf-8".parse().unwrap(),
566    );
567    response
568}
569
570pub(crate) async fn send_command(
571    Path(id): Path<String>,
572    State(state): State<AppState>,
573    Json(command): Json<Command>,
574) -> Response {
575    let active_calls = state.active_calls.lock().unwrap();
576    if let Some(call) = active_calls.get(&id) {
577        if let Ok(_) = call.cmd_sender.send(command) {
578            return Json(serde_json::json!({ "status": "sent", "id": id })).into_response();
579        }
580    }
581
582    (
583        axum::http::StatusCode::NOT_FOUND,
584        Json(serde_json::json!({ "status": "not_found", "id": id })),
585    )
586        .into_response()
587}
588
589/// Pre-generate and cache TTS audio without an active call.
590///
591/// Accepts a `Tts` command (same shape as `/command/{id}`): it synthesizes the
592/// text and stores the audio under the exact cache key the real `do_tts` would
593/// use. A later `Tts` with the same parameters then hits the cache instead of
594/// regenerating.
595///
596/// NOTE: the cache key is derived from the command parameters. For a cache hit
597/// the real command must use the same effective `option` (provider, samplerate,
598/// speaker, speed) — or pass an explicit `cacheKey`.
599pub(crate) async fn precache(
600    State(state): State<AppState>,
601    Json(command): Json<Command>,
602) -> Response {
603    let result = match command {
604        Command::Tts {
605            text,
606            speaker,
607            option,
608            cache_key,
609            ..
610        } => precache_tts(&state, text, speaker, option, cache_key).await,
611        _ => {
612            return (
613                axum::http::StatusCode::BAD_REQUEST,
614                Json(json!({ "status": "error", "error": "precache only accepts tts commands" })),
615            )
616                .into_response();
617        }
618    };
619
620    match result {
621        Ok((cache_key, bytes, existed)) => Json(json!({
622            "status": if existed { "exists" } else { "cached" },
623            "cacheKey": cache_key,
624            "bytes": bytes,
625        }))
626        .into_response(),
627        Err(e) => (
628            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
629            Json(json!({ "status": "error", "error": e.to_string() })),
630        )
631            .into_response(),
632    }
633}
634
635/// Synthesize `text` and store the audio under the same cache key `do_tts` uses.
636/// Returns (cache_key, bytes_stored, already_existed).
637async fn precache_tts(
638    state: &AppState,
639    text: String,
640    speaker: Option<String>,
641    option: Option<SynthesisOption>,
642    cache_key: Option<String>,
643) -> anyhow::Result<(String, usize, bool)> {
644    let mut opt = option.ok_or_else(|| anyhow::anyhow!("tts precache requires an option"))?;
645    // Fold the top-level speaker into the option (same precedence as do_tts)
646    opt.speaker = speaker.or(opt.speaker);
647    opt.check_default();
648
649    let mut client = state.stream_engine.create_tts_client(false, &opt).await?;
650    // Cache key must match tts.rs::handle_cache exactly.
651    let sample_rate = opt.samplerate.unwrap_or(16000) as u32;
652    let cache_key = cache_key.unwrap_or_else(|| {
653        cache::generate_cache_key(
654            &format!("tts:{}{}", client.provider(), text),
655            sample_rate,
656            opt.speaker.as_ref(),
657            opt.speed,
658        )
659    });
660    if cache::is_cached(&cache_key).await.unwrap_or(false) {
661        return Ok((cache_key, 0, true));
662    }
663
664    let mut stream = client.start().await?;
665    client.synthesize(&text, Some(0), Some(opt.clone())).await?;
666    client.stop().await?;
667
668    let mut chunks: Vec<Bytes> = Vec::new();
669    let mut first_chunk = true;
670    while let Some((_seq, res)) = stream.next().await {
671        match res? {
672            SynthesisEvent::AudioChunk(mut chunk) => {
673                // Strip the 44-byte WAV header off the first chunk (same as tts.rs).
674                if first_chunk {
675                    if chunk.len() > 44 && chunk[..4] == [0x52, 0x49, 0x46, 0x46] {
676                        let _ = chunk.split_to(44);
677                    }
678                    first_chunk = false;
679                }
680                chunks.push(chunk);
681            }
682            SynthesisEvent::Finished => break,
683            _ => {}
684        }
685    }
686    if chunks.is_empty() {
687        return Err(anyhow::anyhow!("tts produced no audio"));
688    }
689    let bytes: usize = chunks.iter().map(|c| c.len()).sum();
690    cache::store_in_cache_vectored(&cache_key, &chunks).await?;
691    info!(cache_key = %cache_key, bytes, "precache: stored tts audio");
692    Ok((cache_key, bytes, false))
693}
694
695trait IntoWsMessage {
696    fn into_ws_message(self) -> Result<Message, serde_json::Error>;
697}
698
699impl IntoWsMessage for crate::event::SessionEvent {
700    fn into_ws_message(self) -> Result<Message, serde_json::Error> {
701        match self {
702            SessionEvent::Binary { data, .. } => Ok(Message::Binary(data.into())),
703            SessionEvent::Ping { timestamp, payload } => {
704                let payload = payload.unwrap_or_else(|| timestamp.to_string());
705                Ok(Message::Ping(payload.into()))
706            }
707            event => serde_json::to_string(&event).map(|payload| Message::Text(payload.into())),
708        }
709    }
710}
711
712#[cfg(test)]
713mod tests {
714    use super::*;
715    use serde_json::json;
716    use std::collections::HashMap;
717
718    #[test]
719    fn test_filter_headers() {
720        let mut extras = HashMap::new();
721        extras.insert("X-Tenant-ID".to_string(), json!("123"));
722        extras.insert("X-User-ID".to_string(), json!("456"));
723        extras.insert("Custom-Header".to_string(), json!("abc"));
724        extras.insert("Irrelevant-Header".to_string(), json!("ignore"));
725
726        // Test case-insensitive matching
727        let allowed = vec!["x-tenant-id".to_string(), "Custom-Header".to_string()];
728
729        filter_headers(&mut extras, &allowed);
730
731        assert!(extras.contains_key("X-Tenant-ID"));
732        assert!(extras.contains_key("Custom-Header"));
733        assert!(!extras.contains_key("X-User-ID"));
734        assert!(!extras.contains_key("Irrelevant-Header"));
735
736        // ensure values are preserved
737        assert_eq!(extras.get("X-Tenant-ID").unwrap(), &json!("123"));
738        assert_eq!(extras.get("Custom-Header").unwrap(), &json!("abc"));
739    }
740
741    #[tokio::test]
742    async fn test_call_handler_core_extras_are_session_scoped() {
743        use crate::app::AppStateBuilder;
744        use crate::call::{ActiveCallType, Command};
745        use crate::config::Config;
746
747        let mut config = Config::default();
748        config.udp_port = 0;
749        let app_state = AppStateBuilder::new()
750            .with_config(config)
751            .build()
752            .await
753            .expect("Failed to build app state");
754
755        let session_id = "test-session-scoped".to_string();
756        let cancel_token = CancellationToken::new();
757
758        // Pass extras directly as a parameter (not via global map)
759        let mut extras = HashMap::new();
760        extras.insert("X-Custom".to_string(), json!("value"));
761
762        let (_audio_sender, audio_receiver) = tokio::sync::mpsc::unbounded_channel::<Bytes>();
763        let (command_sender, command_receiver) = tokio::sync::mpsc::unbounded_channel::<Command>();
764        let (event_sender, _event_receiver) =
765            tokio::sync::mpsc::unbounded_channel::<crate::event::SessionEvent>();
766
767        // Send a Hangup command immediately to end the call
768        command_sender
769            .send(Command::Hangup {
770                reason: None,
771                initiator: None,
772                headers: None,
773                refer: None,
774            })
775            .ok();
776        drop(command_sender);
777
778        // Run call_handler_core with extras passed directly
779        let final_extras = call_handler_core(
780            ActiveCallType::Sip,
781            session_id.clone(),
782            app_state.clone(),
783            cancel_token,
784            audio_receiver,
785            None,
786            false,
787            0,
788            command_receiver,
789            event_sender,
790            Some(extras), // extras passed directly
791            None,         // no playbook
792        )
793        .await;
794
795        // Verify that final extras are returned and contain our custom header
796        assert!(final_extras.is_some(), "final extras should be returned");
797        let extras = final_extras.unwrap();
798        assert_eq!(
799            extras.get("X-Custom"),
800            Some(&json!("value")),
801            "session-scoped extras should be preserved"
802        );
803    }
804}