agentroot-core 0.1.1

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
577
578
579
580
581
582
//! LLaMA-based metadata generator using llama-cpp-2

use super::metadata_generator::{DocumentMetadata, MetadataContext, MetadataGenerator};
use crate::error::{AgentRootError, Result};
use crate::index::ast_chunker::ChunkType;
use async_trait::async_trait;
use llama_cpp_2::{
    context::params::LlamaContextParams,
    llama_backend::LlamaBackend,
    llama_batch::LlamaBatch,
    model::{params::LlamaModelParams, LlamaModel},
};
use std::path::Path;
use std::sync::Mutex;

/// Default metadata generation model
pub const DEFAULT_METADATA_MODEL: &str = "llama-3.1-8b-instruct.Q4_K_M.gguf";

/// Maximum tokens to send to LLM
const MAX_CONTENT_TOKENS: usize = 2048;

/// LLaMA-based metadata generator
pub struct LlamaMetadataGenerator {
    #[allow(dead_code)]
    backend: LlamaBackend,
    model: LlamaModel,
    context: Mutex<LlamaMetadataContext>,
    model_name: String,
}

struct LlamaMetadataContext {
    ctx: llama_cpp_2::context::LlamaContext<'static>,
}

unsafe impl Send for LlamaMetadataContext {}
unsafe impl Sync for LlamaMetadataContext {}

impl LlamaMetadataGenerator {
    /// Create a new LlamaMetadataGenerator from a GGUF model file
    pub fn new(model_path: impl AsRef<Path>) -> Result<Self> {
        let model_path = model_path.as_ref();
        let model_name = model_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown")
            .to_string();

        let mut backend = LlamaBackend::init()
            .map_err(|e| AgentRootError::Llm(format!("Failed to init backend: {}", e)))?;
        backend.void_logs();

        let model_params = LlamaModelParams::default();
        let model = LlamaModel::load_from_file(&backend, model_path, &model_params)
            .map_err(|e| AgentRootError::Llm(format!("Failed to load model: {}", e)))?;

        let ctx_size = std::num::NonZeroU32::new(4096).unwrap();
        let ctx_params = LlamaContextParams::default()
            .with_n_ctx(Some(ctx_size))
            .with_n_batch(ctx_size.get())
            .with_n_ubatch(ctx_size.get());

        let ctx = model
            .new_context(&backend, ctx_params)
            .map_err(|e| AgentRootError::Llm(format!("Failed to create context: {}", e)))?;

        let ctx: llama_cpp_2::context::LlamaContext<'static> = unsafe { std::mem::transmute(ctx) };

        Ok(Self {
            backend,
            model,
            context: Mutex::new(LlamaMetadataContext { ctx }),
            model_name,
        })
    }

    /// Create from default model location
    pub fn from_default() -> Result<Self> {
        let model_dir = dirs::data_dir()
            .unwrap_or_else(|| std::path::PathBuf::from("."))
            .join("agentroot")
            .join("models");

        let model_path = model_dir.join(DEFAULT_METADATA_MODEL);

        if !model_path.exists() {
            return Err(AgentRootError::ModelNotFound(format!(
                "Model not found at {}. Download an instruction-tuned model (e.g., llama-3.1-8b-instruct) to this location.",
                model_path.display()
            )));
        }

        Self::new(model_path)
    }

    /// Extract key sections from large documents using smart strategies
    fn extract_key_sections(&self, content: &str, context: &MetadataContext) -> String {
        let est_tokens = content.len() / 4;

        if est_tokens <= MAX_CONTENT_TOKENS {
            return content.to_string();
        }

        match context.file_extension.as_deref() {
            Some("md") | Some("markdown") => self.extract_markdown_sections(content),
            Some("rs") | Some("py") | Some("js") | Some("ts") | Some("go") => {
                self.extract_code_sections(content, context)
            }
            _ => self.extract_generic_sections(content),
        }
    }

    /// Extract key sections from markdown (headers + first paragraph of each section)
    fn extract_markdown_sections(&self, content: &str) -> String {
        let mut result = String::new();
        let lines: Vec<&str> = content.lines().collect();
        let mut i = 0;

        while i < lines.len() && result.len() < MAX_CONTENT_TOKENS * 4 {
            let line = lines[i];

            if line.starts_with('#') {
                result.push_str(line);
                result.push('\n');

                i += 1;
                while i < lines.len() && !lines[i].is_empty() {
                    result.push_str(lines[i]);
                    result.push('\n');
                    i += 1;
                    if result.len() >= MAX_CONTENT_TOKENS * 4 {
                        break;
                    }
                }
            } else if i == 0 && !line.is_empty() {
                result.push_str(line);
                result.push('\n');
            }
            i += 1;
        }

        if result.is_empty() {
            content.chars().take(MAX_CONTENT_TOKENS * 4).collect()
        } else {
            result
        }
    }

    /// Extract key sections from code (function/class signatures + docstrings)
    fn extract_code_sections(&self, content: &str, context: &MetadataContext) -> String {
        let mut result = String::new();
        let lines: Vec<&str> = content.lines().collect();

        for line in lines.iter().take(50) {
            result.push_str(line);
            result.push('\n');
        }

        if let Some(structure) = &context.existing_structure {
            result.push_str("\n\nStructure: ");
            let types: Vec<String> = structure.iter().map(|ct| ct.as_str().to_string()).collect();
            result.push_str(&types.join(", "));
        }

        if result.len() > MAX_CONTENT_TOKENS * 4 {
            result.truncate(MAX_CONTENT_TOKENS * 4);
        }

        result
    }

    /// Extract generic sections (first and last portions)
    fn extract_generic_sections(&self, content: &str) -> String {
        let half_size = (MAX_CONTENT_TOKENS * 4) / 2;
        let chars: Vec<char> = content.chars().collect();

        if chars.len() <= MAX_CONTENT_TOKENS * 4 {
            return content.to_string();
        }

        let first: String = chars.iter().take(half_size).collect();
        let last: String = chars.iter().skip(chars.len() - half_size).collect();

        format!("{}\n\n... [content truncated] ...\n\n{}", first, last)
    }

    /// Build prompt for metadata generation
    fn build_prompt(&self, content: &str, context: &MetadataContext) -> String {
        let structure_info = if let Some(structure) = &context.existing_structure {
            let types: Vec<String> = structure.iter().map(|ct| ct.as_str().to_string()).collect();
            format!("Code structures: {}", types.join(", "))
        } else {
            "No structural information available".to_string()
        };

        format!(
            r#"You are a document analysis assistant. Analyze the following document and provide comprehensive metadata.

DOCUMENT INFORMATION:
- Source: {}
- Language: {}
- Collection: {}
- File Type: {}
- {}

DOCUMENT CONTENT:
{}

Generate the following metadata as valid JSON:
1. summary: A 100-200 word summary of the main content and purpose
2. semantic_title: A clear, descriptive title (improve upon filename if needed)
3. keywords: Array of 5-10 relevant keywords/tags for search
4. category: Document category (choose one: tutorial, reference, configuration, test, documentation, code, research, api, guide, example)
5. intent: Brief description of why this document exists and what problem it solves
6. concepts: Array of key concepts, technologies, frameworks, or entities mentioned
7. difficulty: Target audience level (choose one: beginner, intermediate, advanced)
8. suggested_queries: Array of 3-5 search queries a user might type to find this document

Respond ONLY with valid JSON in this exact format (no markdown, no code blocks):
{{"summary":"...","semantic_title":"...","keywords":["..."],"category":"...","intent":"...","concepts":["..."],"difficulty":"...","suggested_queries":["..."]}}
"#,
            context.source_type,
            context.language.as_deref().unwrap_or("unknown"),
            context.collection_name,
            context.file_extension.as_deref().unwrap_or("unknown"),
            structure_info,
            content
        )
    }

    /// Parse LLM response into structured metadata
    fn parse_metadata_response(&self, response: &str) -> Result<DocumentMetadata> {
        let cleaned = response
            .trim()
            .trim_start_matches("```json")
            .trim_start_matches("```")
            .trim_end_matches("```")
            .trim();

        let start = cleaned.find('{').unwrap_or(0);
        let end = cleaned.rfind('}').map(|i| i + 1).unwrap_or(cleaned.len());
        let json_str = &cleaned[start..end];

        serde_json::from_str::<DocumentMetadata>(json_str).map_err(|e| {
            AgentRootError::Llm(format!(
                "Failed to parse metadata JSON: {}. Response was: {}",
                e, json_str
            ))
        })
    }

    /// Generate text using LLM
    fn generate_sync(&self, prompt: &str, max_tokens: usize) -> Result<String> {
        let mut ctx_guard = self
            .context
            .lock()
            .map_err(|e| AgentRootError::Llm(format!("Lock error: {}", e)))?;

        let tokens = self
            .model
            .str_to_token(prompt, llama_cpp_2::model::AddBos::Always)
            .map_err(|e| AgentRootError::Llm(format!("Tokenization error: {}", e)))?;

        if tokens.is_empty() {
            return Err(AgentRootError::Llm("Empty tokenization".to_string()));
        }

        let mut batch = LlamaBatch::new(tokens.len(), 1);

        for (i, token) in tokens.iter().enumerate() {
            batch
                .add(*token, i as i32, &[0], false)
                .map_err(|e| AgentRootError::Llm(format!("Batch error: {}", e)))?;
        }

        ctx_guard
            .ctx
            .decode(&mut batch)
            .map_err(|e| AgentRootError::Llm(format!("Decode error: {}", e)))?;

        let mut generated = String::new();
        let mut token_count = 0;

        loop {
            if token_count >= max_tokens {
                break;
            }

            let candidates: Vec<_> = ctx_guard.ctx.candidates().collect();
            if candidates.is_empty() {
                break;
            }

            let token = candidates[0].id();

            if token == self.model.token_eos() {
                break;
            }

            let token_str = self
                .model
                .token_to_str(token, llama_cpp_2::model::Special::Tokenize)
                .map_err(|e| AgentRootError::Llm(format!("Token to string error: {}", e)))?;

            generated.push_str(&token_str);
            token_count += 1;

            let mut new_batch = LlamaBatch::new(1, 1);
            new_batch
                .add(
                    token,
                    tokens.len() as i32 + token_count as i32 - 1,
                    &[0],
                    false,
                )
                .map_err(|e| AgentRootError::Llm(format!("Batch add error: {}", e)))?;

            ctx_guard
                .ctx
                .decode(&mut new_batch)
                .map_err(|e| AgentRootError::Llm(format!("Decode error: {}", e)))?;
        }

        Ok(generated)
    }

    /// Generate metadata using LLM with fallback
    async fn generate_with_fallback(
        &self,
        content: &str,
        context: &MetadataContext,
    ) -> DocumentMetadata {
        let truncated_content = self.extract_key_sections(content, context);
        let prompt = self.build_prompt(&truncated_content, context);

        match self.generate_sync(&prompt, 512) {
            Ok(response) => match self.parse_metadata_response(&response) {
                Ok(metadata) => metadata,
                Err(e) => {
                    eprintln!("Failed to parse LLM response: {}. Using fallback.", e);
                    self.generate_fallback_metadata(content, context)
                }
            },
            Err(e) => {
                eprintln!("LLM generation failed: {}. Using fallback.", e);
                self.generate_fallback_metadata(content, context)
            }
        }
    }

    /// Generate fallback metadata using heuristics
    fn generate_fallback_metadata(
        &self,
        content: &str,
        context: &MetadataContext,
    ) -> DocumentMetadata {
        let title = self.improve_title_from_path(&context.collection_name);
        let summary = self.extract_first_paragraph(content);
        let keywords = self.extract_keywords_basic(content);
        let category = self.infer_category_from_context(context);
        let concepts = self.extract_capitalized_terms(content);
        let difficulty = "intermediate".to_string();
        let suggested_queries = vec![title.clone()];

        DocumentMetadata {
            summary,
            semantic_title: title.clone(),
            keywords,
            category,
            intent: format!("Document from {} collection", context.collection_name),
            concepts,
            difficulty,
            suggested_queries,
        }
    }

    /// Improve title from file path
    fn improve_title_from_path(&self, path: &str) -> String {
        path.split('/')
            .next_back()
            .unwrap_or(path)
            .replace(['-', '_'], " ")
            .split_whitespace()
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Extract first paragraph as summary
    fn extract_first_paragraph(&self, content: &str) -> String {
        let mut summary = String::new();
        for line in content.lines() {
            let trimmed = line.trim();
            if !trimmed.is_empty() && !trimmed.starts_with('#') && !trimmed.starts_with("//") {
                summary.push_str(trimmed);
                summary.push(' ');
                if summary.len() > 200 {
                    break;
                }
            }
            if summary.len() > 100 && trimmed.is_empty() {
                break;
            }
        }
        if summary.is_empty() {
            content.chars().take(200).collect()
        } else {
            summary.truncate(200);
            summary
        }
    }

    /// Extract basic keywords from content
    fn extract_keywords_basic(&self, content: &str) -> Vec<String> {
        let words: Vec<&str> = content
            .split_whitespace()
            .filter(|w| w.len() > 4 && w.chars().all(|c| c.is_alphanumeric() || c == '_'))
            .take(50)
            .collect();

        let mut word_counts: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for word in words {
            *word_counts.entry(word.to_lowercase()).or_insert(0) += 1;
        }

        let mut sorted: Vec<_> = word_counts.into_iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(&a.1));

        sorted.into_iter().take(8).map(|(word, _)| word).collect()
    }

    /// Infer category from context
    fn infer_category_from_context(&self, context: &MetadataContext) -> String {
        if let Some(structure) = &context.existing_structure {
            if structure
                .iter()
                .any(|ct| matches!(ct, ChunkType::Function | ChunkType::Method))
            {
                return "code".to_string();
            }
        }

        match context.file_extension.as_deref() {
            Some("md") | Some("markdown") => "documentation".to_string(),
            Some("rs") | Some("py") | Some("js") | Some("ts") | Some("go") => "code".to_string(),
            Some("json") | Some("yaml") | Some("toml") | Some("ini") => "configuration".to_string(),
            Some("txt") => "documentation".to_string(),
            _ => "documentation".to_string(),
        }
    }

    /// Extract capitalized terms as concepts
    fn extract_capitalized_terms(&self, content: &str) -> Vec<String> {
        let mut concepts = std::collections::HashSet::new();

        for word in content.split_whitespace().take(200) {
            let clean: String = word.chars().filter(|c| c.is_alphanumeric()).collect();

            if clean.len() > 2 && clean.chars().next().is_some_and(|c| c.is_uppercase()) {
                concepts.insert(clean);
            }
        }

        concepts.into_iter().take(10).collect()
    }
}

#[async_trait]
impl MetadataGenerator for LlamaMetadataGenerator {
    async fn generate_metadata(
        &self,
        content: &str,
        context: &MetadataContext,
    ) -> Result<DocumentMetadata> {
        Ok(self.generate_with_fallback(content, context).await)
    }

    fn model_name(&self) -> &str {
        &self.model_name
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_improve_title_from_path() {
        let title = improve_title_from_path_standalone("my-test-file");
        assert_eq!(title, "My Test File");
        let title2 = improve_title_from_path_standalone("some_document");
        assert_eq!(title2, "Some Document");
    }

    #[test]
    fn test_extract_first_paragraph_standalone() {
        let content = "This is the first paragraph.\n\nThis is the second paragraph.";
        let result = extract_first_paragraph_standalone(content);
        assert!(result.contains("first paragraph"));
    }

    #[test]
    fn test_extract_keywords_basic_standalone() {
        let content = "testing testing hello world testing hello";
        let keywords = extract_keywords_basic_standalone(content);
        assert!(keywords.contains(&"testing".to_string()));
    }

    #[test]
    fn test_parse_metadata_json() {
        let json = r#"{"summary":"Test summary","semantic_title":"Test Title","keywords":["test","rust"],"category":"code","intent":"Testing","concepts":["Unit Testing"],"difficulty":"beginner","suggested_queries":["rust testing"]}"#;
        let metadata = serde_json::from_str::<DocumentMetadata>(json).unwrap();
        assert_eq!(metadata.semantic_title, "Test Title");
        assert_eq!(metadata.category, "code");
    }

    fn improve_title_from_path_standalone(path: &str) -> String {
        path.split('/')
            .last()
            .unwrap_or(path)
            .replace('-', " ")
            .replace('_', " ")
            .split_whitespace()
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn extract_first_paragraph_standalone(content: &str) -> String {
        let mut summary = String::new();
        for line in content.lines() {
            let trimmed = line.trim();
            if !trimmed.is_empty() && !trimmed.starts_with('#') && !trimmed.starts_with("//") {
                summary.push_str(trimmed);
                summary.push(' ');
                if summary.len() > 200 {
                    break;
                }
            }
            if summary.len() > 100 && trimmed.is_empty() {
                break;
            }
        }
        if summary.is_empty() {
            content.chars().take(200).collect()
        } else {
            summary.truncate(200);
            summary
        }
    }

    fn extract_keywords_basic_standalone(content: &str) -> Vec<String> {
        let words: Vec<&str> = content
            .split_whitespace()
            .filter(|w| w.len() > 4 && w.chars().all(|c| c.is_alphanumeric() || c == '_'))
            .take(50)
            .collect();

        let mut word_counts: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for word in words {
            *word_counts.entry(word.to_lowercase()).or_insert(0) += 1;
        }

        let mut sorted: Vec<_> = word_counts.into_iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(&a.1));

        sorted.into_iter().take(8).map(|(word, _)| word).collect()
    }
}