1use crate::context::{assemble_context, ContextOptions};
7use crate::error::{BrainError, Result};
8use crate::exporter::{BrainExporter, BrainImporter};
9use crate::indexer::WorkspaceIndexer;
10use crate::query::{QueryOptions, RankedHit};
11use crate::storage::Database;
12use crate::types::{ContextBundle, Node, SyncStats};
13use std::path::{Path, PathBuf};
14
15pub struct Brain {
37 workspace: PathBuf,
38 brain_dir: PathBuf,
39 db: Database,
40}
41
42impl Brain {
43 pub fn create(workspace: impl AsRef<Path>) -> Result<Self> {
52 let workspace = canonicalize_or_owned(workspace.as_ref())?;
53 let brain_dir = workspace.join(".brain");
54 std::fs::create_dir_all(&brain_dir)?;
55 let db_path = brain_dir.join("db.sqlite");
56 let db = Database::open(&db_path)?;
57 let marker = brain_dir.join("workspace.json");
58 if !marker.exists() {
59 let meta = serde_json::json!({
60 "version": 1,
61 "workspace": workspace.to_string_lossy(),
62 });
63 std::fs::write(&marker, serde_json::to_string_pretty(&meta)?)?;
64 }
65 Ok(Self {
66 workspace,
67 brain_dir,
68 db,
69 })
70 }
71
72 pub fn open(workspace: impl AsRef<Path>) -> Result<Self> {
78 let workspace = canonicalize_or_owned(workspace.as_ref())?;
79 let brain_dir = workspace.join(".brain");
80 let db_path = brain_dir.join("db.sqlite");
81 if !db_path.exists() {
82 return Err(BrainError::BrainNotFound { path: brain_dir });
83 }
84 let db = Database::open(&db_path)?;
85 Ok(Self {
86 workspace,
87 brain_dir,
88 db,
89 })
90 }
91
92 pub fn open_or_create(workspace: impl AsRef<Path>) -> Result<Self> {
94 let workspace = workspace.as_ref();
95 let db_path = workspace.join(".brain").join("db.sqlite");
96 if db_path.exists() {
97 Self::open(workspace)
98 } else {
99 Self::create(workspace)
100 }
101 }
102
103 pub fn workspace(&self) -> &Path {
105 &self.workspace
106 }
107
108 pub fn brain_dir(&self) -> &Path {
110 &self.brain_dir
111 }
112
113 pub fn database(&self) -> &Database {
115 &self.db
116 }
117
118 pub fn sync(&mut self) -> Result<SyncStats> {
124 let db_path = self.brain_dir.join("db.sqlite");
125 let db = Database::open(&db_path)?;
126 let indexer = WorkspaceIndexer::new(db, self.workspace.clone());
127 let stats = indexer.index_workspace()?;
128 self.db = Database::open(&db_path)?;
130 Ok(stats)
131 }
132
133 pub fn query(&self, q: &str) -> Result<Vec<Node>> {
137 let hits = self.query_ranked(q, &QueryOptions::default())?;
138 Ok(hits.into_iter().map(|h| h.node).collect())
139 }
140
141 pub fn query_ranked(&self, q: &str, opts: &QueryOptions) -> Result<Vec<RankedHit>> {
145 self.db.search_ranked(q, opts)
146 }
147
148 pub fn context_for_prompt(&self, prompt: &str, max_tokens: usize) -> Result<ContextBundle> {
153 let opts = ContextOptions {
154 max_tokens,
155 ..ContextOptions::default()
156 };
157 self.context_for_prompt_with(prompt, &opts)
158 }
159
160 pub fn context_for_prompt_with(
165 &self,
166 prompt: &str,
167 opts: &ContextOptions,
168 ) -> Result<ContextBundle> {
169 assemble_context(&self.db, &self.brain_dir, prompt, opts)
170 }
171
172 pub fn export(&self, out: impl AsRef<Path>, decouple_ast: bool) -> Result<()> {
177 BrainExporter::export_bundle(&self.db, out, decouple_ast)
178 }
179
180 pub fn import(&mut self, input: impl AsRef<Path>) -> Result<usize> {
188 let n = BrainImporter::import_bundle(&self.db, input)?;
189 #[cfg(feature = "mmap")]
190 {
191 let indexer = WorkspaceIndexer::new(
192 Database::open(self.brain_dir.join("db.sqlite"))?,
193 self.workspace.clone(),
194 );
195 let _ = indexer.compile_mmap(&self.brain_dir.join("graph.mmap"));
196 self.db = Database::open(self.brain_dir.join("db.sqlite"))?;
197 }
198 Ok(n)
199 }
200
201 pub fn watch(&self, debounce_ms: u64) -> Result<()> {
207 crate::watch::watch_workspace(
208 &self.workspace,
209 crate::watch::WatchConfig {
210 debounce: std::time::Duration::from_millis(debounce_ms),
211 verbose: true,
212 },
213 )
214 }
215}
216
217fn canonicalize_or_owned(path: &Path) -> Result<PathBuf> {
218 if path.exists() {
219 Ok(fs_canonicalize(path)?)
220 } else {
221 std::fs::create_dir_all(path)?;
222 Ok(fs_canonicalize(path)?)
223 }
224}
225
226fn fs_canonicalize(path: &Path) -> Result<PathBuf> {
227 std::fs::canonicalize(path).map_err(BrainError::from)
228}
229
230#[cfg(test)]
231mod tests {
232 use super::*;
233 use crate::types::ContextRole;
234 use tempfile::tempdir;
235
236 #[test]
237 fn create_sync_query_context_export() {
238 let dir = tempdir().unwrap();
239 let docs = dir.path().join("docs");
240 std::fs::create_dir_all(&docs).unwrap();
241 std::fs::write(
242 docs.join("raft.md"),
243 "---\ntags: [raft]\nnode_type: concept\n---\n# Raft\nSee [[logcompaction]].\n",
244 )
245 .unwrap();
246 std::fs::write(
247 docs.join("logcompaction.md"),
248 "---\ntags: [log]\nnode_type: concept\n---\n# Log Compaction\nSee [[raft]].\n",
249 )
250 .unwrap();
251
252 let mut brain = Brain::create(dir.path()).unwrap();
253 let stats = brain.sync().unwrap();
254 assert_eq!(stats.markdown_files, 2);
255
256 let hits = brain.query("raft").unwrap();
257 assert!(!hits.is_empty());
258
259 let ranked = brain
260 .query_ranked("raft", &QueryOptions::default())
261 .unwrap();
262 assert!(ranked[0].score > 0.0);
263
264 let ctx = brain.context_for_prompt("raft", 512).unwrap();
265 assert!(
266 !ctx.nodes.is_empty(),
267 "expected FTS hits for 'raft', got none"
268 );
269 assert!(ctx.tokens_used > 0);
270 assert!(ctx.nodes.iter().any(|n| n.role == ContextRole::Seed));
271 let xml = ctx.to_xml();
272 assert!(xml.contains("<rustbrain_context"));
273 assert!(xml.contains("tokens_used="));
274
275 let out = dir.path().join("out.brainbundle");
276 brain.export(&out, true).unwrap();
277 assert!(out.exists());
278
279 let _ = brain.sync().unwrap();
280 assert_eq!(brain.database().count_fts_rows().unwrap(), 2);
281 }
282
283 #[test]
284 fn note_anchors_to_symbol() {
285 let dir = tempdir().unwrap();
286 let docs = dir.path().join("docs");
287 let src = dir.path().join("src");
288 std::fs::create_dir_all(&docs).unwrap();
289 std::fs::create_dir_all(&src).unwrap();
290 std::fs::write(
291 dir.path().join("Cargo.toml"),
292 "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
293 )
294 .unwrap();
295 std::fs::write(
296 src.join("lib.rs"),
297 "/// Storage engine\npub struct StorageEngine;\nimpl StorageEngine { pub fn open() {} }\n",
298 )
299 .unwrap();
300 std::fs::write(
301 docs.join("design.md"),
302 "---\nnode_type: adr\n---\n# Design\nUses symbol:StorageEngine for persistence.\n",
303 )
304 .unwrap();
305
306 let mut brain = Brain::create(dir.path()).unwrap();
307 let stats = brain.sync().unwrap();
308 assert!(stats.symbol_anchors >= 1);
309 assert!(stats.markdown_files >= 1);
310
311 let _ = brain.sync().unwrap();
312 let edges = brain.database().get_all_edges().unwrap();
313 let anchors: Vec<_> = edges
314 .iter()
315 .filter(|e| e.relation_type == "anchors")
316 .collect();
317 assert!(
318 !anchors.is_empty(),
319 "expected anchors edge note→symbol, edges={edges:?}"
320 );
321 }
322}