rtb-chat 0.7.4

Unified multi-provider AI chat client for Claude, OpenAI, Gemini, and OpenAI-compatible endpoints. Part of the phpboyscout Rust toolkit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! [`AiClient`] — typed façade over `genai` + the Anthropic-direct
//! path.

use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use futures_util::{Stream, StreamExt};
use schemars::JsonSchema;
use secrecy::ExposeSecret;
use serde::de::DeserializeOwned;

use crate::anthropic::{AnthropicTransport, ReqwestAnthropic};
use crate::config::{validate_base_url, Config, Provider};
use crate::error::{redact, AiError};
use crate::message::{ContentBlock, Message, Usage};
use crate::thinking::ThinkingMode;

/// One-shot or streaming chat request.
#[derive(Debug, Clone, Default)]
pub struct ChatRequest {
    /// Optional system prompt. Goes to Anthropic's top-level
    /// `system` field; for `genai`-backed providers it lands in the
    /// first message with role `system`.
    pub system: Option<String>,
    /// Conversation history + the current user message. Last item
    /// is conventionally the user's turn.
    pub messages: Vec<Message>,
    /// Sampling temperature.
    pub temperature: Option<f32>,
    /// Hard cap on the assistant's reply.
    pub max_tokens: Option<u32>,
    /// Anthropic-only: enables prompt caching at the system prompt
    /// + first user message. Silently ignored on other providers.
    pub cache_control: bool,
    /// Anthropic-only: enables extended-thinking with the supplied
    /// budget. Silently ignored on other providers.
    pub thinking: Option<ThinkingMode>,
}

/// Non-streaming chat response.
#[derive(Debug, Clone)]
pub struct ChatResponse {
    /// Assistant's reply.
    pub message: Message,
    /// Token counts the provider reported.
    pub usage: Usage,
    /// Citations, populated only on the Anthropic-direct path when
    /// the model emits them. Empty otherwise.
    pub citations: Vec<crate::message::Citation>,
}

/// One event from the streaming chat path.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ChatStreamEvent {
    /// Regular assistant token.
    Token(String),
    /// Anthropic-only: a token from the extended-thinking stream.
    /// Other providers never emit this.
    ThinkingToken(String),
    /// Final event, carrying the cumulative usage.
    Done(Usage),
    /// Stream-level error; ends the stream.
    Error(AiError),
}

/// Async stream of [`ChatStreamEvent`]s. The stream is `!Sync` to
/// avoid pinning trade-offs in callers; it is `Send` so it can move
/// across `tokio::spawn` boundaries.
pub struct ChatStream {
    inner: Pin<Box<dyn Stream<Item = ChatStreamEvent> + Send>>,
}

impl ChatStream {
    pub(crate) fn new(stream: Pin<Box<dyn Stream<Item = ChatStreamEvent> + Send>>) -> Self {
        Self { inner: stream }
    }
}

impl Stream for ChatStream {
    type Item = ChatStreamEvent;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.inner.poll_next_unpin(cx)
    }
}

impl std::fmt::Debug for ChatStream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChatStream").finish_non_exhaustive()
    }
}

// ---------------------------------------------------------------------
// AiClient
// ---------------------------------------------------------------------

enum Backend {
    Anthropic(Arc<dyn AnthropicTransport>),
    Genai(genai::Client),
}

impl std::fmt::Debug for Backend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Anthropic(_) => f.debug_struct("Backend::Anthropic").finish_non_exhaustive(),
            Self::Genai(_) => f.debug_struct("Backend::Genai").finish_non_exhaustive(),
        }
    }
}

/// Typed AI client. Construct via [`AiClient::new`].
#[derive(Debug)]
pub struct AiClient {
    config: Config,
    backend: Backend,
}

impl AiClient {
    /// Build a client. Validates `config.base_url`, builds the
    /// underlying HTTP client, and stamps the appropriate backend
    /// (Anthropic-direct or `genai`).
    ///
    /// # Errors
    ///
    /// [`AiError::InvalidConfig`] on a bad base URL, an empty API
    /// key, or a `reqwest::Client` build failure.
    pub fn new(config: Config) -> Result<Self, AiError> {
        Self::validate(&config)?;
        let backend = if config.provider.is_anthropic() {
            let client = build_reqwest_client(&config)?;
            tracing::info!(
                provider = ?config.provider,
                host = %backend_host(&config),
                "rtb-chat: AiClient ready (anthropic-direct)",
            );
            Backend::Anthropic(Arc::new(ReqwestAnthropic::new(Arc::new(client))))
        } else {
            // For genai-backed providers we let genai create the
            // underlying HTTP client. The API key is supplied via
            // env var of the relevant provider; genai resolves it
            // internally. We set the variable for the duration of
            // the constructor — see `genai_set_key` below.
            genai_set_key(&config);
            tracing::info!(
                provider = ?config.provider,
                host = %backend_host(&config),
                "rtb-chat: AiClient ready (genai)",
            );
            Backend::Genai(build_genai_client(&config))
        };
        Ok(Self { config, backend })
    }

    fn validate(config: &Config) -> Result<(), AiError> {
        if config.api_key.expose_secret().is_empty() {
            return Err(AiError::InvalidConfig("api_key must not be empty".into()));
        }
        if config.model.is_empty() {
            return Err(AiError::InvalidConfig("model must not be empty".into()));
        }
        if let Some(url) = &config.base_url {
            validate_base_url(url, config.allow_insecure_base_url)?;
        }
        Ok(())
    }

    /// One-shot chat completion.
    ///
    /// # Errors
    ///
    /// Any [`AiError`] variant — provider errors (4xx / 5xx), HTTP
    /// transport failures, rate-limit responses.
    pub async fn chat(&self, req: ChatRequest) -> Result<ChatResponse, AiError> {
        match &self.backend {
            Backend::Anthropic(t) => t.chat(&self.config, req).await,
            Backend::Genai(c) => genai_chat(c, &self.config, req).await,
        }
    }

    /// Streaming chat completion.
    ///
    /// # Errors
    ///
    /// Connection-time errors surface synchronously; per-event
    /// errors surface as [`ChatStreamEvent::Error`] inside the
    /// returned stream.
    pub async fn chat_stream(&self, req: ChatRequest) -> Result<ChatStream, AiError> {
        match &self.backend {
            Backend::Anthropic(t) => t.chat_stream(&self.config, req).await,
            Backend::Genai(c) => genai_chat_stream(c, &self.config, req).await,
        }
    }

    /// Structured output: validates the response against `T`'s
    /// `JsonSchema` before deserialising.
    ///
    /// The request is augmented to instruct the model to emit JSON
    /// matching the schema — see [`ChatRequest::system`]; the
    /// caller's system prompt (if any) is prepended.
    ///
    /// # Errors
    ///
    /// [`AiError::SchemaValidation`] when the response doesn't match
    /// the schema; [`AiError::Deserialize`] when it matches the
    /// schema but `serde::Deserialize` for `T` rejects it; any
    /// underlying [`AiError`] from the chat call.
    pub async fn chat_structured<T>(&self, req: ChatRequest) -> Result<T, AiError>
    where
        T: DeserializeOwned + JsonSchema,
    {
        let schema = serde_json::to_value(schemars::schema_for!(T))
            .map_err(|e| AiError::InvalidConfig(redact(&e.to_string())))?;
        let augmented = augment_request_for_schema(req, &schema);
        let resp = self.chat(augmented).await?;
        let body =
            resp.message.content.iter().filter_map(ContentBlock::as_text).collect::<String>();
        let parsed: serde_json::Value = serde_json::from_str(&body)
            .map_err(|e| AiError::Deserialize(redact(&e.to_string())))?;
        let validator = jsonschema::validator_for(&schema)
            .map_err(|e| AiError::SchemaValidation(redact(&e.to_string())))?;
        if let Err(err) = validator.validate(&parsed) {
            return Err(AiError::SchemaValidation(redact(&err.to_string())));
        }
        serde_json::from_value::<T>(parsed)
            .map_err(|e| AiError::Deserialize(redact(&e.to_string())))
    }
}

fn build_reqwest_client(config: &Config) -> Result<reqwest::Client, AiError> {
    let mut builder = reqwest::Client::builder()
        .https_only(!config.allow_insecure_base_url)
        .timeout(config.timeout)
        .user_agent(concat!("rtb-chat/", env!("CARGO_PKG_VERSION")));
    if config.allow_insecure_base_url {
        // `https_only(false)` already accepts http; nothing more
        // needed but the explicit reset keeps the call self-
        // documenting.
        builder = builder.https_only(false);
    }
    builder.build().map_err(|e| AiError::InvalidConfig(redact(&e.to_string())))
}

fn backend_host(config: &Config) -> String {
    config.base_url.as_ref().and_then(|u| u.host_str().map(String::from)).unwrap_or_else(|| {
        match config.provider {
            Provider::Anthropic | Provider::AnthropicLocal => "api.anthropic.com".into(),
            Provider::OpenAi => "api.openai.com".into(),
            Provider::Gemini => "generativelanguage.googleapis.com".into(),
            Provider::Ollama => "localhost".into(),
            Provider::OpenAiCompatible => "openai-compatible".into(),
        }
    })
}

fn augment_request_for_schema(mut req: ChatRequest, schema: &serde_json::Value) -> ChatRequest {
    let instructions = format!(
        "You MUST respond with a single JSON value matching this schema. \
         No prose, no code fences:\n{schema}",
    );
    req.system = match req.system.take() {
        Some(prefix) => Some(format!("{prefix}\n\n{instructions}")),
        None => Some(instructions),
    };
    req
}

// ---------------------------------------------------------------------
// genai-backed path
// ---------------------------------------------------------------------

fn genai_set_key(config: &Config) {
    // genai reads provider keys from environment variables. Setting
    // the var here makes the key reachable to genai's lazy client
    // builder. SAFETY: env var mutation is a known footgun under
    // racy multi-threaded constructor calls; rtb-chat callers
    // construct one client per process (the typical pattern).
    let var = match config.provider {
        Provider::OpenAi | Provider::OpenAiCompatible => "OPENAI_API_KEY",
        Provider::Gemini => "GEMINI_API_KEY",
        // Local inference + the Anthropic-direct path don't go
        // through genai's env-var key resolution.
        Provider::Ollama | Provider::Anthropic | Provider::AnthropicLocal => return,
    };
    // Safety: same-process env mutation. Documented above; matches
    // the rtb-config tests' rationale.
    #[allow(unsafe_code)]
    unsafe {
        std::env::set_var(var, config.api_key.expose_secret());
    }
}

fn build_genai_client(config: &Config) -> genai::Client {
    let mut builder = genai::Client::builder()
        .with_web_config(genai::WebConfig::default().with_timeout(config.timeout));
    if let Some(url) = &config.base_url {
        // genai appends paths such as `chat/completions` by string
        // concatenation, so the base must end in `/`.
        let mut base = url.as_str().to_string();
        if !base.ends_with('/') {
            base.push('/');
        }
        let endpoint = genai::resolver::Endpoint::from_owned(base);
        builder =
            builder.with_service_target_resolver_fn(move |mut target: genai::ServiceTarget| {
                target.endpoint = endpoint.clone();
                Ok(target)
            });
        // genai otherwise picks the adapter from the model name, and a
        // name it does not recognise lands on the Ollama adapter.
        if let Some(kind) = genai_adapter_for(config.provider) {
            builder = builder.with_adapter_kind(kind);
        }
    }
    builder.build()
}

const fn genai_adapter_for(provider: Provider) -> Option<genai::adapter::AdapterKind> {
    use genai::adapter::AdapterKind;
    match provider {
        Provider::OpenAi | Provider::OpenAiCompatible => Some(AdapterKind::OpenAI),
        Provider::Gemini => Some(AdapterKind::Gemini),
        Provider::Ollama => Some(AdapterKind::Ollama),
        Provider::Anthropic | Provider::AnthropicLocal => None,
    }
}

async fn genai_chat(
    client: &genai::Client,
    config: &Config,
    req: ChatRequest,
) -> Result<ChatResponse, AiError> {
    let chat_req = build_genai_request(&req);
    let options = genai_options(&req);
    let resp = client
        .exec_chat(&config.model, chat_req, Some(&options))
        .await
        .map_err(|e| AiError::Provider(redact(&e.to_string())))?;
    let text = resp.first_text().unwrap_or_default().to_string();
    let usage = genai_usage(&resp);
    Ok(ChatResponse { message: Message::assistant(text), usage, citations: Vec::new() })
}

async fn genai_chat_stream(
    client: &genai::Client,
    config: &Config,
    req: ChatRequest,
) -> Result<ChatStream, AiError> {
    let chat_req = build_genai_request(&req);
    // genai leaves StreamEnd::captured_usage empty unless asked.
    let options = genai_options(&req).with_capture_usage(true);
    let resp = client
        .exec_chat_stream(&config.model, chat_req, Some(&options))
        .await
        .map_err(|e| AiError::Provider(redact(&e.to_string())))?;
    let stream = futures_util::StreamExt::map(resp.stream, |event| {
        use genai::chat::ChatStreamEvent as G;
        match event {
            Ok(G::Chunk(chunk)) => ChatStreamEvent::Token(chunk.content),
            Ok(G::ReasoningChunk(chunk)) => ChatStreamEvent::ThinkingToken(chunk.content),
            Ok(G::End(end)) => ChatStreamEvent::Done(genai_usage_from_end(&end)),
            // Filtered out below — emitted as empty `Token`s and
            // dropped by the filter step. Keeps the match exhaustive
            // for future genai event variants.
            Ok(G::Start | G::ToolCallChunk(_) | G::ThoughtSignatureChunk(_)) => {
                ChatStreamEvent::Token(String::new())
            }
            Err(e) => ChatStreamEvent::Error(AiError::Provider(redact(&e.to_string()))),
        }
    });
    // Filter out empty `Start` / tool-chunk emits.
    let stream = futures_util::StreamExt::filter(stream, |e| {
        let keep = !matches!(e, ChatStreamEvent::Token(t) if t.is_empty());
        std::future::ready(keep)
    });
    Ok(ChatStream::new(Box::pin(stream)))
}

fn genai_options(req: &ChatRequest) -> genai::chat::ChatOptions {
    let mut options = genai::chat::ChatOptions::default();
    if let Some(temperature) = req.temperature {
        options = options.with_temperature(widen_f32(temperature));
    }
    if let Some(max_tokens) = req.max_tokens {
        options = options.with_max_tokens(max_tokens);
    }
    options
}

/// `f64::from(0.7_f32)` is `0.699999988079071`, and that is what would
/// reach the wire. Going through the shortest decimal keeps it `0.7`.
fn widen_f32(value: f32) -> f64 {
    value.to_string().parse().unwrap_or_else(|_| f64::from(value))
}

fn build_genai_request(req: &ChatRequest) -> genai::chat::ChatRequest {
    let mut chat = genai::chat::ChatRequest::default();
    if let Some(system) = &req.system {
        chat = chat.with_system(system.clone());
    }
    for msg in &req.messages {
        let text =
            msg.content.iter().filter_map(ContentBlock::as_text).collect::<Vec<_>>().join("\n");
        match msg.role {
            crate::message::Role::User => {
                chat = chat.append_message(genai::chat::ChatMessage::user(text));
            }
            crate::message::Role::Assistant => {
                chat = chat.append_message(genai::chat::ChatMessage::assistant(text));
            }
            crate::message::Role::System => {
                chat = chat.with_system(text);
            }
        }
    }
    chat
}

fn genai_usage(resp: &genai::chat::ChatResponse) -> Usage {
    let u = &resp.usage;
    Usage {
        input_tokens: u.prompt_tokens.unwrap_or(0) as u32,
        output_tokens: u.completion_tokens.unwrap_or(0) as u32,
        cache_creation_input_tokens: 0,
        cache_read_input_tokens: 0,
    }
}

fn genai_usage_from_end(end: &genai::chat::StreamEnd) -> Usage {
    end.captured_usage.as_ref().map_or_else(Usage::default, |u| Usage {
        input_tokens: u.prompt_tokens.unwrap_or(0) as u32,
        output_tokens: u.completion_tokens.unwrap_or(0) as u32,
        cache_creation_input_tokens: 0,
        cache_read_input_tokens: 0,
    })
}

#[cfg(test)]
mod tests {
    use super::widen_f32;

    #[test]
    fn widen_f32_keeps_the_decimal_value() {
        assert!((widen_f32(0.7) - 0.7).abs() < f64::EPSILON);
        assert!((widen_f32(1.0) - 1.0).abs() < f64::EPSILON);
    }
}