helix_db/helix_engine/graph_core/
config.rs

1use crate::helix_engine::types::GraphError;
2use std::{
3    path::PathBuf,
4    fmt,
5};
6use serde::{Deserialize, Serialize};
7
8#[derive(Serialize, Deserialize, Debug)]
9pub struct VectorConfig {
10    pub m: Option<usize>,
11    pub ef_construction: Option<usize>,
12    pub ef_search: Option<usize>,
13}
14
15#[derive(Serialize, Deserialize, Debug)]
16pub struct GraphConfig {
17    pub secondary_indices: Option<Vec<String>>,
18}
19
20#[derive(Serialize, Deserialize, Debug)]
21pub struct Config {
22    pub vector_config: VectorConfig,
23    pub graph_config: GraphConfig,
24    pub db_max_size_gb: Option<usize>,
25    pub mcp: bool,
26    pub bm25: bool,
27    pub schema: Option<String>,
28    pub embedding_model: Option<String>,
29    pub graphvis_node_label: Option<String>,
30}
31
32impl Config {
33    pub fn new(
34        m: usize,
35        ef_construction: usize,
36        ef_search: usize,
37        db_max_size_gb: usize,
38        mcp: bool,
39        bm25: bool,
40        schema: Option<String>,
41        embedding_model: Option<String>,
42        graphvis_node_label: Option<String>,
43    ) -> Self {
44        Self {
45            vector_config: VectorConfig {
46                m: Some(m),
47                ef_construction: Some(ef_construction),
48                ef_search: Some(ef_search),
49            },
50            graph_config: GraphConfig {
51                secondary_indices: None,
52            },
53            db_max_size_gb: Some(db_max_size_gb),
54            mcp,
55            bm25,
56            schema,
57            embedding_model,
58            graphvis_node_label,
59        }
60    }
61
62    pub fn from_files(config_path: PathBuf, schema_path:PathBuf) -> Result<Self, GraphError> {
63        if !config_path.exists() {
64            println!("no config path!");
65            return Err(GraphError::ConfigFileNotFound);
66        }
67
68        let config = std::fs::read_to_string(config_path)?;
69        let mut config = sonic_rs::from_str::<Config>(&config)?;
70
71        if schema_path.exists() {
72            let schema_string = std::fs::read_to_string(schema_path)?;
73            config.schema = Some(schema_string);
74        } else {
75            config.schema = None;
76        }
77
78        Ok(config)
79    }
80
81    pub fn init_config() -> String {
82    r#"
83    {
84        "vector_config": {
85            "m": 16,
86            "ef_construction": 128,
87            "ef_search": 768
88        },
89        "graph_config": {
90            "secondary_indices": []
91        },
92        "db_max_size_gb": 10,
93        "mcp": true,
94        "bm25": true,
95        "embedding_model": "text-embedding-ada-002",
96        "graphvis_node_label": ""
97    }
98    "#
99    .to_string()
100    }
101
102    pub fn to_json(&self) -> String {
103        sonic_rs::to_string_pretty(self).unwrap()
104    }
105}
106
107impl Default for Config {
108    fn default() -> Self {
109        Self {
110            vector_config: VectorConfig {
111                m: Some(16),
112                ef_construction: Some(128),
113                ef_search: Some(768),
114            },
115            graph_config: GraphConfig {
116                secondary_indices: None,
117            },
118            db_max_size_gb: Some(10),
119            mcp: true,
120            bm25: true,
121            schema: None,
122            embedding_model: Some("text-embedding-ada-002".to_string()),
123            graphvis_node_label: None,
124        }
125    }
126}
127
128impl fmt::Display for Config {
129    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130        write!(
131            f,
132            "Vector config => m: {:?}, ef_construction: {:?}, ef_search: {:?}\n
133            Graph config => secondary_indicies: {:?}\n
134            db_max_size_gb: {:?}\n
135            mcp: {:?}\n
136            bm25: {:?}\n
137            schema: {:?}\n
138            embedding_model: {:?}\n
139            graphvis_node_label: {:?}",
140            self.vector_config.m,
141            self.vector_config.ef_construction,
142            self.vector_config.ef_search,
143            self.graph_config.secondary_indices,
144            self.db_max_size_gb,
145            self.mcp,
146            self.bm25,
147            self.schema,
148            self.embedding_model,
149            self.graphvis_node_label,
150        )
151    }
152}
153