The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(voice): add live voice provider support
Steven Enamakele2609bdc6479
diff --git a/crates/openhuman-core/src/voice/live/providers.rs b/crates/openhuman-core/src/voice/live/providers.rsnew file mode 100644index 0000000000..7337241e09--- /dev/null+++ b/crates/openhuman-core/src/voice/live/providers.rs@@ -0,0 +1,293 @@+//! Live provider catalogue, configuration and connection preparation.+//!+//! Four providers, two of them hosted by the TinyHumans backend:+//!+//! | id | reached through | credential |+//! | --- | --- | --- |+//! | `gemini-hosted` | backend Gemini Live relay ticket | the backend credential |+//! | `elevenlabs-hosted` | backend-signed ElevenLabs agent URL | the backend credential |+//! | `gemini` | Google directly | `provider:google` |+//! | `sarvam` | Sarvam directly | `provider:sarvam` |+//!+//! Minting tickets and signed URLs happens here, against the backend, because+//! `tinyliveagents` deliberately holds no backend or credential code.++use reqwest::Method;+use serde_json::{json, Value};+use tinyagents_live::tinyliveagents::elevenlabs::ElevenLabsConvai;+use tinyagents_live::tinyliveagents::gemini::{self, GeminiLive, GeminiRelay};+use tinyagents_live::tinyliveagents::sarvam::SarvamCascade;+use tinyagents_live::tinyliveagents::{AudioFormat, LiveConfig, LiveProvider};++use super::error::LiveVoiceError;+use super::types::{LiveProviderInfo, LiveProviderKind};+use crate::backend::BackendClient;+use crate::config::schema::voice_live::{+ LIVE_PROVIDERS, LIVE_PROVIDER_ELEVENLABS_HOSTED, LIVE_PROVIDER_GEMINI,+ LIVE_PROVIDER_GEMINI_HOSTED, LIVE_PROVIDER_SARVAM,+};+use crate::config::Config;++const LOG_PREFIX: &str = "[voice-live]";++/// The backend route that mints a Gemini Live relay ticket.+pub(crate) const GEMINI_LIVE_SESSIONS_PATH: &str = "/agent-integrations/gemini/live/sessions";++/// Gemini Live prebuilt voices.+const GEMINI_VOICES: &[&str] = &[+ "Puck", "Charon", "Kore", "Fenrir", "Aoede", "Leda", "Orus", "Zephyr",+];+/// Languages Gemini Live speaks well (it understands more).+const GEMINI_LANGUAGES: &[&str] = &[+ "en-US", "en-IN", "en-GB", "hi-IN", "es-US", "es-ES", "fr-FR", "de-DE", "it-IT", "pt-BR",+ "ja-JP", "ko-KR", "cmn-CN", "ar-XA", "id-ID", "ru-RU", "pl-PL", "bn-IN",+];+/// Sarvam `bulbul:v3` speakers offered in the UI.+const SARVAM_SPEAKERS: &[&str] = &[+ "shubh", "priya", "aditya", "ritu", "neha", "rahul", "pooja", "rohan", "simran", "kavya",+ "amit", "dev", "ishita", "shreya", "varun", "kabir",+];+/// Languages Sarvam can both hear and speak, plus automatic detection.+const SARVAM_LANGUAGES: &[&str] = &[+ "en-IN", "hi-IN", "bn-IN", "gu-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN",+ "te-IN", "auto",+];++/// The BYOK key slug a provider reads, if any.+pub(crate) fn key_slug(provider: &str) -> Option<&'static str> {+ match provider {+ LIVE_PROVIDER_GEMINI => Some("google"),+ LIVE_PROVIDER_SARVAM => Some("sarvam"),+ _ => None,+ }+}++/// The stored key for `slug`, or `None` when none is stored.+pub(crate) fn stored_key(slug: &str, config: &Config) -> Option<String> {+ crate::inference::provider::factory::credentials::lookup_key_for_slug(slug, config)+ .ok()+ .filter(|key| !key.trim().is_empty())+}++/// Whether a backend credential is available for hosted providers.+fn backend_ready(config: &Config) -> bool {+ crate::security::credentials::session_support::resolve_backend_credential(config).is_ok()+}++fn strings(items: &[&str]) -> Vec<String> {+ items.iter().map(|s| (*s).to_string()).collect()+}++/// The provider catalogue with each provider's readiness.+pub(crate) fn provider_infos(config: &Config) -> Vec<LiveProviderInfo> {+ let hosted = backend_ready(config);+ LIVE_PROVIDERS+ .iter()+ .map(|id| {+ let slug = key_slug(id);+ let (label, kind, voices, languages) = match *id {+ LIVE_PROVIDER_GEMINI_HOSTED => (+ "Gemini Live (TinyHumans)",+ LiveProviderKind::Hosted,+ strings(GEMINI_VOICES),+ strings(GEMINI_LANGUAGES),+ ),+ LIVE_PROVIDER_ELEVENLABS_HOSTED => (+ "ElevenLabs Agent (TinyHumans)",+ LiveProviderKind::Hosted,+ Vec::new(),+ Vec::new(),+ ),+ LIVE_PROVIDER_GEMINI => (+ "Gemini Live (Google API key)",+ LiveProviderKind::Byok,+ strings(GEMINI_VOICES),+ strings(GEMINI_LANGUAGES),+ ),+ _ => (+ "Sarvam AI",+ LiveProviderKind::Byok,+ strings(SARVAM_SPEAKERS),+ strings(SARVAM_LANGUAGES),+ ),+ };+ let configured = match slug {+ Some(slug) => stored_key(slug, config).is_some(),+ None => hosted,+ };+ LiveProviderInfo {+ id: (*id).to_string(),+ label: label.to_string(),+ kind,+ configured,+ key_slug: slug.map(str::to_string),+ voices,+ languages,+ }+ })+ .collect()+}++/// The session configuration for `provider`, from the user's settings.+pub(crate) fn live_config(+ config: &Config,+ provider: &str,+ system_instruction: &str,+ input_sample_rate: u32,+) -> LiveConfig {+ let settings = &config.voice_live;+ let mut live = LiveConfig::new();+ live.input_format = AudioFormat::pcm16(input_sample_rate);+ match provider {+ LIVE_PROVIDER_GEMINI_HOSTED | LIVE_PROVIDER_GEMINI => {+ live.system_instruction = Some(system_instruction.to_string());+ live.model = settings.gemini.model.clone();+ live.voice = settings.gemini.voice.clone();+ live.language = settings.gemini.language.clone();+ // Keep long conversations inside the context window.+ live.provider_options = json!({ "context_window_compression": { "slidingWindow": {} } });+ }+ LIVE_PROVIDER_SARVAM => {+ live.system_instruction = Some(system_instruction.to_string());+ live.model = settings.sarvam.model.clone();+ live.voice = settings.sarvam.speaker.clone();+ match settings.sarvam.language.as_deref() {+ Some("auto") => live.provider_options = json!({ "auto_language": true }),+ other => live.language = Some(other.unwrap_or("en-IN").to_string()),+ }+ }+ LIVE_PROVIDER_ELEVENLABS_HOSTED => {+ // The hosted agent owns its prompt and its brain (OpenHuman over+ // the Custom-LLM relay); only the voice may be overridden.+ live.voice = settings+ .elevenlabs+ .voice_id+ .clone()+ .or_else(|| non_empty(crate::voice::realtime_default_voice_id()));+ }+ _ => {}+ }+ live+}++fn non_empty(value: Option<String>) -> Option<String> {+ value.filter(|v| !v.trim().is_empty())+}++/// A provider ready to connect, with the configuration it must connect with.+pub(crate) struct PreparedProvider {+ pub(crate) provider: Box<dyn LiveProvider>,+ pub(crate) config: LiveConfig,+}++/// Resolves `provider` to something `tinyagents-live` can connect, minting a+/// backend ticket or signed URL for the hosted ones.+pub(crate) async fn prepare(+ config: &Config,+ provider: &str,+ mut live: LiveConfig,+) -> Result<PreparedProvider, LiveVoiceError> {+ match provider {+ LIVE_PROVIDER_GEMINI_HOSTED => {+ let ticket = mint_gemini_ticket(config, &live).await?;+ tracing::debug!(+ "{LOG_PREFIX} minted gemini live ticket session={} model={}",+ ticket.session_id,+ ticket.model+ );+ let relay =+ GeminiRelay::connect_url(ticket.ws_url).with_session(ticket.session_id, ticket.model);+ Ok(PreparedProvider {+ provider: Box::new(relay),+ config: live,+ })+ }+ LIVE_PROVIDER_ELEVENLABS_HOSTED => {+ let signed = crate::voice::realtime::mint_voice_agent_signed_url(config)+ .await+ .map_err(LiveVoiceError::backend)?+ .value;+ // `user_token` is the identity the backend's Custom-LLM relay+ // verifies on every relayed turn (#5399).+ live.provider_options = json!({+ "user_id": signed.user_token,+ "custom_llm_extra_body": { "user": signed.user_token },+ });+ Ok(PreparedProvider {+ provider: Box::new(ElevenLabsConvai::connect_url(signed.signed_url)),+ config: live,+ })+ }+ LIVE_PROVIDER_GEMINI => {+ let key = stored_key("google", config)+ .ok_or_else(|| LiveVoiceError::not_configured("add a Google API key for Gemini Live"))?;+ Ok(PreparedProvider {+ provider: Box::new(GeminiLive::new(key)),+ config: live,+ })+ }+ LIVE_PROVIDER_SARVAM => {+ let key = stored_key("sarvam", config)+ .ok_or_else(|| LiveVoiceError::not_configured("add a Sarvam API key"))?;+ Ok(PreparedProvider {+ provider: Box::new(SarvamCascade::new(key)),+ config: live,+ })+ }+ other => Err(LiveVoiceError::invalid(format!("unknown live provider `{other}`"))),+ }+}++/// A minted relay ticket.+#[derive(Debug, Clone, PartialEq, Eq)]+pub(crate) struct GeminiTicket {+ pub(crate) ws_url: String,+ pub(crate) session_id: String,+ pub(crate) model: String,+}++/// Parses the backend's ticket response (`data`-wrapped or bare).+pub(crate) fn parse_ticket(raw: &Value) -> Result<GeminiTicket, LiveVoiceError> {+ let data = raw.get("data").unwrap_or(raw);+ let field = |name: &str| {+ data.get(name)+ .and_then(Value::as_str)+ .filter(|v| !v.is_empty())+ .map(str::to_string)+ };+ let ws_url = field("wsUrl").ok_or_else(|| {+ LiveVoiceError::backend("backend returned no wsUrl for the live session".to_string())+ })?;+ Ok(GeminiTicket {+ ws_url,+ session_id: field("sessionId").unwrap_or_default(),+ model: field("model").unwrap_or_default(),+ })+}++async fn mint_gemini_ticket(+ config: &Config,+ live: &LiveConfig,+) -> Result<GeminiTicket, LiveVoiceError> {+ let credential =+ crate::security::credentials::session_support::resolve_backend_credential(config)+ .map_err(LiveVoiceError::not_configured)?;+ let api_url = crate::backend::require_base_url(&config.api_url).map_err(LiveVoiceError::backend)?;+ crate::voice::realtime::ensure_secure_backend_url(&api_url).map_err(LiveVoiceError::backend)?;+ let client = BackendClient::new(&api_url).map_err(|e| LiveVoiceError::backend(e.to_string()))?;+ let body = gemini::ticket_request(live);+ let raw = client+ .authed_json(+ &credential,+ Method::POST,+ GEMINI_LIVE_SESSIONS_PATH,+ Some(body),+ )+ .await+ .map_err(|error| LiveVoiceError::backend(crate::backend::flatten_authed_error(error)))?;+ parse_ticket(&raw)+}++#[cfg(test)]+#[path = "providers_tests.rs"]+mod tests;