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//! - [`prepare_compaction`], [`apply_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.
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 recent;
61mod rewrite;
62mod saved;
63mod session;
64mod store;
65mod tokens;
66mod writer;
67
68pub use blobs::BLOBS;
69pub use compaction::{
70 CompactionPreparation, CompactionSettings, FileLists, SUMMARIZATION_SYSTEM_PROMPT,
71 SummaryFailure, SummaryState, already_compacted, apply_summary, clamp_max_tokens,
72 estimate_message_tokens, estimate_tokens, find_cut_point, fit_to_model, merge_split_turn,
73 no_history, prepare_compaction, prepare_compaction_at, request_tokens, serialize_conversation,
74 summarization_request, summarized_in_log, summarized_prefix, summary_author, summary_in,
75 summary_request, summary_state, summary_text, turn_prefix_request,
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};
85pub use saved::{CURRENT, MODEL, TITLE};
86pub use session::{
87 LogRecord, SavedAgent, Session, Unreadable, as_of, history, load, load_async, save, save_async,
88};
89pub use store::{
90 AsyncStore, Files, LogWriteError, MAX_NEW_LOG_NAME, MemoryStore, PLUGINS, PluginFiles, Store,
91 check_log_name, check_name, check_new_log_name, same_log_name,
92};
93pub use tokens::{ATTACHMENT_TOKENS, Anchor, HeuristicTokenCounter, request_estimate};
94pub use writer::Writer;