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
//! Async interface for the memory graph using Tokio runtime
//!
//! This module provides a fully async API for all graph operations, enabling
//! high-performance concurrent operations and non-blocking I/O.
use crate::error::{Error, Result};
use crate::observatory::{
EventPublisher, MemoryGraphEvent, MemoryGraphMetrics, NoOpPublisher, ObservatoryConfig,
};
use crate::storage::{AsyncSledBackend, AsyncStorageBackend, StorageCache};
use crate::types::{
AgentId, AgentNode, Config, ConversationSession, Edge, EdgeType, Node, NodeId, PromptMetadata,
PromptNode, PromptTemplate, ResponseMetadata, ResponseNode, SessionId, TemplateId, TokenUsage,
ToolInvocation,
};
use chrono::Utc;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
/// Type alias for batch conversation data: (SessionId, prompt_content), optional (response_content, TokenUsage)
type ConversationBatchItem = ((SessionId, String), Option<(String, TokenUsage)>);
/// Async interface for interacting with the memory graph
///
/// `AsyncMemoryGraph` provides a fully async, thread-safe API for managing conversation
/// sessions, prompts, responses, agents, templates, and their relationships in a graph structure.
///
/// All operations are non-blocking and can be executed concurrently without performance degradation.
///
/// # Examples
///
/// ```no_run
/// use llm_memory_graph::engine::AsyncMemoryGraph;
/// use llm_memory_graph::Config;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = Config::new("./data/my_graph.db");
/// let graph = AsyncMemoryGraph::open(config).await?;
///
/// let session = graph.create_session().await?;
/// let prompt_id = graph.add_prompt(session.id, "What is Rust?".to_string(), None).await?;
/// Ok(())
/// }
/// ```
pub struct AsyncMemoryGraph {
backend: Arc<dyn AsyncStorageBackend>,
sessions: Arc<RwLock<HashMap<SessionId, ConversationSession>>>,
observatory: Option<Arc<dyn EventPublisher>>,
metrics: Option<Arc<MemoryGraphMetrics>>,
cache: StorageCache,
}
impl AsyncMemoryGraph {
/// Open or create an async memory graph with the given configuration
///
/// This will create the database directory if it doesn't exist and initialize
/// all necessary storage trees. Operations use Tokio's async runtime.
///
/// # Errors
///
/// Returns an error if:
/// - The database path is invalid or inaccessible
/// - Storage initialization fails
/// - Existing data is corrupted
///
/// # Examples
///
/// ```no_run
/// use llm_memory_graph::engine::AsyncMemoryGraph;
/// use llm_memory_graph::Config;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = Config::new("./data/graph.db");
/// let graph = AsyncMemoryGraph::open(config).await?;
/// Ok(())
/// }
/// ```
pub async fn open(config: Config) -> Result<Self> {
let backend = AsyncSledBackend::open(&config.path).await?;
// Convert cache size from MB to approximate entry count
// Assume ~1KB per node, so 100MB = ~100,000 nodes
let node_capacity = (config.cache_size_mb as u64) * 1000;
let edge_capacity = node_capacity * 5; // Edges are smaller, cache more
let cache = StorageCache::with_capacity(node_capacity, edge_capacity);
Ok(Self {
backend: Arc::new(backend),
sessions: Arc::new(RwLock::new(HashMap::new())),
observatory: None,
metrics: None,
cache,
})
}
/// Open graph with Observatory integration
///
/// # Examples
///
/// ```no_run
/// use llm_memory_graph::engine::AsyncMemoryGraph;
/// use llm_memory_graph::observatory::{ObservatoryConfig, InMemoryPublisher};
/// use llm_memory_graph::Config;
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = Config::default();
/// let publisher = Arc::new(InMemoryPublisher::new());
/// let obs_config = ObservatoryConfig::new().enabled();
///
/// let graph = AsyncMemoryGraph::with_observatory(
/// config,
/// Some(publisher),
/// obs_config
/// ).await?;
/// Ok(())
/// }
/// ```
pub async fn with_observatory(
config: Config,
publisher: Option<Arc<dyn EventPublisher>>,
obs_config: ObservatoryConfig,
) -> Result<Self> {
let backend = AsyncSledBackend::open(&config.path).await?;
// Convert cache size from MB to approximate entry count
// Assume ~1KB per node, so 100MB = ~100,000 nodes
let node_capacity = (config.cache_size_mb as u64) * 1000;
let edge_capacity = node_capacity * 5; // Edges are smaller, cache more
let cache = StorageCache::with_capacity(node_capacity, edge_capacity);
let metrics = if obs_config.enable_metrics {
Some(Arc::new(MemoryGraphMetrics::new()))
} else {
None
};
let observatory = if obs_config.enabled {
publisher.or_else(|| Some(Arc::new(NoOpPublisher)))
} else {
None
};
Ok(Self {
backend: Arc::new(backend),
sessions: Arc::new(RwLock::new(HashMap::new())),
observatory,
metrics,
cache,
})
}
/// Get metrics snapshot
pub fn get_metrics(&self) -> Option<crate::observatory::MetricsSnapshot> {
self.metrics.as_ref().map(|m| m.snapshot())
}
/// Publish an event to Observatory (non-blocking)
fn publish_event(&self, event: MemoryGraphEvent) {
if let Some(obs) = &self.observatory {
let obs = Arc::clone(obs);
tokio::spawn(async move {
if let Err(e) = obs.publish(event).await {
tracing::warn!("Failed to publish Observatory event: {}", e);
}
});
}
}
// ===== Session Management =====
/// Create a new conversation session asynchronously
///
/// Sessions are used to group related prompts and responses together.
/// Each session has a unique ID and can store custom metadata.
///
/// # Errors
///
/// Returns an error if the session cannot be persisted to storage.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let session = graph.create_session().await?;
/// println!("Created session: {}", session.id);
/// # Ok(())
/// # }
/// ```
pub async fn create_session(&self) -> Result<ConversationSession> {
let start = Instant::now();
let session = ConversationSession::new();
let node = Node::Session(session.clone());
self.backend.store_node(&node).await?;
// Cache the session in both session cache and node cache
self.sessions
.write()
.await
.insert(session.id, session.clone());
self.cache.insert_node(session.node_id, node).await;
// Record metrics
let latency_us = start.elapsed().as_micros() as u64;
if let Some(metrics) = &self.metrics {
metrics.record_node_created();
metrics.record_write_latency_us(latency_us);
}
// Publish event
self.publish_event(MemoryGraphEvent::NodeCreated {
node_id: session.node_id,
node_type: crate::types::NodeType::Session,
session_id: Some(session.id),
timestamp: Utc::now(),
metadata: session.metadata.clone(),
});
Ok(session)
}
/// Create a session with custom metadata asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # use std::collections::HashMap;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let mut metadata = HashMap::new();
/// metadata.insert("user_id".to_string(), "123".to_string());
/// let session = graph.create_session_with_metadata(metadata).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_session_with_metadata(
&self,
metadata: HashMap<String, String>,
) -> Result<ConversationSession> {
let session = ConversationSession::with_metadata(metadata);
let node = Node::Session(session.clone());
self.backend.store_node(&node).await?;
// Cache the session in both session cache and node cache
self.sessions
.write()
.await
.insert(session.id, session.clone());
self.cache.insert_node(session.node_id, node).await;
Ok(session)
}
/// Get a session by ID asynchronously
///
/// This will first check the in-memory cache, then fall back to storage.
///
/// # Errors
///
/// Returns an error if the session doesn't exist or storage retrieval fails.
pub async fn get_session(&self, session_id: SessionId) -> Result<ConversationSession> {
// Check cache first
{
let sessions = self.sessions.read().await;
if let Some(session) = sessions.get(&session_id) {
return Ok(session.clone());
}
}
// Fall back to storage
let session_nodes = self.backend.get_session_nodes(&session_id).await?;
for node in session_nodes {
if let Node::Session(session) = node {
if session.id == session_id {
// Update cache
self.sessions
.write()
.await
.insert(session_id, session.clone());
return Ok(session);
}
}
}
Err(Error::SessionNotFound(session_id.to_string()))
}
// ===== Prompt Operations =====
/// Add a prompt node to a session asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// let prompt_id = graph.add_prompt(
/// session.id,
/// "Explain async/await in Rust".to_string(),
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_prompt(
&self,
session_id: SessionId,
content: String,
metadata: Option<PromptMetadata>,
) -> Result<NodeId> {
let start = Instant::now();
// Verify session exists
self.get_session(session_id).await?;
let prompt = PromptNode {
id: NodeId::new(),
session_id,
content: content.clone(),
metadata: metadata.clone().unwrap_or_default(),
timestamp: chrono::Utc::now(),
template_id: None,
variables: HashMap::new(),
};
let prompt_id = prompt.id;
let node = Node::Prompt(prompt.clone());
self.backend.store_node(&node).await?;
// Populate cache for immediate read performance
self.cache.insert_node(prompt_id, node).await;
// Create PartOf edge - get session node to get its NodeId
let session_nodes = self.backend.get_session_nodes(&session_id).await?;
if let Some(session_node) = session_nodes.iter().find(|n| matches!(n, Node::Session(_))) {
let edge = Edge::new(prompt_id, session_node.id(), EdgeType::PartOf);
self.backend.store_edge(&edge).await?;
// Cache the edge
self.cache.insert_edge(edge.id, edge).await;
}
// Record metrics
let latency_us = start.elapsed().as_micros() as u64;
if let Some(metrics) = &self.metrics {
metrics.record_node_created();
metrics.record_prompt_submitted();
metrics.record_write_latency_us(latency_us);
}
// Publish event
self.publish_event(MemoryGraphEvent::PromptSubmitted {
prompt_id,
session_id,
content_length: content.len(),
model: metadata.unwrap_or_default().model,
timestamp: Utc::now(),
});
Ok(prompt_id)
}
/// Add multiple prompts concurrently (batch operation)
///
/// This method processes all prompts in parallel for maximum throughput.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// let prompts = vec![
/// (session.id, "First prompt".to_string()),
/// (session.id, "Second prompt".to_string()),
/// ];
/// let ids = graph.add_prompts_batch(prompts).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_prompts_batch(
&self,
prompts: Vec<(SessionId, String)>,
) -> Result<Vec<NodeId>> {
let futures: Vec<_> = prompts
.into_iter()
.map(|(session_id, content)| self.add_prompt(session_id, content, None))
.collect();
futures::future::try_join_all(futures).await
}
// ===== Response Operations =====
/// Add a response node linked to a prompt asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, TokenUsage};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// # let prompt_id = graph.add_prompt(session.id, "Hello".to_string(), None).await?;
/// let usage = TokenUsage::new(10, 50);
/// let response_id = graph.add_response(
/// prompt_id,
/// "Async operations are non-blocking!".to_string(),
/// usage,
/// None
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_response(
&self,
prompt_id: NodeId,
content: String,
token_usage: TokenUsage,
metadata: Option<ResponseMetadata>,
) -> Result<NodeId> {
let start = Instant::now();
let response = ResponseNode {
id: NodeId::new(),
prompt_id,
timestamp: chrono::Utc::now(),
content: content.clone(),
usage: token_usage,
metadata: metadata.unwrap_or_default(),
};
let response_id = response.id;
let node = Node::Response(response.clone());
self.backend.store_node(&node).await?;
// Populate cache for immediate read performance
self.cache.insert_node(response_id, node).await;
// Create RespondsTo edge
let edge = Edge::new(response_id, prompt_id, EdgeType::RespondsTo);
self.backend.store_edge(&edge).await?;
// Cache the edge
self.cache.insert_edge(edge.id, edge).await;
// Record metrics
let latency_us = start.elapsed().as_micros() as u64;
if let Some(metrics) = &self.metrics {
metrics.record_node_created();
metrics.record_response_generated();
metrics.record_write_latency_us(latency_us);
}
// Publish event
let response_latency_ms = latency_us / 1000;
self.publish_event(MemoryGraphEvent::ResponseGenerated {
response_id,
prompt_id,
content_length: content.len(),
tokens_used: token_usage,
latency_ms: response_latency_ms,
timestamp: Utc::now(),
});
Ok(response_id)
}
// ===== Agent Operations =====
/// Add an agent node asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, AgentNode};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let agent = AgentNode::new(
/// "CodeReviewer".to_string(),
/// "code-review".to_string(),
/// vec!["rust".to_string(), "python".to_string()]
/// );
/// let agent_id = graph.add_agent(agent).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_agent(&self, agent: AgentNode) -> Result<AgentId> {
let agent_id = agent.id;
let node_id = agent.node_id;
let node = Node::Agent(agent);
self.backend.store_node(&node).await?;
// Populate cache for immediate read performance
self.cache.insert_node(node_id, node).await;
Ok(agent_id)
}
/// Update an existing agent asynchronously
///
/// This invalidates the cache entry for the agent to ensure consistency.
pub async fn update_agent(&self, agent: AgentNode) -> Result<()> {
let node_id = agent.node_id;
self.backend.store_node(&Node::Agent(agent)).await?;
// Invalidate cache to ensure consistency
self.cache.invalidate_node(&node_id).await;
Ok(())
}
/// Assign an agent to handle a prompt asynchronously
///
/// Creates a HandledBy edge from the prompt to the agent.
pub async fn assign_agent_to_prompt(
&self,
prompt_id: NodeId,
agent_node_id: NodeId,
) -> Result<()> {
let edge = Edge::new(prompt_id, agent_node_id, EdgeType::HandledBy);
self.backend.store_edge(&edge).await
}
/// Transfer from one agent to another asynchronously
///
/// Creates a TransfersTo edge representing agent handoff.
pub async fn transfer_to_agent(
&self,
from_response: NodeId,
to_agent_node_id: NodeId,
) -> Result<()> {
let edge = Edge::new(from_response, to_agent_node_id, EdgeType::TransfersTo);
self.backend.store_edge(&edge).await
}
// ===== Template Operations =====
/// Create a new prompt template asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, PromptTemplate, VariableSpec};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let template = PromptTemplate::new(
/// "Greeting".to_string(),
/// "Hello {{name}}!".to_string(),
/// vec![]
/// );
/// let template_id = graph.create_template(template).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_template(&self, template: PromptTemplate) -> Result<TemplateId> {
let template_id = template.id;
let template_node_id = template.node_id;
let node = Node::Template(template);
self.backend.store_node(&node).await?;
// Populate cache for immediate read performance
self.cache.insert_node(template_node_id, node).await;
Ok(template_id)
}
/// Update an existing template asynchronously
///
/// This invalidates the cache entry for the template to ensure consistency.
pub async fn update_template(&self, template: PromptTemplate) -> Result<()> {
let template_node_id = template.node_id;
self.backend.store_node(&Node::Template(template)).await?;
// Invalidate cache to ensure consistency
self.cache.invalidate_node(&template_node_id).await;
Ok(())
}
/// Get a template by its template ID asynchronously
pub async fn get_template(&self, template_id: TemplateId) -> Result<PromptTemplate> {
// Search through all nodes to find the template
// This is a simplified implementation - in production, you'd want an index
let all_sessions = self.backend.get_session_nodes(&SessionId::new()).await?;
for node in all_sessions {
if let Node::Template(template) = node {
if template.id == template_id {
return Ok(template);
}
}
}
Err(Error::NodeNotFound(format!("Template {}", template_id)))
}
/// Get a template by its node ID asynchronously
pub async fn get_template_by_node_id(&self, node_id: NodeId) -> Result<PromptTemplate> {
if let Some(Node::Template(template)) = self.backend.get_node(&node_id).await? {
return Ok(template);
}
Err(Error::NodeNotFound(node_id.to_string()))
}
/// Create template from parent (inheritance) asynchronously
pub async fn create_template_from_parent(
&self,
template: PromptTemplate,
parent_node_id: NodeId,
) -> Result<TemplateId> {
let template_node_id = template.node_id;
let template_id = template.id;
// Store the new template
self.backend.store_node(&Node::Template(template)).await?;
// Create Inherits edge
let edge = Edge::new(template_node_id, parent_node_id, EdgeType::Inherits);
self.backend.store_edge(&edge).await?;
Ok(template_id)
}
/// Link a prompt to the template it was instantiated from
pub async fn link_prompt_to_template(
&self,
prompt_id: NodeId,
template_node_id: NodeId,
) -> Result<()> {
let edge = Edge::new(prompt_id, template_node_id, EdgeType::Instantiates);
self.backend.store_edge(&edge).await
}
// ===== Tool Invocation Operations =====
/// Add a tool invocation node asynchronously
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, ToolInvocation, NodeId};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let response_id = NodeId::new();
/// let tool = ToolInvocation::new(
/// response_id,
/// "calculator".to_string(),
/// serde_json::json!({"operation": "add", "a": 2, "b": 3})
/// );
/// let tool_id = graph.add_tool_invocation(tool).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_tool_invocation(&self, tool: ToolInvocation) -> Result<NodeId> {
let tool_id = tool.id;
let response_id = tool.response_id;
// Store the tool invocation node
let node = Node::ToolInvocation(tool);
self.backend.store_node(&node).await?;
// Populate cache for immediate read performance
self.cache.insert_node(tool_id, node).await;
// Create INVOKES edge from response to tool
let edge = Edge::new(response_id, tool_id, EdgeType::Invokes);
self.backend.store_edge(&edge).await?;
// Cache the edge
self.cache.insert_edge(edge.id, edge).await;
Ok(tool_id)
}
/// Update tool invocation with results asynchronously
///
/// This invalidates the cache entry for the tool to ensure consistency.
pub async fn update_tool_invocation(&self, tool: ToolInvocation) -> Result<()> {
let tool_id = tool.id;
self.backend.store_node(&Node::ToolInvocation(tool)).await?;
// Invalidate cache to ensure consistency
self.cache.invalidate_node(&tool_id).await;
Ok(())
}
// ===== Edge and Traversal Operations =====
/// Get a node by ID asynchronously (cache-aware)
///
/// This method first checks the cache for the node. If found in cache,
/// it returns immediately (< 1ms latency). Otherwise, it loads from
/// storage and populates the cache for future requests.
///
/// # Performance
///
/// - Cache hit: < 1ms latency
/// - Cache miss: 2-10ms latency (loads from storage)
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, NodeId};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let node_id = NodeId::new();
/// let node = graph.get_node(&node_id).await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_node(&self, id: &NodeId) -> Result<Option<Node>> {
let start = Instant::now();
// Check cache first
if let Some(node) = self.cache.get_node(id).await {
// Record cache hit in metrics
if let Some(metrics) = &self.metrics {
let latency_us = start.elapsed().as_micros() as u64;
metrics.record_read_latency_us(latency_us);
}
return Ok(Some(node));
}
// Cache miss - load from storage
if let Some(node) = self.backend.get_node(id).await? {
// Populate cache for future requests
self.cache.insert_node(*id, node.clone()).await;
// Record read latency
if let Some(metrics) = &self.metrics {
let latency_us = start.elapsed().as_micros() as u64;
metrics.record_read_latency_us(latency_us);
}
return Ok(Some(node));
}
Ok(None)
}
/// Get an edge by ID asynchronously (cache-aware)
///
/// This method first checks the cache for the edge. If found in cache,
/// it returns immediately. Otherwise, it loads from storage and populates
/// the cache for future requests.
///
/// # Performance
///
/// - Cache hit: < 1ms latency
/// - Cache miss: 2-10ms latency (loads from storage)
pub async fn get_edge(&self, id: &crate::types::EdgeId) -> Result<Option<Edge>> {
let start = Instant::now();
// Check cache first
if let Some(edge) = self.cache.get_edge(id).await {
// Record cache hit in metrics
if let Some(metrics) = &self.metrics {
let latency_us = start.elapsed().as_micros() as u64;
metrics.record_read_latency_us(latency_us);
}
return Ok(Some(edge));
}
// Cache miss - load from storage
if let Some(edge) = self.backend.get_edge(id).await? {
// Populate cache for future requests
self.cache.insert_edge(*id, edge.clone()).await;
// Record read latency
if let Some(metrics) = &self.metrics {
let latency_us = start.elapsed().as_micros() as u64;
metrics.record_read_latency_us(latency_us);
}
return Ok(Some(edge));
}
Ok(None)
}
/// Add a custom edge asynchronously
pub async fn add_edge(&self, from: NodeId, to: NodeId, edge_type: EdgeType) -> Result<()> {
let edge = Edge::new(from, to, edge_type);
self.backend.store_edge(&edge).await
}
/// Get all outgoing edges from a node asynchronously
pub async fn get_outgoing_edges(&self, node_id: &NodeId) -> Result<Vec<Edge>> {
self.backend.get_outgoing_edges(node_id).await
}
/// Get all incoming edges to a node asynchronously
pub async fn get_incoming_edges(&self, node_id: &NodeId) -> Result<Vec<Edge>> {
self.backend.get_incoming_edges(node_id).await
}
/// Get all nodes in a session asynchronously
pub async fn get_session_nodes(&self, session_id: &SessionId) -> Result<Vec<Node>> {
self.backend.get_session_nodes(session_id).await
}
// ===== Batch Operations =====
/// Store multiple nodes concurrently asynchronously
///
/// This method leverages async concurrency to store multiple nodes in parallel.
pub async fn store_nodes_batch(&self, nodes: Vec<Node>) -> Result<Vec<NodeId>> {
self.backend.store_nodes_batch(&nodes).await
}
/// Store multiple edges concurrently asynchronously
pub async fn store_edges_batch(&self, edges: Vec<Edge>) -> Result<()> {
self.backend.store_edges_batch(&edges).await?;
Ok(())
}
/// Add multiple responses concurrently (batch operation)
///
/// This method processes all responses in parallel for maximum throughput.
/// Each response is linked to its corresponding prompt.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, TokenUsage};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// # let prompt1 = graph.add_prompt(session.id, "Q1".to_string(), None).await?;
/// # let prompt2 = graph.add_prompt(session.id, "Q2".to_string(), None).await?;
/// let responses = vec![
/// (prompt1, "Answer 1".to_string(), TokenUsage::new(10, 50)),
/// (prompt2, "Answer 2".to_string(), TokenUsage::new(15, 60)),
/// ];
/// let ids = graph.add_responses_batch(responses).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_responses_batch(
&self,
responses: Vec<(NodeId, String, TokenUsage)>,
) -> Result<Vec<NodeId>> {
let futures: Vec<_> = responses
.into_iter()
.map(|(prompt_id, content, usage)| self.add_response(prompt_id, content, usage, None))
.collect();
futures::future::try_join_all(futures).await
}
/// Create multiple sessions concurrently (batch operation)
///
/// This method creates multiple sessions in parallel for maximum throughput.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let sessions = graph.create_sessions_batch(5).await?;
/// assert_eq!(sessions.len(), 5);
/// # Ok(())
/// # }
/// ```
pub async fn create_sessions_batch(&self, count: usize) -> Result<Vec<ConversationSession>> {
let futures: Vec<_> = (0..count).map(|_| self.create_session()).collect();
futures::future::try_join_all(futures).await
}
/// Retrieve multiple nodes concurrently (batch operation)
///
/// This method fetches all nodes in parallel for maximum throughput.
/// Returns nodes in the same order as the input IDs. Missing nodes are None.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # use llm_memory_graph::types::NodeId;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// # let id1 = graph.add_prompt(session.id, "Q1".to_string(), None).await?;
/// # let id2 = graph.add_prompt(session.id, "Q2".to_string(), None).await?;
/// let ids = vec![id1, id2];
/// let nodes = graph.get_nodes_batch(ids).await?;
/// assert_eq!(nodes.len(), 2);
/// # Ok(())
/// # }
/// ```
pub async fn get_nodes_batch(&self, ids: Vec<NodeId>) -> Result<Vec<Option<Node>>> {
let futures: Vec<_> = ids.iter().map(|id| self.get_node(id)).collect();
futures::future::try_join_all(futures).await
}
/// Delete multiple nodes concurrently (batch operation)
///
/// This method deletes all nodes in parallel for maximum throughput.
/// Note: This does not cascade delete related edges - you may want to
/// delete related edges separately.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::Config;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// # let id1 = graph.add_prompt(session.id, "Q1".to_string(), None).await?;
/// # let id2 = graph.add_prompt(session.id, "Q2".to_string(), None).await?;
/// let ids = vec![id1, id2];
/// graph.delete_nodes_batch(ids).await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_nodes_batch(&self, ids: Vec<NodeId>) -> Result<()> {
let futures: Vec<_> = ids.iter().map(|id| self.backend.delete_node(id)).collect();
futures::future::try_join_all(futures).await?;
Ok(())
}
/// Process a mixed batch of prompts and responses concurrently
///
/// This is an advanced operation that allows you to add prompts and their
/// responses in a single concurrent batch operation. This is useful for
/// bulk importing conversation data.
///
/// # Examples
///
/// ```no_run
/// # use llm_memory_graph::engine::AsyncMemoryGraph;
/// # use llm_memory_graph::{Config, TokenUsage};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// # let session = graph.create_session().await?;
/// let conversations = vec![
/// (
/// (session.id, "What is Rust?".to_string()),
/// Some(("Rust is a systems programming language".to_string(), TokenUsage::new(5, 30))),
/// ),
/// (
/// (session.id, "How does async work?".to_string()),
/// Some(("Async in Rust is zero-cost".to_string(), TokenUsage::new(6, 25))),
/// ),
/// ];
/// let results = graph.add_conversations_batch(conversations).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_conversations_batch(
&self,
conversations: Vec<ConversationBatchItem>,
) -> Result<Vec<(NodeId, Option<NodeId>)>> {
let futures: Vec<_> = conversations
.into_iter()
.map(|((session_id, prompt_content), response_data)| async move {
// Add prompt
let prompt_id = self.add_prompt(session_id, prompt_content, None).await?;
// Add response if provided
let response_id = if let Some((response_content, usage)) = response_data {
Some(
self.add_response(prompt_id, response_content, usage, None)
.await?,
)
} else {
None
};
Ok((prompt_id, response_id))
})
.collect();
futures::future::try_join_all(futures).await
}
// ===== Utility Operations =====
/// Flush any pending writes asynchronously
pub async fn flush(&self) -> Result<()> {
self.backend.flush().await
}
/// Get storage statistics asynchronously
pub async fn stats(&self) -> Result<crate::storage::StorageStats> {
self.backend.stats().await
}
// ===== Query Operations =====
/// Create a new async query builder for querying the graph
///
/// Returns an `AsyncQueryBuilder` that provides a fluent API for building
/// and executing queries with filtering, pagination, and streaming support.
///
/// # Examples
///
/// ```no_run
/// use llm_memory_graph::engine::AsyncMemoryGraph;
/// use llm_memory_graph::types::NodeType;
/// use llm_memory_graph::Config;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let graph = AsyncMemoryGraph::open(Config::default()).await?;
/// let session = graph.create_session().await?;
///
/// // Query with fluent API
/// let prompts = graph.query()
/// .session(session.id)
/// .node_type(NodeType::Prompt)
/// .limit(10)
/// .execute()
/// .await?;
///
/// println!("Found {} prompts", prompts.len());
/// Ok(())
/// }
/// ```
pub fn query(&self) -> crate::query::AsyncQueryBuilder {
crate::query::AsyncQueryBuilder::new(Arc::clone(&self.backend))
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[tokio::test]
async fn test_async_graph_creation() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 0);
}
#[tokio::test]
async fn test_async_session_management() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
// Create session
let session = graph.create_session().await.unwrap();
assert!(!session.id.to_string().is_empty());
// Retrieve session
let retrieved = graph.get_session(session.id).await.unwrap();
assert_eq!(retrieved.id, session.id);
}
#[tokio::test]
async fn test_async_prompt_and_response() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
let prompt_id = graph
.add_prompt(session.id, "Test prompt".to_string(), None)
.await
.unwrap();
let usage = TokenUsage::new(10, 20);
let response_id = graph
.add_response(prompt_id, "Test response".to_string(), usage, None)
.await
.unwrap();
// Verify edges
let edges = graph.get_outgoing_edges(&response_id).await.unwrap();
assert_eq!(edges.len(), 1);
assert_eq!(edges[0].edge_type, EdgeType::RespondsTo);
}
#[tokio::test]
async fn test_concurrent_prompts() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = Arc::new(AsyncMemoryGraph::open(config).await.unwrap());
let session = graph.create_session().await.unwrap();
// Create 100 prompts concurrently
let mut handles = vec![];
for i in 0..100 {
let graph_clone = Arc::clone(&graph);
let session_id = session.id;
let handle = tokio::spawn(async move {
graph_clone
.add_prompt(session_id, format!("Prompt {}", i), None)
.await
});
handles.push(handle);
}
// Wait for all to complete
for handle in handles {
handle.await.unwrap().unwrap();
}
// Verify all were stored
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 101); // 1 session + 100 prompts
}
#[tokio::test]
async fn test_batch_operations() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Batch add prompts
let prompts = (0..10)
.map(|i| (session.id, format!("Prompt {}", i)))
.collect();
let ids = graph.add_prompts_batch(prompts).await.unwrap();
assert_eq!(ids.len(), 10);
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 11); // 1 session + 10 prompts
}
#[tokio::test]
async fn test_agent_operations() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let agent = AgentNode::new(
"TestAgent".to_string(),
"tester".to_string(),
vec!["testing".to_string()],
);
let agent_id = graph.add_agent(agent).await.unwrap();
assert!(!agent_id.to_string().is_empty());
}
#[tokio::test]
async fn test_template_operations() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let template = PromptTemplate::new(
"Test Template".to_string(),
"Hello {{name}}!".to_string(),
vec![],
);
let template_id = graph.create_template(template.clone()).await.unwrap();
assert_eq!(template_id, template.id);
}
#[tokio::test]
async fn test_tool_invocation() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
let prompt_id = graph
.add_prompt(session.id, "Calculate 2+2".to_string(), None)
.await
.unwrap();
let usage = TokenUsage::new(5, 10);
let response_id = graph
.add_response(prompt_id, "Using calculator...".to_string(), usage, None)
.await
.unwrap();
let tool = ToolInvocation::new(
response_id,
"calculator".to_string(),
serde_json::json!({"op": "add", "a": 2, "b": 2}),
);
let _tool_id = graph.add_tool_invocation(tool).await.unwrap();
// Verify INVOKES edge was created
let edges = graph.get_outgoing_edges(&response_id).await.unwrap();
let invokes_edge = edges.iter().find(|e| e.edge_type == EdgeType::Invokes);
assert!(invokes_edge.is_some());
}
#[tokio::test]
async fn test_add_responses_batch() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create 5 prompts first
let mut prompt_ids = vec![];
for i in 0..5 {
let id = graph
.add_prompt(session.id, format!("Prompt {}", i), None)
.await
.unwrap();
prompt_ids.push(id);
}
// Batch add responses
let responses: Vec<_> = prompt_ids
.iter()
.enumerate()
.map(|(i, &prompt_id)| {
(
prompt_id,
format!("Response {}", i),
TokenUsage::new(10, 20),
)
})
.collect();
let response_ids = graph.add_responses_batch(responses).await.unwrap();
assert_eq!(response_ids.len(), 5);
// Verify all responses were created with proper edges
for (i, &response_id) in response_ids.iter().enumerate() {
let node = graph.get_node(&response_id).await.unwrap();
assert!(matches!(node, Some(Node::Response(_))));
// Check RESPONDS_TO edge
let edges = graph.get_outgoing_edges(&response_id).await.unwrap();
let responds_to = edges.iter().find(|e| e.edge_type == EdgeType::RespondsTo);
assert!(responds_to.is_some());
assert_eq!(responds_to.unwrap().to, prompt_ids[i]);
}
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 11); // 1 session + 5 prompts + 5 responses
}
#[tokio::test]
async fn test_create_sessions_batch() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
// Create 10 sessions concurrently
let sessions = graph.create_sessions_batch(10).await.unwrap();
assert_eq!(sessions.len(), 10);
// Verify all sessions have unique IDs
let mut ids = std::collections::HashSet::new();
for session in &sessions {
assert!(ids.insert(session.id));
}
// Verify all can be retrieved
for session in &sessions {
let retrieved = graph.get_session(session.id).await.unwrap();
assert_eq!(retrieved.id, session.id);
}
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 10);
assert_eq!(stats.session_count, 10);
}
#[tokio::test]
async fn test_get_nodes_batch() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create 20 prompts
let mut expected_ids = vec![];
for i in 0..20 {
let id = graph
.add_prompt(session.id, format!("Prompt {}", i), None)
.await
.unwrap();
expected_ids.push(id);
}
// Batch retrieve all nodes
let nodes = graph.get_nodes_batch(expected_ids.clone()).await.unwrap();
assert_eq!(nodes.len(), 20);
// Verify all nodes were retrieved
for (i, node_opt) in nodes.iter().enumerate() {
assert!(node_opt.is_some());
let node = node_opt.as_ref().unwrap();
assert_eq!(node.id(), expected_ids[i]);
if let Node::Prompt(prompt) = node {
assert_eq!(prompt.content, format!("Prompt {}", i));
} else {
panic!("Expected Prompt node");
}
}
}
#[tokio::test]
async fn test_get_nodes_batch_with_missing() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create 3 prompts
let mut ids = vec![];
for i in 0..3 {
let id = graph
.add_prompt(session.id, format!("Prompt {}", i), None)
.await
.unwrap();
ids.push(id);
}
// Add non-existent ID in the middle
let fake_id = NodeId::new();
ids.insert(1, fake_id);
// Batch retrieve should return None for missing node
let nodes = graph.get_nodes_batch(ids).await.unwrap();
assert_eq!(nodes.len(), 4);
assert!(nodes[0].is_some());
assert!(nodes[1].is_none()); // Fake ID
assert!(nodes[2].is_some());
assert!(nodes[3].is_some());
}
#[tokio::test]
async fn test_delete_nodes_batch() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create 15 prompts
let mut ids_to_delete = vec![];
for i in 0..15 {
let id = graph
.add_prompt(session.id, format!("Prompt {}", i), None)
.await
.unwrap();
ids_to_delete.push(id);
}
// Verify initial state
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 16); // 1 session + 15 prompts
// Batch delete all prompts
graph
.delete_nodes_batch(ids_to_delete.clone())
.await
.unwrap();
// Note: Current implementation may cache nodes, so deletion might not be immediate
// This test verifies the batch operation completes without errors
// For stricter deletion verification, use flush and clear cache
}
#[tokio::test]
async fn test_add_conversations_batch() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create mixed batch: some with responses, some without
let conversations = vec![
(
(session.id, "Prompt 1".to_string()),
Some(("Response 1".to_string(), TokenUsage::new(10, 20))),
),
(
(session.id, "Prompt 2".to_string()),
None, // No response
),
(
(session.id, "Prompt 3".to_string()),
Some(("Response 3".to_string(), TokenUsage::new(15, 25))),
),
(
(session.id, "Prompt 4".to_string()),
Some(("Response 4".to_string(), TokenUsage::new(12, 22))),
),
(
(session.id, "Prompt 5".to_string()),
None, // No response
),
];
let results = graph.add_conversations_batch(conversations).await.unwrap();
assert_eq!(results.len(), 5);
// Verify structure
assert!(results[0].1.is_some()); // Has response
assert!(results[1].1.is_none()); // No response
assert!(results[2].1.is_some()); // Has response
assert!(results[3].1.is_some()); // Has response
assert!(results[4].1.is_none()); // No response
// Verify all prompts exist
for (prompt_id, _) in &results {
let node = graph.get_node(prompt_id).await.unwrap();
assert!(matches!(node, Some(Node::Prompt(_))));
}
// Verify responses exist and have proper edges
for (prompt_id, response_id_opt) in &results {
if let Some(response_id) = response_id_opt {
let node = graph.get_node(response_id).await.unwrap();
assert!(matches!(node, Some(Node::Response(_))));
// Check RESPONDS_TO edge
let edges = graph.get_outgoing_edges(response_id).await.unwrap();
let responds_to = edges.iter().find(|e| e.edge_type == EdgeType::RespondsTo);
assert!(responds_to.is_some());
assert_eq!(responds_to.unwrap().to, *prompt_id);
}
}
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 9); // 1 session + 5 prompts + 3 responses
}
#[tokio::test]
async fn test_empty_batch_operations() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
// Test empty batches
let sessions = graph.create_sessions_batch(0).await.unwrap();
assert_eq!(sessions.len(), 0);
let nodes = graph.get_nodes_batch(vec![]).await.unwrap();
assert_eq!(nodes.len(), 0);
graph.delete_nodes_batch(vec![]).await.unwrap();
let prompts = graph.add_prompts_batch(vec![]).await.unwrap();
assert_eq!(prompts.len(), 0);
let responses = graph.add_responses_batch(vec![]).await.unwrap();
assert_eq!(responses.len(), 0);
let conversations = graph.add_conversations_batch(vec![]).await.unwrap();
assert_eq!(conversations.len(), 0);
}
#[tokio::test]
async fn test_large_batch_operations() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = AsyncMemoryGraph::open(config).await.unwrap();
let session = graph.create_session().await.unwrap();
// Create 100 prompts in batch
let prompts: Vec<_> = (0..100)
.map(|i| (session.id, format!("Prompt {}", i)))
.collect();
let prompt_ids = graph.add_prompts_batch(prompts).await.unwrap();
assert_eq!(prompt_ids.len(), 100);
// Create 100 responses in batch
let responses: Vec<_> = prompt_ids
.iter()
.enumerate()
.map(|(i, &id)| (id, format!("Response {}", i), TokenUsage::new(10, 20)))
.collect();
let response_ids = graph.add_responses_batch(responses).await.unwrap();
assert_eq!(response_ids.len(), 100);
// Batch retrieve all prompts
let nodes = graph.get_nodes_batch(prompt_ids.clone()).await.unwrap();
assert_eq!(nodes.len(), 100);
assert!(nodes.iter().all(|n| n.is_some()));
let stats = graph.stats().await.unwrap();
assert_eq!(stats.node_count, 201); // 1 session + 100 prompts + 100 responses
}
#[tokio::test]
async fn test_batch_concurrent_execution() {
let dir = tempdir().unwrap();
let config = Config::new(dir.path());
let graph = Arc::new(AsyncMemoryGraph::open(config).await.unwrap());
// Test that batch operations can be called concurrently from multiple tasks
let mut handles = vec![];
for i in 0..5 {
let graph_clone = Arc::clone(&graph);
let handle = tokio::spawn(async move {
// Each task creates its own session and prompts
let sessions = graph_clone.create_sessions_batch(2).await.unwrap();
let prompts = vec![
(sessions[0].id, format!("Task {} Prompt 1", i)),
(sessions[1].id, format!("Task {} Prompt 2", i)),
];
graph_clone.add_prompts_batch(prompts).await.unwrap();
});
handles.push(handle);
}
// Wait for all concurrent operations
for handle in handles {
handle.await.unwrap();
}
// Verify all operations succeeded
let stats = graph.stats().await.unwrap();
assert_eq!(stats.session_count, 10); // 5 tasks × 2 sessions each
assert_eq!(stats.node_count, 20); // 10 sessions + 10 prompts
}
}