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