kaccy-ai 0.2.0

AI-powered intelligence for Kaccy Protocol - forecasting, optimization, and insights
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
//! Knowledge base for domain-specific information
//!
//! This module provides a system for storing and retrieving domain-specific knowledge
//! that can enhance AI evaluations and verifications.
//!
//! # Examples
//!
//! ```
//! use kaccy_ai::knowledge_base::{KnowledgeBase, KnowledgeEntry, KnowledgeDomain};
//!
//! let mut kb = KnowledgeBase::new();
//!
//! // Add knowledge about Rust programming
//! let entry = KnowledgeEntry::new(
//!     KnowledgeDomain::Programming,
//!     "rust_best_practices",
//!     "Rust best practices include using the type system, avoiding unwrap() in production, \
//!      and using Result for error handling.",
//! );
//! kb.add_entry(entry);
//!
//! // Query knowledge
//! let results = kb.search("rust error handling");
//! assert!(results.len() > 0);
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::error::{AiError, Result};

/// Knowledge domain categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum KnowledgeDomain {
    /// Programming and software development
    Programming,
    /// Blockchain and crypto
    Blockchain,
    /// Security and fraud detection
    Security,
    /// Content quality and writing
    Content,
    /// Social media and marketing
    SocialMedia,
    /// General domain knowledge
    General,
}

impl KnowledgeDomain {
    /// Get domain name
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            KnowledgeDomain::Programming => "Programming",
            KnowledgeDomain::Blockchain => "Blockchain",
            KnowledgeDomain::Security => "Security",
            KnowledgeDomain::Content => "Content",
            KnowledgeDomain::SocialMedia => "Social Media",
            KnowledgeDomain::General => "General",
        }
    }
}

/// Knowledge entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeEntry {
    /// Unique identifier
    pub id: String,
    /// Domain category
    pub domain: KnowledgeDomain,
    /// Entry title/key
    pub title: String,
    /// Knowledge content
    pub content: String,
    /// Tags for searching
    pub tags: Vec<String>,
    /// Source/reference
    pub source: Option<String>,
    /// Confidence score (0.0-1.0)
    pub confidence: f64,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Last updated timestamp
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl KnowledgeEntry {
    /// Create a new knowledge entry
    pub fn new(
        domain: KnowledgeDomain,
        title: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        let now = chrono::Utc::now();
        let title = title.into();
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            domain,
            title: title.clone(),
            content: content.into(),
            tags: Self::extract_tags(&title),
            source: None,
            confidence: 1.0,
            created_at: now,
            updated_at: now,
        }
    }

    /// Create with explicit ID
    pub fn with_id(
        id: impl Into<String>,
        domain: KnowledgeDomain,
        title: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        let now = chrono::Utc::now();
        let title = title.into();
        Self {
            id: id.into(),
            domain,
            title: title.clone(),
            content: content.into(),
            tags: Self::extract_tags(&title),
            source: None,
            confidence: 1.0,
            created_at: now,
            updated_at: now,
        }
    }

    /// Add tags
    #[must_use]
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Add source
    #[must_use]
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// Set confidence
    #[must_use]
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Extract tags from text (simple word tokenization)
    fn extract_tags(text: &str) -> Vec<String> {
        text.to_lowercase()
            .split_whitespace()
            .filter(|w| w.len() > 3)
            .take(10)
            .map(std::string::ToString::to_string)
            .collect()
    }

    /// Update content
    pub fn update_content(&mut self, content: impl Into<String>) {
        self.content = content.into();
        self.updated_at = chrono::Utc::now();
    }
}

/// Knowledge base storage and retrieval
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeBase {
    entries: HashMap<String, KnowledgeEntry>,
    domain_index: HashMap<KnowledgeDomain, Vec<String>>,
}

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

impl KnowledgeBase {
    /// Create a new knowledge base
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
            domain_index: HashMap::new(),
        }
    }

    /// Add a knowledge entry
    pub fn add_entry(&mut self, entry: KnowledgeEntry) -> Result<()> {
        let id = entry.id.clone();
        let domain = entry.domain;

        // Add to main storage
        self.entries.insert(id.clone(), entry);

        // Add to domain index
        self.domain_index.entry(domain).or_default().push(id);

        Ok(())
    }

    /// Get entry by ID
    #[must_use]
    pub fn get_entry(&self, id: &str) -> Option<&KnowledgeEntry> {
        self.entries.get(id)
    }

    /// Get mutable entry by ID
    pub fn get_entry_mut(&mut self, id: &str) -> Option<&mut KnowledgeEntry> {
        self.entries.get_mut(id)
    }

    /// Remove entry by ID
    pub fn remove_entry(&mut self, id: &str) -> Option<KnowledgeEntry> {
        if let Some(entry) = self.entries.remove(id) {
            // Remove from domain index
            if let Some(ids) = self.domain_index.get_mut(&entry.domain) {
                ids.retain(|i| i != id);
            }
            Some(entry)
        } else {
            None
        }
    }

    /// Search for entries matching query
    #[must_use]
    pub fn search(&self, query: &str) -> Vec<&KnowledgeEntry> {
        let query_lower = query.to_lowercase();
        let query_words: Vec<&str> = query_lower.split_whitespace().collect();

        let mut results: Vec<(f64, &KnowledgeEntry)> = self
            .entries
            .values()
            .filter_map(|entry| {
                let score = self.calculate_relevance(entry, &query_words, &query_lower);
                if score > 0.0 {
                    Some((score, entry))
                } else {
                    None
                }
            })
            .collect();

        // Sort by relevance score
        results.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());

        results.into_iter().map(|(_, entry)| entry).collect()
    }

    /// Calculate relevance score for an entry
    fn calculate_relevance(
        &self,
        entry: &KnowledgeEntry,
        query_words: &[&str],
        query_full: &str,
    ) -> f64 {
        let mut score = 0.0;

        let title_lower = entry.title.to_lowercase();
        let content_lower = entry.content.to_lowercase();

        // Exact title match
        if title_lower.contains(query_full) {
            score += 10.0;
        }

        // Exact content match
        if content_lower.contains(query_full) {
            score += 5.0;
        }

        // Word matches in title
        for word in query_words {
            if title_lower.contains(word) {
                score += 2.0;
            }
        }

        // Word matches in content
        for word in query_words {
            if content_lower.contains(word) {
                score += 0.5;
            }
        }

        // Tag matches
        for tag in &entry.tags {
            for word in query_words {
                if tag.contains(word) {
                    score += 1.0;
                }
            }
        }

        // Apply confidence multiplier
        score * entry.confidence
    }

    /// Get entries by domain
    #[must_use]
    pub fn get_by_domain(&self, domain: KnowledgeDomain) -> Vec<&KnowledgeEntry> {
        self.domain_index
            .get(&domain)
            .map(|ids| ids.iter().filter_map(|id| self.entries.get(id)).collect())
            .unwrap_or_default()
    }

    /// Get all entries
    #[must_use]
    pub fn all_entries(&self) -> Vec<&KnowledgeEntry> {
        self.entries.values().collect()
    }

    /// Get total entry count
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Save knowledge base to file
    pub fn save_to_file(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
        let json = serde_json::to_string_pretty(self)
            .map_err(|e| AiError::Internal(format!("Failed to serialize knowledge base: {e}")))?;

        std::fs::write(path, json)
            .map_err(|e| AiError::Internal(format!("Failed to write knowledge base: {e}")))?;

        Ok(())
    }

    /// Load knowledge base from file
    pub fn load_from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let json = std::fs::read_to_string(path)
            .map_err(|e| AiError::Internal(format!("Failed to read knowledge base: {e}")))?;

        let kb: KnowledgeBase = serde_json::from_str(&json)
            .map_err(|e| AiError::Internal(format!("Failed to deserialize knowledge base: {e}")))?;

        Ok(kb)
    }

    /// Clear all entries
    pub fn clear(&mut self) {
        self.entries.clear();
        self.domain_index.clear();
    }
}

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

    #[test]
    fn test_knowledge_entry_creation() {
        let entry = KnowledgeEntry::new(
            KnowledgeDomain::Programming,
            "Rust Best Practices",
            "Always use Result for error handling",
        );

        assert_eq!(entry.domain, KnowledgeDomain::Programming);
        assert_eq!(entry.title, "Rust Best Practices");
        assert!(!entry.tags.is_empty());
    }

    #[test]
    fn test_knowledge_base_add_and_get() {
        let mut kb = KnowledgeBase::new();

        let entry = KnowledgeEntry::new(
            KnowledgeDomain::Programming,
            "Rust Ownership",
            "Ownership is a key concept in Rust",
        );

        let id = entry.id.clone();
        kb.add_entry(entry).unwrap();

        assert_eq!(kb.len(), 1);
        assert!(kb.get_entry(&id).is_some());
    }

    #[test]
    fn test_knowledge_base_search() {
        let mut kb = KnowledgeBase::new();

        kb.add_entry(KnowledgeEntry::new(
            KnowledgeDomain::Programming,
            "Rust Ownership",
            "Ownership prevents memory issues",
        ))
        .unwrap();

        kb.add_entry(KnowledgeEntry::new(
            KnowledgeDomain::Programming,
            "Python GIL",
            "Global Interpreter Lock in Python",
        ))
        .unwrap();

        let results = kb.search("rust ownership");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].title, "Rust Ownership");
    }

    #[test]
    fn test_knowledge_base_by_domain() {
        let mut kb = KnowledgeBase::new();

        kb.add_entry(KnowledgeEntry::new(
            KnowledgeDomain::Programming,
            "Test1",
            "Content1",
        ))
        .unwrap();

        kb.add_entry(KnowledgeEntry::new(
            KnowledgeDomain::Blockchain,
            "Test2",
            "Content2",
        ))
        .unwrap();

        let prog_entries = kb.get_by_domain(KnowledgeDomain::Programming);
        assert_eq!(prog_entries.len(), 1);

        let blockchain_entries = kb.get_by_domain(KnowledgeDomain::Blockchain);
        assert_eq!(blockchain_entries.len(), 1);
    }

    #[test]
    fn test_knowledge_base_persistence() {
        let mut kb = KnowledgeBase::new();

        kb.add_entry(KnowledgeEntry::new(
            KnowledgeDomain::Security,
            "Security Best Practices",
            "Always validate input",
        ))
        .unwrap();

        let temp_path = "/tmp/kb_test.json";
        kb.save_to_file(temp_path).unwrap();

        let kb2 = KnowledgeBase::load_from_file(temp_path).unwrap();
        assert_eq!(kb2.len(), 1);

        // Cleanup
        let _ = std::fs::remove_file(temp_path);
    }

    #[test]
    fn test_entry_update() {
        let mut kb = KnowledgeBase::new();

        let entry = KnowledgeEntry::new(KnowledgeDomain::Programming, "Test", "Original content");

        let id = entry.id.clone();
        kb.add_entry(entry).unwrap();

        if let Some(entry) = kb.get_entry_mut(&id) {
            entry.update_content("Updated content");
        }

        let updated = kb.get_entry(&id).unwrap();
        assert_eq!(updated.content, "Updated content");
    }

    #[test]
    fn test_entry_removal() {
        let mut kb = KnowledgeBase::new();

        let entry = KnowledgeEntry::new(KnowledgeDomain::Programming, "Test", "Content");

        let id = entry.id.clone();
        kb.add_entry(entry).unwrap();

        assert_eq!(kb.len(), 1);

        let removed = kb.remove_entry(&id);
        assert!(removed.is_some());
        assert_eq!(kb.len(), 0);
    }
}