langchain_rust/schemas/
memory.rs

1use super::messages::Message;
2
3pub trait BaseMemory: Send + Sync {
4    fn messages(&self) -> Vec<Message>;
5
6    // Use a trait object for Display instead of a generic type
7    fn add_user_message(&mut self, message: &dyn std::fmt::Display) {
8        // Convert the Display trait object to a String and pass it to the constructor
9        self.add_message(Message::new_human_message(message.to_string()));
10    }
11
12    // Use a trait object for Display instead of a generic type
13    fn add_ai_message(&mut self, message: &dyn std::fmt::Display) {
14        // Convert the Display trait object to a String and pass it to the constructor
15        self.add_message(Message::new_ai_message(message.to_string()));
16    }
17
18    fn add_message(&mut self, message: Message);
19
20    fn clear(&mut self);
21
22    fn to_string(&self) -> String {
23        self.messages()
24            .iter()
25            .map(|msg| format!("{}: {}", msg.message_type.to_string(), msg.content))
26            .collect::<Vec<String>>()
27            .join("\n")
28    }
29}
30
31impl<M> From<M> for Box<dyn BaseMemory>
32where
33    M: BaseMemory + 'static,
34{
35    fn from(memory: M) -> Self {
36        Box::new(memory)
37    }
38}