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