Skip to main content

adk_realtime/
model.rs

1//! Core RealtimeModel trait definition.
2
3use crate::audio::AudioFormat;
4use crate::config::RealtimeConfig;
5use crate::error::Result;
6use crate::session::BoxedSession;
7use async_trait::async_trait;
8
9/// A factory for creating real-time sessions.
10///
11/// Each provider (OpenAI, Gemini, etc.) implements this trait to provide
12/// their specific realtime connection logic.
13///
14/// # Example
15///
16/// ```rust,ignore
17/// use adk_realtime::{RealtimeModel, RealtimeConfig};
18/// use adk_realtime::openai::OpenAIRealtimeModel;
19///
20/// #[tokio::main]
21/// async fn main() -> Result<()> {
22///     let model = OpenAIRealtimeModel::new(api_key, "gpt-4o-realtime-preview-2024-12-17");
23///
24///     let config = RealtimeConfig::default()
25///         .with_instruction("You are a helpful assistant.")
26///         .with_voice("alloy");
27///
28///     let session = model.connect(config).await?;
29///
30///     // Use the session...
31///
32///     session.close().await?;
33///     Ok(())
34/// }
35/// ```
36#[async_trait]
37pub trait RealtimeModel: Send + Sync {
38    /// Get the provider name (e.g., "openai", "gemini").
39    fn provider(&self) -> &str;
40
41    /// Get the model identifier.
42    fn model_id(&self) -> &str;
43
44    /// Check if this model supports realtime streaming.
45    fn supports_realtime(&self) -> bool {
46        true
47    }
48
49    /// Get supported input audio formats.
50    fn supported_input_formats(&self) -> Vec<AudioFormat>;
51
52    /// Get supported output audio formats.
53    fn supported_output_formats(&self) -> Vec<AudioFormat>;
54
55    /// Get available voices for this model.
56    fn available_voices(&self) -> Vec<&str>;
57
58    /// Connect and create a new realtime session.
59    ///
60    /// This establishes a WebSocket connection to the provider and
61    /// configures the session with the provided settings.
62    async fn connect(&self, config: RealtimeConfig) -> Result<BoxedSession>;
63}
64
65/// A shared model type for thread-safe dynamic dispatch.
66pub type BoxedModel = std::sync::Arc<dyn RealtimeModel>;