rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
//! # RRAG Memory System
//!
//! Conversation memory and context management with Rust-native async patterns.
//! Designed for efficient state management and persistence.

use crate::{RragError, RragResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use tokio::sync::RwLock;

/// Conversation message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationMessage {
    /// Message ID
    pub id: String,

    /// Role (user, assistant, system, tool)
    pub role: MessageRole,

    /// Message content
    pub content: String,

    /// Message metadata
    pub metadata: HashMap<String, serde_json::Value>,

    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Token count (if available)
    pub token_count: Option<usize>,
}

/// Message roles in conversation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MessageRole {
    /// Message from the user
    User,
    /// Message from the AI assistant
    Assistant,
    /// System message
    System,
    /// Message from a tool execution
    Tool,
}

impl ConversationMessage {
    /// Create a new conversation message
    pub fn new(role: MessageRole, content: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role,
            content: content.into(),
            metadata: HashMap::new(),
            timestamp: chrono::Utc::now(),
            token_count: None,
        }
    }

    /// Create a user message
    pub fn user(content: impl Into<String>) -> Self {
        Self::new(MessageRole::User, content)
    }

    /// Create an assistant message
    pub fn assistant(content: impl Into<String>) -> Self {
        Self::new(MessageRole::Assistant, content)
    }

    /// Create a system message
    pub fn system(content: impl Into<String>) -> Self {
        Self::new(MessageRole::System, content)
    }

    /// Create a tool message
    pub fn tool(content: impl Into<String>) -> Self {
        Self::new(MessageRole::Tool, content)
    }

    /// Add metadata to the message
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Set the token count for the message
    pub fn with_token_count(mut self, count: usize) -> Self {
        self.token_count = Some(count);
        self
    }

    /// Get estimated token count (simple heuristic if not set)
    pub fn estimated_tokens(&self) -> usize {
        self.token_count.unwrap_or_else(|| {
            // Simple estimation: ~4 characters per token
            self.content.len() / 4
        })
    }
}

/// Memory summary for efficient storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemorySummary {
    /// Summary text
    pub summary: String,

    /// Number of messages summarized
    pub message_count: usize,

    /// Token count of original messages
    pub original_tokens: usize,

    /// Token count of summary
    pub summary_tokens: usize,

    /// Time range covered - start time
    pub start_time: chrono::DateTime<chrono::Utc>,
    /// Time range covered - end time
    pub end_time: chrono::DateTime<chrono::Utc>,

    /// Summary metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Core memory trait for conversation management
#[async_trait]
pub trait Memory: Send + Sync {
    /// Add a message to the conversation
    async fn add_message(&self, conversation_id: &str, role: &str, content: &str)
        -> RragResult<()>;

    /// Add a structured message
    async fn add_structured_message(
        &self,
        conversation_id: &str,
        message: ConversationMessage,
    ) -> RragResult<()>;

    /// Get conversation history
    async fn get_conversation_history(&self, conversation_id: &str) -> RragResult<Vec<String>>;

    /// Get structured conversation history
    async fn get_messages(&self, conversation_id: &str) -> RragResult<Vec<ConversationMessage>>;

    /// Get recent messages with limit
    async fn get_recent_messages(
        &self,
        conversation_id: &str,
        limit: usize,
    ) -> RragResult<Vec<ConversationMessage>>;

    /// Clear conversation history
    async fn clear_conversation(&self, conversation_id: &str) -> RragResult<()>;

    /// Get memory variables for prompt injection
    async fn get_memory_variables(
        &self,
        conversation_id: &str,
    ) -> RragResult<HashMap<String, String>>;

    /// Save arbitrary context
    async fn save_context(
        &self,
        conversation_id: &str,
        context: HashMap<String, String>,
    ) -> RragResult<()>;

    /// Health check
    async fn health_check(&self) -> RragResult<bool>;
}

/// Buffer memory - keeps recent messages in memory
pub struct ConversationBufferMemory {
    /// Stored conversations
    conversations: Arc<RwLock<HashMap<String, VecDeque<ConversationMessage>>>>,

    /// Configuration
    config: BufferMemoryConfig,
}

/// Configuration for buffer-based memory
#[derive(Debug, Clone)]
pub struct BufferMemoryConfig {
    /// Maximum messages to keep per conversation
    pub max_messages: Option<usize>,

    /// Maximum age of messages in seconds
    pub max_age_seconds: Option<u64>,

    /// Memory key for prompt variables
    pub memory_key: String,
}

impl Default for BufferMemoryConfig {
    fn default() -> Self {
        Self {
            max_messages: Some(100),
            max_age_seconds: Some(3600 * 24), // 24 hours
            memory_key: "history".to_string(),
        }
    }
}

impl ConversationBufferMemory {
    /// Create a new buffer memory with default configuration
    pub fn new() -> Self {
        Self {
            conversations: Arc::new(RwLock::new(HashMap::new())),
            config: BufferMemoryConfig::default(),
        }
    }

    /// Create a new buffer memory with custom configuration
    pub fn with_config(config: BufferMemoryConfig) -> Self {
        Self {
            conversations: Arc::new(RwLock::new(HashMap::new())),
            config,
        }
    }

    /// Clean up old messages based on configuration
    async fn cleanup_old_messages(&self, conversation_id: &str) {
        let mut conversations = self.conversations.write().await;

        if let Some(messages) = conversations.get_mut(conversation_id) {
            // Remove old messages by age
            if let Some(max_age) = self.config.max_age_seconds {
                let cutoff_time = chrono::Utc::now() - chrono::Duration::seconds(max_age as i64);
                while let Some(front) = messages.front() {
                    if front.timestamp < cutoff_time {
                        messages.pop_front();
                    } else {
                        break;
                    }
                }
            }

            // Limit by count
            if let Some(max_messages) = self.config.max_messages {
                while messages.len() > max_messages {
                    messages.pop_front();
                }
            }
        }
    }
}

impl Default for ConversationBufferMemory {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Memory for ConversationBufferMemory {
    async fn add_message(
        &self,
        conversation_id: &str,
        role: &str,
        content: &str,
    ) -> RragResult<()> {
        let role = match role.to_lowercase().as_str() {
            "user" => MessageRole::User,
            "assistant" => MessageRole::Assistant,
            "system" => MessageRole::System,
            "tool" => MessageRole::Tool,
            _ => MessageRole::User, // Default fallback
        };

        let message = ConversationMessage::new(role, content);
        self.add_structured_message(conversation_id, message).await
    }

    async fn add_structured_message(
        &self,
        conversation_id: &str,
        message: ConversationMessage,
    ) -> RragResult<()> {
        let mut conversations = self.conversations.write().await;

        let messages = conversations
            .entry(conversation_id.to_string())
            .or_insert_with(VecDeque::new);

        messages.push_back(message);

        // Release the lock before cleanup
        drop(conversations);

        // Clean up old messages
        self.cleanup_old_messages(conversation_id).await;

        Ok(())
    }

    async fn get_conversation_history(&self, conversation_id: &str) -> RragResult<Vec<String>> {
        let conversations = self.conversations.read().await;

        if let Some(messages) = conversations.get(conversation_id) {
            let history = messages
                .iter()
                .map(|msg| format!("{:?}: {}", msg.role, msg.content))
                .collect();
            Ok(history)
        } else {
            Ok(Vec::new())
        }
    }

    async fn get_messages(&self, conversation_id: &str) -> RragResult<Vec<ConversationMessage>> {
        let conversations = self.conversations.read().await;

        if let Some(messages) = conversations.get(conversation_id) {
            Ok(messages.iter().cloned().collect())
        } else {
            Ok(Vec::new())
        }
    }

    async fn get_recent_messages(
        &self,
        conversation_id: &str,
        limit: usize,
    ) -> RragResult<Vec<ConversationMessage>> {
        let conversations = self.conversations.read().await;

        if let Some(messages) = conversations.get(conversation_id) {
            let recent: Vec<ConversationMessage> =
                messages.iter().rev().take(limit).rev().cloned().collect();
            Ok(recent)
        } else {
            Ok(Vec::new())
        }
    }

    async fn clear_conversation(&self, conversation_id: &str) -> RragResult<()> {
        let mut conversations = self.conversations.write().await;
        conversations.remove(conversation_id);
        Ok(())
    }

    async fn get_memory_variables(
        &self,
        conversation_id: &str,
    ) -> RragResult<HashMap<String, String>> {
        let history = self.get_conversation_history(conversation_id).await?;
        let mut variables = HashMap::new();

        variables.insert(self.config.memory_key.clone(), history.join("\n"));

        Ok(variables)
    }

    async fn save_context(
        &self,
        _conversation_id: &str,
        _context: HashMap<String, String>,
    ) -> RragResult<()> {
        // Simple buffer memory doesn't persist additional context
        // This could be extended to store context in message metadata
        Ok(())
    }

    async fn health_check(&self) -> RragResult<bool> {
        Ok(true)
    }
}

/// Token-aware buffer memory that respects token limits
pub struct ConversationTokenBufferMemory {
    /// Base buffer memory
    buffer: ConversationBufferMemory,

    /// Token-specific configuration
    token_config: TokenBufferConfig,
}

/// Configuration for token-aware memory buffer
#[derive(Debug, Clone)]
pub struct TokenBufferConfig {
    /// Maximum tokens to keep in memory
    pub max_tokens: usize,

    /// Buffer size to keep below max (for safety)
    pub buffer_tokens: usize,

    /// How to handle token overflow
    pub overflow_strategy: TokenOverflowStrategy,
}

/// Strategy for handling token overflow in memory buffer
#[derive(Debug, Clone)]
pub enum TokenOverflowStrategy {
    /// Remove oldest messages
    RemoveOldest,

    /// Summarize old messages
    Summarize,

    /// Truncate message content
    Truncate,
}

impl Default for TokenBufferConfig {
    fn default() -> Self {
        Self {
            max_tokens: 4000,
            buffer_tokens: 500,
            overflow_strategy: TokenOverflowStrategy::RemoveOldest,
        }
    }
}

impl ConversationTokenBufferMemory {
    /// Create a new token buffer memory with default configuration
    pub fn new() -> Self {
        Self {
            buffer: ConversationBufferMemory::new(),
            token_config: TokenBufferConfig::default(),
        }
    }

    /// Create a new token buffer memory with custom configuration
    pub fn with_config(buffer_config: BufferMemoryConfig, token_config: TokenBufferConfig) -> Self {
        Self {
            buffer: ConversationBufferMemory::with_config(buffer_config),
            token_config,
        }
    }

    /// Calculate total tokens in conversation
    async fn calculate_total_tokens(&self, conversation_id: &str) -> RragResult<usize> {
        let messages = self.buffer.get_messages(conversation_id).await?;
        let total = messages.iter().map(|msg| msg.estimated_tokens()).sum();
        Ok(total)
    }

    /// Handle token overflow
    async fn handle_token_overflow(&self, conversation_id: &str) -> RragResult<()> {
        let current_tokens = self.calculate_total_tokens(conversation_id).await?;

        if current_tokens <= self.token_config.max_tokens {
            return Ok(());
        }

        match self.token_config.overflow_strategy {
            TokenOverflowStrategy::RemoveOldest => {
                let mut conversations = self.buffer.conversations.write().await;

                if let Some(messages) = conversations.get_mut(conversation_id) {
                    while !messages.is_empty() {
                        let total: usize = messages.iter().map(|msg| msg.estimated_tokens()).sum();
                        if total <= self.token_config.max_tokens - self.token_config.buffer_tokens {
                            break;
                        }
                        messages.pop_front();
                    }
                }
            }
            TokenOverflowStrategy::Summarize => {
                // This would require integration with an LLM for summarization
                // For now, fall back to removing oldest
                let mut conversations = self.buffer.conversations.write().await;

                if let Some(messages) = conversations.get_mut(conversation_id) {
                    // Remove half the messages as a simple strategy
                    let remove_count = messages.len() / 2;
                    for _ in 0..remove_count {
                        messages.pop_front();
                    }
                }
            }
            TokenOverflowStrategy::Truncate => {
                // Truncate message content (not implemented in this example)
                return Err(RragError::memory(
                    "token_overflow",
                    "Truncate strategy not implemented",
                ));
            }
        }

        Ok(())
    }
}

impl Default for ConversationTokenBufferMemory {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Memory for ConversationTokenBufferMemory {
    async fn add_message(
        &self,
        conversation_id: &str,
        role: &str,
        content: &str,
    ) -> RragResult<()> {
        self.buffer
            .add_message(conversation_id, role, content)
            .await?;
        self.handle_token_overflow(conversation_id).await?;
        Ok(())
    }

    async fn add_structured_message(
        &self,
        conversation_id: &str,
        message: ConversationMessage,
    ) -> RragResult<()> {
        self.buffer
            .add_structured_message(conversation_id, message)
            .await?;
        self.handle_token_overflow(conversation_id).await?;
        Ok(())
    }

    async fn get_conversation_history(&self, conversation_id: &str) -> RragResult<Vec<String>> {
        self.buffer.get_conversation_history(conversation_id).await
    }

    async fn get_messages(&self, conversation_id: &str) -> RragResult<Vec<ConversationMessage>> {
        self.buffer.get_messages(conversation_id).await
    }

    async fn get_recent_messages(
        &self,
        conversation_id: &str,
        limit: usize,
    ) -> RragResult<Vec<ConversationMessage>> {
        self.buffer
            .get_recent_messages(conversation_id, limit)
            .await
    }

    async fn clear_conversation(&self, conversation_id: &str) -> RragResult<()> {
        self.buffer.clear_conversation(conversation_id).await
    }

    async fn get_memory_variables(
        &self,
        conversation_id: &str,
    ) -> RragResult<HashMap<String, String>> {
        let mut variables = self.buffer.get_memory_variables(conversation_id).await?;

        // Add token information
        let token_count = self.calculate_total_tokens(conversation_id).await?;
        variables.insert("token_count".to_string(), token_count.to_string());
        variables.insert(
            "max_tokens".to_string(),
            self.token_config.max_tokens.to_string(),
        );

        Ok(variables)
    }

    async fn save_context(
        &self,
        conversation_id: &str,
        context: HashMap<String, String>,
    ) -> RragResult<()> {
        self.buffer.save_context(conversation_id, context).await
    }

    async fn health_check(&self) -> RragResult<bool> {
        self.buffer.health_check().await
    }
}

/// Summary memory that automatically summarizes old conversations
pub struct ConversationSummaryMemory {
    /// Current conversation buffer
    current_messages: Arc<RwLock<HashMap<String, VecDeque<ConversationMessage>>>>,

    /// Stored summaries
    summaries: Arc<RwLock<HashMap<String, Vec<MemorySummary>>>>,

    /// Configuration
    config: SummaryMemoryConfig,
}

/// Configuration for summary-based memory management
#[derive(Debug, Clone)]
pub struct SummaryMemoryConfig {
    /// Maximum messages before summarization
    pub max_messages_before_summary: usize,

    /// Maximum total tokens before summarization
    pub max_tokens_before_summary: usize,

    /// Number of recent messages to keep after summarization
    pub keep_recent_messages: usize,

    /// Memory key for variables
    pub memory_key: String,

    /// Summary key for variables
    pub summary_key: String,
}

impl Default for SummaryMemoryConfig {
    fn default() -> Self {
        Self {
            max_messages_before_summary: 20,
            max_tokens_before_summary: 2000,
            keep_recent_messages: 5,
            memory_key: "history".to_string(),
            summary_key: "summary".to_string(),
        }
    }
}

impl ConversationSummaryMemory {
    /// Create a new summary memory with default configuration
    pub fn new() -> Self {
        Self {
            current_messages: Arc::new(RwLock::new(HashMap::new())),
            summaries: Arc::new(RwLock::new(HashMap::new())),
            config: SummaryMemoryConfig::default(),
        }
    }

    /// Create a new summary memory with custom configuration
    pub fn with_config(config: SummaryMemoryConfig) -> Self {
        Self {
            current_messages: Arc::new(RwLock::new(HashMap::new())),
            summaries: Arc::new(RwLock::new(HashMap::new())),
            config,
        }
    }

    /// Check if summarization is needed
    async fn should_summarize(&self, conversation_id: &str) -> RragResult<bool> {
        let messages = self.current_messages.read().await;

        if let Some(msg_deque) = messages.get(conversation_id) {
            // Check message count
            if msg_deque.len() > self.config.max_messages_before_summary {
                return Ok(true);
            }

            // Check token count
            let total_tokens: usize = msg_deque.iter().map(|msg| msg.estimated_tokens()).sum();
            if total_tokens > self.config.max_tokens_before_summary {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Perform summarization (mock implementation)
    async fn summarize_conversation(&self, conversation_id: &str) -> RragResult<()> {
        let mut messages = self.current_messages.write().await;
        let mut summaries = self.summaries.write().await;

        if let Some(msg_deque) = messages.get_mut(conversation_id) {
            if msg_deque.len() <= self.config.keep_recent_messages {
                return Ok(());
            }

            // Calculate how many messages to summarize
            let to_summarize_count = msg_deque.len() - self.config.keep_recent_messages;

            // Extract messages to summarize
            let mut to_summarize = Vec::new();
            for _ in 0..to_summarize_count {
                if let Some(msg) = msg_deque.pop_front() {
                    to_summarize.push(msg);
                }
            }

            if !to_summarize.is_empty() {
                // Create a simple summary (in production, would use LLM)
                let summary_text = format!(
                    "Summary of {} messages from {} to {}",
                    to_summarize.len(),
                    to_summarize
                        .first()
                        .unwrap()
                        .timestamp
                        .format("%Y-%m-%d %H:%M:%S"),
                    to_summarize
                        .last()
                        .unwrap()
                        .timestamp
                        .format("%Y-%m-%d %H:%M:%S")
                );

                let original_tokens = to_summarize.iter().map(|msg| msg.estimated_tokens()).sum();

                let summary = MemorySummary {
                    summary: summary_text,
                    message_count: to_summarize.len(),
                    original_tokens,
                    summary_tokens: 50, // Estimated
                    start_time: to_summarize.first().unwrap().timestamp,
                    end_time: to_summarize.last().unwrap().timestamp,
                    metadata: HashMap::new(),
                };

                // Store the summary
                summaries
                    .entry(conversation_id.to_string())
                    .or_insert_with(Vec::new)
                    .push(summary);
            }
        }

        Ok(())
    }
}

impl Default for ConversationSummaryMemory {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Memory for ConversationSummaryMemory {
    async fn add_message(
        &self,
        conversation_id: &str,
        role: &str,
        content: &str,
    ) -> RragResult<()> {
        let role = match role.to_lowercase().as_str() {
            "user" => MessageRole::User,
            "assistant" => MessageRole::Assistant,
            "system" => MessageRole::System,
            "tool" => MessageRole::Tool,
            _ => MessageRole::User,
        };

        let message = ConversationMessage::new(role, content);
        self.add_structured_message(conversation_id, message).await
    }

    async fn add_structured_message(
        &self,
        conversation_id: &str,
        message: ConversationMessage,
    ) -> RragResult<()> {
        // Add the message
        {
            let mut messages = self.current_messages.write().await;
            let msg_deque = messages
                .entry(conversation_id.to_string())
                .or_insert_with(VecDeque::new);
            msg_deque.push_back(message);
        }

        // Check if summarization is needed
        if self.should_summarize(conversation_id).await? {
            self.summarize_conversation(conversation_id).await?;
        }

        Ok(())
    }

    async fn get_conversation_history(&self, conversation_id: &str) -> RragResult<Vec<String>> {
        let messages = self.current_messages.read().await;
        let summaries = self.summaries.read().await;

        let mut history = Vec::new();

        // Add summaries first
        if let Some(summary_list) = summaries.get(conversation_id) {
            for summary in summary_list {
                history.push(format!("Summary: {}", summary.summary));
            }
        }

        // Add current messages
        if let Some(msg_deque) = messages.get(conversation_id) {
            for msg in msg_deque {
                history.push(format!("{:?}: {}", msg.role, msg.content));
            }
        }

        Ok(history)
    }

    async fn get_messages(&self, conversation_id: &str) -> RragResult<Vec<ConversationMessage>> {
        let messages = self.current_messages.read().await;

        if let Some(msg_deque) = messages.get(conversation_id) {
            Ok(msg_deque.iter().cloned().collect())
        } else {
            Ok(Vec::new())
        }
    }

    async fn get_recent_messages(
        &self,
        conversation_id: &str,
        limit: usize,
    ) -> RragResult<Vec<ConversationMessage>> {
        let messages = self.current_messages.read().await;

        if let Some(msg_deque) = messages.get(conversation_id) {
            let recent: Vec<ConversationMessage> =
                msg_deque.iter().rev().take(limit).rev().cloned().collect();
            Ok(recent)
        } else {
            Ok(Vec::new())
        }
    }

    async fn clear_conversation(&self, conversation_id: &str) -> RragResult<()> {
        let mut messages = self.current_messages.write().await;
        let mut summaries = self.summaries.write().await;

        messages.remove(conversation_id);
        summaries.remove(conversation_id);

        Ok(())
    }

    async fn get_memory_variables(
        &self,
        conversation_id: &str,
    ) -> RragResult<HashMap<String, String>> {
        let mut variables = HashMap::new();

        // Get current conversation
        let history = self.get_conversation_history(conversation_id).await?;
        variables.insert(self.config.memory_key.clone(), history.join("\n"));

        // Get summary
        let summaries = self.summaries.read().await;
        if let Some(summary_list) = summaries.get(conversation_id) {
            let summary_text = summary_list
                .iter()
                .map(|s| s.summary.clone())
                .collect::<Vec<_>>()
                .join("\n");
            variables.insert(self.config.summary_key.clone(), summary_text);
        }

        Ok(variables)
    }

    async fn save_context(
        &self,
        _conversation_id: &str,
        _context: HashMap<String, String>,
    ) -> RragResult<()> {
        // Could store context in message metadata or separate storage
        Ok(())
    }

    async fn health_check(&self) -> RragResult<bool> {
        Ok(true)
    }
}

/// High-level memory service that can switch between different memory types
pub struct MemoryService {
    /// Active memory implementation
    memory: Arc<dyn Memory>,

    /// Service configuration
    config: MemoryServiceConfig,
}

/// Configuration for the memory service
#[derive(Debug, Clone)]
pub struct MemoryServiceConfig {
    /// Default conversation settings
    pub default_conversation_settings: ConversationSettings,

    /// Enable memory persistence
    pub enable_persistence: bool,

    /// Persistence interval in seconds
    pub persistence_interval_seconds: u64,
}

/// Settings for individual conversations
#[derive(Debug, Clone)]
pub struct ConversationSettings {
    /// Maximum messages per conversation
    pub max_messages: Option<usize>,

    /// Maximum age for messages
    pub max_age_hours: Option<u64>,

    /// Auto-summarization threshold
    pub auto_summarize_threshold: Option<usize>,
}

impl Default for MemoryServiceConfig {
    fn default() -> Self {
        Self {
            default_conversation_settings: ConversationSettings::default(),
            enable_persistence: false,
            persistence_interval_seconds: 300, // 5 minutes
        }
    }
}

impl Default for ConversationSettings {
    fn default() -> Self {
        Self {
            max_messages: Some(100),
            max_age_hours: Some(24),
            auto_summarize_threshold: Some(50),
        }
    }
}

impl MemoryService {
    /// Create a new memory service with default configuration
    pub fn new(memory: Arc<dyn Memory>) -> Self {
        Self {
            memory,
            config: MemoryServiceConfig::default(),
        }
    }

    /// Create a new memory service with custom configuration
    pub fn with_config(memory: Arc<dyn Memory>, config: MemoryServiceConfig) -> Self {
        Self { memory, config }
    }

    /// Add a user message
    pub async fn add_user_message(&self, conversation_id: &str, content: &str) -> RragResult<()> {
        self.memory
            .add_message(conversation_id, "user", content)
            .await
    }

    /// Add an assistant message
    pub async fn add_assistant_message(
        &self,
        conversation_id: &str,
        content: &str,
    ) -> RragResult<()> {
        self.memory
            .add_message(conversation_id, "assistant", content)
            .await
    }

    /// Get formatted conversation for prompts
    pub async fn get_conversation_context(&self, conversation_id: &str) -> RragResult<String> {
        let variables = self.memory.get_memory_variables(conversation_id).await?;

        // Return the main history
        Ok(variables.get("history").unwrap_or(&String::new()).clone())
    }

    /// Get memory variables for prompt templates
    pub async fn get_prompt_variables(
        &self,
        conversation_id: &str,
    ) -> RragResult<HashMap<String, String>> {
        self.memory.get_memory_variables(conversation_id).await
    }

    /// Health check
    pub async fn health_check(&self) -> RragResult<bool> {
        self.memory.health_check().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_conversation_message() {
        let msg = ConversationMessage::user("Hello world")
            .with_metadata("source", serde_json::Value::String("test".to_string()))
            .with_token_count(10);

        assert_eq!(msg.role, MessageRole::User);
        assert_eq!(msg.content, "Hello world");
        assert_eq!(msg.estimated_tokens(), 10);
        assert_eq!(
            msg.metadata.get("source").unwrap().as_str().unwrap(),
            "test"
        );
    }

    #[tokio::test]
    async fn test_buffer_memory() {
        let memory = ConversationBufferMemory::new();
        let conv_id = "test_conversation";

        // Add messages
        memory.add_message(conv_id, "user", "Hello").await.unwrap();
        memory
            .add_message(conv_id, "assistant", "Hi there!")
            .await
            .unwrap();

        // Get history
        let history = memory.get_conversation_history(conv_id).await.unwrap();
        assert_eq!(history.len(), 2);
        assert!(history[0].contains("Hello"));
        assert!(history[1].contains("Hi there!"));

        // Get messages
        let messages = memory.get_messages(conv_id).await.unwrap();
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, MessageRole::User);
        assert_eq!(messages[1].role, MessageRole::Assistant);

        // Test recent messages
        let recent = memory.get_recent_messages(conv_id, 1).await.unwrap();
        assert_eq!(recent.len(), 1);
        assert_eq!(recent[0].content, "Hi there!");
    }

    #[tokio::test]
    async fn test_token_buffer_memory() {
        let config = TokenBufferConfig {
            max_tokens: 100,
            buffer_tokens: 10,
            overflow_strategy: TokenOverflowStrategy::RemoveOldest,
        };

        let memory =
            ConversationTokenBufferMemory::with_config(BufferMemoryConfig::default(), config);

        let conv_id = "test_token_conversation";

        // Add many messages to trigger overflow
        for i in 0..20 {
            memory
                .add_message(
                    conv_id,
                    "user",
                    &format!("Message number {} with some content", i),
                )
                .await
                .unwrap();
        }

        let total_tokens = memory.calculate_total_tokens(conv_id).await.unwrap();
        assert!(
            total_tokens <= 100,
            "Total tokens {} should be <= 100",
            total_tokens
        );

        let messages = memory.get_messages(conv_id).await.unwrap();
        assert!(
            messages.len() < 20,
            "Should have removed some messages due to token limit"
        );
    }

    #[tokio::test]
    async fn test_memory_service() {
        let memory = Arc::new(ConversationBufferMemory::new());
        let service = MemoryService::new(memory);

        let conv_id = "service_test";

        service
            .add_user_message(conv_id, "How are you?")
            .await
            .unwrap();
        service
            .add_assistant_message(conv_id, "I'm doing well, thank you!")
            .await
            .unwrap();

        let context = service.get_conversation_context(conv_id).await.unwrap();
        assert!(context.contains("How are you?"));
        assert!(context.contains("I'm doing well"));

        let variables = service.get_prompt_variables(conv_id).await.unwrap();
        assert!(variables.contains_key("history"));

        assert!(service.health_check().await.unwrap());
    }

    #[tokio::test]
    async fn test_summary_memory() {
        let config = SummaryMemoryConfig {
            max_messages_before_summary: 3,
            max_tokens_before_summary: 1000,
            keep_recent_messages: 1,
            memory_key: "history".to_string(),
            summary_key: "summary".to_string(),
        };

        let memory = ConversationSummaryMemory::with_config(config);
        let conv_id = "summary_test";

        // Add enough messages to trigger summarization
        memory
            .add_message(conv_id, "user", "First message")
            .await
            .unwrap();
        memory
            .add_message(conv_id, "assistant", "First response")
            .await
            .unwrap();
        memory
            .add_message(conv_id, "user", "Second message")
            .await
            .unwrap();
        memory
            .add_message(conv_id, "assistant", "Second response")
            .await
            .unwrap();

        // Should have triggered summarization
        let messages = memory.get_messages(conv_id).await.unwrap();
        assert!(messages.len() <= 1, "Should have summarized old messages");

        let variables = memory.get_memory_variables(conv_id).await.unwrap();
        assert!(
            variables.contains_key("summary"),
            "Should have summary in variables"
        );
    }
}