Crate livespeech_sdk

Crate livespeech_sdk 

Source
Expand description

§LiveSpeech SDK

A Rust SDK for real-time speech-to-speech AI conversations.

§Quick Start

use livespeech_sdk::{LiveSpeechClient, Config, Region, SessionConfig, LiveSpeechEvent};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create configuration
    let config = Config::builder()
        .region(Region::ApNortheast2)
        .api_key("your-api-key")
        .build()?;

    // Create client
    let client = LiveSpeechClient::new(config);

    // Subscribe to events (recommended)
    let mut events = client.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = events.recv().await {
            match event {
                LiveSpeechEvent::Ready(_) => println!("Ready for audio"),
                LiveSpeechEvent::UserTranscript(e) => println!("You said: {}", e.text),
                LiveSpeechEvent::Response(e) => println!("AI: {}", e.text),
                LiveSpeechEvent::Audio(e) => println!("Audio: {} bytes", e.data.len()),
                LiveSpeechEvent::TurnComplete(_) => println!("Turn complete"),
                LiveSpeechEvent::Error(e) => eprintln!("Error: {}", e.message),
                _ => {}
            }
        }
    });

    // Connect and start session
    client.connect().await?;
    let session_id = client.start_session(Some(
        SessionConfig::new("You are a helpful assistant.")
    )).await?;
    println!("Session: {}", session_id);

    // Stream audio
    client.audio_start().await?;
    let audio_chunk: Vec<u8> = vec![0u8; 3200]; // PCM16 audio
    client.send_audio_chunk(&audio_chunk).await?;
    client.audio_end().await?;

    // Cleanup
    client.end_session().await?;
    client.disconnect().await;

    Ok(())
}

Re-exports§

pub use client::ConnectionState;
pub use client::LiveSpeechClient;
pub use error::LiveSpeechError;
pub use error::Result;
pub use types::Config;
pub use types::ConfigBuilder;
pub use types::ConfigError;
pub use types::FunctionParameters;
pub use types::PipelineMode;
pub use types::Region;
pub use types::SessionConfig;
pub use types::Tool;
pub use types::AudioEvent;
pub use types::AudioHandler;
pub use types::ConnectedEvent;
pub use types::DisconnectedEvent;
pub use types::DisconnectReason;
pub use types::ErrorCode;
pub use types::ErrorEvent;
pub use types::ErrorHandler;
pub use types::LiveSpeechEvent;
pub use types::ReadyEvent;
pub use types::ReconnectingEvent;
pub use types::ResponseEvent;
pub use types::ResponseHandler;
pub use types::SessionEndedEvent;
pub use types::SessionStartedEvent;
pub use types::ToolCallEvent;
pub use types::TurnCompleteEvent;
pub use types::UserTranscriptEvent;
pub use types::ClientMessage;
pub use types::ServerMessage;
pub use types::ToolResponsePayload;
pub use audio::bytes_to_int16;
pub use audio::create_wav_header;
pub use audio::decode_from_base64;
pub use audio::encode_to_base64;
pub use audio::float32_to_int16;
pub use audio::int16_to_bytes;
pub use audio::int16_to_float32;
pub use audio::wrap_pcm_in_wav;
pub use audio::AudioEncoder;

Modules§

audio
Audio encoding utilities for the LiveSpeech SDK
client
LiveSpeech client implementation
error
Error types for the LiveSpeech SDK
types
Type definitions for the LiveSpeech SDK