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
43#[cfg(feature = "audio")]
44#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
45pub mod audio_generation;
46pub mod catalog;
47pub mod client;
48pub mod completion;
49pub mod conversation;
50pub mod driver;
51pub mod effect;
52pub mod embeddings;
53pub mod error;
54pub use rig_http::http_client;
55pub mod id;
56#[cfg(feature = "image")]
57#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
58pub mod image_generation;
59/// Internal JSON helpers shared with sibling runtime crates (e.g. `rig-agent`).
60/// Not part of rig-core's stable public API.
61#[doc(hidden)]
62pub mod json_utils;
63pub mod loaders;
64pub mod markers;
65pub mod model;
66pub mod observe;
67pub mod operation;
68pub mod prelude;
69pub(crate) mod provider_response;
70pub mod providers;
71pub mod redact;
72pub mod rerank;
73pub mod serve;
74
75pub mod streaming;
76pub mod structured_output;
77#[cfg(any(test, feature = "test-utils"))]
78#[cfg_attr(docsrs, doc(cfg(feature = "test-utils")))]
79pub mod test_utils;
80pub mod tool;
81pub mod transcription;
82pub mod vector_store;
83pub use rig_http::wasm_compat;
84pub mod wire;
85#[cfg(feature = "websocket")]
86#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
87pub use rig_http::ws_client;
88
89pub use completion::message;
90pub use driver::{DynModel, Model};
91pub use embeddings::Embed;
92pub use error::{ErrorKind, ErrorReport, ProviderError};
93pub use provider_response::ProviderResponseError;
94// `schemars`, `serde`, and `serde_json` are re-exported so macro-generated
95// code (and downstream crates) can resolve them through Rig instead of
96// requiring a direct dependency on each.
97pub use schemars;
98pub use serde;
99pub use serde_json;
100
101#[cfg(feature = "derive")]
102#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
103pub use rig_derive::ContextValue;
104#[cfg(feature = "derive")]
105#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
106pub use rig_derive::Embed;
107
108#[cfg(feature = "derive")]
109#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
110pub use rig_derive::rig_tool;
111
112pub mod telemetry;
113
114/// Milliseconds since the Unix epoch at `at`, also on the web; 0 before it.
115pub fn millis(at: web_time::SystemTime) -> u64 {
116    at.duration_since(web_time::UNIX_EPOCH)
117        .map(|elapsed| u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX))
118        .unwrap_or_default()
119}
120
121/// Milliseconds since the Unix epoch, also on the web.
122pub fn now_ms() -> u64 {
123    millis(web_time::SystemTime::now())
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};