pub struct ConversationContext { /* private fields */ }Expand description
A multi-turn conversation with automatic context window management.
Each added turn is stored with its role, content, and token ids.
build_tokens() concatenates all turn token ids in order,
respecting the underlying ContextWindow’s token budget.
Implementations§
Source§impl ConversationContext
impl ConversationContext
Sourcepub fn new(max_tokens: usize) -> Self
pub fn new(max_tokens: usize) -> Self
Create a new conversation context with the given maximum token budget.
Sourcepub fn add_turn(&mut self, role: &str, content: &str, token_ids: Vec<u32>)
pub fn add_turn(&mut self, role: &str, content: &str, token_ids: Vec<u32>)
Add a conversation turn.
The turn’s token ids are appended to the context window.
Sourcepub fn build_tokens(&self) -> Vec<u32>
pub fn build_tokens(&self) -> Vec<u32>
Build a flat token sequence from all turns, respecting the window budget.
Concatenates token ids in turn order. The result is always within
max_tokens after truncation.
Sourcepub fn turn_count(&self) -> usize
pub fn turn_count(&self) -> usize
Number of turns added to this conversation.
Sourcepub fn total_tokens(&self) -> usize
pub fn total_tokens(&self) -> usize
Total token count across the current context window (after truncation).
Sourcepub fn last_turn(&self) -> Option<&ConversationTurn>
pub fn last_turn(&self) -> Option<&ConversationTurn>
Reference to the most recently added turn, if any.
Sourcepub fn utilization(&self) -> f32
pub fn utilization(&self) -> f32
Utilisation of the token budget: total_tokens / max_tokens.
Auto Trait Implementations§
impl Freeze for ConversationContext
impl RefUnwindSafe for ConversationContext
impl Send for ConversationContext
impl Sync for ConversationContext
impl Unpin for ConversationContext
impl UnsafeUnpin for ConversationContext
impl UnwindSafe for ConversationContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more