Skip to main content

ts_client/
lib.rs

1// Copyright (c) 2026 LightSeek Foundation
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! A Rust frontend's connection to TokenSpeed engines.
22//!
23//! The client binds the handshake, input and output sockets; the engines
24//! (one per attention-DP rank) connect in and register. The client then routes
25//! tokenized requests to engines and streams each request's outputs back.
26//! It does not tokenize or detokenize: the frontend owns both, and so also
27//! matches stop strings itself. The engine refuses requests that carry stop
28//! strings on this wire; send stop token ids.
29//!
30//! ```no_run
31//! # async fn run() -> ts_client::Result<()> {
32//! use std::time::Duration;
33//! use ts_client::{ClientConfig, EngineClient, LoadBalance, Routing};
34//! use ts_wire::{SamplingParams, TokenizedGenerateReqInput};
35//!
36//! let client = EngineClient::connect(ClientConfig {
37//!     handshake_address: "tcp://127.0.0.1:30500".into(),
38//!     input_address: "tcp://127.0.0.1:0".into(),
39//!     output_address: "tcp://127.0.0.1:0".into(),
40//!     first_engine_index: 0,
41//!     num_engines: 1,
42//!     routing: Routing::Aggregated(LoadBalance::LeastInFlight),
43//!     handshake_timeout: Duration::from_secs(600),
44//!     send_timeout: Duration::from_secs(5),
45//! })
46//! .await?;
47//! let mut stream = client
48//!     .generate(TokenizedGenerateReqInput {
49//!         input_ids: Some(vec![1, 2, 3]),
50//!         stream: true,
51//!         ..TokenizedGenerateReqInput::new("r1", SamplingParams::default())
52//!     })
53//!     .await?;
54//! while let Some(chunk) = stream.next().await {
55//!     println!("{:?}", chunk.expect("engine alive").output_ids);
56//! }
57//! # Ok(())
58//! # }
59//! ```
60
61#![forbid(unsafe_code)]
62
63mod client;
64mod config;
65mod error;
66mod handshake;
67
68pub use client::{ClientListener, EngineClient, EngineLoad, OutputChunk, RequestStream};
69pub use config::{ClientConfig, LoadBalance, Routing};
70pub use error::{Error, Result, StreamError};
71pub use handshake::EngineInfo;