agentroot-core 0.1.0

Core library for agentroot - semantic search engine with AST-aware chunking and hybrid search
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
//! Document operations

use super::content::docid_from_hash;
use super::Database;
use crate::config::virtual_path::{is_virtual_path, parse_virtual_path};
use crate::error::Result;
use rusqlite::params;
use std::collections::HashMap;
use std::path::PathBuf;

/// Document record from database
#[derive(Debug, Clone)]
pub struct Document {
    pub id: i64,
    pub collection: String,
    pub path: String,
    pub title: String,
    pub hash: String,
    pub created_at: String,
    pub modified_at: String,
    pub active: bool,
    pub source_type: String,
    pub source_uri: Option<String>,
}

/// Document result with content
#[derive(Debug, Clone)]
pub struct DocumentResult {
    pub filepath: String,
    pub display_path: String,
    pub title: String,
    pub context: Option<String>,
    pub hash: String,
    pub docid: String,
    pub collection_name: String,
    pub modified_at: String,
    pub body_length: usize,
    pub body: Option<String>,
}

impl Database {
    /// Insert new document using struct parameters
    pub fn insert_doc(&self, doc: &DocumentInsert) -> Result<i64> {
        self.conn.execute(
            "INSERT INTO documents (
                collection, path, title, hash, created_at, modified_at, active, source_type, source_uri,
                llm_summary, llm_title, llm_keywords, llm_category, llm_intent, llm_concepts,
                llm_difficulty, llm_queries, llm_metadata_generated_at, llm_model
             )
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, 1, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18)",
            params![
                doc.collection,
                doc.path,
                doc.title,
                doc.hash,
                doc.created_at,
                doc.modified_at,
                doc.source_type,
                doc.source_uri,
                doc.llm_summary,
                doc.llm_title,
                doc.llm_keywords,
                doc.llm_category,
                doc.llm_intent,
                doc.llm_concepts,
                doc.llm_difficulty,
                doc.llm_queries,
                doc.llm_metadata_generated_at,
                doc.llm_model,
            ],
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Insert new document (legacy method)
    #[allow(clippy::too_many_arguments)]
    pub fn insert_document(
        &self,
        collection: &str,
        path: &str,
        title: &str,
        hash: &str,
        created_at: &str,
        modified_at: &str,
        source_type: &str,
        source_uri: Option<&str>,
    ) -> Result<i64> {
        let doc = DocumentInsert {
            collection,
            path,
            title,
            hash,
            created_at,
            modified_at,
            source_type,
            source_uri,
            llm_summary: None,
            llm_title: None,
            llm_keywords: None,
            llm_category: None,
            llm_intent: None,
            llm_concepts: None,
            llm_difficulty: None,
            llm_queries: None,
            llm_metadata_generated_at: None,
            llm_model: None,
        };
        self.insert_doc(&doc)
    }

    /// Update existing document (new content hash)
    pub fn update_document(
        &self,
        id: i64,
        title: &str,
        hash: &str,
        modified_at: &str,
    ) -> Result<()> {
        self.conn.execute(
            "UPDATE documents SET title = ?2, hash = ?3, modified_at = ?4 WHERE id = ?1",
            params![id, title, hash, modified_at],
        )?;
        Ok(())
    }

    /// Update document title only
    pub fn update_document_title(&self, id: i64, title: &str, modified_at: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE documents SET title = ?2, modified_at = ?3 WHERE id = ?1",
            params![id, title, modified_at],
        )?;
        Ok(())
    }

    /// Soft-delete document (set active = 0)
    pub fn deactivate_document(&self, collection: &str, path: &str) -> Result<bool> {
        let rows = self.conn.execute(
            "UPDATE documents SET active = 0 WHERE collection = ?1 AND path = ?2",
            params![collection, path],
        )?;
        Ok(rows > 0)
    }

    /// Find active document by collection and path
    pub fn find_active_document(&self, collection: &str, path: &str) -> Result<Option<Document>> {
        let result = self.conn.query_row(
            "SELECT id, collection, path, title, hash, created_at, modified_at, active, source_type, source_uri
             FROM documents WHERE collection = ?1 AND path = ?2 AND active = 1",
            params![collection, path],
            |row| {
                Ok(Document {
                    id: row.get(0)?,
                    collection: row.get(1)?,
                    path: row.get(2)?,
                    title: row.get(3)?,
                    hash: row.get(4)?,
                    created_at: row.get(5)?,
                    modified_at: row.get(6)?,
                    active: row.get::<_, i32>(7)? == 1,
                    source_type: row.get(8)?,
                    source_uri: row.get(9)?,
                })
            },
        );
        match result {
            Ok(doc) => Ok(Some(doc)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Get all active document paths in collection
    pub fn get_active_document_paths(&self, collection: &str) -> Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT path FROM documents WHERE collection = ?1 AND active = 1")?;
        let paths = stmt
            .query_map(params![collection], |row| row.get(0))?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(paths)
    }

    /// Find document by docid (hash prefix)
    pub fn find_by_docid(&self, docid: &str) -> Result<Option<DocumentResult>> {
        let docid = docid.trim_start_matches('#');
        let result = self.conn.query_row(
            "SELECT d.id, d.collection, d.path, d.title, d.hash, d.modified_at,
                    c.doc, LENGTH(c.doc)
             FROM documents d
             JOIN content c ON c.hash = d.hash
             WHERE d.hash LIKE ?1 || '%' AND d.active = 1
             LIMIT 1",
            params![docid],
            |row| {
                Ok(DocumentResult {
                    filepath: format!(
                        "agentroot://{}/{}",
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?
                    ),
                    display_path: format!(
                        "{}/{}",
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?
                    ),
                    title: row.get(3)?,
                    context: None,
                    hash: row.get(4)?,
                    docid: docid_from_hash(&row.get::<_, String>(4)?),
                    collection_name: row.get(1)?,
                    modified_at: row.get(5)?,
                    body: Some(row.get(6)?),
                    body_length: row.get(7)?,
                })
            },
        );
        match result {
            Ok(doc) => Ok(Some(doc)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Hard delete inactive documents
    pub fn delete_inactive_documents(&self) -> Result<usize> {
        let rows = self
            .conn
            .execute("DELETE FROM documents WHERE active = 0", [])?;
        Ok(rows)
    }

    /// Multi-lookup with fallback chain
    pub fn lookup_document(
        &self,
        query: &str,
        collections: &HashMap<String, PathBuf>,
    ) -> Result<Option<DocumentResult>> {
        let query = query.trim();

        // 1. Docid lookup
        if query.starts_with('#')
            || (query.len() == 6 && query.chars().all(|c| c.is_ascii_hexdigit()))
        {
            if let Some(doc) = self.find_by_docid(query)? {
                return Ok(Some(doc));
            }
        }

        // 2. Virtual path lookup
        if is_virtual_path(query) {
            if let Ok((collection, path)) = parse_virtual_path(query) {
                if let Some(doc) = self.find_active_document(&collection, &path)? {
                    return Ok(Some(self.document_to_result(&doc)?));
                }
            }
        }

        // 3. Absolute path with collection lookup
        let expanded = if query.starts_with("~/") {
            dirs::home_dir()
                .map(|home| home.join(&query[2..]).to_string_lossy().to_string())
                .unwrap_or_else(|| query.to_string())
        } else {
            query.to_string()
        };

        let abs_path = std::path::Path::new(&expanded);
        if abs_path.is_absolute() {
            for (coll_name, coll_path) in collections {
                if let Ok(rel_path) = abs_path.strip_prefix(coll_path) {
                    let path = rel_path.to_string_lossy().to_string();
                    if let Some(doc) = self.find_active_document(coll_name, &path)? {
                        return Ok(Some(self.document_to_result(&doc)?));
                    }
                }
            }
        }

        // 4. Fuzzy matching fallback
        let candidates = self.fuzzy_find_documents(query, 1)?;
        Ok(candidates.into_iter().next())
    }

    /// Fuzzy matching using simple contains + length
    pub fn fuzzy_find_documents(&self, query: &str, limit: usize) -> Result<Vec<DocumentResult>> {
        let query_lower = query.to_lowercase();
        let mut stmt = self.conn.prepare(
            "SELECT d.collection, d.path, d.title, d.hash, d.modified_at, c.doc, LENGTH(c.doc)
             FROM documents d
             JOIN content c ON c.hash = d.hash
             WHERE d.active = 1 AND (LOWER(d.path) LIKE '%' || ?1 || '%' OR LOWER(d.title) LIKE '%' || ?1 || '%')
             ORDER BY LENGTH(d.path)
             LIMIT ?2"
        )?;

        let results = stmt
            .query_map(params![query_lower, limit as i64], |row| {
                Ok(DocumentResult {
                    filepath: format!(
                        "agentroot://{}/{}",
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?
                    ),
                    display_path: format!(
                        "{}/{}",
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?
                    ),
                    title: row.get(2)?,
                    context: None,
                    hash: row.get(3)?,
                    docid: docid_from_hash(&row.get::<_, String>(3)?),
                    collection_name: row.get(0)?,
                    modified_at: row.get(4)?,
                    body: Some(row.get(5)?),
                    body_length: row.get(6)?,
                })
            })?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(results)
    }

    fn document_to_result(&self, doc: &Document) -> Result<DocumentResult> {
        let body = self.get_content(&doc.hash)?;
        let body_length = body.as_ref().map(|b| b.len()).unwrap_or(0);

        Ok(DocumentResult {
            filepath: format!("agentroot://{}/{}", doc.collection, doc.path),
            display_path: format!("{}/{}", doc.collection, doc.path),
            title: doc.title.clone(),
            context: None,
            hash: doc.hash.clone(),
            docid: docid_from_hash(&doc.hash),
            collection_name: doc.collection.clone(),
            modified_at: doc.modified_at.clone(),
            body_length,
            body,
        })
    }

    /// Get document content by query (docid, virtual path, etc)
    pub fn get_document(&self, query: &str) -> Result<String> {
        let query = query.trim();

        // Docid lookup
        if query.starts_with('#')
            || (query.len() == 6 && query.chars().all(|c| c.is_ascii_hexdigit()))
        {
            if let Some(doc) = self.find_by_docid(query)? {
                return doc.body.ok_or_else(|| {
                    crate::error::AgentRootError::DocumentNotFound(query.to_string())
                });
            }
        }

        // Virtual path lookup
        if is_virtual_path(query) {
            if let Ok((collection, path)) = parse_virtual_path(query) {
                if let Some(doc) = self.find_active_document(&collection, &path)? {
                    if let Some(content) = self.get_content(&doc.hash)? {
                        return Ok(content);
                    }
                }
            }
        }

        // Path prefix lookup (collection/path)
        if query.contains('/') {
            let parts: Vec<&str> = query.splitn(2, '/').collect();
            if parts.len() == 2 {
                if let Some(doc) = self.find_active_document(parts[0], parts[1])? {
                    if let Some(content) = self.get_content(&doc.hash)? {
                        return Ok(content);
                    }
                }
            }
        }

        Err(crate::error::AgentRootError::DocumentNotFound(
            query.to_string(),
        ))
    }

    /// List documents by prefix
    pub fn list_documents_by_prefix(&self, prefix: &str) -> Result<Vec<DocumentListItem>> {
        let prefix = prefix.trim_start_matches("agentroot://");
        let like_pattern = format!("{}%", prefix);

        let mut stmt = self.conn.prepare(
            "SELECT d.collection, d.path, d.title, d.hash
             FROM documents d
             WHERE d.active = 1 AND (d.collection || '/' || d.path) LIKE ?1
             ORDER BY d.collection, d.path",
        )?;

        let results = stmt
            .query_map(params![like_pattern], |row| {
                Ok(DocumentListItem {
                    path: format!("{}/{}", row.get::<_, String>(0)?, row.get::<_, String>(1)?),
                    title: row.get(2)?,
                    docid: docid_from_hash(&row.get::<_, String>(3)?),
                })
            })?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        Ok(results)
    }

    /// Get multiple documents by pattern
    pub fn get_documents_by_pattern(&self, pattern: &str) -> Result<Vec<DocumentContent>> {
        // Handle comma-separated list of docids
        if pattern.contains(',') {
            let mut results = Vec::new();
            for part in pattern.split(',') {
                let part = part.trim();
                if let Ok(content) = self.get_document(part) {
                    results.push(DocumentContent {
                        path: part.to_string(),
                        content,
                    });
                }
            }
            return Ok(results);
        }

        // Glob pattern matching
        let pattern = glob::Pattern::new(pattern)?;
        let mut stmt = self.conn.prepare(
            "SELECT d.collection, d.path, c.doc
             FROM documents d
             JOIN content c ON c.hash = d.hash
             WHERE d.active = 1",
        )?;

        let results = stmt
            .query_map([], |row| {
                let path = format!("{}/{}", row.get::<_, String>(0)?, row.get::<_, String>(1)?);
                Ok((path, row.get::<_, String>(2)?))
            })?
            .filter_map(|r| r.ok())
            .filter(|(path, _)| pattern.matches(path))
            .map(|(path, content)| DocumentContent { path, content })
            .collect();

        Ok(results)
    }
}

/// Document list item (for ls command)
#[derive(Debug, Clone, serde::Serialize)]
pub struct DocumentListItem {
    pub path: String,
    pub title: String,
    pub docid: String,
}

/// Document content (for multi-get)
#[derive(Debug, Clone)]
pub struct DocumentContent {
    pub path: String,
    pub content: String,
}

/// Document insert parameters
#[derive(Debug, Clone)]
pub struct DocumentInsert<'a> {
    pub collection: &'a str,
    pub path: &'a str,
    pub title: &'a str,
    pub hash: &'a str,
    pub created_at: &'a str,
    pub modified_at: &'a str,
    pub source_type: &'a str,
    pub source_uri: Option<&'a str>,
    pub llm_summary: Option<&'a str>,
    pub llm_title: Option<&'a str>,
    pub llm_keywords: Option<&'a str>,
    pub llm_category: Option<&'a str>,
    pub llm_intent: Option<&'a str>,
    pub llm_concepts: Option<&'a str>,
    pub llm_difficulty: Option<&'a str>,
    pub llm_queries: Option<&'a str>,
    pub llm_metadata_generated_at: Option<&'a str>,
    pub llm_model: Option<&'a str>,
}

impl<'a> DocumentInsert<'a> {
    /// Create new document insert parameters
    pub fn new(
        collection: &'a str,
        path: &'a str,
        title: &'a str,
        hash: &'a str,
        created_at: &'a str,
        modified_at: &'a str,
    ) -> Self {
        Self {
            collection,
            path,
            title,
            hash,
            created_at,
            modified_at,
            source_type: "file",
            source_uri: None,
            llm_summary: None,
            llm_title: None,
            llm_keywords: None,
            llm_category: None,
            llm_intent: None,
            llm_concepts: None,
            llm_difficulty: None,
            llm_queries: None,
            llm_metadata_generated_at: None,
            llm_model: None,
        }
    }

    /// Set source type
    pub fn with_source_type(mut self, source_type: &'a str) -> Self {
        self.source_type = source_type;
        self
    }

    /// Set source URI
    pub fn with_source_uri(mut self, source_uri: &'a str) -> Self {
        self.source_uri = Some(source_uri);
        self
    }

    /// Set LLM metadata fields from DocumentMetadata
    pub fn with_llm_metadata(
        mut self,
        metadata: &'a crate::llm::DocumentMetadata,
        _metadata_json: &'a str,
        model_name: &'a str,
        generated_at: &'a str,
    ) -> Self {
        self.llm_summary = Some(&metadata.summary);
        self.llm_title = Some(&metadata.semantic_title);
        self.llm_category = Some(&metadata.category);
        self.llm_intent = Some(&metadata.intent);
        self.llm_difficulty = Some(&metadata.difficulty);
        self.llm_metadata_generated_at = Some(generated_at);
        self.llm_model = Some(model_name);
        self
    }

    /// Set LLM metadata JSON strings (pre-serialized)
    pub fn with_llm_metadata_strings(
        mut self,
        summary: &'a str,
        title: &'a str,
        keywords: &'a str,
        category: &'a str,
        intent: &'a str,
        concepts: &'a str,
        difficulty: &'a str,
        queries: &'a str,
        model_name: &'a str,
        generated_at: &'a str,
    ) -> Self {
        self.llm_summary = Some(summary);
        self.llm_title = Some(title);
        self.llm_keywords = Some(keywords);
        self.llm_category = Some(category);
        self.llm_intent = Some(intent);
        self.llm_concepts = Some(concepts);
        self.llm_difficulty = Some(difficulty);
        self.llm_queries = Some(queries);
        self.llm_metadata_generated_at = Some(generated_at);
        self.llm_model = Some(model_name);
        self
    }
}