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//! ```no_run
19//! use rig_core::DynModel;
20//! use rig_core::completion::{CompletionRequest, CompletionResponse};
21//! use rig_core::error::ProviderError;
22//! use rig_core::operation::Completion;
23//!
24//! async fn ask(model: &DynModel<Completion>) -> Result<CompletionResponse, ProviderError> {
25//!     let request = CompletionRequest::new("Who are you?");
26//!     model.call(request).await
27//! }
28//! ```
29
30extern crate self as rig;
31
32#[cfg(feature = "audio")]
33#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
34pub mod audio_generation;
35pub mod catalog;
36pub mod client;
37pub mod completion;
38pub mod conversation;
39pub mod driver;
40pub mod effect;
41pub mod embeddings;
42pub mod error;
43pub use rig_http::http_client;
44pub mod id;
45#[cfg(feature = "image")]
46#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
47pub mod image_generation;
48/// Internal JSON helpers shared with sibling runtime crates (e.g. `rig-agent`).
49/// Not part of rig-core's stable public API.
50#[doc(hidden)]
51pub mod json_utils;
52pub mod loaders;
53pub mod markers;
54pub mod model;
55pub mod observe;
56pub mod operation;
57pub mod prelude;
58pub(crate) mod provider_response;
59pub mod providers;
60pub mod redact;
61pub mod rerank;
62pub mod serve;
63
64pub mod streaming;
65pub mod structured_output;
66#[cfg(any(test, feature = "test-utils"))]
67#[cfg_attr(docsrs, doc(cfg(feature = "test-utils")))]
68pub mod test_utils;
69pub mod tool;
70pub mod transcription;
71pub mod vector_store;
72pub use rig_http::wasm_compat;
73pub mod wire;
74#[cfg(feature = "websocket")]
75#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
76pub use rig_http::ws_client;
77
78pub use completion::message;
79pub use driver::{DynModel, Model};
80pub use embeddings::Embed;
81pub use error::{ErrorKind, ErrorReport, ProviderError};
82pub use provider_response::ProviderResponseError;
83// `schemars`, `serde`, and `serde_json` are re-exported so macro-generated
84// code (and downstream crates) can resolve them through Rig instead of
85// requiring a direct dependency on each.
86pub use schemars;
87pub use serde;
88pub use serde_json;
89
90#[cfg(feature = "derive")]
91#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
92pub use rig_derive::ContextValue;
93#[cfg(feature = "derive")]
94#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
95pub use rig_derive::Embed;
96
97#[cfg(feature = "derive")]
98#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
99pub use rig_derive::rig_tool;
100
101pub mod telemetry;
102
103/// Milliseconds since the Unix epoch at `at`, also on the web; 0 before it.
104pub fn millis(at: web_time::SystemTime) -> u64 {
105    at.duration_since(web_time::UNIX_EPOCH)
106        .map(|elapsed| u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX))
107        .unwrap_or_default()
108}
109
110/// Milliseconds since the Unix epoch, also on the web.
111pub fn now_ms() -> u64 {
112    millis(web_time::SystemTime::now())
113}
114
115// Native runtime values must retain their thread-safety bounds.
116#[cfg(not(target_family = "wasm"))]
117const _: fn() = || {
118    fn assert_send_sync_static<T: Send + Sync + 'static>() {}
119    fn assert_send_static<T: Send + 'static>() {}
120    assert_send_sync_static::<tool::ManagedToolToken>();
121    assert_send_sync_static::<streaming::StreamEvent>();
122    // The serializable identity a typed view is resolved from.
123    assert_send_sync_static::<completion::ModelRef>();
124    assert_send_sync_static::<tool::DynamicTool>();
125    // One erased transport, shared by every provider a host binds.
126    assert_send_sync_static::<http_client::DynHttpClient>();
127    // A live stream is owned by one poller: `Send` so it can move to a worker,
128    // not `Sync`.
129    assert_send_static::<streaming::CompletionStream>();
130};