liteforge 0.2.5

Rust SDK for LiteForge - LLM completions via OpenAI-compatible API
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
//! Local in-memory knowledge backend for development and testing.

use super::client::KnowledgeClient;
use super::types::{Document, KnowledgeStats, ListOptions, SearchOptions, SearchResult};
use crate::error::ForgeError;
use async_trait::async_trait;
use std::collections::{HashMap, HashSet};
use std::sync::RwLock;
use std::time::{SystemTime, UNIX_EPOCH};

/// An in-memory knowledge backend for development and testing.
///
/// Uses simple TF-IDF-like text matching for search.
/// Not suitable for production use with large document sets.
///
/// # Example
///
/// ```
/// use liteforge::knowledge::{LocalKnowledgeBackend, Document, SearchOptions, KnowledgeClient};
///
/// #[tokio::main]
/// async fn main() {
///     let backend = LocalKnowledgeBackend::new();
///
///     // Upload documents
///     let docs = vec![
///         Document::new("1", "Rust is a systems programming language"),
///         Document::new("2", "Python is great for data science"),
///     ];
///     backend.upload(docs).await.unwrap();
///
///     // Search
///     let results = backend.search("rust programming", SearchOptions::new().limit(5)).await.unwrap();
///     println!("Found {} results", results.len());
/// }
/// ```
pub struct LocalKnowledgeBackend {
    documents: RwLock<HashMap<String, Document>>,
}

impl LocalKnowledgeBackend {
    /// Create a new empty local backend.
    pub fn new() -> Self {
        Self {
            documents: RwLock::new(HashMap::new()),
        }
    }

    /// Create a local backend with initial documents.
    pub fn with_documents(documents: Vec<Document>) -> Self {
        let mut map = HashMap::new();
        for doc in documents {
            map.insert(doc.id.clone(), doc);
        }
        Self {
            documents: RwLock::new(map),
        }
    }

    /// Get the current timestamp.
    fn now() -> i64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0)
    }

    /// Simple text search scoring using term frequency.
    fn score_document(query: &str, document: &Document) -> f32 {
        let query_lower = query.to_lowercase();
        let query_terms: HashSet<&str> = query_lower.split_whitespace().collect();

        if query_terms.is_empty() {
            return 0.0;
        }

        let content_lower = document.content.to_lowercase();
        let content_words: Vec<&str> = content_lower.split_whitespace().collect();

        let mut matches = 0;
        let mut exact_matches = 0;

        for term in &query_terms {
            // Count occurrences
            let term_count = content_words.iter().filter(|w| w.contains(term)).count();
            matches += term_count;

            // Bonus for exact word matches
            if content_words.contains(term) {
                exact_matches += 1;
            }
        }

        if matches == 0 {
            return 0.0;
        }

        // Score based on:
        // 1. Number of matching terms
        // 2. Frequency of matches
        // 3. Document length normalization
        let term_coverage = exact_matches as f32 / query_terms.len() as f32;
        let frequency_score = matches as f32 / content_words.len().max(1) as f32;

        // Combine scores (term coverage weighted higher)
        term_coverage * 0.7 + frequency_score * 0.3
    }

    /// Extract highlighted snippets from document.
    fn extract_highlights(query: &str, document: &Document, max_highlights: usize) -> Vec<String> {
        let query_lower = query.to_lowercase();
        let query_terms: Vec<&str> = query_lower.split_whitespace().collect();
        if query_terms.is_empty() {
            return Vec::new();
        }

        let sentences: Vec<&str> = document
            .content
            .split(['.', '!', '?'])
            .filter(|s| !s.trim().is_empty())
            .collect();

        let mut scored_sentences: Vec<(f32, String)> = sentences
            .iter()
            .map(|s| {
                let sentence_lower = s.to_lowercase();
                let score = query_terms
                    .iter()
                    .filter(|term| sentence_lower.contains(*term))
                    .count() as f32;
                (score, s.trim().to_string())
            })
            .filter(|(score, _)| *score > 0.0)
            .collect();

        scored_sentences.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
        scored_sentences.truncate(max_highlights);

        scored_sentences.into_iter().map(|(_, s)| s).collect()
    }

    /// Check if a document matches the metadata filters.
    fn matches_filters(document: &Document, filters: &HashMap<String, serde_json::Value>) -> bool {
        for (key, expected) in filters {
            match document.metadata.get(key) {
                Some(actual) if actual == expected => continue,
                _ => return false,
            }
        }
        true
    }
}

impl Default for LocalKnowledgeBackend {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl KnowledgeClient for LocalKnowledgeBackend {
    async fn search(
        &self,
        query: &str,
        options: SearchOptions,
    ) -> Result<Vec<SearchResult>, ForgeError> {
        let documents = self
            .documents
            .read()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        let mut results: Vec<SearchResult> = documents
            .values()
            .filter(|doc| {
                // Filter by namespace
                if let Some(ref ns) = options.namespace {
                    if doc.namespace.as_ref() != Some(ns) {
                        return false;
                    }
                }
                // Filter by metadata
                if !Self::matches_filters(doc, &options.filters) {
                    return false;
                }
                true
            })
            .map(|doc| {
                let score = Self::score_document(query, doc);
                let highlights = if options.include_highlights {
                    Self::extract_highlights(query, doc, 3)
                } else {
                    Vec::new()
                };
                SearchResult {
                    document: doc.clone(),
                    score,
                    highlights,
                }
            })
            .filter(|r| {
                // Filter by minimum score
                if let Some(min) = options.min_score {
                    r.score >= min
                } else {
                    r.score > 0.0
                }
            })
            .collect();

        // Sort by score descending
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Apply limit
        if let Some(limit) = options.limit {
            results.truncate(limit);
        }

        Ok(results)
    }

    async fn upload(&self, documents: Vec<Document>) -> Result<Vec<String>, ForgeError> {
        let mut store = self
            .documents
            .write()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        let now = Self::now();
        let mut ids = Vec::with_capacity(documents.len());

        for mut doc in documents {
            // Set timestamps
            if doc.created_at.is_none() {
                doc.created_at = Some(now);
            }
            doc.updated_at = Some(now);

            ids.push(doc.id.clone());
            store.insert(doc.id.clone(), doc);
        }

        Ok(ids)
    }

    async fn get(&self, id: &str) -> Result<Option<Document>, ForgeError> {
        let documents = self
            .documents
            .read()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;
        Ok(documents.get(id).cloned())
    }

    async fn list(&self, options: ListOptions) -> Result<Vec<Document>, ForgeError> {
        let documents = self
            .documents
            .read()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        let mut docs: Vec<Document> = documents
            .values()
            .filter(|doc| {
                if let Some(ref ns) = options.namespace {
                    doc.namespace.as_ref() == Some(ns)
                } else {
                    true
                }
            })
            .cloned()
            .collect();

        // Sort by ID for consistent ordering
        docs.sort_by(|a, b| a.id.cmp(&b.id));

        // Apply pagination
        let offset = options.offset.unwrap_or(0);
        if offset > 0 {
            docs = docs.into_iter().skip(offset).collect();
        }

        if let Some(limit) = options.limit {
            docs.truncate(limit);
        }

        Ok(docs)
    }

    async fn delete(&self, id: &str) -> Result<bool, ForgeError> {
        let mut documents = self
            .documents
            .write()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;
        Ok(documents.remove(id).is_some())
    }

    async fn update(&self, document: Document) -> Result<bool, ForgeError> {
        let mut store = self
            .documents
            .write()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        if store.contains_key(&document.id) {
            let mut doc = document;
            doc.updated_at = Some(Self::now());
            store.insert(doc.id.clone(), doc);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn stats(&self) -> Result<KnowledgeStats, ForgeError> {
        let documents = self
            .documents
            .read()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        let mut namespaces: HashSet<String> = HashSet::new();
        for doc in documents.values() {
            if let Some(ref ns) = doc.namespace {
                namespaces.insert(ns.clone());
            }
        }

        let mut ns_list: Vec<String> = namespaces.into_iter().collect();
        ns_list.sort();

        Ok(KnowledgeStats {
            document_count: documents.len(),
            namespace_count: ns_list.len(),
            namespaces: ns_list,
        })
    }

    async fn clear(&self, namespace: Option<&str>) -> Result<usize, ForgeError> {
        let mut documents = self
            .documents
            .write()
            .map_err(|e| ForgeError::internal(format!("Lock error: {}", e)))?;

        match namespace {
            Some(ns) => {
                let to_remove: Vec<String> = documents
                    .iter()
                    .filter(|(_, doc)| doc.namespace.as_deref() == Some(ns))
                    .map(|(id, _)| id.clone())
                    .collect();
                let count = to_remove.len();
                for id in to_remove {
                    documents.remove(&id);
                }
                Ok(count)
            }
            None => {
                let count = documents.len();
                documents.clear();
                Ok(count)
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_upload_and_get() {
        let backend = LocalKnowledgeBackend::new();

        let doc = Document::new("doc1", "Hello world");
        backend.upload(vec![doc]).await.unwrap();

        let retrieved = backend.get("doc1").await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().content, "Hello world");
    }

    #[tokio::test]
    async fn test_search() {
        let backend = LocalKnowledgeBackend::new();

        let docs = vec![
            Document::new(
                "1",
                "Rust is a systems programming language focused on safety",
            ),
            Document::new("2", "Python is great for data science and machine learning"),
            Document::new(
                "3",
                "JavaScript runs in the browser and on servers with Node.js",
            ),
        ];
        backend.upload(docs).await.unwrap();

        let results = backend
            .search("rust programming", SearchOptions::new())
            .await
            .unwrap();
        assert!(!results.is_empty());
        assert_eq!(results[0].document.id, "1");
    }

    #[tokio::test]
    async fn test_search_with_namespace() {
        let backend = LocalKnowledgeBackend::new();

        let docs = vec![
            Document::new("1", "Rust programming").namespace("tech"),
            Document::new("2", "Rust the game").namespace("games"),
        ];
        backend.upload(docs).await.unwrap();

        let results = backend
            .search("rust", SearchOptions::new().namespace("tech"))
            .await
            .unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.id, "1");
    }

    #[tokio::test]
    async fn test_search_with_highlights() {
        let backend = LocalKnowledgeBackend::new();

        let doc = Document::new("1", "Rust is fast. Rust is safe. Rust is fun to learn.");
        backend.upload(vec![doc]).await.unwrap();

        let results = backend
            .search("rust", SearchOptions::new().include_highlights(true))
            .await
            .unwrap();
        assert!(!results.is_empty());
        assert!(!results[0].highlights.is_empty());
    }

    #[tokio::test]
    async fn test_list_with_pagination() {
        let backend = LocalKnowledgeBackend::new();

        let docs: Vec<Document> = (0..10)
            .map(|i| Document::new(format!("doc{}", i), format!("Content {}", i)))
            .collect();
        backend.upload(docs).await.unwrap();

        let page1 = backend
            .list(ListOptions::new().limit(3).offset(0))
            .await
            .unwrap();
        assert_eq!(page1.len(), 3);

        let page2 = backend
            .list(ListOptions::new().limit(3).offset(3))
            .await
            .unwrap();
        assert_eq!(page2.len(), 3);
        assert_ne!(page1[0].id, page2[0].id);
    }

    #[tokio::test]
    async fn test_delete() {
        let backend = LocalKnowledgeBackend::new();

        let doc = Document::new("doc1", "Hello");
        backend.upload(vec![doc]).await.unwrap();

        assert!(backend.delete("doc1").await.unwrap());
        assert!(!backend.delete("doc1").await.unwrap()); // Already deleted
        assert!(backend.get("doc1").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_update() {
        let backend = LocalKnowledgeBackend::new();

        let doc = Document::new("doc1", "Original content");
        backend.upload(vec![doc]).await.unwrap();

        let updated = Document::new("doc1", "Updated content");
        assert!(backend.update(updated).await.unwrap());

        let retrieved = backend.get("doc1").await.unwrap().unwrap();
        assert_eq!(retrieved.content, "Updated content");
    }

    #[tokio::test]
    async fn test_stats() {
        let backend = LocalKnowledgeBackend::new();

        let docs = vec![
            Document::new("1", "Content").namespace("ns1"),
            Document::new("2", "Content").namespace("ns1"),
            Document::new("3", "Content").namespace("ns2"),
        ];
        backend.upload(docs).await.unwrap();

        let stats = backend.stats().await.unwrap();
        assert_eq!(stats.document_count, 3);
        assert_eq!(stats.namespace_count, 2);
    }

    #[tokio::test]
    async fn test_clear_namespace() {
        let backend = LocalKnowledgeBackend::new();

        let docs = vec![
            Document::new("1", "Content").namespace("ns1"),
            Document::new("2", "Content").namespace("ns2"),
        ];
        backend.upload(docs).await.unwrap();

        let deleted = backend.clear(Some("ns1")).await.unwrap();
        assert_eq!(deleted, 1);
        assert_eq!(backend.stats().await.unwrap().document_count, 1);
    }

    #[tokio::test]
    async fn test_metadata_filter() {
        let backend = LocalKnowledgeBackend::new();

        let docs = vec![
            Document::new("1", "Article about Rust").metadata("type", json!("article")),
            Document::new("2", "Tutorial about Rust").metadata("type", json!("tutorial")),
        ];
        backend.upload(docs).await.unwrap();

        let results = backend
            .search(
                "rust",
                SearchOptions::new().filter("type", json!("article")),
            )
            .await
            .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.id, "1");
    }
}