fauna-api 1.0.785

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use futures_util::{SinkExt, StreamExt};
use serde_json::{Value, json};
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;

use crate::authentification::CredentialsOrApiKey;
use crate::http::{encode, websocket};

// The lab server sends panorama frames as data URLs (e.g.
// "data:image/jpeg;base64,...") for both RGB and depth. The SDK decodes them
// into image bytes so consumers can hand them straight to an image decoder.

/// The `callbacks` argument of `generateWorld` and `extendWorld`.
#[allow(clippy::type_complexity)]
#[derive(Default)]
pub struct WorldCallbacks {
    pub on_connection: Option<Box<dyn Fn(bool) + Send + Sync>>,
    pub on_panorama: Option<Box<dyn Fn(Vec<u8>, Vec<u8>) + Send + Sync>>,
    pub on_gaussians: Option<Box<dyn Fn(Vec<u8>) + Send + Sync>>,
    pub on_progress: Option<Box<dyn Fn(&str, f64) + Send + Sync>>,
    pub on_done: Option<Box<dyn Fn() + Send + Sync>>,
    pub on_cancelled: Option<Box<dyn Fn(&str) + Send + Sync>>,
    pub on_error: Option<Box<dyn Fn(&str) + Send + Sync>>,
}

/// A lab WebSocket, the `ws` `cancelWorld` takes.
#[derive(Clone)]
pub struct LabSocket(mpsc::UnboundedSender<Message>);

fn data_url(value: &Value) -> Vec<u8> {
    let encoded = value
        .as_str()
        .and_then(|value| value.split(',').nth(1))
        .unwrap_or_default();
    STANDARD.decode(encoded).unwrap_or_default()
}

async fn run(
    authorization: &CredentialsOrApiKey,
    path: &str,
    first: impl Fn() -> Value,
    callbacks: WorldCallbacks,
) {
    let params = json!({
        "Authorization": match authorization {
            CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
            CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
        }
    });
    let url = format!(
        "{}/lab/{path}?authorization={}",
        websocket(&crate::get_url()),
        encode(&params.to_string())
    );

    let Ok((stream, _)) = tokio_tungstenite::connect_async(url).await else {
        if let Some(on_error) = &callbacks.on_error {
            on_error("WebSocket connection failed");
        }
        return;
    };
    let (mut write, mut read) = stream.split();
    let (ws, mut outbox) = mpsc::unbounded_channel::<Message>();
    let socket = LabSocket(ws);
    tokio::spawn(async move {
        while let Some(message) = outbox.recv().await {
            let closing = matches!(message, Message::Close(_));
            if write.send(message).await.is_err() || closing {
                break;
            }
        }
        let _ = write.close().await;
    });
    let close = || {
        let _ = socket.0.send(Message::Close(None));
    };

    // The server registers its 'message' listener only after an async auth
    // check completes, so we wait for the 'connection' event before sending
    // the prompt — otherwise the prompt arrives during the auth await and
    // the server's WS EventEmitter drops it.
    while let Some(message) = read.next().await {
        let raw = match message {
            Ok(Message::Text(text)) => text.to_string(),
            Ok(Message::Close(_)) => return,
            Ok(_) => continue,
            Err(_) => {
                if let Some(on_error) = &callbacks.on_error {
                    on_error("WebSocket connection failed");
                }
                return;
            }
        };
        let Ok(packet) = serde_json::from_str::<Value>(&raw) else {
            continue;
        };

        match packet["event"].as_str().unwrap_or_default() {
            "connection" => {
                if let Some(on_connection) = &callbacks.on_connection {
                    on_connection(packet["ongoing"] == true);
                }
                let _ = socket.0.send(Message::text(first().to_string()));
            }
            "panorama" => {
                if let Some(on_panorama) = &callbacks.on_panorama {
                    on_panorama(data_url(&packet["rgb"]), data_url(&packet["depth"]));
                }
            }
            "gaussians" => {
                // Strip the data URL prefix and decode the base64 .splat blob.
                if let Some(on_gaussians) = &callbacks.on_gaussians {
                    on_gaussians(data_url(&packet["splat"]));
                }
            }
            "progress" => {
                if let Some(on_progress) = &callbacks.on_progress {
                    on_progress(
                        packet["stage"].as_str().unwrap_or_default(),
                        packet["progress"].as_f64().unwrap_or(0.0),
                    );
                }
            }
            "done" => {
                if let Some(on_done) = &callbacks.on_done {
                    on_done();
                }
                close();
                return;
            }
            "cancelled" => {
                if let Some(on_cancelled) = &callbacks.on_cancelled {
                    on_cancelled(packet["reason"].as_str().unwrap_or("server"));
                }
                close();
                return;
            }
            "error" => {
                if let Some(on_error) = &callbacks.on_error {
                    on_error(packet["message"].as_str().unwrap_or_default());
                }
                close();
                return;
            }
            _ => {}
        }
    }
}

pub async fn generate_world(
    authorization: &CredentialsOrApiKey,
    prompt: &str,
    callbacks: WorldCallbacks,
) {
    run(
        authorization,
        "world",
        || json!({ "event": "prompt", "prompt": prompt }),
        callbacks,
    )
    .await;
}

pub async fn cancel_world(ws: &LabSocket) {
    let _ =
        ws.0.send(Message::text(json!({ "event": "cancel" }).to_string()));
}

// Same protocol as generate_world, but the initial outbound packet is an
// `extend` event carrying both the prompt and the new camera position. The
// server runs the extend pipeline and streams back panorama / gaussians /
// progress / done in the same shape, so consumers can reuse most of the
// callback wiring.
pub async fn extend_world(
    authorization: &CredentialsOrApiKey,
    prompt: &str,
    position: [f64; 3],
    callbacks: WorldCallbacks,
) {
    run(
        authorization,
        "extend",
        || json!({ "event": "extend", "prompt": prompt, "position": position }),
        callbacks,
    )
    .await;
}