1use async_trait::async_trait;
9use chrono::{DateTime, Utc};
10use serde::{Deserialize, Serialize};
11use uuid::Uuid;
12
13use crate::scope::Scope;
14use crate::store::MemoryError;
15
16pub type MessageId = Uuid;
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct ChatMessage {
22 pub id: MessageId,
23 pub conversation_id: String,
24 pub role: String,
25 pub content: String,
26 pub scope: Scope,
27 pub seq: i32,
28 pub created_at: DateTime<Utc>,
29 #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
30 pub metadata: serde_json::Map<String, serde_json::Value>,
31}
32
33impl ChatMessage {
34 pub fn new(
35 conversation_id: impl Into<String>,
36 role: impl Into<String>,
37 content: impl Into<String>,
38 scope: Scope,
39 seq: i32,
40 ) -> Self {
41 Self {
42 id: Uuid::new_v4(),
43 conversation_id: conversation_id.into(),
44 role: role.into(),
45 content: content.into(),
46 scope,
47 seq,
48 created_at: Utc::now(),
49 metadata: serde_json::Map::new(),
50 }
51 }
52}
53
54#[async_trait]
59pub trait MessageStore: Send + Sync {
60 async fn save_messages(
63 &self,
64 conversation_id: &str,
65 messages: &[ChatMessage],
66 scope: &Scope,
67 ) -> Result<Vec<MessageId>, MemoryError>;
68
69 async fn get_messages(
72 &self,
73 conversation_id: &str,
74 last_n: Option<usize>,
75 scope: &Scope,
76 ) -> Result<Vec<ChatMessage>, MemoryError>;
77
78 async fn list_conversations(&self, scope: &Scope) -> Result<Vec<String>, MemoryError>;
80
81 async fn delete_messages(
83 &self,
84 conversation_id: &str,
85 scope: &Scope,
86 ) -> Result<u64, MemoryError>;
87}