terraphim_config 1.19.3

Terraphim configuration
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
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
//! Configuration management for Terraphim AI.
//!
//! Provides role-based configuration where each [`Role`] describes a user profile with
//! a set of [`Haystack`]s (data sources), a relevance function, and optional LLM settings.
//!
//! # Loading Priority
//!
//! 1. Explicit path via `TERRAPHIM_CONFIG` environment variable
//! 2. Saved config retrieved from the persistence layer
//! 3. Hard-coded defaults in `terraphim_server/default/`
//!
//! # Key Types
//!
//! - [`Config`] -- top-level configuration holding all roles
//! - [`Role`] -- user profile with haystacks, relevance function, and theme
//! - [`Haystack`] -- a data source descriptor (path, service type, extra parameters)
//! - [`ServiceType`] -- enum of supported haystack backends

use std::{path::PathBuf, sync::Arc};

use terraphim_automata::{
    AutomataPath,
    builder::{Logseq, ThesaurusBuilder},
    load_thesaurus, parse_markdown_directives_dir,
};
use terraphim_persistence::Persistable;
use terraphim_rolegraph::{RoleGraph, RoleGraphSync};
use terraphim_types::{
    Document, IndexedDocument, KnowledgeGraphInputType, RelevanceFunction, RoleName, SearchQuery,
};

use terraphim_settings::DeviceSettings;

use ahash::AHashMap;
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;
use tokio::sync::Mutex;
#[cfg(feature = "typescript")]
use tsify::Tsify;

use crate::llm_router::LlmRouterConfig;

// LLM Router configuration
pub mod llm_router;

// Project-level configuration discovery
pub mod project;

/// Convenience alias for `Result<T, TerraphimConfigError>` used throughout this crate.
pub type Result<T> = std::result::Result<T, TerraphimConfigError>;

use opendal::Result as OpendalResult;

type PersistenceResult<T> = std::result::Result<T, terraphim_persistence::Error>;

/// Errors arising from loading, validating, or persisting Terraphim configuration.
#[derive(Error, Debug)]
pub enum TerraphimConfigError {
    #[error("Unable to load config")]
    NotFound,

    #[error("At least one role is required")]
    NoRoles,

    #[error("Profile error")]
    Profile(String),

    #[error("Persistence error")]
    Persistence(Box<terraphim_persistence::Error>),

    #[error("Serde JSON error")]
    Json(#[from] serde_json::Error),

    #[error("Cannot initialize tracing subscriber")]
    TracingSubscriber(Box<dyn std::error::Error + Send + Sync>),

    #[error("Pipe error")]
    Pipe(#[from] terraphim_rolegraph::Error),

    #[error("Automata error")]
    Automata(#[from] terraphim_automata::TerraphimAutomataError),

    #[error("Url error")]
    Url(#[from] url::ParseError),

    #[error("IO error")]
    Io(#[from] std::io::Error),

    #[error("Config error")]
    Config(String),
}

impl From<terraphim_persistence::Error> for TerraphimConfigError {
    fn from(error: terraphim_persistence::Error) -> Self {
        TerraphimConfigError::Persistence(Box::new(error))
    }
}

/// Expand shell-like variables in a path string.
///
/// Supports:
/// - `${HOME}` or `$HOME` -> user's home directory
/// - `${TERRAPHIM_DATA_PATH:-default}` -> environment variable with default value
/// - `~` at the start -> user's home directory
pub fn expand_path(path: &str) -> PathBuf {
    let mut result = path.to_string();

    /// Get home directory using multiple fallback strategies
    fn get_home_dir() -> Option<PathBuf> {
        // Try dirs crate first
        if let Some(home) = dirs::home_dir() {
            return Some(home);
        }
        // Fallback to HOME environment variable
        if let Ok(home) = std::env::var("HOME") {
            return Some(PathBuf::from(home));
        }
        // Fallback to USERPROFILE on Windows
        if let Ok(profile) = std::env::var("USERPROFILE") {
            return Some(PathBuf::from(profile));
        }
        None
    }

    // Handle ${VAR:-default} syntax (environment variable with default)
    // This regex handles nested ${...} in the default value by using a greedy match
    // that captures everything until the last }
    loop {
        // Find ${VAR:-...} pattern manually to handle nested braces
        if let Some(start) = result.find("${") {
            if let Some(colon_pos) = result[start..].find(":-") {
                let colon_pos = start + colon_pos;
                // Find the variable name
                let var_name = &result[start + 2..colon_pos];
                // Find the matching closing brace by counting braces
                let after_colon = colon_pos + 2;
                let mut depth = 1;
                let mut end_pos = after_colon;
                for (i, c) in result[after_colon..].char_indices() {
                    match c {
                        '{' => depth += 1,
                        '}' => {
                            depth -= 1;
                            if depth == 0 {
                                end_pos = after_colon + i;
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                if depth == 0 {
                    let default_value = &result[after_colon..end_pos];
                    let replacement =
                        std::env::var(var_name).unwrap_or_else(|_| default_value.to_string());
                    result = format!(
                        "{}{}{}",
                        &result[..start],
                        replacement,
                        &result[end_pos + 1..]
                    );
                    continue; // Process again in case there are more patterns
                }
            }
        }
        break;
    }

    // Handle ${VAR} syntax
    let re_braces = regex::Regex::new(r"\$\{([^}]+)\}").unwrap();
    result = re_braces
        .replace_all(&result, |caps: &regex::Captures| {
            let var_name = &caps[1];
            if var_name == "HOME" {
                get_home_dir()
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_else(|| format!("${{{}}}", var_name))
            } else {
                std::env::var(var_name).unwrap_or_else(|_| format!("${{{}}}", var_name))
            }
        })
        .to_string();

    // Handle $VAR syntax (without braces)
    let re_dollar = regex::Regex::new(r"\$([A-Za-z_][A-Za-z0-9_]*)").unwrap();
    result = re_dollar
        .replace_all(&result, |caps: &regex::Captures| {
            let var_name = &caps[1];
            if var_name == "HOME" {
                get_home_dir()
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_else(|| format!("${}", var_name))
            } else {
                std::env::var(var_name).unwrap_or_else(|_| format!("${}", var_name))
            }
        })
        .to_string();

    // Handle ~ at the beginning of the path
    if result.starts_with('~') {
        if let Some(home) = get_home_dir() {
            result = result.replacen('~', &home.to_string_lossy(), 1);
        }
    }

    PathBuf::from(result)
}

/// Default context window size for LLM requests
fn default_context_window() -> Option<u64> {
    Some(32768)
}

/// A role is a collection of settings for a specific user
///
/// It contains a user's knowledge graph, a list of haystacks, as
/// well as preferences for the relevance function and theme
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Default)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Role {
    pub shortname: Option<String>,
    pub name: RoleName,
    /// The relevance function used to rank search results
    pub relevance_function: RelevanceFunction,
    pub terraphim_it: bool,
    pub theme: String,
    pub kg: Option<KnowledgeGraph>,
    pub haystacks: Vec<Haystack>,
    /// Enable AI-powered article summaries using LLM providers
    #[serde(default)]
    pub llm_enabled: bool,
    /// API key for LLM service
    #[serde(default)]
    pub llm_api_key: Option<String>,
    /// Model to use for generating summaries (e.g., "openai/gpt-3.5-turbo", "gemma3:270m")
    #[serde(default)]
    pub llm_model: Option<String>,
    /// Automatically summarize search results using LLM
    #[serde(default)]
    pub llm_auto_summarize: bool,
    /// Enable Chat interface backed by LLM
    #[serde(default)]
    pub llm_chat_enabled: bool,
    /// Optional system prompt to use for chat conversations
    #[serde(default)]
    pub llm_chat_system_prompt: Option<String>,
    /// Optional chat model override (falls back to llm_model)
    #[serde(default)]
    pub llm_chat_model: Option<String>,
    /// Maximum tokens for LLM context window (default: 32768)
    #[serde(default = "default_context_window")]
    pub llm_context_window: Option<u64>,
    #[serde(flatten)]
    #[schemars(skip)]
    #[cfg_attr(feature = "typescript", tsify(type = "Record<string, unknown>"))]
    pub extra: AHashMap<String, Value>,
    /// Enable intelligent LLM routing with 6-phase architecture
    #[serde(default)]
    pub llm_router_enabled: bool,
    /// Configuration for intelligent routing behavior
    #[serde(default)]
    pub llm_router_config: Option<LlmRouterConfig>,
}

impl std::fmt::Debug for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Role")
            .field("shortname", &self.shortname)
            .field("name", &self.name)
            .field("relevance_function", &self.relevance_function)
            .field("terraphim_it", &self.terraphim_it)
            .field("theme", &self.theme)
            .field("kg", &self.kg)
            .field("haystacks", &self.haystacks)
            .field("llm_enabled", &self.llm_enabled)
            .field(
                "llm_api_key",
                &self.llm_api_key.as_ref().map(|_| "***REDACTED***"),
            )
            .field("llm_model", &self.llm_model)
            .field("llm_auto_summarize", &self.llm_auto_summarize)
            .field("llm_chat_enabled", &self.llm_chat_enabled)
            .field("llm_chat_system_prompt", &self.llm_chat_system_prompt)
            .field("llm_chat_model", &self.llm_chat_model)
            .field("llm_context_window", &self.llm_context_window)
            .field("llm_router_enabled", &self.llm_router_enabled)
            .field("llm_router_config", &self.llm_router_config)
            .finish_non_exhaustive()
    }
}

impl Role {
    /// Create a new Role with default values for all fields
    pub fn new(name: impl Into<RoleName>) -> Self {
        Self {
            shortname: None,
            name: name.into(),
            relevance_function: RelevanceFunction::TitleScorer,
            terraphim_it: false,
            theme: "default".to_string(),
            kg: None,
            haystacks: vec![],
            llm_enabled: false,
            llm_api_key: None,
            llm_model: None,
            llm_auto_summarize: false,
            llm_chat_enabled: false,
            llm_chat_system_prompt: None,
            llm_chat_model: None,
            llm_context_window: default_context_window(),
            extra: AHashMap::new(),
            llm_router_enabled: false,
            llm_router_config: None,
        }
    }

    /// Check if LLM is properly configured for this role
    pub fn has_llm_config(&self) -> bool {
        self.llm_enabled && self.llm_api_key.is_some() && self.llm_model.is_some()
    }

    /// Get the LLM model name, providing a sensible default
    pub fn get_llm_model(&self) -> Option<&str> {
        self.llm_model.as_deref()
    }
}

use anyhow::Context;
/// The service used for indexing documents
///
/// Each service assumes documents to be stored in a specific format
/// and uses a specific indexing algorithm
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub enum ServiceType {
    /// Use ripgrep as the indexing service
    Ripgrep,
    /// Use an Atomic Server as the indexing service
    Atomic,
    /// Use query.rs as the indexing service
    QueryRs,
    /// Use ClickUp API as the indexing service
    ClickUp,
    /// Use an MCP client to query a Model Context Protocol server
    Mcp,
    /// Use Perplexity AI-powered web search for indexing
    Perplexity,
    /// Use grep.app for searching code across GitHub repositories
    GrepApp,
    /// Use AI coding assistant session logs (Claude Code, OpenCode, Cursor, Aider, Codex)
    AiAssistant,
    /// Use Quickwit search engine for log and observability data indexing
    Quickwit,
    /// Use JMAP protocol for email search (RFC 8620/8621)
    Jmap,
}

/// A haystack is a collection of documents that can be indexed and searched
///
/// One user can have multiple haystacks
/// Each haystack is indexed using a specific service
#[derive(Deserialize, Clone, PartialEq, Eq, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Haystack {
    /// The location of the haystack - can be a filesystem path or URL
    pub location: String,
    /// The service used for indexing documents in the haystack
    pub service: ServiceType,
    /// When set to `true` the haystack is treated as read-only; documents found
    /// inside will not be modified on disk by Terraphim (e.g. via the Novel
    /// editor). Defaults to `false` for backwards-compatibility.
    #[serde(default)]
    pub read_only: bool,
    /// When set to `true`, fetch the actual content of documents from URLs
    /// instead of just indexing metadata. Useful for web-based haystacks.
    /// Defaults to `false` for backwards-compatibility.
    #[serde(default)]
    pub fetch_content: bool,
    /// The secret for connecting to an Atomic Server.
    /// This field is only serialized for Atomic service haystacks.
    #[serde(default)]
    pub atomic_server_secret: Option<String>,
    /// Extra parameters specific to the service type.
    /// For Ripgrep: can include additional command-line arguments like filtering by tags.
    /// For Atomic: can include additional API parameters.
    #[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
    pub extra_parameters: std::collections::HashMap<String, String>,
}

impl Serialize for Haystack {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;

        // Determine how many fields to include based on service type
        let mut field_count = 3; // location, service, read_only

        // Include atomic_server_secret only for Atomic service and if it's present
        let include_atomic_secret =
            self.service == ServiceType::Atomic && self.atomic_server_secret.is_some();
        if include_atomic_secret {
            field_count += 1;
        }

        // Include extra_parameters if not empty
        if !self.extra_parameters.is_empty() {
            field_count += 1;
        }

        let mut state = serializer.serialize_struct("Haystack", field_count)?;
        state.serialize_field("location", &self.location)?;
        state.serialize_field("service", &self.service)?;
        state.serialize_field("read_only", &self.read_only)?;

        // Only include atomic_server_secret for Atomic service
        if include_atomic_secret {
            state.serialize_field("atomic_server_secret", &self.atomic_server_secret)?;
        }

        // Include extra_parameters if not empty
        if !self.extra_parameters.is_empty() {
            state.serialize_field("extra_parameters", &self.extra_parameters)?;
        }

        state.end()
    }
}

impl std::fmt::Debug for Haystack {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Haystack")
            .field("location", &self.location)
            .field("service", &self.service)
            .field("read_only", &self.read_only)
            .field("fetch_content", &self.fetch_content)
            .field(
                "atomic_server_secret",
                &self.atomic_server_secret.as_ref().map(|_| "***REDACTED***"),
            )
            .field("extra_parameters", &self.extra_parameters)
            .finish()
    }
}

impl Haystack {
    /// Create a new Haystack with extra parameters
    pub fn new(location: String, service: ServiceType, read_only: bool) -> Self {
        Self {
            location,
            service,
            read_only,
            fetch_content: false,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
        }
    }

    /// Create a new Haystack with atomic server secret
    pub fn with_atomic_secret(mut self, secret: Option<String>) -> Self {
        // Only set secret for Atomic service
        if self.service == ServiceType::Atomic {
            self.atomic_server_secret = secret;
        }
        self
    }

    /// Add extra parameters to the haystack
    pub fn with_extra_parameters(
        mut self,
        params: std::collections::HashMap<String, String>,
    ) -> Self {
        self.extra_parameters = params;
        self
    }

    /// Add a single extra parameter
    pub fn with_extra_parameter(mut self, key: String, value: String) -> Self {
        self.extra_parameters.insert(key, value);
        self
    }

    /// Get a reference to extra parameters for this service type
    pub fn get_extra_parameters(&self) -> &std::collections::HashMap<String, String> {
        &self.extra_parameters
    }
}

/// A knowledge graph is the collection of documents which were indexed
/// using a specific service
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct KnowledgeGraph {
    /// automata path refering to the published automata and can be online url or local file with pre-build automata
    #[schemars(with = "Option<String>")]
    pub automata_path: Option<AutomataPath>,
    /// Knowlege graph can be re-build from local files, for example Markdown files
    pub knowledge_graph_local: Option<KnowledgeGraphLocal>,
    pub public: bool,
    pub publish: bool,
}
impl KnowledgeGraph {
    /// Return `true` if either an automata path or a local KG directory is configured.
    pub fn is_set(&self) -> bool {
        self.automata_path.is_some() || self.knowledge_graph_local.is_some()
    }
}

/// Local knowledge-graph source: an input type paired with a filesystem path.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct KnowledgeGraphLocal {
    pub input_type: KnowledgeGraphInputType,
    pub path: PathBuf,
}
/// Builder, which allows to create a new `Config`
///
/// The first role added will be set as the default role.
/// This can be changed by calling `default_role` with the role name.
///
/// # Example
///
/// ```rs
/// use terraphim_config::ConfigBuilder;
///
/// let config = ConfigBuilder::new()
///    .global_shortcut("Ctrl+X")
///    .with_role("Default", role)
///    .with_role("Engineer", role)
///    .with_role("System Operator", role)
///    .default_role("Default")
///    .build();
/// ```
#[derive(Debug)]
pub struct ConfigBuilder {
    config: Config,
    device_settings: DeviceSettings,
    #[allow(dead_code)]
    settings_path: PathBuf,
}

impl ConfigBuilder {
    /// Create a new `ConfigBuilder`
    pub fn new() -> Self {
        Self {
            config: Config::empty(),
            device_settings: DeviceSettings::new(),
            settings_path: PathBuf::new(),
        }
    }
    /// Create a new builder seeded with the given [`ConfigId`].
    ///
    /// Chooses the appropriate [`DeviceSettings`] variant for the id:
    /// `ConfigId::Embedded` uses embedded defaults; all others call
    /// [`DeviceSettings::new`].
    pub fn new_with_id(id: ConfigId) -> Self {
        let device_settings = match id {
            ConfigId::Embedded => DeviceSettings::default_embedded(),
            _ => DeviceSettings::new(),
        };

        Self {
            config: Config {
                id,
                ..Config::empty()
            },
            device_settings,
            settings_path: PathBuf::new(),
        }
    }
    /// Populate the builder with a minimal embedded (WASM/library) configuration.
    pub fn build_default_embedded(mut self) -> Self {
        self.config.id = ConfigId::Embedded;

        // Add Default role with basic functionality
        let mut default_role = Role::new("Default");
        default_role.shortname = Some("Default".to_string());
        default_role.theme = "spacelab".to_string();
        default_role.extra.insert(
            "llm_provider".to_string(),
            Value::String("ollama".to_string()),
        );
        default_role.extra.insert(
            "llm_model".to_string(),
            Value::String("llama3.2:3b".to_string()),
        );
        default_role.haystacks = vec![Haystack {
            location: "docs/src".to_string(),
            service: ServiceType::Ripgrep,
            read_only: true,
            fetch_content: false,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
        }];

        self = self.add_role("Default", default_role);

        // Add Terraphim Engineer role with knowledge graph
        let mut terraphim_role = Role::new("Terraphim Engineer");
        terraphim_role.shortname = Some("TerraEng".to_string());
        terraphim_role.relevance_function = RelevanceFunction::TerraphimGraph;
        terraphim_role.terraphim_it = true;
        terraphim_role.theme = "lumen".to_string();
        terraphim_role.extra.insert(
            "llm_provider".to_string(),
            Value::String("ollama".to_string()),
        );
        terraphim_role.extra.insert(
            "llm_model".to_string(),
            Value::String("llama3.2:3b".to_string()),
        );
        terraphim_role.kg = Some(KnowledgeGraph {
            automata_path: None,
            knowledge_graph_local: Some(KnowledgeGraphLocal {
                input_type: KnowledgeGraphInputType::Markdown,
                path: self.get_default_data_path().join("kg"),
            }),
            public: true,
            publish: true,
        });
        terraphim_role.haystacks = vec![Haystack {
            location: "docs/src".to_string(),
            service: ServiceType::Ripgrep,
            read_only: true,
            fetch_content: false,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
        }];

        self = self.add_role("Terraphim Engineer", terraphim_role);

        // Add Rust Engineer role with QueryRs
        let mut rust_engineer_role = Role::new("Rust Engineer");
        rust_engineer_role.shortname = Some("rust-engineer".to_string());
        rust_engineer_role.theme = "cosmo".to_string();
        rust_engineer_role.extra.insert(
            "llm_provider".to_string(),
            Value::String("ollama".to_string()),
        );
        rust_engineer_role.extra.insert(
            "llm_model".to_string(),
            Value::String("qwen2.5-coder:latest".to_string()),
        );
        rust_engineer_role.haystacks = vec![Haystack {
            location: "https://query.rs".to_string(),
            service: ServiceType::QueryRs,
            read_only: true,
            fetch_content: false,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
        }];

        self = self.add_role("Rust Engineer", rust_engineer_role);

        // Set Terraphim Engineer as default and selected role
        self.config.default_role = RoleName::new("Terraphim Engineer");
        self.config.selected_role = RoleName::new("Terraphim Engineer");
        self
    }

    /// Resolve the configured `default_data_path`, expanding `~` and `${VAR}` tokens.
    pub fn get_default_data_path(&self) -> PathBuf {
        expand_path(&self.device_settings.default_data_path)
    }

    /// Populate the builder with a server configuration sourced from the current working directory.
    pub fn build_default_server(mut self) -> Self {
        self.config.id = ConfigId::Server;
        // mind where cargo run is triggered from
        let cwd = std::env::current_dir()
            .context("Failed to get current directory")
            .unwrap();
        log::info!("Current working directory: {}", cwd.display());
        let system_operator_haystack = if cwd.ends_with("terraphim_server") {
            cwd.join("fixtures/haystack/")
        } else {
            cwd.join("terraphim_server/fixtures/haystack/")
        };

        log::debug!("system_operator_haystack: {:?}", system_operator_haystack);
        let automata_test_path = if cwd.ends_with("terraphim_server") {
            cwd.join("fixtures/term_to_id.json")
        } else {
            cwd.join("terraphim_server/fixtures/term_to_id.json")
        };
        log::debug!("Test automata_test_path {:?}", automata_test_path);
        let automata_remote = AutomataPath::from_remote(
            "https://staging-storage.terraphim.io/thesaurus_Default.json",
        )
        .unwrap();
        log::info!("Automata remote URL: {automata_remote}");
        self.global_shortcut("Ctrl+X")
            .add_role("Default", {
                let mut default_role = Role::new("Default");
                default_role.shortname = Some("Default".to_string());
                default_role.theme = "spacelab".to_string();
                default_role.haystacks = vec![Haystack {
                    location: system_operator_haystack.to_string_lossy().to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                default_role
            })
            .add_role("Engineer", {
                let mut engineer_role = Role::new("Engineer");
                engineer_role.shortname = Some("Engineer".into());
                engineer_role.relevance_function = RelevanceFunction::TerraphimGraph;
                engineer_role.terraphim_it = true;
                engineer_role.theme = "lumen".to_string();
                engineer_role.kg = Some(KnowledgeGraph {
                    automata_path: Some(automata_remote.clone()),
                    knowledge_graph_local: Some(KnowledgeGraphLocal {
                        input_type: KnowledgeGraphInputType::Markdown,
                        path: system_operator_haystack.clone(),
                    }),
                    public: true,
                    publish: true,
                });
                engineer_role.haystacks = vec![Haystack {
                    location: system_operator_haystack.to_string_lossy().to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                engineer_role
            })
            .add_role("System Operator", {
                let mut system_operator_role = Role::new("System Operator");
                system_operator_role.shortname = Some("operator".to_string());
                system_operator_role.relevance_function = RelevanceFunction::TerraphimGraph;
                system_operator_role.terraphim_it = true;
                system_operator_role.theme = "superhero".to_string();
                system_operator_role.kg = Some(KnowledgeGraph {
                    automata_path: Some(automata_remote.clone()),
                    knowledge_graph_local: Some(KnowledgeGraphLocal {
                        input_type: KnowledgeGraphInputType::Markdown,
                        path: system_operator_haystack.clone(),
                    }),
                    public: true,
                    publish: true,
                });
                system_operator_role.haystacks = vec![Haystack {
                    location: system_operator_haystack.to_string_lossy().to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                system_operator_role
            })
            .default_role("Default")
            .unwrap()
    }

    /// Populate the builder with desktop defaults, using the resolved data path for haystacks.
    pub fn build_default_desktop(mut self) -> Self {
        let default_data_path = self.get_default_data_path();
        // Remove the automata_path - let it be built from local KG files during startup
        log::info!("Documents path: {:?}", default_data_path);
        self.config.id = ConfigId::Desktop;
        self.global_shortcut("Ctrl+X")
            .add_role("Default", {
                let mut default_role = Role::new("Default");
                default_role.shortname = Some("Default".to_string());
                default_role.theme = "spacelab".to_string();
                default_role.haystacks = vec![Haystack {
                    location: default_data_path.to_string_lossy().to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                default_role
            })
            .add_role("Terraphim Engineer", {
                let mut terraphim_engineer_role = Role::new("Terraphim Engineer");
                terraphim_engineer_role.shortname = Some("TerraEng".to_string());
                terraphim_engineer_role.relevance_function = RelevanceFunction::TerraphimGraph;
                terraphim_engineer_role.terraphim_it = true;
                terraphim_engineer_role.theme = "lumen".to_string();
                terraphim_engineer_role.kg = Some(KnowledgeGraph {
                    automata_path: None, // Set to None so it builds from local KG files during startup
                    knowledge_graph_local: Some(KnowledgeGraphLocal {
                        input_type: KnowledgeGraphInputType::Markdown,
                        path: default_data_path.join("kg"),
                    }),
                    public: true,
                    publish: true,
                });
                terraphim_engineer_role.haystacks = vec![Haystack {
                    location: default_data_path.to_string_lossy().to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                terraphim_engineer_role
            })
            .add_role("Rust Engineer", {
                let mut rust_engineer_role = Role::new("Rust Engineer");
                rust_engineer_role.shortname = Some("rust-engineer".to_string());
                rust_engineer_role.theme = "cosmo".to_string();
                rust_engineer_role.haystacks = vec![Haystack {
                    location: "https://query.rs".to_string(),
                    service: ServiceType::QueryRs,
                    read_only: true,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                rust_engineer_role
            })
            .default_role("Terraphim Engineer")
            .unwrap()
    }

    /// Start from an existing config
    ///
    /// This is useful when you want to start from an setup and modify some
    /// fields
    pub fn from_config(
        config: Config,
        device_settings: DeviceSettings,
        settings_path: PathBuf,
    ) -> Self {
        Self {
            config,
            device_settings,
            settings_path,
        }
    }

    /// Set the global shortcut for the config
    pub fn global_shortcut(mut self, global_shortcut: &str) -> Self {
        self.config.global_shortcut = Some(global_shortcut.to_string());
        self
    }

    /// Merge with a project config.
    ///
    /// Project roles fully replace global roles (by RoleName), not deep-merge.
    /// Project global_shortcut, if present, overrides the global one.
    pub fn merge_with(mut self, project_config: &crate::project::ProjectConfig) -> Self {
        if let Some(ref shortcut) = project_config.global_shortcut {
            self.config.global_shortcut = Some(shortcut.clone());
        }
        for (name, role) in &project_config.roles {
            let role_name = RoleName::new(name);
            self.config.roles.insert(role_name, role.clone());
        }
        self
    }

    /// Apply project config discovery and merge if found.
    ///
    /// Returns self unchanged if no project config is found.
    pub fn with_project(self) -> Self {
        if let Ok(Some(path)) = crate::project::discover(None) {
            let config_path = path.join("config.json");
            if config_path.is_file() {
                if let Ok(project_config) = crate::project::ProjectConfig::from_file(&config_path) {
                    return self.merge_with(&project_config);
                }
            }
        }
        self
    }

    /// Add a new role to the config
    pub fn add_role(mut self, role_name: &str, role: Role) -> Self {
        let role_name = RoleName::new(role_name);
        // Set to default role if this is the first role
        if self.config.roles.is_empty() {
            self.config.default_role = role_name.clone();
        }
        self.config.roles.insert(role_name, role);

        self
    }

    /// Set the default role for the config
    pub fn default_role(mut self, default_role: &str) -> Result<Self> {
        let default_role = RoleName::new(default_role);
        // Check if the role exists
        if !self.config.roles.contains_key(&default_role) {
            return Err(TerraphimConfigError::Profile(format!(
                "Role `{}` does not exist",
                default_role
            )));
        }

        self.config.default_role = default_role;
        Ok(self)
    }

    /// Build the config
    pub fn build(self) -> Result<Config> {
        // Make sure that we have at least one role
        // if self.config.roles.is_empty() {
        //     return Err(TerraphimConfigError::NoRoles);
        // }

        Ok(self.config)
    }
}

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

/// Distinguishes how the configuration is deployed: as a background server,
/// a desktop application, or compiled-in (WASM/library) mode.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub enum ConfigId {
    Server,
    Desktop,
    Embedded,
}

/// The Terraphim config is the main configuration for terraphim
///
/// It contains the global shortcut, roles, and the default role
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Config {
    /// Identifier for the config
    pub id: ConfigId,
    /// Global shortcut for activating terraphim desktop
    #[schemars(default)]
    pub global_shortcut: Option<String>,
    /// User roles with their respective settings
    #[schemars(skip)]
    pub roles: AHashMap<RoleName, Role>,
    /// The default role to use if no role is specified
    pub default_role: RoleName,
    pub selected_role: RoleName,
}

impl Config {
    fn empty() -> Self {
        Self {
            id: ConfigId::Server, // Default to Server
            global_shortcut: None,
            roles: AHashMap::new(),
            default_role: RoleName::new("Default"),
            selected_role: RoleName::new("Default"),
        }
    }

    /// Load a Config from a JSON file path.
    ///
    /// The path is expanded using `expand_path()` to support ~, $HOME, and
    /// ${VAR:-default} syntax.
    pub fn load_from_json_file(path: &str) -> Result<Self> {
        let expanded = expand_path(path);
        log::info!("Loading role configuration from: {}", expanded.display());

        let content = std::fs::read_to_string(&expanded).map_err(|e| {
            log::error!(
                "Failed to read role config file '{}' (expanded from '{}'): {}",
                expanded.display(),
                path,
                e
            );
            TerraphimConfigError::Config(format!(
                "Cannot read role config file '{}': {}",
                expanded.display(),
                e
            ))
        })?;

        let config: Config = serde_json::from_str(&content)?;

        log::info!(
            "Loaded {} role(s) from '{}': {:?}",
            config.roles.len(),
            expanded.display(),
            config
                .roles
                .keys()
                .map(|k| k.to_string())
                .collect::<Vec<_>>()
        );

        Ok(config)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::empty()
    }
}

#[async_trait]
impl Persistable for Config {
    fn new(_key: String) -> Self {
        // Key is not used because we use the `id` field
        Config::empty()
    }

    /// Save to a single profile
    async fn save_to_one(&self, profile_name: &str) -> PersistenceResult<()> {
        self.save_to_profile(profile_name).await?;
        Ok(())
    }

    // Saves to all profiles
    async fn save(&self) -> PersistenceResult<()> {
        let _op = &self.load_config().await?.1;
        let _ = self.save_to_all().await?;
        Ok(())
    }

    /// Load key from the fastest operator
    async fn load(&mut self) -> PersistenceResult<Self> {
        let op = &self.load_config().await?.1;
        let key = self.get_key();
        let obj = self.load_from_operator(&key, op).await?;
        Ok(obj)
    }

    /// returns ulid as key + .json
    fn get_key(&self) -> String {
        match self.id {
            ConfigId::Server => "server",
            ConfigId::Desktop => "desktop",
            ConfigId::Embedded => "embedded",
        }
        .to_string()
            + "_config.json"
    }
}

/// ConfigState for the Terraphim (Actor)
/// Extract trigger and pinned directives from KG markdown files.
///
/// Parses markdown directives from the given directory, looks up each concept
/// in the thesaurus to obtain its node ID, and returns a map of node IDs to
/// trigger text plus a list of pinned node IDs.
fn extract_triggers_from_kg(
    kg_path: &PathBuf,
    thesaurus: &terraphim_types::Thesaurus,
) -> (ahash::AHashMap<u64, String>, Vec<u64>) {
    let mut triggers = ahash::AHashMap::new();
    let mut pinned = Vec::new();

    let parsed = match parse_markdown_directives_dir(kg_path.as_path()) {
        Ok(result) => result,
        Err(err) => {
            log::warn!(
                "Failed to parse markdown directives from {:?}: {}",
                kg_path,
                err
            );
            return (triggers, pinned);
        }
    };

    for (concept_name, directives) in parsed.directives {
        let normalized_value = terraphim_types::NormalizedTermValue::new(concept_name.clone());
        if let Some(term) = thesaurus.get(&normalized_value) {
            let node_id = term.id;
            if let Some(trigger_text) = directives.trigger {
                if !trigger_text.trim().is_empty() {
                    triggers.insert(node_id, trigger_text.trim().to_string());
                }
            }
            if directives.pinned {
                pinned.push(node_id);
            }
        } else {
            log::debug!(
                "Concept '{}' not found in thesaurus for trigger extraction",
                concept_name
            );
        }
    }

    (triggers, pinned)
}

/// Config state can be updated using the API or Atomic Server
///
/// Holds the Terraphim Config and the RoleGraphs
#[derive(Debug, Clone)]
pub struct ConfigState {
    /// Terraphim Config
    pub config: Arc<Mutex<Config>>,
    /// RoleGraphs
    pub roles: AHashMap<RoleName, RoleGraphSync>,
}

impl ConfigState {
    /// Create a new ConfigState
    ///
    /// For each role in a config, initialize a rolegraph
    /// and add it to the config state
    pub async fn new(config: &mut Config) -> Result<Self> {
        let mut roles = AHashMap::new();
        for (name, role) in &config.roles {
            let role_name = name.clone();
            log::info!("Creating role {}", role_name);
            if role.relevance_function == RelevanceFunction::TerraphimGraph {
                if let Some(kg) = &role.kg {
                    if let Some(automata_path) = &kg.automata_path {
                        log::info!(
                            "Role {} is configured correctly with automata_path",
                            role_name
                        );
                        log::info!("Loading Role `{}` - URL: {:?}", role_name, automata_path);

                        // Try to load from automata path first
                        match load_thesaurus(automata_path).await {
                            Ok(thesaurus) => {
                                log::info!("Successfully loaded thesaurus from automata path");
                                let mut rolegraph =
                                    RoleGraph::new(role_name.clone(), thesaurus.clone()).await?;
                                // Load trigger/pinned directives from local KG if available
                                if let Some(kg_local) = &kg.knowledge_graph_local {
                                    let (triggers, pinned) =
                                        extract_triggers_from_kg(&kg_local.path, &thesaurus);
                                    if !triggers.is_empty() || !pinned.is_empty() {
                                        log::info!(
                                            "Loading {} triggers and {} pinned entries for role {} from local KG",
                                            triggers.len(),
                                            pinned.len(),
                                            role_name
                                        );
                                        rolegraph.load_trigger_index(triggers, pinned, 0.3);
                                    }
                                }
                                roles.insert(role_name.clone(), RoleGraphSync::from(rolegraph));
                            }
                            Err(e) => {
                                log::warn!("Failed to load thesaurus from automata path: {:?}", e);
                                if let Some(kg_local) = &kg.knowledge_graph_local {
                                    log::info!(
                                        "Falling back to local KG for role {} at {:?}",
                                        role_name,
                                        kg_local.path
                                    );
                                    let logseq_builder = Logseq::default();
                                    match logseq_builder
                                        .build(
                                            role_name.as_lowercase().to_string(),
                                            kg_local.path.clone(),
                                        )
                                        .await
                                    {
                                        Ok(thesaurus) => {
                                            log::info!(
                                                "Successfully built thesaurus from local KG fallback for role {}",
                                                role_name
                                            );
                                            let mut rolegraph = RoleGraph::new(
                                                role_name.clone(),
                                                thesaurus.clone(),
                                            )
                                            .await?;
                                            let (triggers, pinned) = extract_triggers_from_kg(
                                                &kg_local.path,
                                                &thesaurus,
                                            );
                                            if !triggers.is_empty() || !pinned.is_empty() {
                                                log::info!(
                                                    "Loading {} triggers and {} pinned entries for role {} from local KG fallback",
                                                    triggers.len(),
                                                    pinned.len(),
                                                    role_name
                                                );
                                                rolegraph.load_trigger_index(triggers, pinned, 0.3);
                                            }
                                            roles.insert(
                                                role_name.clone(),
                                                RoleGraphSync::from(rolegraph),
                                            );
                                        }
                                        Err(e2) => {
                                            log::error!(
                                                "Failed to build thesaurus from local KG fallback for role {}: {:?}",
                                                role_name,
                                                e2
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    } else if let Some(kg_local) = &kg.knowledge_graph_local {
                        // If automata_path is None, but a local KG is defined, build it now
                        log::info!(
                            "Role {} has no automata_path, building thesaurus from local KG files at {:?}",
                            role_name,
                            kg_local.path
                        );
                        let logseq_builder = Logseq::default();
                        match logseq_builder
                            .build(role_name.as_lowercase().to_string(), kg_local.path.clone())
                            .await
                        {
                            Ok(thesaurus) => {
                                log::info!(
                                    "Successfully built thesaurus from local KG for role {}",
                                    role_name
                                );
                                let mut rolegraph =
                                    RoleGraph::new(role_name.clone(), thesaurus.clone()).await?;
                                let (triggers, pinned) =
                                    extract_triggers_from_kg(&kg_local.path, &thesaurus);
                                if !triggers.is_empty() || !pinned.is_empty() {
                                    log::info!(
                                        "Loading {} triggers and {} pinned entries for role {} from local KG",
                                        triggers.len(),
                                        pinned.len(),
                                        role_name
                                    );
                                    rolegraph.load_trigger_index(triggers, pinned, 0.3);
                                }
                                roles.insert(role_name.clone(), RoleGraphSync::from(rolegraph));
                            }
                            Err(e) => {
                                log::error!(
                                    "Failed to build thesaurus from local KG for role {}: {:?}",
                                    role_name,
                                    e
                                );
                            }
                        }
                    } else {
                        log::warn!(
                            "Role {} is configured for TerraphimGraph but has neither automata_path nor knowledge_graph_local defined.",
                            role_name
                        );
                    }
                }
            }
        }

        Ok(ConfigState {
            config: Arc::new(Mutex::new(config.clone())),
            roles,
        })
    }

    /// Get the default role from the config
    pub async fn get_default_role(&self) -> RoleName {
        let config = self.config.lock().await;
        config.default_role.clone()
    }

    pub async fn get_selected_role(&self) -> RoleName {
        let config = self.config.lock().await;
        config.selected_role.clone()
    }

    /// Get a role from the config
    pub async fn get_role(&self, role: &RoleName) -> Option<Role> {
        let config = self.config.lock().await;
        config.roles.get(role).cloned()
    }

    /// Insert document into all rolegraphs
    pub async fn add_to_roles(&mut self, document: &Document) -> OpendalResult<()> {
        let id = document.id.clone();

        for rolegraph_state in self.roles.values() {
            let mut rolegraph = rolegraph_state.lock().await;
            rolegraph.insert_document(&id, document.clone());
        }
        Ok(())
    }

    /// Search documents in rolegraph index using matching Knowledge Graph
    /// If knowledge graph isn't defined for the role, RoleGraph isn't build for the role
    pub async fn search_indexed_documents(
        &self,
        search_query: &SearchQuery,
        role: &Role,
    ) -> Vec<IndexedDocument> {
        log::debug!("search_documents: {:?}", search_query);

        log::debug!("Role for search_documents: {:#?}", role);

        let role_name = &role.name;
        log::debug!("Role name for searching {role_name}");
        log::debug!("All roles defined  {:?}", self.roles.clone().into_keys());
        //FIXME: breaks here for ripgrep, means KB based search is triggered before KG build
        let role = match self.roles.get(role_name) {
            Some(role) => role.lock().await,
            None => {
                // Handle the None case, e.g., return an empty vector since the function expects Vec<IndexedDocument>
                log::error!(
                    "Role `{}` does not exist or RoleGraph isn't populated",
                    role_name
                );
                return Vec::new();
            }
        };
        let documents = if search_query.is_multi_term_query() {
            // Use multi-term search with logical operators
            let all_terms: Vec<&str> = search_query
                .get_all_terms()
                .iter()
                .map(|t| t.as_str())
                .collect();
            let operator = search_query.get_operator();

            log::debug!(
                "Performing multi-term search with {} terms using {:?} operator",
                all_terms.len(),
                operator
            );

            role.query_graph_with_operators(
                &all_terms,
                &operator,
                search_query.skip,
                search_query.limit,
            )
            .unwrap_or_else(|e| {
                log::error!(
                    "Error while searching graph with operators for documents: {:?}",
                    e
                );
                vec![]
            })
        } else if search_query.include_pinned {
            // Use trigger fallback path which supports include_pinned
            role.query_graph_with_trigger_fallback(
                search_query.search_term.as_str(),
                search_query.skip,
                search_query.limit,
                true,
            )
            .unwrap_or_else(|e| {
                log::error!(
                    "Error while searching graph with trigger fallback for documents: {:?}",
                    e
                );
                vec![]
            })
        } else {
            // Use single-term search (backward compatibility)
            role.query_graph(
                search_query.search_term.as_str(),
                search_query.skip,
                search_query.limit,
            )
            .unwrap_or_else(|e| {
                log::error!("Error while searching graph for documents: {:?}", e);
                vec![]
            })
        };

        documents.into_iter().map(|(_id, doc)| doc).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::tempfile;
    use terraphim_test_utils::EnvVarGuard;
    use tokio::test;

    #[test]
    async fn test_write_config_to_json() {
        let config = Config::empty();
        let json_str = serde_json::to_string_pretty(&config).unwrap();

        let mut tempfile = tempfile().unwrap();
        tempfile.write_all(json_str.as_bytes()).unwrap();
    }

    #[test]
    async fn test_get_key() {
        let config = Config::empty();
        serde_json::to_string_pretty(&config).unwrap();
        assert!(config.get_key().ends_with(".json"));
    }

    #[tokio::test]
    async fn test_save_all() {
        // Force in-memory persistence to avoid external/backing store locks in CI
        terraphim_persistence::DeviceStorage::init_memory_only()
            .await
            .unwrap();
        let config = Config::empty();
        config.save().await.unwrap();
    }

    #[tokio::test]
    async fn test_save_one_s3() {
        // Force in-memory persistence to avoid external/backing store locks in CI
        terraphim_persistence::DeviceStorage::init_memory_only()
            .await
            .unwrap();
        let config = Config::empty();
        println!("{:#?}", config);
        match config.save_to_one("s3").await {
            Ok(_) => println!("Successfully saved to s3 (env provides s3 profile)"),
            Err(e) => {
                println!(
                    "Expected error saving to s3 in test environment without s3 profile: {:?}",
                    e
                );
                // Acceptable in CI: no s3 profile available when using memory-only init
            }
        }
    }

    #[tokio::test]
    async fn load_s3() {
        let mut config = Config::empty();
        match config.load().await {
            Ok(loaded_config) => {
                println!("Successfully loaded config: {:#?}", loaded_config);
            }
            Err(e) => {
                println!(
                    "Expected error loading config (no S3 data in test environment): {:?}",
                    e
                );
                // This is expected in test environment where S3 data doesn't exist
            }
        }
    }

    #[tokio::test]
    async fn test_save_one_memory() {
        // Try to force in-memory persistence; may be a no-op if another test
        // already initialized the global singleton with different profiles.
        let _ = terraphim_persistence::DeviceStorage::init_memory_only().await;
        let config = Config::empty();
        match config.save_to_one("memory").await {
            Ok(_) => println!("Successfully saved to memory profile"),
            Err(_) => {
                // Memory profile not available (global storage initialized with
                // different profiles by another test). Save to first available profile.
                config.save().await.unwrap();
            }
        }
    }

    #[test]
    async fn test_write_config_to_toml() {
        let config = Config::empty();
        let toml = toml::to_string_pretty(&config).unwrap();
        // Ensure that the toml is valid
        toml::from_str::<Config>(&toml).unwrap();
    }

    #[tokio::test]
    async fn test_config_builder() {
        let automata_remote = AutomataPath::from_remote(
            "https://staging-storage.terraphim.io/thesaurus_Default.json",
        )
        .unwrap();
        let config = ConfigBuilder::new()
            .global_shortcut("Ctrl+X")
            .add_role("Default", {
                let mut default_role = Role::new("Default");
                default_role.shortname = Some("Default".to_string());
                default_role.theme = "spacelab".to_string();
                default_role.haystacks = vec![Haystack {
                    location: "localsearch".to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                default_role
            })
            .add_role("Engineer", {
                let mut engineer_role = Role::new("Engineer");
                engineer_role.shortname = Some("Engineer".to_string());
                engineer_role.theme = "lumen".to_string();
                engineer_role.haystacks = vec![Haystack {
                    location: "localsearch".to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                engineer_role
            })
            .add_role("System Operator", {
                let mut system_operator_role = Role::new("System Operator");
                system_operator_role.shortname = Some("operator".to_string());
                system_operator_role.relevance_function = RelevanceFunction::TerraphimGraph;
                system_operator_role.terraphim_it = true;
                system_operator_role.theme = "superhero".to_string();
                system_operator_role.kg = Some(KnowledgeGraph {
                    automata_path: Some(automata_remote.clone()),
                    knowledge_graph_local: Some(KnowledgeGraphLocal {
                        input_type: KnowledgeGraphInputType::Markdown,
                        path: PathBuf::from("~/pkm"),
                    }),
                    public: true,
                    publish: true,
                });
                system_operator_role.haystacks = vec![Haystack {
                    location: "/tmp/system_operator/pages/".to_string(),
                    service: ServiceType::Ripgrep,
                    read_only: false,
                    fetch_content: false,
                    atomic_server_secret: None,
                    extra_parameters: std::collections::HashMap::new(),
                }];
                system_operator_role
            })
            .default_role("Default")
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(config.roles.len(), 3);
        assert_eq!(config.default_role, RoleName::new("Default"));
    }

    #[test]
    async fn test_update_global_shortcut() {
        let config = ConfigBuilder::new()
            .add_role("dummy", dummy_role())
            .build()
            .unwrap();
        assert_eq!(config.global_shortcut, None);
        let device_settings = DeviceSettings::new();
        let settings_path = PathBuf::from(".");
        let new_config = ConfigBuilder::from_config(config, device_settings, settings_path)
            .global_shortcut("Ctrl+/")
            .build()
            .unwrap();

        assert_eq!(new_config.global_shortcut, Some("Ctrl+/".to_string()));
    }

    fn dummy_role() -> Role {
        let mut role = Role::new("Father");
        role.shortname = Some("father".into());
        role.theme = "lumen".to_string();
        role.kg = Some(KnowledgeGraph {
            automata_path: Some(AutomataPath::local_example()),
            knowledge_graph_local: None,
            public: true,
            publish: true,
        });
        role.haystacks = vec![Haystack {
            location: "localsearch".to_string(),
            service: ServiceType::Ripgrep,
            read_only: false,
            fetch_content: false,
            atomic_server_secret: None,
            extra_parameters: std::collections::HashMap::new(),
        }];
        role
    }

    #[test]
    async fn test_add_role() {
        // Create a new role by building a new config
        let config = ConfigBuilder::new()
            .add_role("Father", dummy_role())
            .build()
            .unwrap();

        assert!(config.roles.contains_key(&RoleName::new("Father")));
        assert_eq!(config.roles.len(), 1);
        assert_eq!(&config.default_role, &RoleName::new("Father"));
        assert_eq!(config.roles[&RoleName::new("Father")], dummy_role());
    }

    ///test to create config with different id - server, desktop, embedded
    #[tokio::test]
    async fn test_config_with_id_desktop() {
        let config = match ConfigBuilder::new_with_id(ConfigId::Desktop).build() {
            Ok(mut config) => match config.load().await {
                Ok(config) => config,
                Err(e) => {
                    log::info!("Failed to load config: {:?}", e);

                    ConfigBuilder::new()
                        .build_default_desktop()
                        .build()
                        .unwrap()
                }
            },
            Err(e) => panic!("Failed to build config: {:?}", e),
        };
        assert_eq!(config.id, ConfigId::Desktop);
    }
    /// repeat the test with server and embedded
    #[tokio::test]
    async fn test_config_with_id_server() {
        let config = match ConfigBuilder::new_with_id(ConfigId::Server).build() {
            Ok(mut local_config) => match local_config.load().await {
                Ok(config) => config,
                Err(e) => {
                    log::info!("Failed to load config: {:?}", e);

                    ConfigBuilder::new().build_default_server().build().unwrap()
                }
            },
            Err(e) => panic!("Failed to build config: {:?}", e),
        };
        assert_eq!(config.id, ConfigId::Server);
    }

    #[tokio::test]
    async fn test_config_with_id_embedded() {
        let config = match ConfigBuilder::new_with_id(ConfigId::Embedded).build() {
            Ok(mut config) => match config.load().await {
                Ok(config) => config,
                Err(e) => {
                    log::info!("Failed to load config: {:?}", e);

                    ConfigBuilder::new()
                        .build_default_embedded()
                        .build()
                        .unwrap()
                }
            },
            Err(e) => panic!("Failed to build config: {:?}", e),
        };
        assert_eq!(config.id, ConfigId::Embedded);
    }

    #[tokio::test]
    #[ignore]
    async fn test_at_least_one_role() {
        let config = ConfigBuilder::new().build();
        assert!(config.is_err());
        assert!(matches!(config.unwrap_err(), TerraphimConfigError::NoRoles));
    }

    #[tokio::test]
    async fn test_json_serialization() {
        let config = Config::default();
        let json = serde_json::to_string_pretty(&config).unwrap();
        log::debug!("Config: {:#?}", config);
        assert!(!json.is_empty());
    }

    #[tokio::test]
    async fn test_toml_serialization() {
        let config = Config::default();
        let toml = toml::to_string_pretty(&config).unwrap();
        log::debug!("Config: {:#?}", config);
        assert!(!toml.is_empty());
    }

    #[tokio::test]
    async fn test_expand_path_home() {
        let home = dirs::home_dir().expect("HOME should be set");
        let home_str = home.to_string_lossy();

        // Test ${HOME} expansion
        let result = expand_path("${HOME}/.terraphim");
        assert_eq!(result, home.join(".terraphim"));

        // Test $HOME expansion
        let result = expand_path("$HOME/.terraphim");
        assert_eq!(result, home.join(".terraphim"));

        // Test ~ expansion
        let result = expand_path("~/.terraphim");
        assert_eq!(result, home.join(".terraphim"));

        // Test nested ${VAR:-default} with ${HOME}
        let result = expand_path("${TERRAPHIM_DATA_PATH:-${HOME}/.terraphim}");
        assert_eq!(result, home.join(".terraphim"));

        // Test when env var is set
        let _guard = EnvVarGuard::set("TERRAPHIM_TEST_PATH", "/custom/path");
        let result = expand_path("${TERRAPHIM_TEST_PATH:-${HOME}/.default}");
        assert_eq!(result, PathBuf::from("/custom/path"));
        drop(_guard);

        println!("expand_path tests passed!");
        println!("HOME = {}", home_str);
        println!(
            "${{HOME}}/.terraphim -> {:?}",
            expand_path("${HOME}/.terraphim")
        );
    }

    #[test]
    async fn test_load_from_json_file_with_fixture() {
        // Build path relative to workspace root
        let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .to_path_buf();
        let fixture_path =
            workspace_root.join("terraphim_server/default/terraphim_engineer_config.json");
        let config = Config::load_from_json_file(fixture_path.to_str().unwrap()).unwrap();
        assert!(
            !config.roles.is_empty(),
            "Config should have at least one role"
        );
        assert!(
            config
                .roles
                .contains_key(&RoleName::new("Terraphim Engineer")),
            "Config should contain Terraphim Engineer role"
        );
    }

    #[test]
    async fn test_load_from_json_file_not_found() {
        let result = Config::load_from_json_file("/nonexistent/path/does_not_exist.json");
        assert!(result.is_err(), "Should return error for missing file");
    }

    #[test]
    async fn test_load_from_json_file_invalid_json() {
        // Create a temp file with invalid JSON
        let mut tmpfile = tempfile().unwrap();
        tmpfile.write_all(b"this is not json").unwrap();
        // We can't easily get a path from tempfile(), so test with a known bad path pattern
        // The real test is that the error type is correct
        let result = Config::load_from_json_file("/dev/null");
        assert!(
            result.is_err(),
            "Should return error for empty/invalid JSON"
        );
    }

    #[test]
    async fn role_llm_api_key_redacted_in_debug() {
        let role = Role {
            llm_api_key: Some("super-secret-api-key-do-not-leak".to_string()),
            ..Default::default()
        };
        let dbg = format!("{:?}", role);
        assert!(
            !dbg.contains("super-secret-api-key-do-not-leak"),
            "llm_api_key must be redacted in Debug output, got: {dbg}"
        );
        assert!(
            dbg.contains("***REDACTED***"),
            "Debug output should mark llm_api_key as redacted, got: {dbg}"
        );
    }

    #[test]
    async fn role_none_llm_api_key_debug_shows_none() {
        let role = Role::default();
        let dbg = format!("{:?}", role);
        assert!(
            dbg.contains("llm_api_key: None"),
            "None llm_api_key should show as None in Debug, got: {dbg}"
        );
    }

    #[test]
    async fn haystack_atomic_server_secret_redacted_in_debug() {
        let mut haystack =
            Haystack::new("http://example.com".to_string(), ServiceType::Atomic, false);
        haystack.atomic_server_secret = Some("atomic-secret-do-not-leak".to_string());
        let dbg = format!("{:?}", haystack);
        assert!(
            !dbg.contains("atomic-secret-do-not-leak"),
            "atomic_server_secret must be redacted in Debug output, got: {dbg}"
        );
        assert!(
            dbg.contains("***REDACTED***"),
            "Debug output should mark atomic_server_secret as redacted, got: {dbg}"
        );
    }

    #[test]
    async fn haystack_none_atomic_server_secret_debug_shows_none() {
        let haystack = Haystack::new(
            "http://example.com".to_string(),
            ServiceType::Ripgrep,
            false,
        );
        let dbg = format!("{:?}", haystack);
        assert!(
            dbg.contains("atomic_server_secret: None"),
            "None atomic_server_secret should show as None in Debug, got: {dbg}"
        );
    }
}