Skip to main content

rig_memory/
lib.rs

1#![deny(missing_docs)]
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//! Conversations over time, over rig-core's [`Conversation`]:
13//! - [`recent`]: which newest messages a request sends.
14//! - [`CompactionPolicy`], [`apply_summary`], [`undo_summary`]: compacting a long conversation.
15//! - [`load`], [`save`], [`Session`], [`Writer`]: saving and resuming, in the one format
16//!   [`FORMAT`]. Reading is strict: a session of another format, or that does not read whole,
17//!   is refused and nothing is written, unless [`migration`] brings it to the format first.
18//!   [`load_async`] and [`save_async`] keep a conversation
19//!   in any [`AsyncStore`], such as a database or IndexedDB.
20//! - [`history`], [`as_of`]: how a conversation got where it is.
21//! - [`rewrite`] (what it promises is on it): rewriting a saved session.
22//! - [`SessionMeta`], [`read_conversation`]: what a session list shows, read without folding
23//!   every log.
24//! - [`Store`], [`MemoryStore`], [`DirStore`] (feature `fs`): where a session is kept; [`Files`],
25//!   [`DirFiles`] and [`PluginFiles`] for named files on their own. Every [`Store`] is an
26//!   [`AsyncStore`], its calls done when made. A log write appends only if the log is as long as
27//!   the writer read it, the store checking and writing in one step, so of two saves that read
28//!   one log the second is refused ([`SessionError::Changed`]) and writes nothing to it.
29//!   `rig-sqlite` and `rig-postgres` keep sessions in a database with their `memory` feature, and
30//!   `conformance` (feature `test-utils`) checks a store against the contract.
31//!
32//! ```
33//! use rig_core::message::Message;
34//! use rig_memory::recent::{LastMessages, view};
35//! use rig_memory::{MemoryStore, load, save};
36//!
37//! let store = MemoryStore::default();
38//! let mut conversation = load(&store, "user-123")?;
39//! conversation.extend([Message::user("My name is Alice."), Message::assistant("Hi Alice!")]);
40//! save(&store, "user-123", &mut conversation)?;
41//!
42//! let resumed = load(&store, "user-123")?;
43//! assert_eq!(resumed, conversation);
44//! assert_eq!(view(resumed.messages(), &LastMessages(1)).len(), 1);
45//! # Ok::<(), rig_memory::SessionError>(())
46//! ```
47//!
48//! [`Conversation`]: rig_core::conversation::Conversation
49
50mod blobs;
51mod compaction;
52#[cfg(any(test, feature = "test-utils"))]
53pub mod conformance;
54#[cfg(feature = "fs")]
55mod dir;
56mod fork;
57mod format;
58mod listing;
59mod meta;
60pub mod migration;
61pub mod recent;
62mod rewrite;
63mod saved;
64mod session;
65mod store;
66mod tokens;
67mod writer;
68
69pub use blobs::{BLOBS, load_images, store_images};
70pub use compaction::{
71    AFTER_OVERFLOW, COMPACTED, ClearPlan, ClearToolOutputs, Cleared, CompactReason, Compacted,
72    CompactionPolicy, NearWindow, Summarized, Summarizer, SummaryCut, SummaryError, SummaryLimits,
73    SummaryPlan, SummaryPrompts, SummaryState, TrackArgument, TrackedSet, Uncompacted, UndoError,
74    apply_summary, estimate_tokens, known_limits, summarized_in_log, summarized_prefix,
75    summary_author, summary_in, summary_state, undo_summary,
76};
77#[cfg(feature = "fs")]
78pub use dir::{DirFiles, DirStore};
79pub use format::{FORMAT, LogRewrite, Repair, SESSION, SessionError, Stamp};
80pub use listing::read_conversation;
81pub use meta::{META, SessionMeta, excerpt, title_of};
82pub use rewrite::{
83    Messages, Place, Rewrite, Rewritten, RewrittenLog, Strings, map_strings, rewrite, rewrite_file,
84    rewrite_value,
85};
86pub use saved::{MODEL, TITLE, main_order};
87pub use session::{
88    LogRecord, SavedAgent, Session, Unreadable, as_of, history, load, load_async, save, save_async,
89};
90pub use store::{
91    AsyncStore, Files, LogWriteError, MemoryStore, PLUGINS, PluginFiles, Store, check_log_name,
92    check_name,
93};
94pub use tokens::{
95    ATTACHMENT_TOKENS, Anchor, HeuristicTokenCounter, OUTPUT_MARGIN, Prepared, RequestStart,
96    TURN_OUTPUT, TokenCounter, next_request_tokens,
97};
98pub use writer::Writer;