diff --git a/Cargo.toml b/Cargo.toml index 8c7cb45..8bc5370 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,10 @@ license = "GPL-3.0-or-later" description = "Rust SDK for the TinyHumans backend" repository = "https://github.com/tinyhumansai/sdk" +[features] +default = ["socket"] +socket = ["dep:rust_socketio"] + [dependencies] base64 = "0.22" # `stream` is for the SSE session stream in `src/sse.rs`; every other route @@ -21,7 +25,7 @@ percent-encoding = "2" # what earns these two their place in an otherwise dependency-light crate. futures = "0.3" # Socket.IO is a distinct framed protocol over Engine.IO, not a raw WebSocket. -rust_socketio = { version = "0.6", features = ["async"] } +rust_socketio = { version = "0.6", features = ["async"], optional = true } tokio = { version = "1", features = ["sync", "time"] } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index d494ef2..143bc75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ use url::Url; pub mod api; pub mod generated_public_routes; pub mod jwt; +#[cfg(feature = "socket")] pub mod socket; pub mod sse; @@ -50,14 +51,19 @@ pub enum Error { Header(#[from] reqwest::header::InvalidHeaderValue), #[error("response decoding failed: {0}")] Decode(#[from] serde_json::Error), + #[cfg(feature = "socket")] #[error("socket.io transport failed: {0}")] Socket(Box), + #[cfg(feature = "socket")] #[error("a bearer token is required for socket.io connections")] MissingSocketToken, + #[cfg(feature = "socket")] #[error("socket event {0} did not carry a JSON payload")] UnexpectedSocketPayload(String), + #[cfg(feature = "socket")] #[error("socket acknowledgement timed out")] SocketAckTimeout, + #[cfg(feature = "socket")] #[error("socket acknowledgement channel closed")] SocketAckClosed, #[error("route is intentionally not exposed by the SDK: {0} {1}")] @@ -75,6 +81,7 @@ pub enum Error { }, } +#[cfg(feature = "socket")] impl From for Error { fn from(error: rust_socketio::Error) -> Self { Self::Socket(Box::new(error)) @@ -208,6 +215,7 @@ impl TinyHumansClient { /// The returned connection receives every public socket event through one /// generic stream and also exposes typed helpers for the Medulla harness /// and workflow protocol. + #[cfg(feature = "socket")] pub async fn connect_socket(&self) -> Result { let token = self.http.token.clone().ok_or(Error::MissingSocketToken)?; socket::SocketConnection::connect(self.http.base_url.clone(), token).await diff --git a/tests/socket.rs b/tests/socket.rs index a92917f..d7ced44 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "socket")] + use serde_json::json; use tinyhumans_sdk::socket::medulla::{ CapabilitiesRequest, RegisterWorkflows, TaskRun, WorkflowRequest, WorkflowRequestOp,