zai-rs 0.1.15

一个 Rust SDK, 用于调用 智普AI API
Documentation
//! Real-time API client

use std::sync::Arc;

use super::{models::*, session::*, types::*};

/// Real-time API client
///
/// This client manages WebSocket connections for real-time audio/video
/// communication with Zhipu AI models.
///
/// # Example
///
/// ```rust,ignore
/// use zai_rs::realTime::*;
///
/// let client = RealTimeClient::new(api_key);
///
/// // Create an audio session
/// let session = client
///     .audio_session()
///     .model(RealTimeModel::Glm4Voice)
///     .build()
///     .await?;
/// ```
pub struct RealTimeClient {
    api_key: Arc<String>,
    base_url: String,
}

impl RealTimeClient {
    /// Create a new real-time client
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: Arc::new(api_key.into()),
            base_url: "wss://open.bigmodel.cn/api/realtime".to_string(),
        }
    }

    /// Create a new client with custom base URL
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Start building an audio session
    pub fn audio_session(&self) -> AudioSessionBuilder {
        AudioSessionBuilder {
            client: self.clone(),
            model: None,
            config: None,
        }
    }

    /// Get the API key
    pub fn api_key(&self) -> &str {
        &self.api_key
    }

    /// Get the base URL
    pub fn base_url(&self) -> &str {
        &self.base_url
    }
}

impl Clone for RealTimeClient {
    fn clone(&self) -> Self {
        Self {
            api_key: Arc::clone(&self.api_key),
            base_url: self.base_url.clone(),
        }
    }
}

/// Builder for creating audio sessions
pub struct AudioSessionBuilder {
    #[allow(dead_code)]
    client: RealTimeClient,
    model: Option<RealTimeModel>,
    config: Option<SessionConfig>,
}

impl AudioSessionBuilder {
    /// Set the model to use
    pub fn model(mut self, model: RealTimeModel) -> Self {
        self.model = Some(model);
        self
    }

    /// Set the session configuration
    pub fn config(mut self, config: SessionConfig) -> Self {
        self.config = Some(config);
        self
    }

    /// Build the session
    ///
    /// Note: This is a placeholder implementation. The actual WebSocket
    /// connection will be established when the full implementation is complete.
    pub async fn build(self) -> Result<RealTimeSession, Box<dyn std::error::Error>> {
        let _model = self.model.unwrap_or_default();
        let config = self.config.unwrap_or_default();

        // Generate a session ID (in real implementation, this would come from server)
        let session_id = format!("session_{}", uuid::Uuid::new_v4());

        // In a full implementation, this would:
        // 1. Establish WebSocket connection
        // 2. Send initialization message with model and config
        // 3. Wait for session confirmation
        // 4. Return an active session handle

        let session = RealTimeSession::new(session_id, config);
        session.update_state(SessionState::Connected).await;

        Ok(session)
    }
}