llm-memory-graph 0.1.0

Graph-based context-tracking and prompt-lineage database for LLM systems
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
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
//! Node types for the memory graph

use super::{AgentId, NodeId, SessionId, TemplateId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Enum representing different node types in the graph
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeType {
    /// A prompt sent to an LLM
    Prompt,
    /// A response received from an LLM
    Response,
    /// A conversation session
    Session,
    /// A tool invocation by an LLM
    ToolInvocation,
    /// An autonomous agent
    Agent,
    /// A versioned prompt template
    Template,
}

/// Generic node wrapper that contains any node type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Node {
    /// Prompt node
    Prompt(PromptNode),
    /// Response node
    Response(ResponseNode),
    /// Session node
    Session(ConversationSession),
    /// Tool invocation node
    ToolInvocation(ToolInvocation),
    /// Agent node
    Agent(AgentNode),
    /// Template node
    Template(PromptTemplate),
}

impl Node {
    /// Get the node ID
    #[must_use]
    pub fn id(&self) -> NodeId {
        match self {
            Node::Prompt(p) => p.id,
            Node::Response(r) => r.id,
            Node::Session(s) => s.node_id,
            Node::ToolInvocation(t) => t.id,
            Node::Agent(a) => a.node_id,
            Node::Template(t) => t.node_id,
        }
    }

    /// Get the node type
    #[must_use]
    pub fn node_type(&self) -> NodeType {
        match self {
            Node::Prompt(_) => NodeType::Prompt,
            Node::Response(_) => NodeType::Response,
            Node::Session(_) => NodeType::Session,
            Node::ToolInvocation(_) => NodeType::ToolInvocation,
            Node::Agent(_) => NodeType::Agent,
            Node::Template(_) => NodeType::Template,
        }
    }
}

/// A conversation session that groups related prompts and responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationSession {
    /// Internal node ID for the session
    pub node_id: NodeId,
    /// Unique session identifier
    pub id: SessionId,
    /// When the session was created
    pub created_at: DateTime<Utc>,
    /// When the session was last updated
    pub updated_at: DateTime<Utc>,
    /// Custom metadata for the session
    pub metadata: HashMap<String, String>,
    /// Tags for categorization
    pub tags: Vec<String>,
}

impl ConversationSession {
    /// Create a new conversation session
    #[must_use]
    pub fn new() -> Self {
        let now = Utc::now();
        Self {
            node_id: NodeId::new(),
            id: SessionId::new(),
            created_at: now,
            updated_at: now,
            metadata: HashMap::new(),
            tags: Vec::new(),
        }
    }

    /// Create a session with custom metadata
    #[must_use]
    pub fn with_metadata(metadata: HashMap<String, String>) -> Self {
        let mut session = Self::new();
        session.metadata = metadata;
        session
    }

    /// Add a tag to the session
    pub fn add_tag(&mut self, tag: String) {
        if !self.tags.contains(&tag) {
            self.tags.push(tag);
        }
    }

    /// Update the last modified timestamp
    pub fn touch(&mut self) {
        self.updated_at = Utc::now();
    }
}

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

/// Metadata associated with a prompt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptMetadata {
    /// The LLM model name (e.g., "gpt-4", "claude-3-opus")
    pub model: String,
    /// Temperature parameter for generation
    pub temperature: f32,
    /// Maximum tokens to generate
    pub max_tokens: Option<usize>,
    /// List of tools/functions available to the model
    pub tools_available: Vec<String>,
    /// Additional custom metadata
    pub custom: HashMap<String, String>,
}

impl Default for PromptMetadata {
    fn default() -> Self {
        Self {
            model: String::from("unknown"),
            temperature: 0.7,
            max_tokens: None,
            tools_available: Vec::new(),
            custom: HashMap::new(),
        }
    }
}

/// A prompt node representing input to an LLM
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptNode {
    /// Unique node identifier
    pub id: NodeId,
    /// Session this prompt belongs to
    pub session_id: SessionId,
    /// When the prompt was created
    pub timestamp: DateTime<Utc>,
    /// Optional template this prompt was instantiated from
    pub template_id: Option<TemplateId>,
    /// The actual prompt content
    pub content: String,
    /// Variables used if instantiated from a template
    pub variables: HashMap<String, String>,
    /// Metadata about the prompt
    pub metadata: PromptMetadata,
}

impl PromptNode {
    /// Create a new prompt node
    #[must_use]
    pub fn new(session_id: SessionId, content: String) -> Self {
        Self {
            id: NodeId::new(),
            session_id,
            timestamp: Utc::now(),
            template_id: None,
            content,
            variables: HashMap::new(),
            metadata: PromptMetadata::default(),
        }
    }

    /// Create a prompt with custom metadata
    #[must_use]
    pub fn with_metadata(session_id: SessionId, content: String, metadata: PromptMetadata) -> Self {
        Self {
            id: NodeId::new(),
            session_id,
            timestamp: Utc::now(),
            template_id: None,
            content,
            variables: HashMap::new(),
            metadata,
        }
    }

    /// Create a prompt from a template
    #[must_use]
    pub fn from_template(
        session_id: SessionId,
        template_id: TemplateId,
        content: String,
        variables: HashMap<String, String>,
    ) -> Self {
        Self {
            id: NodeId::new(),
            session_id,
            timestamp: Utc::now(),
            template_id: Some(template_id),
            content,
            variables,
            metadata: PromptMetadata::default(),
        }
    }
}

/// Token usage statistics for a response
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TokenUsage {
    /// Number of tokens in the prompt
    pub prompt_tokens: u32,
    /// Number of tokens in the completion
    pub completion_tokens: u32,
    /// Total tokens used
    pub total_tokens: u32,
}

impl TokenUsage {
    /// Create new token usage stats
    #[must_use]
    pub const fn new(prompt_tokens: u32, completion_tokens: u32) -> Self {
        Self {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens + completion_tokens,
        }
    }
}

/// Metadata associated with a response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseMetadata {
    /// The model that generated the response
    pub model: String,
    /// Reason why generation stopped
    pub finish_reason: String,
    /// Latency in milliseconds
    pub latency_ms: u64,
    /// Additional custom metadata
    pub custom: HashMap<String, String>,
}

impl Default for ResponseMetadata {
    fn default() -> Self {
        Self {
            model: String::from("unknown"),
            finish_reason: String::from("stop"),
            latency_ms: 0,
            custom: HashMap::new(),
        }
    }
}

/// A response node representing output from an LLM
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseNode {
    /// Unique node identifier
    pub id: NodeId,
    /// The prompt this response is replying to
    pub prompt_id: NodeId,
    /// When the response was created
    pub timestamp: DateTime<Utc>,
    /// The response content
    pub content: String,
    /// Token usage statistics
    pub usage: TokenUsage,
    /// Metadata about the response
    pub metadata: ResponseMetadata,
}

impl ResponseNode {
    /// Create a new response node
    #[must_use]
    pub fn new(prompt_id: NodeId, content: String, usage: TokenUsage) -> Self {
        Self {
            id: NodeId::new(),
            prompt_id,
            timestamp: Utc::now(),
            content,
            usage,
            metadata: ResponseMetadata::default(),
        }
    }

    /// Create a response with custom metadata
    #[must_use]
    pub fn with_metadata(
        prompt_id: NodeId,
        content: String,
        usage: TokenUsage,
        metadata: ResponseMetadata,
    ) -> Self {
        Self {
            id: NodeId::new(),
            prompt_id,
            timestamp: Utc::now(),
            content,
            usage,
            metadata,
        }
    }
}

/// A tool invocation node representing a function call by an LLM
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolInvocation {
    /// Unique node identifier
    pub id: NodeId,
    /// Response that triggered this tool call
    pub response_id: NodeId,
    /// Name of the tool/function
    pub tool_name: String,
    /// JSON parameters passed to the tool
    pub parameters: serde_json::Value,
    /// Tool execution result (if completed)
    pub result: Option<serde_json::Value>,
    /// Error message (if failed)
    pub error: Option<String>,
    /// Execution duration in milliseconds
    pub duration_ms: u64,
    /// When the tool was invoked
    pub timestamp: DateTime<Utc>,
    /// Success/failure status
    pub success: bool,
    /// Retry count (for failed invocations)
    pub retry_count: u32,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

impl ToolInvocation {
    /// Create a new pending tool invocation
    #[must_use]
    pub fn new(response_id: NodeId, tool_name: String, parameters: serde_json::Value) -> Self {
        Self {
            id: NodeId::new(),
            response_id,
            tool_name,
            parameters,
            result: None,
            error: None,
            duration_ms: 0,
            timestamp: Utc::now(),
            success: false,
            retry_count: 0,
            metadata: HashMap::new(),
        }
    }

    /// Mark tool invocation as successful
    pub fn mark_success(&mut self, result: serde_json::Value, duration_ms: u64) {
        self.success = true;
        self.result = Some(result);
        self.error = None;
        self.duration_ms = duration_ms;
    }

    /// Mark tool invocation as failed
    pub fn mark_failed(&mut self, error: String, duration_ms: u64) {
        self.success = false;
        self.error = Some(error);
        self.result = None;
        self.duration_ms = duration_ms;
    }

    /// Record retry attempt
    pub fn record_retry(&mut self) {
        self.retry_count += 1;
        self.timestamp = Utc::now();
    }

    /// Check if tool invocation is pending (not completed)
    #[must_use]
    pub fn is_pending(&self) -> bool {
        self.result.is_none() && self.error.is_none()
    }

    /// Check if tool invocation succeeded
    #[must_use]
    pub const fn is_success(&self) -> bool {
        self.success
    }

    /// Check if tool invocation failed
    #[must_use]
    pub fn is_failed(&self) -> bool {
        self.error.is_some()
    }

    /// Get the tool execution status as a string
    #[must_use]
    pub fn status(&self) -> &str {
        if self.is_pending() {
            "pending"
        } else if self.success {
            "success"
        } else {
            "failed"
        }
    }

    /// Add metadata to the tool invocation
    pub fn add_metadata(&mut self, key: String, value: String) {
        self.metadata.insert(key, value);
    }
}

/// Agent status enum
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum AgentStatus {
    /// Agent is active and ready to process tasks
    Active,
    /// Agent is idle, waiting for work
    #[default]
    Idle,
    /// Agent is currently busy processing a task
    Busy,
    /// Agent is paused and not accepting new tasks
    Paused,
    /// Agent has been terminated
    Terminated,
}

impl AgentStatus {
    /// Check if agent can accept new tasks
    #[must_use]
    pub const fn can_accept_tasks(&self) -> bool {
        matches!(self, Self::Active | Self::Idle)
    }

    /// Check if agent is processing a task
    #[must_use]
    pub const fn is_busy(&self) -> bool {
        matches!(self, Self::Busy)
    }

    /// Check if agent is operational
    #[must_use]
    pub const fn is_operational(&self) -> bool {
        !matches!(self, Self::Terminated)
    }
}

/// Agent configuration parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    /// Temperature parameter for generation
    pub temperature: f32,
    /// Maximum tokens to generate
    pub max_tokens: usize,
    /// Timeout in seconds for agent operations
    pub timeout_seconds: u64,
    /// Maximum number of retries for failed operations
    pub max_retries: u32,
    /// List of tools/functions available to the agent
    pub tools_enabled: Vec<String>,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            temperature: 0.7,
            max_tokens: 2000,
            timeout_seconds: 300,
            max_retries: 3,
            tools_enabled: Vec::new(),
        }
    }
}

/// Agent performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentMetrics {
    /// Total number of prompts processed
    pub total_prompts: u64,
    /// Number of successfully completed tasks
    pub successful_tasks: u64,
    /// Number of failed tasks
    pub failed_tasks: u64,
    /// Average latency in milliseconds
    pub average_latency_ms: f64,
    /// Total tokens used across all operations
    pub total_tokens_used: u64,
}

impl Default for AgentMetrics {
    fn default() -> Self {
        Self {
            total_prompts: 0,
            successful_tasks: 0,
            failed_tasks: 0,
            average_latency_ms: 0.0,
            total_tokens_used: 0,
        }
    }
}

impl AgentMetrics {
    /// Calculate success rate as a percentage
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        let total = self.successful_tasks + self.failed_tasks;
        if total == 0 {
            0.0
        } else {
            (self.successful_tasks as f64 / total as f64) * 100.0
        }
    }

    /// Update average latency with new measurement
    pub fn update_average_latency(&mut self, new_latency_ms: u64, current_count: u64) {
        if current_count == 0 {
            self.average_latency_ms = new_latency_ms as f64;
        } else {
            self.average_latency_ms = (self.average_latency_ms * current_count as f64
                + new_latency_ms as f64)
                / (current_count + 1) as f64;
        }
    }
}

/// An agent node representing an autonomous AI agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentNode {
    /// Unique agent identifier
    pub id: AgentId,
    /// Internal node ID for graph storage
    pub node_id: NodeId,
    /// Human-readable agent name
    pub name: String,
    /// Agent role/specialization (e.g., "researcher", "coder", "reviewer")
    pub role: String,
    /// List of agent capabilities
    pub capabilities: Vec<String>,
    /// Model used by this agent
    pub model: String,
    /// When the agent was created
    pub created_at: DateTime<Utc>,
    /// Last activity timestamp
    pub last_active: DateTime<Utc>,
    /// Agent status (active, idle, busy, paused, terminated)
    pub status: AgentStatus,
    /// Configuration parameters
    pub config: AgentConfig,
    /// Performance metrics
    pub metrics: AgentMetrics,
    /// Tags for categorization
    pub tags: Vec<String>,
}

impl AgentNode {
    /// Create a new agent
    #[must_use]
    pub fn new(name: String, role: String, capabilities: Vec<String>) -> Self {
        let now = Utc::now();
        Self {
            id: AgentId::new(),
            node_id: NodeId::new(),
            name,
            role,
            capabilities,
            model: String::from("gpt-4"),
            created_at: now,
            last_active: now,
            status: AgentStatus::Idle,
            config: AgentConfig::default(),
            metrics: AgentMetrics::default(),
            tags: Vec::new(),
        }
    }

    /// Create an agent with custom configuration
    #[must_use]
    pub fn with_config(
        name: String,
        role: String,
        capabilities: Vec<String>,
        config: AgentConfig,
    ) -> Self {
        let mut agent = Self::new(name, role, capabilities);
        agent.config = config;
        agent
    }

    /// Create an agent with a specific model
    #[must_use]
    pub fn with_model(
        name: String,
        role: String,
        capabilities: Vec<String>,
        model: String,
    ) -> Self {
        let mut agent = Self::new(name, role, capabilities);
        agent.model = model;
        agent
    }

    /// Update agent status
    pub fn set_status(&mut self, status: AgentStatus) {
        self.status = status;
        self.last_active = Utc::now();
    }

    /// Record agent activity
    pub fn record_activity(&mut self) {
        self.last_active = Utc::now();
    }

    /// Update performance metrics
    pub fn update_metrics(&mut self, success: bool, latency_ms: u64, tokens: u64) {
        let current_count = self.metrics.total_prompts;
        self.metrics.total_prompts += 1;
        if success {
            self.metrics.successful_tasks += 1;
        } else {
            self.metrics.failed_tasks += 1;
        }
        self.metrics
            .update_average_latency(latency_ms, current_count);
        self.metrics.total_tokens_used += tokens;
        self.record_activity();
    }

    /// Add a capability to the agent
    pub fn add_capability(&mut self, capability: String) {
        if !self.capabilities.contains(&capability) {
            self.capabilities.push(capability);
        }
    }

    /// Remove a capability from the agent
    pub fn remove_capability(&mut self, capability: &str) {
        self.capabilities.retain(|c| c != capability);
    }

    /// Check if agent has a specific capability
    #[must_use]
    pub fn has_capability(&self, capability: &str) -> bool {
        self.capabilities.contains(&String::from(capability))
    }

    /// Add a tag to the agent
    pub fn add_tag(&mut self, tag: String) {
        if !self.tags.contains(&tag) {
            self.tags.push(tag);
        }
    }

    /// Get agent uptime in seconds
    #[must_use]
    pub fn uptime_seconds(&self) -> i64 {
        (Utc::now() - self.created_at).num_seconds()
    }

    /// Get time since last activity in seconds
    #[must_use]
    pub fn idle_time_seconds(&self) -> i64 {
        (Utc::now() - self.last_active).num_seconds()
    }

    /// Check if agent is healthy (active and operational)
    #[must_use]
    pub fn is_healthy(&self) -> bool {
        self.status.is_operational() && self.idle_time_seconds() < 3600 // Not idle for more than 1 hour
    }
}

/// Semantic version for template versioning
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Version {
    /// Major version (breaking changes)
    pub major: u16,
    /// Minor version (new features, backwards compatible)
    pub minor: u16,
    /// Patch version (bug fixes)
    pub patch: u16,
}

impl Version {
    /// Create a new version
    #[must_use]
    pub const fn new(major: u16, minor: u16, patch: u16) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }

    /// Bump major version (resets minor and patch to 0)
    pub fn bump_major(&mut self) {
        self.major += 1;
        self.minor = 0;
        self.patch = 0;
    }

    /// Bump minor version (resets patch to 0)
    pub fn bump_minor(&mut self) {
        self.minor += 1;
        self.patch = 0;
    }

    /// Bump patch version
    pub fn bump_patch(&mut self) {
        self.patch += 1;
    }
}

impl Default for Version {
    fn default() -> Self {
        Self::new(1, 0, 0)
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

impl std::str::FromStr for Version {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split('.').collect();
        if parts.len() != 3 {
            return Err(format!("Invalid version format: {}", s));
        }

        let major = parts[0]
            .parse::<u16>()
            .map_err(|_| format!("Invalid major version: {}", parts[0]))?;
        let minor = parts[1]
            .parse::<u16>()
            .map_err(|_| format!("Invalid minor version: {}", parts[1]))?;
        let patch = parts[2]
            .parse::<u16>()
            .map_err(|_| format!("Invalid patch version: {}", parts[2]))?;

        Ok(Self::new(major, minor, patch))
    }
}

/// Version bump level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersionLevel {
    /// Major version bump (breaking changes)
    Major,
    /// Minor version bump (new features)
    Minor,
    /// Patch version bump (bug fixes)
    Patch,
}

/// Variable specification for template variables
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableSpec {
    /// Variable name (e.g., "user_query")
    pub name: String,
    /// Type hint (e.g., "string", "number", "array")
    pub type_hint: String,
    /// Whether the variable is required
    pub required: bool,
    /// Default value if not provided
    pub default: Option<String>,
    /// Validation regex pattern
    pub validation_pattern: Option<String>,
    /// Human-readable description
    pub description: String,
}

impl VariableSpec {
    /// Create a new variable specification
    #[must_use]
    pub fn new(name: String, type_hint: String, required: bool, description: String) -> Self {
        Self {
            name,
            type_hint,
            required,
            default: None,
            validation_pattern: None,
            description,
        }
    }

    /// Create a variable spec with a default value
    #[must_use]
    pub fn with_default(mut self, default: String) -> Self {
        self.default = Some(default);
        self
    }

    /// Create a variable spec with validation pattern
    #[must_use]
    pub fn with_validation(mut self, pattern: String) -> Self {
        self.validation_pattern = Some(pattern);
        self
    }

    /// Validate a value against this spec
    pub fn validate(&self, value: &Option<String>) -> Result<(), String> {
        // Check if required
        if self.required && value.is_none() {
            return Err(format!("Required variable '{}' is missing", self.name));
        }

        // If value is present, validate pattern
        if let Some(val) = value {
            if let Some(ref pattern) = self.validation_pattern {
                let re = regex::Regex::new(pattern)
                    .map_err(|e| format!("Invalid regex pattern: {}", e))?;
                if !re.is_match(val) {
                    return Err(format!(
                        "Variable '{}' value '{}' does not match pattern '{}'",
                        self.name, val, pattern
                    ));
                }
            }
        }

        Ok(())
    }
}

/// A prompt template node for reusable prompts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptTemplate {
    /// Unique template identifier
    pub id: TemplateId,
    /// Internal node ID for graph storage
    pub node_id: NodeId,
    /// Semantic version
    pub version: Version,
    /// Human-readable template name
    pub name: String,
    /// Template description
    pub description: String,
    /// Template content with {{variables}}
    pub template: String,
    /// Variable specifications
    pub variables: Vec<VariableSpec>,
    /// Parent template ID (for inheritance)
    pub parent_id: Option<TemplateId>,
    /// When the template was created
    pub created_at: DateTime<Utc>,
    /// Last modification timestamp
    pub updated_at: DateTime<Utc>,
    /// Template author
    pub author: String,
    /// Usage count
    pub usage_count: u64,
    /// Tags for categorization
    pub tags: Vec<String>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

impl PromptTemplate {
    /// Create a new template
    #[must_use]
    pub fn new(name: String, template: String, variables: Vec<VariableSpec>) -> Self {
        let now = Utc::now();
        Self {
            id: TemplateId::new(),
            node_id: NodeId::new(),
            version: Version::default(),
            name,
            description: String::new(),
            template,
            variables,
            parent_id: None,
            created_at: now,
            updated_at: now,
            author: String::from("unknown"),
            usage_count: 0,
            tags: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Create template from parent (inheritance)
    #[must_use]
    pub fn from_parent(
        parent_id: TemplateId,
        name: String,
        template: String,
        variables: Vec<VariableSpec>,
    ) -> Self {
        let mut tmpl = Self::new(name, template, variables);
        tmpl.parent_id = Some(parent_id);
        tmpl
    }

    /// Set template description
    #[must_use]
    pub fn with_description(mut self, description: String) -> Self {
        self.description = description;
        self
    }

    /// Set template author
    #[must_use]
    pub fn with_author(mut self, author: String) -> Self {
        self.author = author;
        self
    }

    /// Instantiate template with variable values
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Required variables are missing
    /// - Variable validation fails
    pub fn instantiate(&self, values: &HashMap<String, String>) -> Result<String, String> {
        // Validate all variables
        self.validate(values)?;

        // Build final variable map with defaults
        let mut final_values = HashMap::new();
        for var in &self.variables {
            if let Some(value) = values.get(&var.name) {
                final_values.insert(var.name.clone(), value.clone());
            } else if let Some(ref default) = var.default {
                final_values.insert(var.name.clone(), default.clone());
            }
        }

        // Replace variables in template
        let mut result = self.template.clone();
        for (key, value) in final_values {
            let placeholder = format!("{{{{{}}}}}", key);
            result = result.replace(&placeholder, &value);
        }

        Ok(result)
    }

    /// Validate variable values
    ///
    /// # Errors
    ///
    /// Returns an error if validation fails
    pub fn validate(&self, values: &HashMap<String, String>) -> Result<(), String> {
        for var in &self.variables {
            let value = values.get(&var.name).cloned();
            var.validate(&value)?;
        }
        Ok(())
    }

    /// Increment usage counter
    pub fn record_usage(&mut self) {
        self.usage_count += 1;
        self.updated_at = Utc::now();
    }

    /// Bump version
    pub fn bump_version(&mut self, level: VersionLevel) {
        match level {
            VersionLevel::Major => self.version.bump_major(),
            VersionLevel::Minor => self.version.bump_minor(),
            VersionLevel::Patch => self.version.bump_patch(),
        }
        self.updated_at = Utc::now();
    }

    /// Add a tag to the template
    pub fn add_tag(&mut self, tag: String) {
        if !self.tags.contains(&tag) {
            self.tags.push(tag);
        }
    }

    /// Add metadata to the template
    pub fn add_metadata(&mut self, key: String, value: String) {
        self.metadata.insert(key, value);
    }
}

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

    #[test]
    fn test_session_creation() {
        let session = ConversationSession::new();
        assert!(!session.tags.is_empty() || session.tags.is_empty()); // Always true, just checking API
        assert!(session.metadata.is_empty());
    }

    #[test]
    fn test_session_tags() {
        let mut session = ConversationSession::new();
        session.add_tag("test".to_string());
        session.add_tag("test".to_string()); // Should not duplicate
        assert_eq!(session.tags.len(), 1);
    }

    #[test]
    fn test_prompt_creation() {
        let session_id = SessionId::new();
        let prompt = PromptNode::new(session_id, "Test prompt".to_string());
        assert_eq!(prompt.session_id, session_id);
        assert_eq!(prompt.content, "Test prompt");
    }

    #[test]
    fn test_response_creation() {
        let prompt_id = NodeId::new();
        let usage = TokenUsage::new(10, 20);
        let response = ResponseNode::new(prompt_id, "Test response".to_string(), usage);
        assert_eq!(response.prompt_id, prompt_id);
        assert_eq!(response.usage.total_tokens, 30);
    }

    #[test]
    fn test_token_usage() {
        let usage = TokenUsage::new(100, 50);
        assert_eq!(usage.prompt_tokens, 100);
        assert_eq!(usage.completion_tokens, 50);
        assert_eq!(usage.total_tokens, 150);
    }

    #[test]
    fn test_node_type() {
        let session = ConversationSession::new();
        let node = Node::Session(session);
        assert_eq!(node.node_type(), NodeType::Session);
    }

    #[test]
    fn test_tool_invocation_creation() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"operation": "add", "a": 2, "b": 3});
        let tool = ToolInvocation::new(response_id, "calculator".to_string(), params.clone());

        assert_eq!(tool.response_id, response_id);
        assert_eq!(tool.tool_name, "calculator");
        assert_eq!(tool.parameters, params);
        assert!(tool.is_pending());
        assert!(!tool.is_success());
        assert!(!tool.is_failed());
        assert_eq!(tool.retry_count, 0);
    }

    #[test]
    fn test_tool_invocation_mark_success() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"operation": "add", "a": 2, "b": 3});
        let mut tool = ToolInvocation::new(response_id, "calculator".to_string(), params);

        let result = serde_json::json!({"result": 5});
        tool.mark_success(result.clone(), 150);

        assert!(tool.is_success());
        assert!(!tool.is_pending());
        assert!(!tool.is_failed());
        assert_eq!(tool.result, Some(result));
        assert_eq!(tool.duration_ms, 150);
        assert_eq!(tool.error, None);
        assert_eq!(tool.status(), "success");
    }

    #[test]
    fn test_tool_invocation_mark_failed() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"operation": "divide", "a": 10, "b": 0});
        let mut tool = ToolInvocation::new(response_id, "calculator".to_string(), params);

        tool.mark_failed("Division by zero".to_string(), 50);

        assert!(!tool.is_success());
        assert!(!tool.is_pending());
        assert!(tool.is_failed());
        assert_eq!(tool.error, Some("Division by zero".to_string()));
        assert_eq!(tool.duration_ms, 50);
        assert_eq!(tool.result, None);
        assert_eq!(tool.status(), "failed");
    }

    #[test]
    fn test_tool_invocation_retry() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"url": "https://api.example.com"});
        let mut tool = ToolInvocation::new(response_id, "http_request".to_string(), params);

        assert_eq!(tool.retry_count, 0);

        tool.record_retry();
        assert_eq!(tool.retry_count, 1);

        tool.record_retry();
        assert_eq!(tool.retry_count, 2);
    }

    #[test]
    fn test_tool_invocation_metadata() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"query": "test"});
        let mut tool = ToolInvocation::new(response_id, "search".to_string(), params);

        tool.add_metadata("provider".to_string(), "google".to_string());
        tool.add_metadata("cache_hit".to_string(), "true".to_string());

        assert_eq!(tool.metadata.len(), 2);
        assert_eq!(tool.metadata.get("provider"), Some(&"google".to_string()));
        assert_eq!(tool.metadata.get("cache_hit"), Some(&"true".to_string()));
    }

    #[test]
    fn test_tool_invocation_node_type() {
        let response_id = NodeId::new();
        let params = serde_json::json!({"test": "value"});
        let tool = ToolInvocation::new(response_id, "test_tool".to_string(), params);
        let node = Node::ToolInvocation(tool);

        assert_eq!(node.node_type(), NodeType::ToolInvocation);
    }

    // AgentNode tests

    #[test]
    fn test_agent_creation() {
        let agent = AgentNode::new(
            "Researcher".to_string(),
            "research".to_string(),
            vec!["web_search".to_string(), "summarize".to_string()],
        );

        assert_eq!(agent.name, "Researcher");
        assert_eq!(agent.role, "research");
        assert_eq!(agent.capabilities.len(), 2);
        assert_eq!(agent.status, AgentStatus::Idle);
        assert_eq!(agent.model, "gpt-4");
        assert_eq!(agent.metrics.total_prompts, 0);
    }

    #[test]
    fn test_agent_with_config() {
        let config = AgentConfig {
            temperature: 0.5,
            max_tokens: 1000,
            timeout_seconds: 60,
            max_retries: 5,
            tools_enabled: vec!["calculator".to_string()],
        };

        let agent = AgentNode::with_config(
            "Calculator".to_string(),
            "math".to_string(),
            vec!["calculate".to_string()],
            config.clone(),
        );

        assert_eq!(agent.config.temperature, 0.5);
        assert_eq!(agent.config.max_tokens, 1000);
        assert_eq!(agent.config.timeout_seconds, 60);
        assert_eq!(agent.config.max_retries, 5);
        assert_eq!(agent.config.tools_enabled.len(), 1);
    }

    #[test]
    fn test_agent_with_model() {
        let agent = AgentNode::with_model(
            "Claude Agent".to_string(),
            "assistant".to_string(),
            vec![],
            "claude-3-opus".to_string(),
        );

        assert_eq!(agent.model, "claude-3-opus");
    }

    #[test]
    fn test_agent_status_transitions() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        assert_eq!(agent.status, AgentStatus::Idle);
        assert!(agent.status.can_accept_tasks());

        agent.set_status(AgentStatus::Busy);
        assert_eq!(agent.status, AgentStatus::Busy);
        assert!(agent.status.is_busy());
        assert!(!agent.status.can_accept_tasks());

        agent.set_status(AgentStatus::Active);
        assert_eq!(agent.status, AgentStatus::Active);
        assert!(agent.status.can_accept_tasks());

        agent.set_status(AgentStatus::Paused);
        assert!(!agent.status.can_accept_tasks());

        agent.set_status(AgentStatus::Terminated);
        assert!(!agent.status.is_operational());
    }

    #[test]
    fn test_agent_metrics_update() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        assert_eq!(agent.metrics.total_prompts, 0);
        assert_eq!(agent.metrics.success_rate(), 0.0);

        agent.update_metrics(true, 100, 50);
        assert_eq!(agent.metrics.total_prompts, 1);
        assert_eq!(agent.metrics.successful_tasks, 1);
        assert_eq!(agent.metrics.failed_tasks, 0);
        assert_eq!(agent.metrics.average_latency_ms, 100.0);
        assert_eq!(agent.metrics.total_tokens_used, 50);
        assert_eq!(agent.metrics.success_rate(), 100.0);

        agent.update_metrics(false, 200, 30);
        assert_eq!(agent.metrics.total_prompts, 2);
        assert_eq!(agent.metrics.successful_tasks, 1);
        assert_eq!(agent.metrics.failed_tasks, 1);
        assert_eq!(agent.metrics.total_tokens_used, 80);
        assert_eq!(agent.metrics.success_rate(), 50.0);

        // Average latency should be (100 + 200) / 2 = 150
        assert_eq!(agent.metrics.average_latency_ms, 150.0);
    }

    #[test]
    fn test_agent_capabilities() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        assert!(!agent.has_capability("web_search"));

        agent.add_capability("web_search".to_string());
        assert!(agent.has_capability("web_search"));
        assert_eq!(agent.capabilities.len(), 1);

        agent.add_capability("web_search".to_string()); // Duplicate
        assert_eq!(agent.capabilities.len(), 1); // Should not add duplicate

        agent.add_capability("summarize".to_string());
        assert_eq!(agent.capabilities.len(), 2);

        agent.remove_capability("web_search");
        assert!(!agent.has_capability("web_search"));
        assert_eq!(agent.capabilities.len(), 1);
    }

    #[test]
    fn test_agent_tags() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        assert_eq!(agent.tags.len(), 0);

        agent.add_tag("production".to_string());
        assert_eq!(agent.tags.len(), 1);

        agent.add_tag("production".to_string()); // Duplicate
        assert_eq!(agent.tags.len(), 1); // Should not add duplicate

        agent.add_tag("critical".to_string());
        assert_eq!(agent.tags.len(), 2);
    }

    #[test]
    fn test_agent_activity_tracking() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        let initial_active = agent.last_active;
        std::thread::sleep(std::time::Duration::from_millis(10));

        agent.record_activity();
        assert!(agent.last_active > initial_active);
        assert_eq!(agent.idle_time_seconds(), 0);
    }

    #[test]
    fn test_agent_uptime() {
        let agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        let uptime = agent.uptime_seconds();
        assert!(uptime >= 0);
        assert!(uptime < 5); // Should be very small since just created
    }

    #[test]
    fn test_agent_health_check() {
        let mut agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);

        agent.set_status(AgentStatus::Active);
        assert!(agent.is_healthy());

        agent.set_status(AgentStatus::Terminated);
        assert!(!agent.is_healthy());
    }

    #[test]
    fn test_agent_node_type() {
        let agent = AgentNode::new("Test".to_string(), "test".to_string(), vec![]);
        let node = Node::Agent(agent);

        assert_eq!(node.node_type(), NodeType::Agent);
    }

    #[test]
    fn test_agent_status_helpers() {
        assert!(AgentStatus::Active.can_accept_tasks());
        assert!(AgentStatus::Idle.can_accept_tasks());
        assert!(!AgentStatus::Busy.can_accept_tasks());
        assert!(!AgentStatus::Paused.can_accept_tasks());
        assert!(!AgentStatus::Terminated.can_accept_tasks());

        assert!(!AgentStatus::Active.is_busy());
        assert!(AgentStatus::Busy.is_busy());

        assert!(AgentStatus::Active.is_operational());
        assert!(AgentStatus::Idle.is_operational());
        assert!(AgentStatus::Busy.is_operational());
        assert!(AgentStatus::Paused.is_operational());
        assert!(!AgentStatus::Terminated.is_operational());
    }

    #[test]
    fn test_agent_metrics_success_rate() {
        let mut metrics = AgentMetrics::default();

        assert_eq!(metrics.success_rate(), 0.0);

        metrics.successful_tasks = 5;
        metrics.failed_tasks = 5;
        assert_eq!(metrics.success_rate(), 50.0);

        metrics.successful_tasks = 9;
        metrics.failed_tasks = 1;
        assert_eq!(metrics.success_rate(), 90.0);

        metrics.successful_tasks = 0;
        metrics.failed_tasks = 10;
        assert_eq!(metrics.success_rate(), 0.0);
    }

    #[test]
    fn test_agent_config_defaults() {
        let config = AgentConfig::default();

        assert_eq!(config.temperature, 0.7);
        assert_eq!(config.max_tokens, 2000);
        assert_eq!(config.timeout_seconds, 300);
        assert_eq!(config.max_retries, 3);
        assert_eq!(config.tools_enabled.len(), 0);
    }

    // ===== Template Tests =====

    #[test]
    fn test_version_creation() {
        let version = Version::new(1, 2, 3);
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 2);
        assert_eq!(version.patch, 3);
    }

    #[test]
    fn test_version_display() {
        let version = Version::new(2, 5, 10);
        assert_eq!(version.to_string(), "2.5.10");
    }

    #[test]
    fn test_version_from_str() {
        let version: Version = "1.2.3".parse().unwrap();
        assert_eq!(version, Version::new(1, 2, 3));

        let version: Version = "10.0.5".parse().unwrap();
        assert_eq!(version, Version::new(10, 0, 5));

        assert!("invalid".parse::<Version>().is_err());
        assert!("1.2".parse::<Version>().is_err());
        assert!("1.2.3.4".parse::<Version>().is_err());
    }

    #[test]
    fn test_version_comparison() {
        let v1 = Version::new(1, 0, 0);
        let v2 = Version::new(1, 0, 1);
        let v3 = Version::new(1, 1, 0);
        let v4 = Version::new(2, 0, 0);

        assert!(v1 < v2);
        assert!(v2 < v3);
        assert!(v3 < v4);
        assert_eq!(v1, Version::new(1, 0, 0));
    }

    #[test]
    fn test_version_bumping() {
        let mut version = Version::new(1, 2, 3);

        version.bump_patch();
        assert_eq!(version, Version::new(1, 2, 4));

        version.bump_minor();
        assert_eq!(version, Version::new(1, 3, 0));

        version.bump_major();
        assert_eq!(version, Version::new(2, 0, 0));
    }

    #[test]
    fn test_variable_spec_creation() {
        let var = VariableSpec::new(
            "user_input".to_string(),
            "String".to_string(),
            true,
            "User's input text".to_string(),
        );

        assert_eq!(var.name, "user_input");
        assert_eq!(var.type_hint, "String");
        assert!(var.required);
        assert_eq!(var.description, "User's input text");
        assert!(var.default.is_none());
        assert!(var.validation_pattern.is_none());
    }

    #[test]
    fn test_variable_spec_with_default() {
        let var = VariableSpec::new(
            "count".to_string(),
            "Number".to_string(),
            false,
            "Item count".to_string(),
        )
        .with_default("10".to_string());

        assert_eq!(var.default, Some("10".to_string()));
    }

    #[test]
    fn test_variable_spec_validation() {
        let var = VariableSpec::new(
            "email".to_string(),
            "String".to_string(),
            true,
            "Email address".to_string(),
        )
        .with_validation(r"^[\w\.-]+@[\w\.-]+\.\w+$".to_string());

        // Valid email
        let result = var.validate(&Some("test@example.com".to_string()));
        assert!(result.is_ok());

        // Invalid email
        let result = var.validate(&Some("invalid-email".to_string()));
        assert!(result.is_err());

        // Missing required value
        let result = var.validate(&None);
        assert!(result.is_err());
    }

    #[test]
    fn test_variable_spec_optional_validation() {
        let var = VariableSpec::new(
            "optional".to_string(),
            "String".to_string(),
            false,
            "Optional field".to_string(),
        );

        // Missing optional value is OK
        let result = var.validate(&None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_prompt_template_creation() {
        let variables = vec![VariableSpec::new(
            "name".to_string(),
            "String".to_string(),
            true,
            "User's name".to_string(),
        )];

        let template = PromptTemplate::new(
            "Greeting Template".to_string(),
            "Hello, {{name}}!".to_string(),
            variables,
        );

        assert_eq!(template.name, "Greeting Template");
        assert_eq!(template.template, "Hello, {{name}}!");
        assert_eq!(template.variables.len(), 1);
        assert_eq!(template.version, Version::new(1, 0, 0));
        assert_eq!(template.usage_count, 0);
    }

    #[test]
    fn test_prompt_template_with_description() {
        let template = PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![])
            .with_description("A test template".to_string());

        assert_eq!(template.description, "A test template");
    }

    #[test]
    fn test_prompt_template_with_author() {
        let template = PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![])
            .with_author("John Doe".to_string());

        assert_eq!(template.author, "John Doe");
    }

    #[test]
    fn test_prompt_template_instantiation() {
        let variables = vec![
            VariableSpec::new(
                "name".to_string(),
                "String".to_string(),
                true,
                "Name".to_string(),
            ),
            VariableSpec::new(
                "action".to_string(),
                "String".to_string(),
                true,
                "Action".to_string(),
            ),
        ];

        let template = PromptTemplate::new(
            "Action Template".to_string(),
            "{{name}} is {{action}}".to_string(),
            variables,
        );

        let mut values = HashMap::new();
        values.insert("name".to_string(), "Alice".to_string());
        values.insert("action".to_string(), "coding".to_string());

        let result = template.instantiate(&values).unwrap();
        assert_eq!(result, "Alice is coding");
    }

    #[test]
    fn test_prompt_template_instantiation_with_defaults() {
        let variables = vec![
            VariableSpec::new(
                "name".to_string(),
                "String".to_string(),
                true,
                "Name".to_string(),
            ),
            VariableSpec::new(
                "greeting".to_string(),
                "String".to_string(),
                false,
                "Greeting".to_string(),
            )
            .with_default("Hello".to_string()),
        ];

        let template = PromptTemplate::new(
            "Greeting".to_string(),
            "{{greeting}}, {{name}}!".to_string(),
            variables,
        );

        let mut values = HashMap::new();
        values.insert("name".to_string(), "Bob".to_string());

        let result = template.instantiate(&values).unwrap();
        assert_eq!(result, "Hello, Bob!");
    }

    #[test]
    fn test_prompt_template_validation_missing_required() {
        let variables = vec![VariableSpec::new(
            "required_field".to_string(),
            "String".to_string(),
            true,
            "Required".to_string(),
        )];

        let template = PromptTemplate::new(
            "Test".to_string(),
            "{{required_field}}".to_string(),
            variables,
        );

        let values = HashMap::new();
        let result = template.validate(&values);
        assert!(result.is_err());
    }

    #[test]
    fn test_prompt_template_validation_pattern() {
        let variables = vec![VariableSpec::new(
            "number".to_string(),
            "String".to_string(),
            true,
            "Number".to_string(),
        )
        .with_validation(r"^\d+$".to_string())];

        let template = PromptTemplate::new(
            "Test".to_string(),
            "Count: {{number}}".to_string(),
            variables,
        );

        let mut values = HashMap::new();
        values.insert("number".to_string(), "123".to_string());
        assert!(template.validate(&values).is_ok());

        let mut values = HashMap::new();
        values.insert("number".to_string(), "abc".to_string());
        assert!(template.validate(&values).is_err());
    }

    #[test]
    fn test_prompt_template_usage_tracking() {
        let mut template =
            PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![]);

        assert_eq!(template.usage_count, 0);

        template.record_usage();
        assert_eq!(template.usage_count, 1);

        template.record_usage();
        assert_eq!(template.usage_count, 2);
    }

    #[test]
    fn test_prompt_template_version_bumping() {
        let mut template =
            PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![]);

        assert_eq!(template.version, Version::new(1, 0, 0));

        template.bump_version(VersionLevel::Patch);
        assert_eq!(template.version, Version::new(1, 0, 1));

        template.bump_version(VersionLevel::Minor);
        assert_eq!(template.version, Version::new(1, 1, 0));

        template.bump_version(VersionLevel::Major);
        assert_eq!(template.version, Version::new(2, 0, 0));
    }

    #[test]
    fn test_prompt_template_from_parent() {
        let parent_id = TemplateId::new();
        let template = PromptTemplate::from_parent(
            parent_id,
            "Child Template".to_string(),
            "Extended: {{content}}".to_string(),
            vec![],
        );

        assert_eq!(template.parent_id, Some(parent_id));
        assert_eq!(template.name, "Child Template");
    }

    #[test]
    fn test_prompt_template_tags() {
        let mut template =
            PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![]);

        assert_eq!(template.tags.len(), 0);

        template.add_tag("production".to_string());
        template.add_tag("verified".to_string());

        assert_eq!(template.tags.len(), 2);
        assert!(template.tags.contains(&"production".to_string()));
        assert!(template.tags.contains(&"verified".to_string()));
    }

    #[test]
    fn test_prompt_template_metadata() {
        let mut template =
            PromptTemplate::new("Test".to_string(), "{{content}}".to_string(), vec![]);

        assert_eq!(template.metadata.len(), 0);

        template.add_metadata("category".to_string(), "greeting".to_string());
        template.add_metadata("priority".to_string(), "high".to_string());

        assert_eq!(template.metadata.len(), 2);
        assert_eq!(
            template.metadata.get("category"),
            Some(&"greeting".to_string())
        );
        assert_eq!(template.metadata.get("priority"), Some(&"high".to_string()));
    }
}