pub struct ChatHistory { /* private fields */ }
Expand description
Container for managing chat history with serialization support.
Provides automatic message limit management and convenient methods for adding and retrieving messages.
§Examples
use graph_flow::ChatHistory;
let mut history = ChatHistory::new();
history.add_user_message("Hello".to_string());
history.add_assistant_message("Hi there!".to_string());
assert_eq!(history.len(), 2);
assert!(!history.is_empty());
Implementations§
Source§impl ChatHistory
impl ChatHistory
Sourcepub fn with_max_messages(max: usize) -> Self
pub fn with_max_messages(max: usize) -> Self
Create a new chat history with a maximum message limit.
When the limit is exceeded, older messages are automatically removed.
§Examples
use graph_flow::ChatHistory;
let mut history = ChatHistory::with_max_messages(10);
// Add 15 messages
for i in 0..15 {
history.add_user_message(format!("Message {}", i));
}
// Only the last 10 are kept
assert_eq!(history.len(), 10);
Sourcepub fn add_user_message(&mut self, content: String)
pub fn add_user_message(&mut self, content: String)
Add a user message to the chat history.
Sourcepub fn add_assistant_message(&mut self, content: String)
pub fn add_assistant_message(&mut self, content: String)
Add an assistant message to the chat history.
Sourcepub fn add_system_message(&mut self, content: String)
pub fn add_system_message(&mut self, content: String)
Add a system message to the chat history.
Sourcepub fn messages(&self) -> &[SerializableMessage]
pub fn messages(&self) -> &[SerializableMessage]
Get a reference to all messages.
Sourcepub fn last_messages(&self, n: usize) -> &[SerializableMessage]
pub fn last_messages(&self, n: usize) -> &[SerializableMessage]
Get the last N messages.
If N is greater than the total number of messages, all messages are returned.
§Examples
use graph_flow::ChatHistory;
let mut history = ChatHistory::new();
history.add_user_message("Message 1".to_string());
history.add_user_message("Message 2".to_string());
history.add_user_message("Message 3".to_string());
let last_two = history.last_messages(2);
assert_eq!(last_two.len(), 2);
assert_eq!(last_two[0].content, "Message 2");
assert_eq!(last_two[1].content, "Message 3");
Trait Implementations§
Source§impl Clone for ChatHistory
impl Clone for ChatHistory
Source§fn clone(&self) -> ChatHistory
fn clone(&self) -> ChatHistory
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for ChatHistory
impl Debug for ChatHistory
Source§impl Default for ChatHistory
impl Default for ChatHistory
Source§fn default() -> ChatHistory
fn default() -> ChatHistory
Returns the “default value” for a type. Read more
Source§impl<'de> Deserialize<'de> for ChatHistory
impl<'de> Deserialize<'de> for ChatHistory
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ChatHistory
impl RefUnwindSafe for ChatHistory
impl Send for ChatHistory
impl Sync for ChatHistory
impl Unpin for ChatHistory
impl UnwindSafe for ChatHistory
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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