Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

3.4 KB
Raw Blame
//! Provider traits and shared result types for STT / TTS dispatch.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::config::Config;
use crate::core::Outcome;
use tinyinference_voice::reply::ReplySpeech as ReplySpeechResult;

// ---------------------------------------------------------------------------
// Shared result type
// ---------------------------------------------------------------------------

/// Common shape both STT branches return after dispatch. Keeps the wire
/// contract identical regardless of provider — the UI only sees `text`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SttResult {
    pub text: String,
    /// Lowercase provider id (`"cloud"`, or the third-party slug) — exposed on
    /// the wire so the renderer can show which engine actually ran.
    pub provider: String,
}

// ---------------------------------------------------------------------------
// Provider traits
// ---------------------------------------------------------------------------

/// Speech-to-text provider abstraction. The backend proxy and every
/// third-party engine implement this; the factory hands the caller a boxed
/// trait object.
#[async_trait]
pub trait SttProvider: Send + Sync {
    /// Stable identifier used in logs and config (`"cloud"`, `"external"`).
    fn name(&self) -> &'static str;

    /// Transcribe a single base64-encoded audio blob.
    ///
    /// `mime_type` and `file_name` are hints; providers that don't care
    /// may ignore them. `language` is BCP-47 (`"en"`, `"es"`); pass `None`
    /// to let the provider auto-detect.
    async fn transcribe(
        &self,
        config: &Config,
        audio_base64: &str,
        mime_type: Option<&str>,
        file_name: Option<&str>,
        language: Option<&str>,
    ) -> Result<Outcome<SttResult>, String>;

    /// The model selected when the provider was constructed.
    ///
    /// Test-only: factory tests use this to ensure an empty external-provider
    /// model defers to the registry default rather than inheriting the cloud
    /// proxy's model id.
    #[cfg(test)]
    fn configured_model(&self) -> Option<&str> {
        None
    }
}

/// Text-to-speech provider abstraction. Cloud returns rich viseme alignment
/// (used by the mascot lip-sync); Piper returns audio only and the caller
/// derives a flat viseme timeline downstream.
#[async_trait]
pub trait TtsProvider: Send + Sync {
    fn name(&self) -> &'static str;

    /// Synthesize speech for `text`. Returns the same envelope shape as
    /// `voice.reply_synthesize` so the renderer can swap providers without
    /// branching on the response.
    async fn synthesize(
        &self,
        config: &Config,
        text: &str,
        voice: Option<&str>,
    ) -> Result<Outcome<ReplySpeechResult>, String>;

    /// The voice this provider was constructed with, or `None` when it defers
    /// to a downstream default (cloud omits `voice_id` so the backend picks).
    ///
    /// Test-only, and deliberately so: the factory hands back a
    /// `Box<dyn TtsProvider>`, so the voice a provider actually carries is
    /// otherwise unobservable without a live synthesis call. That blind spot is
    /// how a Piper voice id reached the cloud proxy unnoticed (#5355) — the
    /// pre-existing factory tests asserted only [`TtsProvider::name`], which
    /// stayed green throughout.
    #[cfg(test)]
    fn configured_voice(&self) -> Option<&str> {
        None
    }
}