Skip to main content

History

Struct History 

Source
pub struct History { /* private fields */ }
Expand description

Owns the message vector backing a single conversation, plus the budget and pin mask that govern how compaction reduces it.

Lifecycle: append turns with add_message / extend, inspect with messages, pin survivors with pin_last / pin_at / pin_with_tool_result, and call compact_if_needed before each outgoing ChatRequest to hold the history under the configured budget. Restore from a ConversationSnapshot via from_messages.

The façade Conversation wires one of these automatically — touch this type directly only when driving advanced::run_chat or building tests.

Implementations§

Source§

impl History

Source

pub fn builder(max_tokens: usize) -> HistoryBuilder

Begin configuring a new manager with max_tokens as the budget compact_if_needed will hold the history under. Defaults: TruncateStrategy for reduction, CharTokenizer (len() / 4) for sizing, four preserved tail messages.

Source

pub fn from_messages( builder: HistoryBuilder, messages: Vec<Message>, pinned: Vec<bool>, ) -> Result<History, FromMessagesError>

Restore a History whose history is messages and whose pin mask is pinned. The two vectors must have the same length; otherwise a FromMessagesError::LengthMismatch is returned. All other configuration (budget, strategy, tokenizer, preserved tail size) comes from builder — pass the same configuration the original conversation used so compaction behaves consistently across resumes.

Source§

impl History

Source

pub fn add_message(&mut self, message: Message)

Append message to the history with pinned = false. Use pin_last immediately after the call to mark it as a survivor.

Source

pub fn estimated_tokens(&self) -> usize

Approximate token cost of the entire history under the configured Tokenizer. The accuracy of this number is only as good as the tokenizer wired into the builder — under the default CharTokenizer it is a len() / 4 ballpark.

Source

pub fn messages(&self) -> &[Message]

Borrow the current history slice. Indices into this slice align with pinned.

Source

pub fn extend(&mut self, new_messages: Vec<Message>)

Append every message in new_messages, all with pinned = false. The engine uses this after a turn to fold the assistant reply (and any tool results) back into the history.

Source

pub fn pinned(&self) -> &[bool]

Pinned-state slice, parallel to Self::messages. Useful for tests; production callers normally interact via the pin_* helpers and inspect Self::is_pinned.

Source

pub fn is_pinned(&self, idx: usize) -> bool

Whether messages[idx] is currently pinned. Out-of-bounds indices return false rather than panicking.

Source

pub fn pin_last(&mut self)

Pin the most recently added message so it survives every future compaction. No-op when the history is empty.

Indices are not stable across compactions: a pin_last() made before compaction stays pinned (the strategy keeps it), but its numeric index in the new history may shift. Re-derive indices (or rely on pin_last) after compaction runs.

Source

pub fn pin_at(&mut self, idx: usize)

Pin the message at idx. Panics on out-of-bounds, matching the convention of Vec::index.

Source

pub fn unpin_at(&mut self, idx: usize)

Clear the pin on the message at idx. Panics on out-of-bounds.

Source

pub fn pin_with_tool_result(&mut self, idx: usize)

Pin idx together with every message that pairs with it via a tool_calltool_result link. Without this, pinning a lone Assistant ToolCall (or a lone User ToolResult) and then letting compaction run would strand the partner — most providers reject that as a malformed history.

Resolution rules:

  • If messages[idx] is an Assistant with ToolCall blocks, any User message containing a ToolResult whose call_id matches one of those calls is also pinned.
  • If messages[idx] is a User with ToolResult blocks, any Assistant message containing a ToolCall whose id matches is also pinned.
  • Messages without tool blocks are pinned alone (effectively a pin_at).

Panics on out-of-bounds. The lookup scans the full history but each message is examined at most once.

Source

pub async fn compact_if_needed( &mut self, ) -> Result<Option<CompactionReport>, CompactionError>

Run the configured CompactionStrategy when estimated_tokens reaches the budget; otherwise return Ok(None) and leave the history untouched.

On success returns Ok(Some(report)) describing the before/after counts and the strategy name. The engine emits StreamChunk::HistoryCompacted carrying the same fields so observability middlewares can correlate the compaction with the run that triggered it.

Errors propagate from the strategy (commonly CompactionError::SummarizationFailed when the summarizer model itself fails). CompactionError::NotEnoughHistory surfaces when the history has fewer messages than preserve_n_last and there is nothing to drop.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.