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
// SPDX-License-Identifier: Apache-2.0
//
//! Service-processor trait *signatures* the cascaded path builds against
//! (PROCESSOR-DESIGN §6.1) + no-op mock impls + the audio-intelligence traits.
//!
//! These traits are **frozen here** so providers and the audio-intelligence layer
//! build against a stable surface. The core ships only the trait + a mock impl of
//! each, and the mock cascaded pipeline fixture test (mock-STT → mock-LLM →
//! mock-TTS through a real [`Pipeline`](crate::pipeline::Pipeline)) that provider
//! crates extend.

pub mod adapters;

use std::sync::Arc;

use async_trait::async_trait;
use futures::stream::{self, BoxStream};
use futures::StreamExt;

use crate::error::Result;
use crate::processor::frame::{AudioFrame, Frame, LlmContext, StartParams, TurnParams, VadParams};
use crate::types::{RealtimeEvent, RealtimeSetup, ToolDecl};

/// Connection-time setup for a [`RealtimeLlmService`] — the realtime S2S contract
/// (today's [`crate::realtime::RealtimeLlm`], restated as the canonical service
/// trait the processor wraps). Aliased to the existing [`RealtimeSetup`].
pub type RealtimeServiceSetup = RealtimeSetup;

/// A tool declaration for the cascaded LLM service (alias to the existing
/// [`ToolDecl`] data shape so the realtime + cascaded paths agree).
pub type Tool = ToolDecl;

// ---------------------------------------------------------------------------
// Streaming service-processor traits (PROCESSOR-DESIGN §6.1).
// ---------------------------------------------------------------------------

/// Streaming speech→text. Consumes `InputAudio`, emits `InterimTranscription`
/// then final `Transcription`. Mirrors pipecat `STTService`.
#[async_trait]
pub trait SttService: Send {
    fn name(&self) -> &str;
    async fn start(&mut self, params: &StartParams) -> Result<()>;
    /// Feed one audio chunk; transcript frames are returned for the processor to
    /// forward downstream.
    async fn run_stt(&mut self, audio: Arc<AudioFrame>) -> Result<Vec<Frame>>;
    async fn set_muted(&mut self, muted: bool);
}

/// Streaming text→speech. Consumes `TtsSpeak`/`Text`, emits `TtsStarted`,
/// `TtsAudio`*, `TtsStopped`. Mirrors pipecat `TTSService`.
#[async_trait]
pub trait TtsService: Send {
    fn name(&self) -> &str;
    fn sample_rate(&self) -> u32;
    async fn start(&mut self, params: &StartParams) -> Result<()>;
    async fn run_tts(&mut self, text: &str) -> Result<Vec<Frame>>;
}

/// Context-driven LLM. Consumes `LlmContext`/`LlmRun`, emits `LlmResponseStart`,
/// `LlmText`*, optional `FunctionCallsStarted`, `LlmResponseEnd`.
#[async_trait]
pub trait LlmService: Send {
    fn name(&self) -> &str;
    async fn start(&mut self, params: &StartParams) -> Result<()>;
    async fn run_llm<'a>(&'a mut self, ctx: &'a LlmContext) -> Result<BoxStream<'a, Frame>>;
    fn set_tools(&mut self, tools: Vec<Tool>);
}

// ---------------------------------------------------------------------------
// Object-forwarding blanket impls.
//
// `build_cascaded_task`/`build_cascaded_pipeline` are generic over the three
// service traits (`St: SttService`, …). A host that picks a provider at runtime
// (a provider-string factory, e.g. the embedder's `cascaded_factory`) must unify
// the per-provider concrete types behind a trait object, so it builds
// `Box<dyn SttService>` and passes that as the generic argument. These blanket
// impls make `Box<dyn Trait>: Trait` hold — the standard "boxed trait object is
// itself the trait" idiom — so the generic builders accept the boxed services
// without any change to their signatures. Additive + behaviour-preserving.
// ---------------------------------------------------------------------------

#[async_trait]
impl SttService for Box<dyn SttService> {
    fn name(&self) -> &str {
        (**self).name()
    }
    async fn start(&mut self, params: &StartParams) -> Result<()> {
        (**self).start(params).await
    }
    async fn run_stt(&mut self, audio: Arc<AudioFrame>) -> Result<Vec<Frame>> {
        (**self).run_stt(audio).await
    }
    async fn set_muted(&mut self, muted: bool) {
        (**self).set_muted(muted).await
    }
}

#[async_trait]
impl TtsService for Box<dyn TtsService> {
    fn name(&self) -> &str {
        (**self).name()
    }
    fn sample_rate(&self) -> u32 {
        (**self).sample_rate()
    }
    async fn start(&mut self, params: &StartParams) -> Result<()> {
        (**self).start(params).await
    }
    async fn run_tts(&mut self, text: &str) -> Result<Vec<Frame>> {
        (**self).run_tts(text).await
    }
}

#[async_trait]
impl LlmService for Box<dyn LlmService> {
    fn name(&self) -> &str {
        (**self).name()
    }
    async fn start(&mut self, params: &StartParams) -> Result<()> {
        (**self).start(params).await
    }
    async fn run_llm<'a>(&'a mut self, ctx: &'a LlmContext) -> Result<BoxStream<'a, Frame>> {
        (**self).run_llm(ctx).await
    }
    fn set_tools(&mut self, tools: Vec<Tool>) {
        (**self).set_tools(tools)
    }
}

/// The realtime S2S contract (today's [`crate::realtime::RealtimeLlm`] — UNCHANGED
/// shape, restated as the canonical service trait the processor wraps).
#[async_trait]
pub trait RealtimeLlmService: Send {
    async fn connect(&mut self, setup: RealtimeServiceSetup) -> Result<()>;
    async fn send_audio(&mut self, chunk: Arc<AudioFrame>) -> Result<()>;
    async fn update_system(&mut self, prompt: String, tools: Vec<Tool>) -> Result<()>;
    async fn send_tool_result(&mut self, id: String, result: serde_json::Value) -> Result<()>;
    async fn next_event(&mut self) -> Option<RealtimeEvent>;

    /// The PCM sample rate (Hz) this model expects on its audio **input**. The s2s
    /// pipeline resamples caller audio to this before `send_audio` and advertises it
    /// in [`RealtimeSetup::input_sample_rate`]. Default 16 kHz; a provider that needs
    /// another rate (OpenAI Realtime requires ≥ 24 kHz) overrides this. Lives on the
    /// connector so the consuming app never has to know provider rates.
    fn input_sample_rate(&self) -> u32 {
        16_000
    }

    /// Trigger an initial **bot-first** turn so the agent greets before the caller
    /// speaks. Default no-op (caller speaks first); a provider that supports it
    /// overrides (e.g. OpenAI Realtime sends a `response.create`). The pipeline calls
    /// this through [`ServiceRealtimeAdapter`](crate::realtime::ServiceRealtimeAdapter)
    /// → [`RealtimeKickoff`](crate::realtime::RealtimeKickoff).
    async fn kickoff(&mut self) -> Result<()> {
        Ok(())
    }

    /// Readiness notify for the **lock-free** event path (mirrors
    /// [`RealtimeLlm::event_notify`](crate::realtime::RealtimeLlm::event_notify)).
    /// `None` (default) → the pipeline falls back to the blocking `next_event`, which
    /// holds the session lock across the idle wait between bot turns and **starves
    /// `send_audio`** (caller audio stalls after the greeting). A connector with an
    /// internal event channel SHOULD return a `Notify` it fires on each event so the
    /// pipeline can await readiness without the lock.
    fn event_notify(&self) -> Option<Arc<tokio::sync::Notify>> {
        None
    }

    /// Non-blocking sibling of [`next_event`](Self::next_event) (mirrors
    /// [`RealtimeLlm::poll_event`](crate::realtime::RealtimeLlm::poll_event)). The
    /// default blocks; a connector that returns a real [`event_notify`](Self::event_notify)
    /// MUST override this to be genuinely non-blocking (e.g. a channel `try_recv`).
    async fn poll_event(&mut self) -> crate::realtime::PollEvent {
        crate::realtime::PollEvent::Ready(self.next_event().await)
    }
}

// ---------------------------------------------------------------------------
// Audio-intelligence traits — signatures only (PROCESSOR-DESIGN §6.1).
// ---------------------------------------------------------------------------

/// VAD classification of a frame of audio. Mirrors pipecat `VADState`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VadState {
    Quiet,
    Starting,
    Speaking,
    Stopping,
}

/// An end-of-turn prediction. Mirrors pipecat's turn-analyzer output.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TurnPrediction {
    pub is_complete: bool,
    pub probability: f32,
}

/// Voice-activity detector. Mirrors pipecat `VADAnalyzer`. Silero (ONNX/`ort`) is
/// the reference impl (behind the `vad-ort` feature — the *trait* is always here).
pub trait VadAnalyzer: Send {
    fn sample_rate(&self) -> u32;
    /// Classify a frame of audio: Quiet / Starting / Speaking / Stopping.
    fn analyze(&mut self, audio: &AudioFrame) -> VadState;
    fn set_params(&mut self, params: VadParams);
}

/// End-of-turn / semantic-completion analyzer. Mirrors pipecat `BaseTurnAnalyzer`
/// (Smart-Turn v2/v3 is the reference impl).
pub trait TurnAnalyzer: Send {
    /// Given accumulated speech + the VAD edge, predict whether the turn is
    /// complete.
    fn analyze_turn(&mut self, audio: &AudioFrame, vad: VadState) -> TurnPrediction;
    fn set_params(&mut self, params: TurnParams);
}

// ---------------------------------------------------------------------------
// No-op / mock impls (PROCESSOR-DESIGN §6.1 — cascaded pipeline fixtures).
// ---------------------------------------------------------------------------

/// A mock STT: turns any audio chunk into a fixed final transcription. Provider
/// implementations replace this with real backends.
pub struct MockStt {
    pub transcript: String,
    pub user_id: Arc<str>,
}

impl MockStt {
    pub fn new(transcript: impl Into<String>) -> Self {
        Self {
            transcript: transcript.into(),
            user_id: Arc::from("user"),
        }
    }
}

#[async_trait]
impl SttService for MockStt {
    fn name(&self) -> &str {
        "MockStt"
    }
    async fn start(&mut self, _params: &StartParams) -> Result<()> {
        Ok(())
    }
    async fn run_stt(&mut self, _audio: Arc<AudioFrame>) -> Result<Vec<Frame>> {
        Ok(vec![
            Frame::InterimTranscription {
                text: self.transcript.clone(),
                user_id: self.user_id.clone(),
                language: None,
            },
            Frame::Transcription {
                text: self.transcript.clone(),
                user_id: self.user_id.clone(),
                language: None,
                final_: true,
            },
        ])
    }
    async fn set_muted(&mut self, _muted: bool) {}
}

/// A mock LLM: echoes the last user transcription back as a single LLM text +
/// response framing. Provider implementations replace this with real backends.
pub struct MockLlm {
    pub prefix: String,
    pub tools: Vec<Tool>,
}

impl MockLlm {
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
            tools: Vec::new(),
        }
    }
}

#[async_trait]
impl LlmService for MockLlm {
    fn name(&self) -> &str {
        "MockLlm"
    }
    async fn start(&mut self, _params: &StartParams) -> Result<()> {
        Ok(())
    }
    async fn run_llm<'a>(&'a mut self, ctx: &'a LlmContext) -> Result<BoxStream<'a, Frame>> {
        // Echo the last message's "content" text if present, else a canned reply.
        let user_text = ctx
            .messages
            .last()
            .and_then(|m| m.get("content"))
            .and_then(|c| c.as_str())
            .unwrap_or("hello")
            .to_string();
        let reply = format!("{}{}", self.prefix, user_text);
        let frames = vec![
            Frame::LlmResponseStart,
            Frame::LlmText(reply),
            Frame::LlmResponseEnd,
        ];
        Ok(stream::iter(frames).boxed())
    }
    fn set_tools(&mut self, tools: Vec<Tool>) {
        self.tools = tools;
    }
}

/// A mock TTS: turns text into a `TtsStarted` + one `TtsAudio` chunk (silence
/// sized to the text length) + `TtsStopped`. Provider implementations replace this
/// with real backends.
pub struct MockTts {
    pub rate: u32,
}

impl MockTts {
    pub fn new(rate: u32) -> Self {
        Self { rate }
    }
}

#[async_trait]
impl TtsService for MockTts {
    fn name(&self) -> &str {
        "MockTts"
    }
    fn sample_rate(&self) -> u32 {
        self.rate
    }
    async fn start(&mut self, _params: &StartParams) -> Result<()> {
        Ok(())
    }
    async fn run_tts(&mut self, text: &str) -> Result<Vec<Frame>> {
        // One sample per character — deterministic, no real audio model.
        let pcm = vec![0i16; text.len().max(1)];
        let audio = Arc::new(AudioFrame::mono(pcm, self.rate));
        Ok(vec![
            Frame::TtsStarted { context_id: None },
            Frame::TtsAudio {
                audio,
                context_id: None,
            },
            Frame::TtsStopped { context_id: None },
        ])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // The service-processor adapters now live in the public `adapters` module
    // (promoted out of this test block — PROCESSOR-DESIGN §6.1);
    // the fixture below drives those promoted processors.
    use crate::observer::{FrameEvent, FrameObserver};
    use crate::pipeline::{Pipeline, PipelineTask, PipelineTaskParams};
    use crate::processor::frame::Frame;
    use crate::service::adapters::{LlmProcessor, SttProcessor, TtsProcessor};
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};

    // An observer that records every frame name it saw processed.
    #[derive(Default)]
    struct Tap {
        names: Mutex<Vec<&'static str>>,
        saw_output_audio: AtomicBool,
    }
    #[async_trait]
    impl FrameObserver for Tap {
        async fn on_process(&self, e: &FrameEvent<'_>) {
            self.names.lock().unwrap().push(e.frame.name());
            if matches!(e.frame, Frame::OutputAudio(_)) {
                self.saw_output_audio.store(true, Ordering::Relaxed);
            }
        }
    }

    /// The §9 step-10 gate: mock-STT → mock-LLM → mock-TTS runs a turn end-to-end
    /// through a **real** `Pipeline`. Provider crates build against this fixture.
    #[tokio::test]
    async fn mock_cascaded_pipeline_runs_a_turn_end_to_end() {
        let stt = SttProcessor::new(MockStt::new("book a dentist appointment"));
        let llm = LlmProcessor::new(MockLlm::new("you said: "));
        let tts = TtsProcessor::new(MockTts::new(24_000));
        let pipeline = Pipeline::new(vec![Box::new(stt), Box::new(llm), Box::new(tts)]);

        let tap = Arc::new(Tap::default());
        let task = PipelineTask::new(
            pipeline,
            PipelineTaskParams::default(),
            vec![tap.clone() as Arc<dyn FrameObserver>],
        );

        // Drive a turn: feed one InputAudio chunk, then end.
        let audio = Arc::new(AudioFrame::mono(vec![1, 2, 3, 4], 16_000));
        task.queue_frame(Frame::InputAudio(audio)).await;
        task.stop_when_done().await;
        let _ = task.cancel_token();

        // Run with a generous timeout.
        tokio::time::timeout(std::time::Duration::from_secs(5), task.run())
            .await
            .expect("cascaded pipeline timed out")
            .expect("run ok");

        // The turn produced output audio (STT→LLM→TTS all fired).
        assert!(
            tap.saw_output_audio.load(Ordering::Relaxed),
            "mock cascaded pipeline must emit OutputAudio; saw {:?}",
            tap.names.lock().unwrap()
        );
        let names = tap.names.lock().unwrap().clone();
        assert!(
            names.contains(&"Transcription"),
            "STT must emit Transcription"
        );
        assert!(names.contains(&"LlmText"), "LLM must emit LlmText");
    }
}