next_web_ai/chat/messages/
user_message.rs

1use std::collections::HashMap;
2
3use bytes::Bytes;
4
5use crate::chat::messages::{message::Message, message_type::MessageType};
6
7#[derive(Clone)]
8pub struct UserMessage {
9    message_type: MessageType,
10    text_content: Bytes,
11    metadata: HashMap<String, String>,
12}
13
14impl UserMessage {
15    pub fn new(
16        message_type: MessageType,
17        text_content: impl Into<Bytes>,
18        mut metadata: HashMap<String, String>,
19    ) -> Self {
20        let text_content = text_content.into();
21        if message_type == MessageType::User || message_type == MessageType::System {
22            assert!(text_content.len() > 0);
23        }
24
25        metadata.insert("messageType".into(), message_type.as_ref().into());
26
27        Self {
28            message_type,
29            text_content,
30            metadata,
31        }
32    }
33
34    pub fn meta_data(&self) -> &HashMap<String, String> {
35        &self.metadata
36    }
37}
38
39impl Message for UserMessage {
40    fn message_type(&self) -> super::message_type::MessageType {
41        self.message_type.clone()
42    }
43
44    fn text(&self) -> &str {
45        std::str::from_utf8(self.text_content.as_ref()).unwrap_or("")
46    }
47}