Skip to main content

rig_core/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(
3    test,
4    allow(
5        clippy::expect_used,
6        clippy::indexing_slicing,
7        clippy::panic,
8        clippy::unwrap_used,
9        clippy::unreachable
10    )
11)]
12//! Provider-agnostic model, message, conversation, tool, and vector-store contracts.
13//! Provider configurations build endpoint wires, and a [`Model`] binds a
14//! wire to a [`driver::Transport`]; a [`DynModel`] is a model erased to
15//! its operation, for consumers that store one. Companion crates supply
16//! transports, agent runtimes, and external storage integrations.
17//!
18//! `rig-core` does not force serde_json's `preserve_order` or `float_roundtrip`.
19//! Without them, schema properties and JSON text in prompts, embeddings and
20//! tool results use sorted object keys, and parsed floats can differ in their
21//! low bits. Restore the previous configuration in your application's
22//! dependencies with
23//! `serde_json = { version = "1", features = ["preserve_order", "float_roundtrip"] }`.
24//!
25//! Cargo unifies these features: `rig-cassette` with `traffic` (including
26//! `rig-traffic`) enables both; `rig-claude-cloak`, `rig-mcp-client` and
27//! `rig-mongodb` through BSON enable `preserve_order`. Rig has no forwarding feature for either setting.
28//!
29//! ```no_run
30//! use rig_core::DynModel;
31//! use rig_core::completion::{CompletionRequest, CompletionResponse};
32//! use rig_core::error::ProviderError;
33//! use rig_core::operation::Completion;
34//!
35//! async fn ask(model: &DynModel<Completion>) -> Result<CompletionResponse, ProviderError> {
36//!     let request = CompletionRequest::new("Who are you?");
37//!     model.call(request).await
38//! }
39//! ```
40
41extern crate self as rig;
42
43pub mod audio_generation;
44pub mod catalog;
45pub mod client;
46pub mod completion;
47pub mod conversation;
48pub mod driver;
49pub mod effect;
50pub mod embeddings;
51pub mod error;
52pub use rig_http::http_client;
53pub mod id;
54pub mod image_generation;
55/// Internal JSON helpers shared with sibling runtime crates (e.g. `rig-agent`).
56/// Not part of rig-core's stable public API.
57#[doc(hidden)]
58pub mod json_utils;
59pub mod loaders;
60pub mod markers;
61pub mod model;
62pub mod operation;
63pub mod prelude;
64pub(crate) mod provider_response;
65pub mod providers;
66pub mod redact;
67pub mod rerank;
68pub mod serve;
69
70pub mod streaming;
71pub mod structured_output;
72#[cfg(any(test, feature = "test-utils"))]
73#[cfg_attr(docsrs, doc(cfg(feature = "test-utils")))]
74pub mod test_utils;
75pub mod tool;
76pub mod transcription;
77pub mod vector_store;
78pub use rig_http::wasm_compat;
79pub mod wire;
80pub use rig_http::ws_client;
81
82pub use completion::message;
83pub use driver::{DynModel, Model};
84pub use embeddings::Embed;
85pub use error::{ErrorKind, ErrorReport, ProviderError};
86pub use provider_response::ProviderResponseError;
87// `schemars`, `serde`, and `serde_json` are re-exported so macro-generated
88// code (and downstream crates) can resolve them through Rig instead of
89// requiring a direct dependency on each.
90pub use schemars;
91pub use serde;
92pub use serde_json;
93
94#[cfg(feature = "derive")]
95#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
96pub use rig_derive::ContextValue;
97#[cfg(feature = "derive")]
98#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
99pub use rig_derive::Embed;
100
101#[cfg(feature = "derive")]
102#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
103pub use rig_derive::rig_tool;
104
105pub mod telemetry;
106
107/// Milliseconds since the Unix epoch at `at`, also on the web; 0 before it.
108pub fn millis(at: web_time::SystemTime) -> u64 {
109    at.duration_since(web_time::UNIX_EPOCH)
110        .map(|elapsed| u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX))
111        .unwrap_or_default()
112}
113
114/// Milliseconds since the Unix epoch, also on the web.
115pub fn now_ms() -> u64 {
116    millis(web_time::SystemTime::now())
117}
118
119/// Lock `mutex`, taking the guard even if a panicking holder poisoned it.
120pub(crate) fn lock<T>(mutex: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
121    mutex
122        .lock()
123        .unwrap_or_else(std::sync::PoisonError::into_inner)
124}
125
126// Native runtime values must retain their thread-safety bounds.
127#[cfg(not(target_family = "wasm"))]
128const _: fn() = || {
129    fn assert_send_sync_static<T: Send + Sync + 'static>() {}
130    fn assert_send_static<T: Send + 'static>() {}
131    assert_send_sync_static::<tool::ManagedToolToken>();
132    assert_send_sync_static::<streaming::StreamEvent>();
133    // The serializable identity a typed view is resolved from.
134    assert_send_sync_static::<completion::ModelRef>();
135    assert_send_sync_static::<tool::DynamicTool>();
136    // One erased transport, shared by every provider a host binds.
137    assert_send_sync_static::<http_client::DynHttpClient>();
138    // A live stream is owned by one poller: `Send` so it can move to a worker,
139    // not `Sync`.
140    assert_send_static::<streaming::CompletionStream>();
141};