Skip to main content

Crate ahp_ws

Crate ahp_ws 

Source
Expand description

WebSocket transport adapter for the Agent Host Protocol Rust SDK.

This crate provides WebSocketTransport, an implementation of ahp::Transport backed by tokio-tungstenite. It supports both ws:// and wss:// URLs.

§TLS backends

wss:// support is selected by Cargo feature. The default, rustls-tls-native-roots, uses rustls (a pure-Rust stack, so no OpenSSL on Linux) with roots loaded from the OS trust store, which keeps dials working through a TLS-intercepting egress proxy whose CA lives in the platform store. Override it with default-features = false plus one of:

  • native-tls — the platform TLS stack (SChannel / Secure Transport / OpenSSL).
  • rustls-tls-native-roots — rustls with OS-trust-store roots (default).
  • rustls-tls-webpki-roots — rustls with the bundled Mozilla root set; does not see enterprise/proxy CAs that live only in the OS store.

With no TLS feature enabled, only ws:// works and wss:// fails at connect time.

If more than one backend ends up enabled — which Cargo feature unification can do when several crates in the graph request different ones — native-tls takes precedence over the rustls backends, because tokio-tungstenite’s automatic connector prefers it. Disable the default with default-features = false if you need to guarantee a rustls backend.

§Companion crates

  • ahp — the async client and reducers
  • ahp-types — wire types only

§Quickstart

use ahp::{Client, ClientConfig, SubscriptionEvent};
use ahp_ws::WebSocketTransport;

let transport = WebSocketTransport::connect("ws://localhost:12345").await?;
let client = Client::connect(transport, ClientConfig::default()).await?;

client.initialize("my-client".into(), vec!["0.1.0".into()], vec!["ahp-root://".into()]).await?;

let mut sub = client.attach_subscription("ahp-root://").await;
while let Some(SubscriptionEvent::Action(env)) = sub.recv().await {
    println!("seq={} action={:?}", env.server_seq, env.action);
}

client.shutdown().await;

§Bring your own connection

When you need custom TLS, headers, or a pre-existing socket, drive tokio-tungstenite yourself and wrap the result with WebSocketTransport::from_stream:

use ahp_ws::WebSocketTransport;
use tokio_tungstenite::connect_async;

let (stream, _resp) = connect_async("wss://example.com/ahp").await?;
let transport = WebSocketTransport::from_stream(stream);

§Errors

Connection-time errors surface as WebSocketTransportError (URL parse or handshake failure). Once the transport is handed to ahp::Client, runtime errors are reported as ahp::TransportError on the client’s request and subscription futures.

Structs§

WebSocketTransport
A Transport backed by a tokio-tungstenite WebSocket stream.

Enums§

WebSocketTransportError
Errors that can occur while building a WebSocketTransport.