Skip to main content

RealtimeSession

Trait RealtimeSession 

Source
pub trait RealtimeSession: Send + Sync {
Show 15 methods // Required methods fn session_id(&self) -> &str; fn is_connected(&self) -> bool; fn send_audio<'life0, 'life1, 'async_trait>( &'life0 self, audio: &'life1 AudioChunk, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; fn send_audio_base64<'life0, 'life1, 'async_trait>( &'life0 self, audio_base64: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; fn send_text<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait; fn send_tool_response<'life0, 'async_trait>( &'life0 self, response: ToolResponse, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn commit_audio<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn clear_audio<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn create_response<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn interrupt<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn send_event<'life0, 'async_trait>( &'life0 self, event: ClientEvent, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn next_event<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<Result<ServerEvent, RealtimeError>>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn events( &self, ) -> Pin<Box<dyn Stream<Item = Result<ServerEvent, RealtimeError>> + Send + '_>>; fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn mutate_context<'life0, 'async_trait>( &'life0 self, config: RealtimeConfig, ) -> Pin<Box<dyn Future<Output = Result<ContextMutationOutcome, RealtimeError>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait;
}
Available on crate feature realtime only.
Expand description

A real-time bidirectional streaming session.

This trait provides a unified interface for real-time voice/audio sessions across different providers (OpenAI, Gemini, etc.).

§Example

use adk_realtime::{RealtimeSession, ServerEvent};

async fn handle_session(session: &dyn RealtimeSession) -> Result<()> {
    // Send audio
    session.send_audio(audio_chunk).await?;

    // Receive events
    while let Some(event) = session.next_event().await {
        match event? {
            ServerEvent::AudioDelta { delta, .. } => { /* play audio */ }
            ServerEvent::FunctionCallDone { name, arguments, call_id, .. } => {
                // Execute tool and respond
                let result = execute_tool(&name, &arguments);
                session.send_tool_response(call_id, result).await?;
            }
            _ => {}
        }
    }
    Ok(())
}

Required Methods§

Source

fn session_id(&self) -> &str

Get the session ID.

Source

fn is_connected(&self) -> bool

Check if the session is currently connected.

Source

fn send_audio<'life0, 'life1, 'async_trait>( &'life0 self, audio: &'life1 AudioChunk, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Send raw audio data to the server.

The audio should be in the format specified in the session configuration.

Source

fn send_audio_base64<'life0, 'life1, 'async_trait>( &'life0 self, audio_base64: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Send base64-encoded audio directly.

Source

fn send_text<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Send a text message.

Source

fn send_tool_response<'life0, 'async_trait>( &'life0 self, response: ToolResponse, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Send a tool/function response.

Source

fn commit_audio<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Commit the audio buffer (for manual VAD mode).

Source

fn clear_audio<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Clear the audio input buffer.

Source

fn create_response<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Trigger a response from the model.

Source

fn interrupt<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Interrupt/cancel the current response.

Source

fn send_event<'life0, 'async_trait>( &'life0 self, event: ClientEvent, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Send a raw client event.

Source

fn next_event<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<Result<ServerEvent, RealtimeError>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Get the next event from the server.

Returns None when the session is closed.

Source

fn events( &self, ) -> Pin<Box<dyn Stream<Item = Result<ServerEvent, RealtimeError>> + Send + '_>>

Get a stream of server events.

Source

fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Close the session gracefully.

Source

fn mutate_context<'life0, 'async_trait>( &'life0 self, config: RealtimeConfig, ) -> Pin<Box<dyn Future<Output = Result<ContextMutationOutcome, RealtimeError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Attempt to mutate the session parameters mid-flight.

For providers that support native hot-swapping (e.g., OpenAI), this mutates the parameters without tearing down the connection and returns Ok(ContextMutationOutcome::Applied). For providers that require a static configuration (e.g., Gemini), this returns Ok(ContextMutationOutcome::RequiresResumption(config)) to signal the runner to queue a session reconnect or resumption safely.

Implementors§