chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Memory and RAG (Retrieval-Augmented Generation) System
//!
//! Provides persistent context, knowledge retrieval, and semantic search for agents.
//!
//! ## Features
//!
//! - **Vector Store**: Semantic similarity search using embeddings
//! - **Memory Types**: Short-term, long-term, episodic, and semantic memory
//! - **Knowledge Base**: Structured document storage with chunking
//! - **Context Window**: Smart context management for LLM prompts

#![allow(dead_code)]
//! - **Caching**: Frequently accessed information caching

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

// =============================================================================
// Core Types
// =============================================================================

/// Unique identifier for memory entries
pub type MemoryId = String;

/// Vector embedding (typically 384-1536 dimensions depending on model)
pub type Embedding = Vec<f32>;

/// Memory entry representing a piece of stored knowledge
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
    /// Unique identifier
    pub id: MemoryId,
    /// The content/text of this memory
    pub content: String,
    /// Vector embedding for similarity search
    #[serde(skip_serializing_if = "Option::is_none")]
    pub embedding: Option<Embedding>,
    /// Memory type classification
    pub memory_type: MemoryType,
    /// Source of this memory (conversation, document, user input, etc.)
    pub source: MemorySource,
    /// Importance score (0.0 - 1.0)
    pub importance: f32,
    /// Access count for LRU caching
    pub access_count: u64,
    /// Last accessed timestamp
    pub last_accessed: DateTime<Utc>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Optional expiration
    pub expires_at: Option<DateTime<Utc>>,
    /// Associated agent ID
    pub agent_id: Option<String>,
    /// Associated session ID
    pub session_id: Option<String>,
    /// Custom metadata
    pub metadata: HashMap<String, serde_json::Value>,
    /// Tags for filtering
    pub tags: Vec<String>,
}

impl MemoryEntry {
    /// Create a new memory entry
    pub fn new(content: impl Into<String>, memory_type: MemoryType, source: MemorySource) -> Self {
        let now = Utc::now();
        Self {
            id: generate_memory_id(),
            content: content.into(),
            embedding: None,
            memory_type,
            source,
            importance: 0.5,
            access_count: 0,
            last_accessed: now,
            created_at: now,
            expires_at: None,
            agent_id: None,
            session_id: None,
            metadata: HashMap::new(),
            tags: Vec::new(),
        }
    }

    /// Set the embedding
    pub fn with_embedding(mut self, embedding: Embedding) -> Self {
        self.embedding = Some(embedding);
        self
    }

    /// Set importance score
    pub fn with_importance(mut self, importance: f32) -> Self {
        self.importance = importance.clamp(0.0, 1.0);
        self
    }

    /// Set agent ID
    pub fn with_agent(mut self, agent_id: impl Into<String>) -> Self {
        self.agent_id = Some(agent_id.into());
        self
    }

    /// Set session ID
    pub fn with_session(mut self, session_id: impl Into<String>) -> Self {
        self.session_id = Some(session_id.into());
        self
    }

    /// Add a tag
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Set expiration
    pub fn expires_in(mut self, duration: chrono::Duration) -> Self {
        self.expires_at = Some(Utc::now() + duration);
        self
    }

    /// Check if expired
    pub fn is_expired(&self) -> bool {
        self.expires_at.map(|exp| Utc::now() > exp).unwrap_or(false)
    }
}

/// Types of memory
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryType {
    /// Short-term working memory (current conversation context)
    ShortTerm,
    /// Long-term persistent memory (facts, preferences, learned info)
    LongTerm,
    /// Episodic memory (specific events and experiences)
    Episodic,
    /// Semantic memory (concepts, relationships, general knowledge)
    Semantic,
    /// Procedural memory (how to do things, workflows)
    Procedural,
    /// User preferences and settings
    Preference,
    /// Cached computation results
    Cache,
}

/// Source of memory entry
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemorySource {
    /// From a conversation message
    Conversation {
        session_id: String,
        message_id: String,
    },
    /// From a document/file
    Document { path: String, chunk_index: u32 },
    /// Direct user input/instruction
    UserInput,
    /// Agent reasoning/reflection
    AgentReasoning { agent_id: String },
    /// External API or tool result
    ToolResult { tool_name: String },
    /// Web page or URL
    WebPage { url: String },
    /// System-generated summary
    Summary { source_ids: Vec<String> },
    /// Custom source
    Custom { source_type: String },
}

// =============================================================================
// Vector Store
// =============================================================================

/// Configuration for the vector store
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorStoreConfig {
    /// Embedding model to use
    pub embedding_model: EmbeddingModel,
    /// Dimension of embeddings
    pub embedding_dim: usize,
    /// Similarity metric
    pub similarity_metric: SimilarityMetric,
    /// Maximum entries before pruning
    pub max_entries: usize,
    /// Database path
    pub db_path: Option<String>,
}

impl Default for VectorStoreConfig {
    fn default() -> Self {
        Self {
            embedding_model: EmbeddingModel::default(),
            embedding_dim: 384,
            similarity_metric: SimilarityMetric::Cosine,
            max_entries: 100_000,
            db_path: None,
        }
    }
}

/// Embedding model options
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EmbeddingModel {
    /// OpenAI text-embedding-3-small (1536 dims)
    OpenAISmall,
    /// OpenAI text-embedding-3-large (3072 dims)
    OpenAILarge,
    /// OpenAI text-embedding-ada-002 (1536 dims)
    OpenAIAda,
    /// Sentence Transformers all-MiniLM-L6-v2 (384 dims)
    #[default]
    MiniLM,
    /// Sentence Transformers all-mpnet-base-v2 (768 dims)
    MPNet,
    /// Cohere embed-english-v3.0 (1024 dims)
    Cohere,
    /// Google text-embedding-004 (768 dims)
    GoogleGecko,
    /// Voyage AI voyage-2 (1024 dims)
    Voyage,
    /// Local model via Ollama
    Ollama { model: String },
    /// Custom model
    Custom { name: String, dim: usize },
}

impl EmbeddingModel {
    /// Get the dimension for this model
    pub fn dimension(&self) -> usize {
        match self {
            EmbeddingModel::OpenAISmall => 1536,
            EmbeddingModel::OpenAILarge => 3072,
            EmbeddingModel::OpenAIAda => 1536,
            EmbeddingModel::MiniLM => 384,
            EmbeddingModel::MPNet => 768,
            EmbeddingModel::Cohere => 1024,
            EmbeddingModel::GoogleGecko => 768,
            EmbeddingModel::Voyage => 1024,
            EmbeddingModel::Ollama { .. } => 4096, // Typical for Ollama models
            EmbeddingModel::Custom { dim, .. } => *dim,
        }
    }
}

/// Similarity metrics for vector search
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SimilarityMetric {
    #[default]
    Cosine,
    Euclidean,
    DotProduct,
    Manhattan,
}

impl SimilarityMetric {
    /// Calculate similarity between two vectors
    pub fn calculate(&self, a: &[f32], b: &[f32]) -> f32 {
        assert_eq!(a.len(), b.len(), "Vector dimensions must match");

        match self {
            SimilarityMetric::Cosine => {
                let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
                let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
                let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
                if norm_a == 0.0 || norm_b == 0.0 {
                    0.0
                } else {
                    dot / (norm_a * norm_b)
                }
            }
            SimilarityMetric::Euclidean => {
                let dist: f32 = a
                    .iter()
                    .zip(b.iter())
                    .map(|(x, y)| (x - y).powi(2))
                    .sum::<f32>()
                    .sqrt();
                1.0 / (1.0 + dist) // Convert distance to similarity
            }
            SimilarityMetric::DotProduct => a.iter().zip(b.iter()).map(|(x, y)| x * y).sum(),
            SimilarityMetric::Manhattan => {
                let dist: f32 = a.iter().zip(b.iter()).map(|(x, y)| (x - y).abs()).sum();
                1.0 / (1.0 + dist)
            }
        }
    }
}

/// Search result from vector store
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    /// The memory entry
    pub entry: MemoryEntry,
    /// Similarity score (0.0 - 1.0)
    pub score: f32,
    /// Rank in results
    pub rank: usize,
}

/// Vector store for semantic search
pub struct VectorStore {
    config: VectorStoreConfig,
    entries: Vec<MemoryEntry>,
    db: Option<rusqlite::Connection>,
}

impl VectorStore {
    /// Create a new in-memory vector store
    pub fn new(config: VectorStoreConfig) -> Self {
        Self {
            config,
            entries: Vec::new(),
            db: None,
        }
    }

    /// Create a vector store with SQLite persistence
    pub fn with_persistence(
        config: VectorStoreConfig,
        db_path: impl AsRef<Path>,
    ) -> Result<Self, MemoryError> {
        let db = rusqlite::Connection::open(db_path.as_ref())
            .map_err(|e| MemoryError::Database(e.to_string()))?;

        // Initialize schema
        db.execute_batch(
            r#"
            CREATE TABLE IF NOT EXISTS memory_entries (
                id TEXT PRIMARY KEY,
                content TEXT NOT NULL,
                embedding BLOB,
                memory_type TEXT NOT NULL,
                source TEXT NOT NULL,
                importance REAL NOT NULL,
                access_count INTEGER NOT NULL DEFAULT 0,
                last_accessed TEXT NOT NULL,
                created_at TEXT NOT NULL,
                expires_at TEXT,
                agent_id TEXT,
                session_id TEXT,
                metadata TEXT,
                tags TEXT
            );
            
            CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(memory_type);
            CREATE INDEX IF NOT EXISTS idx_agent_id ON memory_entries(agent_id);
            CREATE INDEX IF NOT EXISTS idx_session_id ON memory_entries(session_id);
            CREATE INDEX IF NOT EXISTS idx_created_at ON memory_entries(created_at);
            CREATE INDEX IF NOT EXISTS idx_importance ON memory_entries(importance DESC);
        "#,
        )
        .map_err(|e| MemoryError::Database(e.to_string()))?;

        let mut store = Self {
            config,
            entries: Vec::new(),
            db: Some(db),
        };

        store.load_from_db()?;
        Ok(store)
    }

    /// Load entries from database
    fn load_from_db(&mut self) -> Result<(), MemoryError> {
        if let Some(ref db) = self.db {
            let mut stmt = db
                .prepare(
                    "SELECT id, content, embedding, memory_type, source, importance, 
                        access_count, last_accessed, created_at, expires_at, 
                        agent_id, session_id, metadata, tags 
                 FROM memory_entries 
                 ORDER BY importance DESC, created_at DESC",
                )
                .map_err(|e| MemoryError::Database(e.to_string()))?;

            let entries = stmt
                .query_map([], |row| {
                    let embedding_blob: Option<Vec<u8>> = row.get(2)?;
                    let embedding = embedding_blob.map(|blob| {
                        blob.chunks(4)
                            .map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap_or([0; 4])))
                            .collect()
                    });

                    Ok(MemoryEntry {
                        id: row.get(0)?,
                        content: row.get(1)?,
                        embedding,
                        memory_type: serde_json::from_str(&row.get::<_, String>(3)?)
                            .unwrap_or(MemoryType::LongTerm),
                        source: serde_json::from_str(&row.get::<_, String>(4)?)
                            .unwrap_or(MemorySource::UserInput),
                        importance: row.get(5)?,
                        access_count: row.get(6)?,
                        last_accessed: row
                            .get::<_, String>(7)?
                            .parse()
                            .unwrap_or_else(|_| Utc::now()),
                        created_at: row
                            .get::<_, String>(8)?
                            .parse()
                            .unwrap_or_else(|_| Utc::now()),
                        expires_at: row
                            .get::<_, Option<String>>(9)?
                            .and_then(|s| s.parse().ok()),
                        agent_id: row.get(10)?,
                        session_id: row.get(11)?,
                        metadata: row
                            .get::<_, Option<String>>(12)?
                            .and_then(|s| serde_json::from_str(&s).ok())
                            .unwrap_or_default(),
                        tags: row
                            .get::<_, Option<String>>(13)?
                            .and_then(|s| serde_json::from_str(&s).ok())
                            .unwrap_or_default(),
                    })
                })
                .map_err(|e| MemoryError::Database(e.to_string()))?;

            self.entries = entries.filter_map(|e| e.ok()).collect();
        }
        Ok(())
    }

    /// Add a memory entry
    pub fn add(&mut self, entry: MemoryEntry) -> Result<MemoryId, MemoryError> {
        let id = entry.id.clone();

        // Persist to database if available
        if let Some(ref db) = self.db {
            let embedding_blob: Option<Vec<u8>> = entry
                .embedding
                .as_ref()
                .map(|emb| emb.iter().flat_map(|f| f.to_le_bytes()).collect());

            db.execute(
                "INSERT OR REPLACE INTO memory_entries 
                 (id, content, embedding, memory_type, source, importance, 
                  access_count, last_accessed, created_at, expires_at, 
                  agent_id, session_id, metadata, tags)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)",
                rusqlite::params![
                    entry.id,
                    entry.content,
                    embedding_blob,
                    serde_json::to_string(&entry.memory_type).unwrap_or_default(),
                    serde_json::to_string(&entry.source).unwrap_or_default(),
                    entry.importance,
                    entry.access_count,
                    entry.last_accessed.to_rfc3339(),
                    entry.created_at.to_rfc3339(),
                    entry.expires_at.map(|e| e.to_rfc3339()),
                    entry.agent_id,
                    entry.session_id,
                    serde_json::to_string(&entry.metadata).ok(),
                    serde_json::to_string(&entry.tags).ok(),
                ],
            )
            .map_err(|e| MemoryError::Database(e.to_string()))?;
        }

        self.entries.push(entry);

        // Prune if needed
        if self.entries.len() > self.config.max_entries {
            self.prune()?;
        }

        Ok(id)
    }

    /// Search for similar entries
    pub fn search(&mut self, query_embedding: &Embedding, limit: usize) -> Vec<SearchResult> {
        let mut results: Vec<(usize, f32)> = self
            .entries
            .iter()
            .enumerate()
            .filter(|(_, e)| !e.is_expired() && e.embedding.is_some())
            .map(|(i, e)| {
                let score = self
                    .config
                    .similarity_metric
                    .calculate(query_embedding, e.embedding.as_ref().unwrap());
                (i, score)
            })
            .collect();

        // Sort by score descending
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Take top results and update access counts
        results
            .into_iter()
            .take(limit)
            .enumerate()
            .map(|(rank, (idx, score))| {
                self.entries[idx].access_count += 1;
                self.entries[idx].last_accessed = Utc::now();

                SearchResult {
                    entry: self.entries[idx].clone(),
                    score,
                    rank,
                }
            })
            .collect()
    }

    /// Search by memory type
    pub fn search_by_type(&self, memory_type: MemoryType, limit: usize) -> Vec<&MemoryEntry> {
        self.entries
            .iter()
            .filter(|e| e.memory_type == memory_type && !e.is_expired())
            .take(limit)
            .collect()
    }

    /// Search by tags
    pub fn search_by_tags(&self, tags: &[String], limit: usize) -> Vec<&MemoryEntry> {
        self.entries
            .iter()
            .filter(|e| !e.is_expired() && tags.iter().any(|t| e.tags.contains(t)))
            .take(limit)
            .collect()
    }

    /// Get entry by ID
    pub fn get(&self, id: &str) -> Option<&MemoryEntry> {
        self.entries.iter().find(|e| e.id == id)
    }

    /// Delete entry
    pub fn delete(&mut self, id: &str) -> Result<bool, MemoryError> {
        if let Some(pos) = self.entries.iter().position(|e| e.id == id) {
            self.entries.remove(pos);

            if let Some(ref db) = self.db {
                db.execute("DELETE FROM memory_entries WHERE id = ?1", [id])
                    .map_err(|e| MemoryError::Database(e.to_string()))?;
            }

            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Prune old/low-importance entries
    fn prune(&mut self) -> Result<(), MemoryError> {
        // Remove expired entries
        self.entries.retain(|e| !e.is_expired());

        // If still over limit, remove lowest importance entries
        if self.entries.len() > self.config.max_entries {
            self.entries.sort_by(|a, b| {
                b.importance
                    .partial_cmp(&a.importance)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            self.entries.truncate(self.config.max_entries);
        }

        Ok(())
    }

    /// Get statistics
    pub fn stats(&self) -> VectorStoreStats {
        VectorStoreStats {
            total_entries: self.entries.len(),
            entries_by_type: self.entries.iter().fold(HashMap::new(), |mut acc, e| {
                *acc.entry(format!("{:?}", e.memory_type)).or_insert(0) += 1;
                acc
            }),
            total_access_count: self.entries.iter().map(|e| e.access_count).sum(),
            avg_importance: if self.entries.is_empty() {
                0.0
            } else {
                self.entries.iter().map(|e| e.importance).sum::<f32>() / self.entries.len() as f32
            },
        }
    }
}

/// Vector store statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorStoreStats {
    pub total_entries: usize,
    pub entries_by_type: HashMap<String, usize>,
    pub total_access_count: u64,
    pub avg_importance: f32,
}

// =============================================================================
// Knowledge Base / RAG
// =============================================================================

/// Document for the knowledge base
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    /// Unique identifier
    pub id: String,
    /// Document title
    pub title: String,
    /// Full content
    pub content: String,
    /// Document type
    pub doc_type: DocumentType,
    /// Source URL or path
    pub source: String,
    /// Chunked content for embedding
    pub chunks: Vec<DocumentChunk>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last updated
    pub updated_at: DateTime<Utc>,
    /// Metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Document types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DocumentType {
    Text,
    Markdown,
    Code { language: String },
    Html,
    Pdf,
    Json,
    Yaml,
    Csv,
    Custom { mime_type: String },
}

/// A chunk of a document
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentChunk {
    /// Chunk index
    pub index: u32,
    /// Chunk content
    pub content: String,
    /// Start position in original document
    pub start_pos: usize,
    /// End position in original document
    pub end_pos: usize,
    /// Vector embedding
    pub embedding: Option<Embedding>,
    /// Token count estimate
    pub token_count: u32,
}

/// Configuration for document chunking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChunkingConfig {
    /// Target chunk size in tokens
    pub chunk_size: usize,
    /// Overlap between chunks in tokens
    pub chunk_overlap: usize,
    /// Chunking strategy
    pub strategy: ChunkingStrategy,
}

impl Default for ChunkingConfig {
    fn default() -> Self {
        Self {
            chunk_size: 512,
            chunk_overlap: 50,
            strategy: ChunkingStrategy::Semantic,
        }
    }
}

/// Chunking strategies
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChunkingStrategy {
    /// Fixed-size character chunks
    FixedSize,
    /// Split on sentences
    Sentence,
    /// Split on paragraphs
    Paragraph,
    /// Semantic chunking (respects structure)
    #[default]
    Semantic,
    /// Code-aware chunking
    Code,
}

/// Knowledge base for RAG
pub struct KnowledgeBase {
    /// Vector store for semantic search
    vector_store: VectorStore,
    /// Documents
    documents: HashMap<String, Document>,
    /// Chunking configuration
    chunking_config: ChunkingConfig,
}

impl KnowledgeBase {
    /// Create a new knowledge base
    pub fn new(vector_config: VectorStoreConfig) -> Self {
        Self {
            vector_store: VectorStore::new(vector_config),
            documents: HashMap::new(),
            chunking_config: ChunkingConfig::default(),
        }
    }

    /// Create with persistence
    pub fn with_persistence(
        vector_config: VectorStoreConfig,
        db_path: impl AsRef<Path>,
    ) -> Result<Self, MemoryError> {
        Ok(Self {
            vector_store: VectorStore::with_persistence(vector_config, db_path)?,
            documents: HashMap::new(),
            chunking_config: ChunkingConfig::default(),
        })
    }

    /// Add a document
    pub fn add_document(&mut self, mut document: Document) -> Result<String, MemoryError> {
        // Chunk the document
        document.chunks = self.chunk_document(&document.content);

        let doc_id = document.id.clone();

        // Add chunks to vector store
        for chunk in &document.chunks {
            if let Some(ref embedding) = chunk.embedding {
                let entry = MemoryEntry::new(
                    &chunk.content,
                    MemoryType::Semantic,
                    MemorySource::Document {
                        path: document.source.clone(),
                        chunk_index: chunk.index,
                    },
                )
                .with_embedding(embedding.clone())
                .with_tag(format!("doc:{}", doc_id));

                self.vector_store.add(entry)?;
            }
        }

        self.documents.insert(doc_id.clone(), document);
        Ok(doc_id)
    }

    /// Chunk a document
    fn chunk_document(&self, content: &str) -> Vec<DocumentChunk> {
        match self.chunking_config.strategy {
            ChunkingStrategy::Semantic => self.semantic_chunk(content),
            ChunkingStrategy::Paragraph => self.paragraph_chunk(content),
            ChunkingStrategy::Sentence => self.sentence_chunk(content),
            ChunkingStrategy::FixedSize => self.fixed_chunk(content),
            ChunkingStrategy::Code => self.code_chunk(content),
        }
    }

    fn semantic_chunk(&self, content: &str) -> Vec<DocumentChunk> {
        // Split on double newlines (paragraphs) but respect size limits
        let mut chunks = Vec::new();
        let mut current_chunk = String::new();
        let mut start_pos = 0;
        let mut chunk_index = 0;

        for para in content.split("\n\n") {
            let para = para.trim();
            if para.is_empty() {
                continue;
            }

            let para_tokens = estimate_tokens(para);
            let current_tokens = estimate_tokens(&current_chunk);

            if current_tokens + para_tokens > self.chunking_config.chunk_size
                && !current_chunk.is_empty()
            {
                // Save current chunk
                let end_pos = start_pos + current_chunk.len();
                chunks.push(DocumentChunk {
                    index: chunk_index,
                    content: current_chunk.trim().to_string(),
                    start_pos,
                    end_pos,
                    embedding: None,
                    token_count: estimate_tokens(&current_chunk) as u32,
                });
                chunk_index += 1;
                start_pos = end_pos;
                current_chunk = String::new();
            }

            if !current_chunk.is_empty() {
                current_chunk.push_str("\n\n");
            }
            current_chunk.push_str(para);
        }

        // Add remaining content
        if !current_chunk.is_empty() {
            let end_pos = start_pos + current_chunk.len();
            chunks.push(DocumentChunk {
                index: chunk_index,
                content: current_chunk.trim().to_string(),
                start_pos,
                end_pos,
                embedding: None,
                token_count: estimate_tokens(&current_chunk) as u32,
            });
        }

        chunks
    }

    fn paragraph_chunk(&self, content: &str) -> Vec<DocumentChunk> {
        content
            .split("\n\n")
            .filter(|p| !p.trim().is_empty())
            .enumerate()
            .scan(0usize, |pos, (i, para)| {
                let start = *pos;
                *pos += para.len() + 2;
                Some(DocumentChunk {
                    index: i as u32,
                    content: para.trim().to_string(),
                    start_pos: start,
                    end_pos: *pos,
                    embedding: None,
                    token_count: estimate_tokens(para) as u32,
                })
            })
            .collect()
    }

    fn sentence_chunk(&self, content: &str) -> Vec<DocumentChunk> {
        // Simple sentence splitting (could be improved with NLP)
        let sentences: Vec<&str> = content
            .split(['.', '!', '?'])
            .filter(|s| !s.trim().is_empty())
            .collect();

        let mut chunks = Vec::new();
        let mut current = String::new();
        let mut start = 0;
        let mut idx = 0;

        for sentence in sentences {
            let sentence = sentence.trim();
            if estimate_tokens(&current) + estimate_tokens(sentence)
                > self.chunking_config.chunk_size
                && !current.is_empty()
            {
                chunks.push(DocumentChunk {
                    index: idx,
                    content: current.clone(),
                    start_pos: start,
                    end_pos: start + current.len(),
                    embedding: None,
                    token_count: estimate_tokens(&current) as u32,
                });
                idx += 1;
                start += current.len();
                current.clear();
            }
            if !current.is_empty() {
                current.push(' ');
            }
            current.push_str(sentence);
            current.push('.');
        }

        if !current.is_empty() {
            chunks.push(DocumentChunk {
                index: idx,
                content: current.clone(),
                start_pos: start,
                end_pos: start + current.len(),
                embedding: None,
                token_count: estimate_tokens(&current) as u32,
            });
        }

        chunks
    }

    fn fixed_chunk(&self, content: &str) -> Vec<DocumentChunk> {
        let chars_per_chunk = self.chunking_config.chunk_size * 4; // Rough estimate
        content
            .chars()
            .collect::<Vec<_>>()
            .chunks(chars_per_chunk)
            .enumerate()
            .map(|(i, chars)| {
                let s: String = chars.iter().collect();
                DocumentChunk {
                    index: i as u32,
                    content: s.clone(),
                    start_pos: i * chars_per_chunk,
                    end_pos: (i + 1) * chars_per_chunk,
                    embedding: None,
                    token_count: estimate_tokens(&s) as u32,
                }
            })
            .collect()
    }

    fn code_chunk(&self, content: &str) -> Vec<DocumentChunk> {
        // Split on function/class definitions (simple heuristic)
        let mut chunks = Vec::new();
        let mut current = String::new();
        let mut start = 0;
        let mut idx = 0;

        for line in content.lines() {
            let is_boundary = line.starts_with("fn ")
                || line.starts_with("pub fn ")
                || line.starts_with("async fn ")
                || line.starts_with("impl ")
                || line.starts_with("struct ")
                || line.starts_with("enum ")
                || line.starts_with("trait ")
                || line.starts_with("class ")
                || line.starts_with("def ")
                || line.starts_with("function ")
                || line.starts_with("const ")
                || line.starts_with("export ");

            if is_boundary && !current.is_empty() {
                chunks.push(DocumentChunk {
                    index: idx,
                    content: current.clone(),
                    start_pos: start,
                    end_pos: start + current.len(),
                    embedding: None,
                    token_count: estimate_tokens(&current) as u32,
                });
                idx += 1;
                start += current.len();
                current.clear();
            }

            current.push_str(line);
            current.push('\n');
        }

        if !current.is_empty() {
            chunks.push(DocumentChunk {
                index: idx,
                content: current.clone(),
                start_pos: start,
                end_pos: start + current.len(),
                embedding: None,
                token_count: estimate_tokens(&current) as u32,
            });
        }

        chunks
    }

    /// Retrieve relevant context for a query
    pub fn retrieve(&mut self, query_embedding: &Embedding, limit: usize) -> Vec<SearchResult> {
        self.vector_store.search(query_embedding, limit)
    }

    /// Get document by ID
    pub fn get_document(&self, id: &str) -> Option<&Document> {
        self.documents.get(id)
    }

    /// List all documents
    pub fn list_documents(&self) -> Vec<&Document> {
        self.documents.values().collect()
    }

    /// Delete document
    pub fn delete_document(&mut self, id: &str) -> bool {
        self.documents.remove(id).is_some()
    }
}

// =============================================================================
// Context Window Manager
// =============================================================================

/// Manages context for LLM prompts
#[derive(Debug, Clone)]
pub struct ContextWindow {
    /// Maximum tokens for context
    pub max_tokens: usize,
    /// Reserved tokens for response
    pub reserved_for_response: usize,
    /// Context segments
    segments: Vec<ContextSegment>,
}

/// A segment of context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextSegment {
    /// Segment type
    pub segment_type: ContextSegmentType,
    /// Content
    pub content: String,
    /// Token count
    pub tokens: usize,
    /// Priority (higher = more important)
    pub priority: u32,
    /// Whether this segment is required
    pub required: bool,
}

/// Types of context segments
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextSegmentType {
    SystemPrompt,
    UserPreferences,
    ConversationHistory,
    RetrievedContext,
    ToolResults,
    CurrentQuery,
    Custom { name: String },
}

impl ContextWindow {
    /// Create a new context window
    pub fn new(max_tokens: usize) -> Self {
        Self {
            max_tokens,
            reserved_for_response: max_tokens / 4, // Reserve 25% for response
            segments: Vec::new(),
        }
    }

    /// Add a context segment
    pub fn add_segment(&mut self, segment: ContextSegment) {
        self.segments.push(segment);
    }

    /// Build the final context, respecting token limits
    pub fn build(&mut self) -> String {
        let available = self.max_tokens - self.reserved_for_response;

        // Sort by priority (required first, then by priority)
        self.segments
            .sort_by(|a, b| match (a.required, b.required) {
                (true, false) => std::cmp::Ordering::Less,
                (false, true) => std::cmp::Ordering::Greater,
                _ => b.priority.cmp(&a.priority),
            });

        let mut total_tokens = 0;
        let mut result = Vec::new();

        for segment in &self.segments {
            if total_tokens + segment.tokens <= available {
                result.push(segment.content.clone());
                total_tokens += segment.tokens;
            } else if segment.required {
                // Truncate if required
                let remaining = available.saturating_sub(total_tokens);
                if remaining > 0 {
                    let truncated = truncate_to_tokens(&segment.content, remaining);
                    result.push(truncated);
                    break;
                }
            }
        }

        result.join("\n\n")
    }

    /// Get current token usage
    pub fn token_usage(&self) -> (usize, usize) {
        let used: usize = self.segments.iter().map(|s| s.tokens).sum();
        (used, self.max_tokens - self.reserved_for_response)
    }
}

// =============================================================================
// Cache
// =============================================================================

/// Cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheEntry<T> {
    pub key: String,
    pub value: T,
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
    pub access_count: u64,
}

impl<T> CacheEntry<T> {
    pub fn is_expired(&self) -> bool {
        self.expires_at.map(|exp| Utc::now() > exp).unwrap_or(false)
    }
}

/// Simple LRU cache for agent computations
pub struct AgentCache<T> {
    entries: HashMap<String, CacheEntry<T>>,
    max_size: usize,
}

impl<T: Clone> AgentCache<T> {
    /// Create a new cache
    pub fn new(max_size: usize) -> Self {
        Self {
            entries: HashMap::new(),
            max_size,
        }
    }

    /// Get a value from cache
    pub fn get(&mut self, key: &str) -> Option<T> {
        if let Some(entry) = self.entries.get_mut(key) {
            if entry.is_expired() {
                self.entries.remove(key);
                return None;
            }
            entry.access_count += 1;
            Some(entry.value.clone())
        } else {
            None
        }
    }

    /// Set a value in cache
    pub fn set(&mut self, key: impl Into<String>, value: T, ttl: Option<chrono::Duration>) {
        let key = key.into();
        let now = Utc::now();

        self.entries.insert(
            key.clone(),
            CacheEntry {
                key,
                value,
                created_at: now,
                expires_at: ttl.map(|d| now + d),
                access_count: 0,
            },
        );

        // Evict if over size
        if self.entries.len() > self.max_size {
            self.evict_lru();
        }
    }

    /// Remove a value
    pub fn remove(&mut self, key: &str) -> Option<T> {
        self.entries.remove(key).map(|e| e.value)
    }

    /// Clear the cache
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Evict least recently used entries
    fn evict_lru(&mut self) {
        // Remove expired first
        self.entries.retain(|_, v| !v.is_expired());

        // If still over, remove least accessed
        if self.entries.len() > self.max_size {
            // Collect keys to remove (sorted by access count)
            let mut entries: Vec<_> = self
                .entries
                .iter()
                .map(|(k, v)| (k.clone(), v.access_count))
                .collect();
            entries.sort_by_key(|(_, count)| *count);

            let to_remove = self.entries.len() - self.max_size;
            let keys_to_remove: Vec<String> = entries
                .into_iter()
                .take(to_remove)
                .map(|(k, _)| k)
                .collect();

            for key in keys_to_remove {
                self.entries.remove(&key);
            }
        }
    }
}

// =============================================================================
// Memory Manager (Unified Interface)
// =============================================================================

/// Configuration for the memory manager
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Vector store configuration
    pub vector_store: VectorStoreConfig,
    /// Chunking configuration
    pub chunking: ChunkingConfig,
    /// Context window size
    pub context_window_tokens: usize,
    /// Cache size
    pub cache_size: usize,
    /// Database path (None for in-memory)
    pub db_path: Option<String>,
    /// Auto-summarize long conversations
    pub auto_summarize: bool,
    /// Summarize after this many messages
    pub summarize_threshold: usize,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            vector_store: VectorStoreConfig::default(),
            chunking: ChunkingConfig::default(),
            context_window_tokens: 8192,
            cache_size: 1000,
            db_path: None,
            auto_summarize: true,
            summarize_threshold: 20,
        }
    }
}

/// Unified memory manager for agents
pub struct MemoryManager {
    config: MemoryConfig,
    vector_store: VectorStore,
    knowledge_base: KnowledgeBase,
    cache: AgentCache<String>,
}

impl MemoryManager {
    /// Create a new memory manager
    pub fn new(config: MemoryConfig) -> Result<Self, MemoryError> {
        let vector_store = if let Some(ref path) = config.db_path {
            VectorStore::with_persistence(config.vector_store.clone(), path)?
        } else {
            VectorStore::new(config.vector_store.clone())
        };

        let knowledge_base = if let Some(ref path) = config.db_path {
            let kb_path = format!("{}_kb", path);
            KnowledgeBase::with_persistence(config.vector_store.clone(), kb_path)?
        } else {
            KnowledgeBase::new(config.vector_store.clone())
        };

        Ok(Self {
            config: config.clone(),
            vector_store,
            knowledge_base,
            cache: AgentCache::new(config.cache_size),
        })
    }

    /// Store a memory
    pub fn remember(
        &mut self,
        content: impl Into<String>,
        memory_type: MemoryType,
        source: MemorySource,
    ) -> Result<MemoryId, MemoryError> {
        let entry = MemoryEntry::new(content, memory_type, source);
        self.vector_store.add(entry)
    }

    /// Store a memory with embedding
    pub fn remember_with_embedding(
        &mut self,
        content: impl Into<String>,
        embedding: Embedding,
        memory_type: MemoryType,
        source: MemorySource,
    ) -> Result<MemoryId, MemoryError> {
        let entry = MemoryEntry::new(content, memory_type, source).with_embedding(embedding);
        self.vector_store.add(entry)
    }

    /// Recall memories similar to a query
    pub fn recall(&mut self, query_embedding: &Embedding, limit: usize) -> Vec<SearchResult> {
        self.vector_store.search(query_embedding, limit)
    }

    /// Recall by type
    pub fn recall_by_type(&self, memory_type: MemoryType, limit: usize) -> Vec<&MemoryEntry> {
        self.vector_store.search_by_type(memory_type, limit)
    }

    /// Add document to knowledge base
    pub fn add_document(&mut self, document: Document) -> Result<String, MemoryError> {
        self.knowledge_base.add_document(document)
    }

    /// Retrieve from knowledge base
    pub fn retrieve(&mut self, query_embedding: &Embedding, limit: usize) -> Vec<SearchResult> {
        self.knowledge_base.retrieve(query_embedding, limit)
    }

    /// Build context for a prompt
    pub fn build_context(
        &mut self,
        query_embedding: &Embedding,
        system_prompt: &str,
        conversation: &[String],
    ) -> String {
        let mut context = ContextWindow::new(self.config.context_window_tokens);

        // System prompt (required)
        context.add_segment(ContextSegment {
            segment_type: ContextSegmentType::SystemPrompt,
            content: system_prompt.to_string(),
            tokens: estimate_tokens(system_prompt),
            priority: 100,
            required: true,
        });

        // Retrieved context
        let retrieved = self.recall(query_embedding, 5);
        if !retrieved.is_empty() {
            let retrieved_text: String = retrieved
                .iter()
                .map(|r| format!("- {}", r.entry.content))
                .collect::<Vec<_>>()
                .join("\n");

            context.add_segment(ContextSegment {
                segment_type: ContextSegmentType::RetrievedContext,
                content: format!("Relevant context:\n{}", retrieved_text),
                tokens: estimate_tokens(&retrieved_text) + 20,
                priority: 80,
                required: false,
            });
        }

        // Conversation history
        let conv_text = conversation.join("\n");
        context.add_segment(ContextSegment {
            segment_type: ContextSegmentType::ConversationHistory,
            content: conv_text.clone(),
            tokens: estimate_tokens(&conv_text),
            priority: 90,
            required: false,
        });

        context.build()
    }

    /// Cache a computation result
    pub fn cache_result(
        &mut self,
        key: impl Into<String>,
        value: String,
        ttl: Option<chrono::Duration>,
    ) {
        self.cache.set(key, value, ttl);
    }

    /// Get cached result
    pub fn get_cached(&mut self, key: &str) -> Option<String> {
        self.cache.get(key)
    }

    /// Get statistics
    pub fn stats(&self) -> MemoryStats {
        MemoryStats {
            vector_store: self.vector_store.stats(),
            document_count: self.knowledge_base.list_documents().len(),
        }
    }
}

/// Memory statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryStats {
    pub vector_store: VectorStoreStats,
    pub document_count: usize,
}

// =============================================================================
// Error Types
// =============================================================================

/// Memory system errors
#[derive(Debug, Clone)]
pub enum MemoryError {
    /// Database error
    Database(String),
    /// Embedding error
    Embedding(String),
    /// Not found
    NotFound(String),
    /// Invalid input
    InvalidInput(String),
    /// IO error
    Io(String),
}

impl std::fmt::Display for MemoryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MemoryError::Database(e) => write!(f, "Database error: {}", e),
            MemoryError::Embedding(e) => write!(f, "Embedding error: {}", e),
            MemoryError::NotFound(e) => write!(f, "Not found: {}", e),
            MemoryError::InvalidInput(e) => write!(f, "Invalid input: {}", e),
            MemoryError::Io(e) => write!(f, "IO error: {}", e),
        }
    }
}

impl std::error::Error for MemoryError {}

// =============================================================================
// Utility Functions
// =============================================================================

fn generate_memory_id() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    format!("mem_{:x}", timestamp)
}

/// Estimate token count (rough approximation: 4 chars per token)
fn estimate_tokens(text: &str) -> usize {
    (text.len() as f32 / 4.0).ceil() as usize
}

/// Truncate text to approximate token count
fn truncate_to_tokens(text: &str, max_tokens: usize) -> String {
    let max_chars = max_tokens * 4;
    if text.len() <= max_chars {
        text.to_string()
    } else {
        format!("{}...", &text[..max_chars.min(text.len())])
    }
}

// =============================================================================
// Embedding Provider Trait
// =============================================================================

/// Trait for embedding providers
#[async_trait::async_trait]
pub trait EmbeddingProvider: Send + Sync {
    /// Generate embedding for text
    async fn embed(&self, text: &str) -> Result<Embedding, MemoryError>;

    /// Generate embeddings for multiple texts
    async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Embedding>, MemoryError>;

    /// Get the embedding dimension
    fn dimension(&self) -> usize;
}

/// OpenAI embedding provider
pub struct OpenAIEmbedding {
    #[allow(dead_code)]
    api_key: String,
    model: String,
}

impl OpenAIEmbedding {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            model: "text-embedding-3-small".to_string(),
        }
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }
}

#[async_trait::async_trait]
impl EmbeddingProvider for OpenAIEmbedding {
    async fn embed(&self, _text: &str) -> Result<Embedding, MemoryError> {
        // Implementation would call OpenAI API
        // For now, return a placeholder
        Ok(vec![0.0; 1536])
    }

    async fn embed_batch(&self, texts: &[String]) -> Result<Vec<Embedding>, MemoryError> {
        let mut results = Vec::new();
        for text in texts {
            results.push(self.embed(text).await?);
        }
        Ok(results)
    }

    fn dimension(&self) -> usize {
        match self.model.as_str() {
            "text-embedding-3-large" => 3072,
            _ => 1536,
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_memory_entry_creation() {
        let entry = MemoryEntry::new(
            "Test content",
            MemoryType::LongTerm,
            MemorySource::UserInput,
        );
        assert!(!entry.id.is_empty());
        assert_eq!(entry.content, "Test content");
        assert_eq!(entry.memory_type, MemoryType::LongTerm);
    }

    #[test]
    fn test_similarity_metrics() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let c = vec![0.0, 1.0, 0.0];

        assert!((SimilarityMetric::Cosine.calculate(&a, &b) - 1.0).abs() < 0.001);
        assert!((SimilarityMetric::Cosine.calculate(&a, &c) - 0.0).abs() < 0.001);
    }

    #[test]
    fn test_vector_store() {
        let config = VectorStoreConfig::default();
        let mut store = VectorStore::new(config);

        let entry = MemoryEntry::new("Test", MemoryType::ShortTerm, MemorySource::UserInput)
            .with_embedding(vec![1.0, 0.0, 0.0]);

        let id = store.add(entry).unwrap();
        assert!(!id.is_empty());
        assert!(store.get(&id).is_some());
    }

    #[test]
    fn test_context_window() {
        let mut ctx = ContextWindow::new(1000);

        ctx.add_segment(ContextSegment {
            segment_type: ContextSegmentType::SystemPrompt,
            content: "You are helpful".to_string(),
            tokens: 10,
            priority: 100,
            required: true,
        });

        let result = ctx.build();
        assert!(result.contains("You are helpful"));
    }

    #[test]
    fn test_cache() {
        let mut cache: AgentCache<String> = AgentCache::new(10);

        cache.set("key1", "value1".to_string(), None);
        assert_eq!(cache.get("key1"), Some("value1".to_string()));
        assert_eq!(cache.get("key2"), None);
    }

    #[test]
    fn test_estimate_tokens() {
        assert_eq!(estimate_tokens("hello"), 2); // 5 chars / 4 = 1.25 -> 2
        assert_eq!(estimate_tokens("hello world"), 3); // 11 chars / 4 = 2.75 -> 3
    }
}