flowcat-core 0.1.0

Self-hosted native-Rust real-time voice-agent runtime — one binary, no Python or softswitch: transports, serializers, codec, and a speech-to-speech (Gemini Live) pipeline.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// SPDX-License-Identifier: Apache-2.0
//
//! Turn start/stop/mute + interruption strategies (pure framework logic).
//!
//! These mirror pipecat's `pipecat/turns/user_start`, `user_stop`, `user_mute`
//! and the interruption-strategy family. They are **pure, synchronous decision
//! functions** with no heavy dep, no I/O, and no `async`: each consumes the
//! lifecycle frames a [`VadProcessor`](crate::audio::vad)/`TurnProcessor` already
//! emits ([`Frame::VadUserStartedSpeaking`], [`Frame::Transcription`],
//! [`Frame::BotStartedSpeaking`], …) and returns a decision. The owning processor
//! (the turn controller in the cascaded path) turns those decisions into
//! [`Frame::UserStartedSpeaking`]/[`Frame::UserStoppedSpeaking`]/[`Frame::SttMute`]
//! /[`Frame::Interruption`] frames.
//!
//! Keeping them pure (vs. baked into the processor) means they are exhaustively
//! unit-testable and composable — exactly pipecat's split of "strategy" vs
//! "processor". The `*_secs`/timeout-driven branches are modeled as **explicit
//! tick inputs** ([`TurnStopStrategy::on_silence_tick`]) instead of `asyncio`
//! timers, so the logic is deterministic and clock-free; the processor feeds
//! wall-clock ticks.

use crate::processor::frame::Frame;

/// What a turn-*start* strategy decides for the frame it just saw. Mirrors
/// pipecat's `ProcessFrameResult` (`turns/types.py`): `Triggered` ⇒ the user turn
/// has started (emit `UserStartedSpeaking` + interrupt the bot); `Continue` ⇒
/// keep evaluating later strategies / frames; `ResetAggregation` ⇒ a partial
/// transcript fell below the word floor, drop the accumulated text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TurnStartDecision {
    /// The user turn has started.
    Triggered,
    /// No decision yet — keep going.
    Continue,
    /// Discard the in-progress user aggregation (a sub-threshold interim).
    ResetAggregation,
}

/// A user-turn **start** strategy: decides when the user has begun a turn.
/// Mirrors pipecat `BaseUserTurnStartStrategy`. Pure + synchronous.
pub trait TurnStartStrategy: Send {
    /// Inspect one frame and decide. Stateful strategies mutate `self`.
    fn on_frame(&mut self, frame: &Frame) -> TurnStartDecision;
    /// Reset to the initial state at the start of a new turn.
    fn reset(&mut self);
}

/// Start the turn the instant VAD says the user started speaking. Mirrors
/// `VADUserTurnStartStrategy`. The simplest, lowest-latency strategy — the
/// default for the cascaded path.
#[derive(Debug, Default, Clone)]
pub struct VadTurnStart;

impl TurnStartStrategy for VadTurnStart {
    fn on_frame(&mut self, frame: &Frame) -> TurnStartDecision {
        match frame {
            Frame::VadUserStartedSpeaking { .. } => TurnStartDecision::Triggered,
            _ => TurnStartDecision::Continue,
        }
    }
    fn reset(&mut self) {}
}

/// Start the turn once the user has spoken at least `min_words` (counted from
/// transcription frames). While the bot is speaking the full `min_words` floor
/// applies (avoid spurious barge-in on a single back-channel word); once the bot
/// is silent a **single** word triggers. Mirrors `MinWordsUserTurnStartStrategy`.
#[derive(Debug, Clone)]
pub struct MinWordsTurnStart {
    min_words: usize,
    use_interim: bool,
    bot_speaking: bool,
}

impl MinWordsTurnStart {
    /// `min_words` while the bot is speaking; `use_interim` counts interim
    /// transcripts too (earlier, noisier detection).
    pub fn new(min_words: usize, use_interim: bool) -> Self {
        Self {
            min_words,
            use_interim,
            bot_speaking: false,
        }
    }

    fn handle_transcription(&self, text: &str) -> TurnStartDecision {
        // When the bot is silent a single word is enough to start a turn.
        let floor = if self.bot_speaking {
            self.min_words.max(1)
        } else {
            1
        };
        let words = text.split_whitespace().count();
        if words >= floor {
            TurnStartDecision::Triggered
        } else {
            TurnStartDecision::ResetAggregation
        }
    }
}

impl TurnStartStrategy for MinWordsTurnStart {
    fn on_frame(&mut self, frame: &Frame) -> TurnStartDecision {
        match frame {
            Frame::BotStartedSpeaking => {
                self.bot_speaking = true;
                TurnStartDecision::Continue
            }
            Frame::BotStoppedSpeaking => {
                self.bot_speaking = false;
                TurnStartDecision::Continue
            }
            Frame::Transcription { text, .. } => self.handle_transcription(text),
            Frame::InterimTranscription { text, .. } if self.use_interim => {
                self.handle_transcription(text)
            }
            _ => TurnStartDecision::Continue,
        }
    }
    fn reset(&mut self) {
        self.bot_speaking = false;
    }
}

/// A turn start driven entirely by an out-of-band signal (e.g. a client
/// "push-to-talk" message) — VAD/transcription frames never trigger it. Mirrors
/// `ExternalUserTurnStartStrategy`. The processor calls [`Self::trigger`] when the
/// external signal arrives.
#[derive(Debug, Default, Clone)]
pub struct ExternalTurnStart {
    triggered: bool,
}

impl ExternalTurnStart {
    /// Arm the external trigger; the next `on_frame` reports `Triggered` once.
    pub fn trigger(&mut self) {
        self.triggered = true;
    }
}

impl TurnStartStrategy for ExternalTurnStart {
    fn on_frame(&mut self, _frame: &Frame) -> TurnStartDecision {
        if self.triggered {
            self.triggered = false;
            TurnStartDecision::Triggered
        } else {
            TurnStartDecision::Continue
        }
    }
    fn reset(&mut self) {
        self.triggered = false;
    }
}

/// What a turn-*stop* strategy decides. `Triggered` ⇒ the user turn is over (emit
/// `UserStoppedSpeaking`, run the LLM); `Waiting` ⇒ all conditions not yet met.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TurnStopDecision {
    /// The user turn has ended.
    Triggered,
    /// Conditions not met yet — keep waiting (more audio / a transcript / a tick).
    Waiting,
}

/// A user-turn **stop** strategy: decides when the user has finished a turn.
/// Mirrors pipecat `BaseUserTurnStopStrategy`. The wall-clock timers pipecat runs
/// as `asyncio` tasks are modeled here as explicit
/// [`on_silence_tick`](TurnStopStrategy::on_silence_tick) calls the processor
/// drives, so the logic is deterministic and clock-free.
pub trait TurnStopStrategy: Send {
    /// Inspect one frame and (maybe) decide the turn ended.
    fn on_frame(&mut self, frame: &Frame) -> TurnStopDecision;
    /// Advance `elapsed_ms` of post-VAD-stop silence; may push past a timeout.
    fn on_silence_tick(&mut self, elapsed_ms: f32) -> TurnStopDecision;
    /// Reset for the next turn.
    fn reset(&mut self);
}

/// End the turn after the user stops speaking (VAD) **and** the turn analyzer says
/// the turn is semantically complete **and** a transcript has arrived. A finalized
/// transcript triggers immediately; otherwise the configured `stt_timeout_ms` of
/// post-stop silence is the fallback. Mirrors `TurnAnalyzerUserTurnStopStrategy`
/// (the timeout-driven path, reduced to its decision core).
#[derive(Debug, Clone)]
pub struct TurnAnalyzerStop {
    /// Post-VAD-stop silence (ms) after which a non-finalized transcript still
    /// triggers (the STT-latency safety net).
    stt_timeout_ms: f32,
    vad_user_speaking: bool,
    /// Turn analyzer reported the turn complete (set by feeding turn predictions).
    turn_complete: bool,
    have_text: bool,
    transcript_finalized: bool,
    timeout_expired: bool,
}

impl TurnAnalyzerStop {
    /// `stt_timeout_ms`: post-stop wait for a final transcript before triggering.
    pub fn new(stt_timeout_ms: f32) -> Self {
        Self {
            stt_timeout_ms,
            vad_user_speaking: false,
            turn_complete: false,
            have_text: false,
            transcript_finalized: false,
            timeout_expired: false,
        }
    }

    /// Feed an end-of-turn prediction from the turn analyzer (the processor calls
    /// this when its `TurnAnalyzer` returns a complete/incomplete result).
    pub fn set_turn_complete(&mut self, complete: bool) -> TurnStopDecision {
        self.turn_complete = complete;
        self.evaluate()
    }

    fn evaluate(&self) -> TurnStopDecision {
        if !self.have_text || !self.turn_complete || self.vad_user_speaking {
            return TurnStopDecision::Waiting;
        }
        // Finalized transcript ⇒ trigger now; else wait for the silence timeout.
        if self.transcript_finalized || self.timeout_expired {
            TurnStopDecision::Triggered
        } else {
            TurnStopDecision::Waiting
        }
    }
}

impl TurnStopStrategy for TurnAnalyzerStop {
    fn on_frame(&mut self, frame: &Frame) -> TurnStopDecision {
        match frame {
            Frame::VadUserStartedSpeaking { .. } => {
                self.vad_user_speaking = true;
                self.turn_complete = false;
                self.transcript_finalized = false;
                self.timeout_expired = false;
                TurnStopDecision::Waiting
            }
            Frame::VadUserStoppedSpeaking { .. } => {
                self.vad_user_speaking = false;
                self.evaluate()
            }
            Frame::Transcription { text, final_, .. } => {
                if !text.is_empty() {
                    self.have_text = true;
                }
                if *final_ {
                    self.transcript_finalized = true;
                }
                self.evaluate()
            }
            _ => TurnStopDecision::Waiting,
        }
    }

    fn on_silence_tick(&mut self, elapsed_ms: f32) -> TurnStopDecision {
        if !self.vad_user_speaking && elapsed_ms >= self.stt_timeout_ms {
            self.timeout_expired = true;
        }
        self.evaluate()
    }

    fn reset(&mut self) {
        self.vad_user_speaking = false;
        self.turn_complete = false;
        self.have_text = false;
        self.transcript_finalized = false;
        self.timeout_expired = false;
    }
}

/// End the turn purely on a silence timeout after the user stops speaking, once at
/// least one transcript exists. Two independent waits (the policy floor
/// `user_speech_timeout_ms` and the STT safety-net `stt_timeout_ms`) must both
/// elapse — a finalized transcript short-circuits the STT wait. Mirrors
/// `SpeechTimeoutUserTurnStopStrategy`, reduced to its decision core.
#[derive(Debug, Clone)]
pub struct SpeechTimeoutStop {
    user_speech_timeout_ms: f32,
    stt_timeout_ms: f32,
    vad_user_speaking: bool,
    have_text: bool,
    transcript_finalized: bool,
    user_speech_done: bool,
    stt_done: bool,
}

impl SpeechTimeoutStop {
    /// `user_speech_timeout_ms`: floor window the user may resume in.
    /// `stt_timeout_ms`: STT-latency safety net (short-circuited on finalize).
    pub fn new(user_speech_timeout_ms: f32, stt_timeout_ms: f32) -> Self {
        Self {
            user_speech_timeout_ms,
            stt_timeout_ms,
            vad_user_speaking: false,
            have_text: false,
            transcript_finalized: false,
            user_speech_done: false,
            stt_done: false,
        }
    }

    fn evaluate(&self) -> TurnStopDecision {
        if self.vad_user_speaking || !self.have_text {
            return TurnStopDecision::Waiting;
        }
        if self.user_speech_done && self.stt_done {
            TurnStopDecision::Triggered
        } else {
            TurnStopDecision::Waiting
        }
    }
}

impl TurnStopStrategy for SpeechTimeoutStop {
    fn on_frame(&mut self, frame: &Frame) -> TurnStopDecision {
        match frame {
            Frame::VadUserStartedSpeaking { .. } => {
                self.vad_user_speaking = true;
                self.transcript_finalized = false;
                self.user_speech_done = false;
                self.stt_done = false;
                TurnStopDecision::Waiting
            }
            Frame::VadUserStoppedSpeaking { stop_secs } => {
                self.vad_user_speaking = false;
                // If VAD stop_secs already covered the STT wait, mark it done.
                if self.transcript_finalized || stop_secs * 1000.0 >= self.stt_timeout_ms {
                    self.stt_done = true;
                }
                self.evaluate()
            }
            Frame::Transcription { text, final_, .. } => {
                if !text.is_empty() {
                    self.have_text = true;
                }
                if *final_ {
                    self.transcript_finalized = true;
                    self.stt_done = true;
                }
                self.evaluate()
            }
            _ => TurnStopDecision::Waiting,
        }
    }

    fn on_silence_tick(&mut self, elapsed_ms: f32) -> TurnStopDecision {
        if !self.vad_user_speaking {
            if elapsed_ms >= self.user_speech_timeout_ms {
                self.user_speech_done = true;
            }
            if elapsed_ms >= self.stt_timeout_ms {
                self.stt_done = true;
            }
        }
        self.evaluate()
    }

    fn reset(&mut self) {
        self.vad_user_speaking = false;
        self.have_text = false;
        self.transcript_finalized = false;
        self.user_speech_done = false;
        self.stt_done = false;
    }
}

/// A user-**mute** strategy: decides whether inbound user audio/transcription
/// should be suppressed given the current system state. Mirrors pipecat
/// `BaseUserMuteStrategy`. Pure + synchronous; the processor emits
/// [`Frame::SttMute`] when [`MuteStrategy::muted`] flips.
pub trait MuteStrategy: Send {
    /// Update state from one frame, then return whether the user is muted.
    fn on_frame(&mut self, frame: &Frame) -> bool;
    /// Current muted state (without consuming a frame).
    fn muted(&self) -> bool;
}

/// Always mute. Mirrors `AlwaysUserMuteStrategy` (bot fully owns the floor).
#[derive(Debug, Default, Clone)]
pub struct AlwaysMute;

impl MuteStrategy for AlwaysMute {
    fn on_frame(&mut self, _frame: &Frame) -> bool {
        true
    }
    fn muted(&self) -> bool {
        true
    }
}

/// Mute from the start of the call until the bot finishes its **first** speaking
/// turn; then never again. Mirrors `MuteUntilFirstBotCompleteUserMuteStrategy`.
#[derive(Debug, Default, Clone)]
pub struct MuteUntilFirstBotComplete {
    first_speech_handled: bool,
}

impl MuteStrategy for MuteUntilFirstBotComplete {
    fn on_frame(&mut self, frame: &Frame) -> bool {
        if let Frame::BotStoppedSpeaking = frame {
            self.first_speech_handled = true;
        }
        self.muted()
    }
    fn muted(&self) -> bool {
        !self.first_speech_handled
    }
}

/// Mute the user **only during** the bot's first speaking turn (early user input
/// before the bot speaks is allowed). Mirrors `FirstSpeechUserMuteStrategy`.
#[derive(Debug, Default, Clone)]
pub struct FirstSpeechMute {
    bot_speaking: bool,
    first_speech_handled: bool,
}

impl MuteStrategy for FirstSpeechMute {
    fn on_frame(&mut self, frame: &Frame) -> bool {
        match frame {
            Frame::BotStartedSpeaking => self.bot_speaking = true,
            Frame::BotStoppedSpeaking => {
                self.bot_speaking = false;
                self.first_speech_handled = true;
            }
            _ => {}
        }
        self.muted()
    }
    fn muted(&self) -> bool {
        self.bot_speaking && !self.first_speech_handled
    }
}

/// Mute the user while **any** function call is in flight; unmute when every call
/// has resolved (result) or cancelled. Mirrors `FunctionCallUserMuteStrategy`.
#[derive(Debug, Default, Clone)]
pub struct FunctionCallMute {
    in_progress: std::collections::HashSet<String>,
}

impl MuteStrategy for FunctionCallMute {
    fn on_frame(&mut self, frame: &Frame) -> bool {
        match frame {
            Frame::FunctionCallsStarted(calls) => {
                for c in calls {
                    self.in_progress.insert(c.tool_call_id.clone());
                }
            }
            Frame::FunctionCallResult(r) => {
                self.in_progress.remove(&r.tool_call_id);
            }
            Frame::FunctionCallCancel { tool_call_id, .. } => {
                self.in_progress.remove(tool_call_id);
            }
            _ => {}
        }
        self.muted()
    }
    fn muted(&self) -> bool {
        !self.in_progress.is_empty()
    }
}

/// Whether a detected user turn during bot speech should actually interrupt the
/// bot. Mirrors pipecat's interruption-strategy family: a turn-start
/// (`UserStartedSpeaking`) is *proposed* by a [`TurnStartStrategy`], and an
/// interruption strategy is the final gate deciding whether to broadcast a
/// [`Frame::Interruption`]. Pure + synchronous.
pub trait InterruptionStrategy: Send {
    /// Should the accumulated user speech-so-far interrupt the bot? Called when a
    /// turn-start is proposed while the bot is speaking.
    fn should_interrupt(&self) -> bool;
    /// Feed a transcription frame so word/char-count strategies accumulate.
    fn append_transcription(&mut self, frame: &Frame);
    /// Feed an audio frame's volume so amplitude strategies accumulate.
    fn append_audio_volume(&mut self, _volume: f32) {}
    /// Reset accumulated state after a (non-)interruption decision.
    fn reset(&mut self);
}

/// Always allow interruptions (the default; barge-in on any detected turn-start).
/// Mirrors pipecat's default of "no interruption strategy = always interrupt".
#[derive(Debug, Default, Clone)]
pub struct AlwaysInterrupt;

impl InterruptionStrategy for AlwaysInterrupt {
    fn should_interrupt(&self) -> bool {
        true
    }
    fn append_transcription(&mut self, _frame: &Frame) {}
    fn reset(&mut self) {}
}

/// Interrupt the bot only once the user has spoken at least `min_words` (counted
/// across the accumulated transcriptions of the in-progress turn). Mirrors
/// pipecat's `MinWordsInterruptionStrategy` — filters spurious one-word barge-ins.
#[derive(Debug, Clone)]
pub struct MinWordsInterrupt {
    min_words: usize,
    accumulated_words: usize,
}

impl MinWordsInterrupt {
    /// Require `min_words` accumulated before an interruption is allowed.
    pub fn new(min_words: usize) -> Self {
        Self {
            min_words,
            accumulated_words: 0,
        }
    }
}

impl InterruptionStrategy for MinWordsInterrupt {
    fn should_interrupt(&self) -> bool {
        self.accumulated_words >= self.min_words
    }
    fn append_transcription(&mut self, frame: &Frame) {
        match frame {
            Frame::Transcription { text, .. } | Frame::InterimTranscription { text, .. } => {
                self.accumulated_words += text.split_whitespace().count();
            }
            _ => {}
        }
    }
    fn reset(&mut self) {
        self.accumulated_words = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::processor::frame::{FunctionCall, FunctionCallResult};
    use std::sync::Arc;

    fn transcription(text: &str, final_: bool) -> Frame {
        Frame::Transcription {
            text: text.into(),
            user_id: Arc::from("u"),
            language: None,
            final_,
        }
    }
    fn interim(text: &str) -> Frame {
        Frame::InterimTranscription {
            text: text.into(),
            user_id: Arc::from("u"),
            language: None,
        }
    }

    // ---- start strategies ----

    #[test]
    fn vad_start_triggers_on_vad_started() {
        let mut s = VadTurnStart;
        assert_eq!(
            s.on_frame(&Frame::Text("noise".into())),
            TurnStartDecision::Continue
        );
        assert_eq!(
            s.on_frame(&Frame::VadUserStartedSpeaking { start_secs: 0.2 }),
            TurnStartDecision::Triggered
        );
    }

    #[test]
    fn min_words_start_single_word_when_bot_silent() {
        let mut s = MinWordsTurnStart::new(3, true);
        // Bot silent: 1 word is enough.
        assert_eq!(
            s.on_frame(&transcription("hello", true)),
            TurnStartDecision::Triggered
        );
    }

    #[test]
    fn min_words_start_needs_min_while_bot_speaking() {
        let mut s = MinWordsTurnStart::new(3, true);
        s.on_frame(&Frame::BotStartedSpeaking);
        // One word while bot speaks ⇒ reset (sub-threshold).
        assert_eq!(
            s.on_frame(&transcription("hi", false)),
            TurnStartDecision::ResetAggregation
        );
        // Three words ⇒ trigger (barge-in).
        assert_eq!(
            s.on_frame(&transcription("hold on please", false)),
            TurnStartDecision::Triggered
        );
        // After bot stops, a single word triggers again.
        s.on_frame(&Frame::BotStoppedSpeaking);
        assert_eq!(
            s.on_frame(&transcription("yes", false)),
            TurnStartDecision::Triggered
        );
    }

    #[test]
    fn min_words_start_ignores_interim_when_disabled() {
        let mut s = MinWordsTurnStart::new(2, false);
        assert_eq!(
            s.on_frame(&interim("one two three")),
            TurnStartDecision::Continue
        );
    }

    #[test]
    fn external_start_only_triggers_once_armed() {
        let mut s = ExternalTurnStart::default();
        assert_eq!(
            s.on_frame(&Frame::VadUserStartedSpeaking { start_secs: 0.2 }),
            TurnStartDecision::Continue,
            "VAD must not trigger an external strategy"
        );
        s.trigger();
        assert_eq!(
            s.on_frame(&Frame::Text("x".into())),
            TurnStartDecision::Triggered
        );
        // Fires once.
        assert_eq!(
            s.on_frame(&Frame::Text("x".into())),
            TurnStartDecision::Continue
        );
    }

    // ---- stop strategies ----

    #[test]
    fn turn_analyzer_stop_finalized_transcript_triggers_immediately() {
        let mut s = TurnAnalyzerStop::new(1000.0);
        assert_eq!(
            s.on_frame(&Frame::VadUserStoppedSpeaking { stop_secs: 0.2 }),
            TurnStopDecision::Waiting
        );
        // Turn analyzer says complete, but no text yet.
        assert_eq!(s.set_turn_complete(true), TurnStopDecision::Waiting);
        // Finalized transcript ⇒ trigger.
        assert_eq!(
            s.on_frame(&transcription("done", true)),
            TurnStopDecision::Triggered
        );
    }

    #[test]
    fn turn_analyzer_stop_waits_for_timeout_without_finalize() {
        let mut s = TurnAnalyzerStop::new(800.0);
        s.on_frame(&Frame::VadUserStoppedSpeaking { stop_secs: 0.2 });
        s.set_turn_complete(true);
        // Non-final transcript: still waiting.
        assert_eq!(
            s.on_frame(&transcription("partial", false)),
            TurnStopDecision::Waiting
        );
        // Below timeout: still waiting.
        assert_eq!(s.on_silence_tick(500.0), TurnStopDecision::Waiting);
        // Past timeout: trigger.
        assert_eq!(s.on_silence_tick(800.0), TurnStopDecision::Triggered);
    }

    #[test]
    fn turn_analyzer_stop_blocks_while_user_speaking() {
        let mut s = TurnAnalyzerStop::new(0.0);
        s.on_frame(&Frame::VadUserStartedSpeaking { start_secs: 0.2 });
        s.set_turn_complete(true);
        // User is speaking again — never trigger even with text + complete + timeout.
        assert_eq!(
            s.on_frame(&transcription("more", true)),
            TurnStopDecision::Waiting
        );
    }

    #[test]
    fn speech_timeout_stop_needs_both_waits_and_text() {
        let mut s = SpeechTimeoutStop::new(600.0, 1200.0);
        s.on_frame(&Frame::VadUserStoppedSpeaking { stop_secs: 0.2 });
        s.on_frame(&transcription("ok", false));
        // Only the user-speech wait elapsed.
        assert_eq!(s.on_silence_tick(600.0), TurnStopDecision::Waiting);
        // Both waits elapsed ⇒ trigger.
        assert_eq!(s.on_silence_tick(1200.0), TurnStopDecision::Triggered);
    }

    #[test]
    fn speech_timeout_stop_finalize_shortcircuits_stt_wait() {
        let mut s = SpeechTimeoutStop::new(600.0, 5000.0);
        s.on_frame(&Frame::VadUserStoppedSpeaking { stop_secs: 0.2 });
        // Finalized transcript marks stt_done immediately.
        s.on_frame(&transcription("done", true));
        // Only the user-speech floor remains.
        assert_eq!(s.on_silence_tick(600.0), TurnStopDecision::Triggered);
    }

    #[test]
    fn speech_timeout_stop_no_text_never_triggers() {
        let mut s = SpeechTimeoutStop::new(0.0, 0.0);
        s.on_frame(&Frame::VadUserStoppedSpeaking { stop_secs: 0.5 });
        // Both timeouts are zero, but with no transcript it never fires.
        assert_eq!(s.on_silence_tick(1000.0), TurnStopDecision::Waiting);
    }

    // ---- mute strategies ----

    #[test]
    fn always_mute_is_always_muted() {
        let mut s = AlwaysMute;
        assert!(s.on_frame(&Frame::BotStoppedSpeaking));
        assert!(s.muted());
    }

    #[test]
    fn mute_until_first_bot_complete() {
        let mut s = MuteUntilFirstBotComplete::default();
        assert!(s.muted(), "muted from the very start");
        assert!(
            s.on_frame(&Frame::BotStartedSpeaking),
            "still muted while speaking"
        );
        assert!(
            !s.on_frame(&Frame::BotStoppedSpeaking),
            "unmuted after first complete"
        );
        // Subsequent bot turns no longer mute.
        assert!(!s.on_frame(&Frame::BotStartedSpeaking));
    }

    #[test]
    fn first_speech_mute_only_during_first_bot_turn() {
        let mut s = FirstSpeechMute::default();
        assert!(!s.muted(), "not muted before the bot speaks");
        assert!(
            s.on_frame(&Frame::BotStartedSpeaking),
            "muted during first speech"
        );
        assert!(!s.on_frame(&Frame::BotStoppedSpeaking), "unmuted after");
        // A later bot turn no longer mutes.
        assert!(!s.on_frame(&Frame::BotStartedSpeaking));
    }

    #[test]
    fn function_call_mute_tracks_in_flight_calls() {
        let mut s = FunctionCallMute::default();
        let call = FunctionCall {
            function_name: "lookup".into(),
            tool_call_id: "c1".into(),
            arguments: serde_json::json!({}),
        };
        assert!(s.on_frame(&Frame::FunctionCallsStarted(vec![call])));
        assert!(s.muted());
        // Resolving the call unmutes.
        assert!(!s.on_frame(&Frame::FunctionCallResult(FunctionCallResult {
            function_name: "lookup".into(),
            tool_call_id: "c1".into(),
            result: serde_json::json!({"ok": true}),
        })));
    }

    #[test]
    fn function_call_mute_cancel_also_unmutes() {
        let mut s = FunctionCallMute::default();
        let call = FunctionCall {
            function_name: "lookup".into(),
            tool_call_id: "c2".into(),
            arguments: serde_json::json!({}),
        };
        s.on_frame(&Frame::FunctionCallsStarted(vec![call]));
        assert!(!s.on_frame(&Frame::FunctionCallCancel {
            function_name: "lookup".into(),
            tool_call_id: "c2".into(),
        }));
    }

    // ---- interruption strategies ----

    #[test]
    fn always_interrupt_allows_barge_in() {
        let s = AlwaysInterrupt;
        assert!(s.should_interrupt());
    }

    #[test]
    fn min_words_interrupt_gates_on_word_count() {
        let mut s = MinWordsInterrupt::new(3);
        assert!(!s.should_interrupt());
        s.append_transcription(&interim("hold"));
        assert!(!s.should_interrupt(), "1 word < 3");
        s.append_transcription(&transcription("on please", false));
        assert!(s.should_interrupt(), "3 words >= 3");
        s.reset();
        assert!(!s.should_interrupt());
    }
}