Skip to main content

rtb_chat/
lib.rs

1//! Unified AI chat client.
2//!
3//! Wraps [`genai`] for the multi-provider mainstream (`OpenAI` /
4//! Gemini / Ollama / OpenAI-compatible) and drops down to a direct
5//! `reqwest`-on-Anthropic-Messages path for features `genai` does not
6//! yet surface — prompt caching, extended thinking, citations.
7//!
8//! Structured output uses `schemars::JsonSchema` on caller-supplied
9//! types: the schema is sent with the request, and the response is
10//! validated with `jsonschema` before deserialising.
11//!
12//! Formerly `rtb-ai` in the
13//! [rust-tool-base](https://gitlab.com/phpboyscout/rust-tool-base)
14//! monorepo; renamed to `rtb-chat` at extraction. The authoritative
15//! contract remains the [rtb-ai v0.1 spec](https://gitlab.com/phpboyscout/rust-tool-base/-/wikis/specs/0016-rtb-ai-v0.1).
16//!
17//! # Lint exception
18//!
19//! Crate-level `deny(unsafe_code)` (not `forbid`) so the genai-key
20//! shim in [`client`] can locally `allow(unsafe_code)` the
21//! `std::env::set_var` it needs to hand the API key to genai. No
22//! hand-rolled `unsafe` blocks anywhere else.
23
24#![deny(unsafe_code)]
25// Token counts cross the u64 (provider response) ↔ u32 (Usage)
26// boundary frequently; the saturating defaults are intentional.
27#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
28// The Anthropic helpers are `pub(crate)` for the `test_hooks` re-
29// export pattern; clippy's `redundant_pub_crate` is overzealous here.
30#![allow(clippy::redundant_pub_crate)]
31
32pub mod client;
33pub mod config;
34pub mod error;
35pub mod message;
36pub mod thinking;
37
38pub(crate) mod anthropic;
39
40pub use client::{AiClient, ChatRequest, ChatResponse, ChatStream, ChatStreamEvent};
41pub use config::{validate_base_url, Config, Provider};
42pub use error::AiError;
43pub use message::{Citation, ContentBlock, Message, Role, Usage};
44pub use thinking::ThinkingMode;
45
46/// Internal hooks exposed for unit-test reach-throughs. Not part of
47/// the stable public API and may change between minor releases.
48#[doc(hidden)]
49pub mod test_hooks {
50    pub use crate::anthropic::{build_request_body, parse_chat_response};
51}