inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#![allow(dead_code)]
use crate::config::Config;
use crate::interfaces::cli::{Command, CommandContext, CommandOutput};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::json;
use std::collections::HashMap;
use tracing::info;

/// Fuzzy matching utility for CLI commands and suggestions
pub struct FuzzyMatcher {
    commands: Vec<String>,
    aliases: HashMap<String, String>,
}

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

impl FuzzyMatcher {
    pub fn new() -> Self {
        let mut matcher = Self {
            commands: Vec::new(),
            aliases: HashMap::new(),
        };

        matcher.initialize_commands();
        matcher.initialize_aliases();
        matcher
    }

    fn initialize_commands(&mut self) {
        // Main commands
        self.commands.extend(vec![
            "run".to_string(),
            "batch".to_string(),
            "serve".to_string(),
            "models".to_string(),
            "metrics".to_string(),
            "bench".to_string(),
            "validate".to_string(),
            "config".to_string(),
            "cache".to_string(),
            "convert".to_string(),
            "marketplace".to_string(),
            "package".to_string(),
            "install".to_string(),
            "remove".to_string(),
            "search".to_string(),
            "list".to_string(),
            "repo".to_string(),
            "tui".to_string(),
        ]);

        // Package subcommands
        self.commands.extend(vec![
            "package install".to_string(),
            "package remove".to_string(),
            "package search".to_string(),
            "package list".to_string(),
            "package update".to_string(),
            "package upgrade".to_string(),
            "package autoremove".to_string(),
            "package clean".to_string(),
            "package info".to_string(),
            "package depends".to_string(),
            "package check".to_string(),
        ]);

        // Repository subcommands
        self.commands.extend(vec![
            "repo add".to_string(),
            "repo remove".to_string(),
            "repo list".to_string(),
            "repo update".to_string(),
            "repo info".to_string(),
            "repo test".to_string(),
            "repo toggle".to_string(),
            "repo priority".to_string(),
            "repo clean".to_string(),
        ]);

        // Marketplace subcommands
        self.commands.extend(vec![
            "marketplace search".to_string(),
            "marketplace download".to_string(),
            "marketplace install".to_string(),
            "marketplace list".to_string(),
            "marketplace updates".to_string(),
        ]);
    }

    fn initialize_aliases(&mut self) {
        // Common typos and alternatives
        self.aliases
            .insert("instal".to_string(), "install".to_string());
        self.aliases
            .insert("instll".to_string(), "install".to_string());
        self.aliases
            .insert("isntall".to_string(), "install".to_string());
        self.aliases
            .insert("add".to_string(), "install".to_string());
        self.aliases
            .insert("get".to_string(), "install".to_string());

        self.aliases.insert("rm".to_string(), "remove".to_string());
        self.aliases.insert("del".to_string(), "remove".to_string());
        self.aliases
            .insert("delete".to_string(), "remove".to_string());
        self.aliases
            .insert("uninstall".to_string(), "remove".to_string());

        self.aliases
            .insert("find".to_string(), "search".to_string());
        self.aliases
            .insert("query".to_string(), "search".to_string());
        self.aliases
            .insert("lookup".to_string(), "search".to_string());

        self.aliases.insert("ls".to_string(), "list".to_string());
        self.aliases.insert("show".to_string(), "list".to_string());
        self.aliases
            .insert("display".to_string(), "list".to_string());

        self.aliases
            .insert("update".to_string(), "package update".to_string());
        self.aliases
            .insert("upgrade".to_string(), "package upgrade".to_string());
        self.aliases
            .insert("autoremove".to_string(), "package autoremove".to_string());
        self.aliases
            .insert("autoclean".to_string(), "package clean".to_string());

        self.aliases
            .insert("repository".to_string(), "repo".to_string());
        self.aliases
            .insert("repositories".to_string(), "repo".to_string());
        self.aliases.insert("repos".to_string(), "repo".to_string());

        self.aliases
            .insert("market".to_string(), "marketplace".to_string());
        self.aliases
            .insert("store".to_string(), "marketplace".to_string());
        self.aliases
            .insert("registry".to_string(), "marketplace".to_string());

        self.aliases
            .insert("pkg".to_string(), "package".to_string());
        self.aliases
            .insert("packages".to_string(), "package".to_string());

        self.aliases.insert("cfg".to_string(), "config".to_string());
        self.aliases
            .insert("configuration".to_string(), "config".to_string());
        self.aliases
            .insert("settings".to_string(), "config".to_string());

        self.aliases.insert("ui".to_string(), "tui".to_string());
        self.aliases
            .insert("terminal".to_string(), "tui".to_string());
        self.aliases
            .insert("interface".to_string(), "tui".to_string());
    }

    /// Find the best command suggestion for a given input
    pub fn suggest_command(&self, input: &str) -> Option<String> {
        let input_lower = input.to_lowercase();

        // Check exact aliases first
        if let Some(alias) = self.aliases.get(&input_lower) {
            return Some(alias.clone());
        }

        // Find best fuzzy match
        let mut best_match = None;
        let mut best_distance = usize::MAX;

        for command in &self.commands {
            let distance = levenshtein_distance(&input_lower, &command.to_lowercase());

            // Only suggest if it's a reasonable match (within 3 edits for longer commands)
            let max_distance = if command.len() > 6 { 3 } else { 2 };

            if distance <= max_distance && distance < best_distance {
                best_distance = distance;
                best_match = Some(command.clone());
            }
        }

        // Also check if input is a prefix of any command
        if best_match.is_none() {
            for command in &self.commands {
                if command.to_lowercase().starts_with(&input_lower) && input.len() >= 3 {
                    return Some(command.clone());
                }
            }
        }

        best_match
    }

    /// Get multiple suggestions for a command
    pub fn suggest_multiple(&self, input: &str, limit: usize) -> Vec<String> {
        let input_lower = input.to_lowercase();
        let mut suggestions = Vec::new();

        // Check aliases first
        if let Some(alias) = self.aliases.get(&input_lower) {
            suggestions.push(alias.clone());
        }

        // Add prefix matches first (highest priority for autocomplete-like behavior)
        if input.len() >= 2 {
            for command in &self.commands {
                if command.to_lowercase().starts_with(&input_lower)
                    && !suggestions.contains(command)
                {
                    suggestions.push(command.clone());
                    if suggestions.len() >= limit {
                        return suggestions;
                    }
                }
            }
        }

        // Get fuzzy matches to fill remaining slots
        let mut matches: Vec<(String, usize)> = self
            .commands
            .iter()
            .filter(|cmd| !suggestions.contains(cmd))
            .map(|cmd| {
                let distance = levenshtein_distance(&input_lower, &cmd.to_lowercase());
                (cmd.clone(), distance)
            })
            .filter(|(_, distance)| *distance <= 3)
            .collect();

        // Sort by distance
        matches.sort_by_key(|(_, distance)| *distance);

        // Add unique suggestions
        for (command, _) in matches.into_iter().take(limit - suggestions.len()) {
            if !suggestions.contains(&command) {
                suggestions.push(command);
            }
        }

        suggestions
    }

    /// Check if a command exists or can be corrected
    pub fn validate_command(&self, input: &str) -> CommandValidation {
        let input_lower = input.to_lowercase();

        // Check if command exists exactly
        if self.commands.contains(&input.to_string()) {
            return CommandValidation::Valid;
        }

        // Check aliases
        if let Some(alias) = self.aliases.get(&input_lower) {
            return CommandValidation::Alias(alias.clone());
        }

        // Get suggestion
        if let Some(suggestion) = self.suggest_command(input) {
            CommandValidation::Suggestion(suggestion)
        } else {
            CommandValidation::Invalid
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum CommandValidation {
    Valid,
    Alias(String),
    Suggestion(String),
    Invalid,
}

/// Calculate Levenshtein distance between two strings
fn levenshtein_distance(s1: &str, s2: &str) -> usize {
    let len1 = s1.chars().count();
    let len2 = s2.chars().count();

    if len1 == 0 {
        return len2;
    }
    if len2 == 0 {
        return len1;
    }

    let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

    // Initialize first row and column
    for i in 0..=len1 {
        matrix[i][0] = i;
    }
    for j in 0..=len2 {
        matrix[0][j] = j;
    }

    let s1_chars: Vec<char> = s1.chars().collect();
    let s2_chars: Vec<char> = s2.chars().collect();

    for i in 1..=len1 {
        for j in 1..=len2 {
            let cost = if s1_chars[i - 1] == s2_chars[j - 1] {
                0
            } else {
                1
            };

            matrix[i][j] = std::cmp::min(
                std::cmp::min(
                    matrix[i - 1][j] + 1, // deletion
                    matrix[i][j - 1] + 1, // insertion
                ),
                matrix[i - 1][j - 1] + cost, // substitution
            );
        }
    }

    matrix[len1][len2]
}

// ============================================================================
// FuzzyMatch - Find best fuzzy match for input
// ============================================================================

/// Find best fuzzy match for input string
pub struct FuzzyMatch {
    config: Config,
    input: String,
}

impl FuzzyMatch {
    pub fn new(config: Config, input: String) -> Self {
        Self { config, input }
    }
}

#[async_trait]
impl Command for FuzzyMatch {
    fn name(&self) -> &str {
        "fuzzy match"
    }

    fn description(&self) -> &str {
        "Find best fuzzy match for input"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        if self.input.is_empty() {
            anyhow::bail!("Input cannot be empty");
        }

        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Finding fuzzy match for: {}", self.input);

        let matcher = FuzzyMatcher::new();
        let suggestion = matcher.suggest_command(&self.input);

        // Human-readable output
        if !ctx.json_output {
            match &suggestion {
                Some(sug) => {
                    println!("Best match for '{}':", self.input);
                    println!("  → {}", sug);
                }
                None => {
                    println!("No match found for '{}'", self.input);
                }
            }
        }

        // Structured output
        Ok(CommandOutput::success_with_data(
            match &suggestion {
                Some(sug) => format!("Found match: {}", sug),
                None => "No match found".to_string(),
            },
            json!({
                "input": self.input,
                "match": suggestion,
                "found": suggestion.is_some(),
            }),
        ))
    }
}

// ============================================================================
// FuzzyMultiMatch - Find multiple fuzzy matches
// ============================================================================

/// Find multiple fuzzy matches for input
pub struct FuzzyMultiMatch {
    config: Config,
    input: String,
    limit: usize,
}

impl FuzzyMultiMatch {
    pub fn new(config: Config, input: String, limit: usize) -> Self {
        Self {
            config,
            input,
            limit,
        }
    }
}

#[async_trait]
impl Command for FuzzyMultiMatch {
    fn name(&self) -> &str {
        "fuzzy multimatch"
    }

    fn description(&self) -> &str {
        "Find multiple fuzzy matches for input"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        if self.input.is_empty() {
            anyhow::bail!("Input cannot be empty");
        }

        if self.limit == 0 {
            anyhow::bail!("Limit must be at least 1");
        }

        if self.limit > 50 {
            anyhow::bail!("Limit cannot exceed 50");
        }

        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Finding {} fuzzy matches for: {}", self.limit, self.input);

        let matcher = FuzzyMatcher::new();
        let suggestions = matcher.suggest_multiple(&self.input, self.limit);

        // Human-readable output
        if !ctx.json_output {
            if suggestions.is_empty() {
                println!("No matches found for '{}'", self.input);
            } else {
                println!("Top {} matches for '{}':", suggestions.len(), self.input);
                for (i, suggestion) in suggestions.iter().enumerate() {
                    println!("  {}. {}", i + 1, suggestion);
                }
            }
        }

        // Structured output
        Ok(CommandOutput::success_with_data(
            format!("Found {} matches", suggestions.len()),
            json!({
                "input": self.input,
                "matches": suggestions,
                "count": suggestions.len(),
                "limit": self.limit,
            }),
        ))
    }
}

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

    #[test]
    fn test_levenshtein_distance() {
        assert_eq!(levenshtein_distance("cat", "cat"), 0);
        assert_eq!(levenshtein_distance("cat", "bat"), 1);
        assert_eq!(levenshtein_distance("install", "instal"), 1);
        assert_eq!(levenshtein_distance("search", "serch"), 1);
    }

    #[test]
    fn test_command_suggestions() {
        let matcher = FuzzyMatcher::new();

        assert_eq!(
            matcher.suggest_command("instal"),
            Some("install".to_string())
        );
        assert_eq!(matcher.suggest_command("serch"), Some("search".to_string()));
        assert_eq!(matcher.suggest_command("rm"), Some("remove".to_string()));
        assert_eq!(matcher.suggest_command("ls"), Some("list".to_string()));
    }

    #[test]
    fn test_command_validation() {
        let matcher = FuzzyMatcher::new();

        assert_eq!(
            matcher.validate_command("install"),
            CommandValidation::Valid
        );
        assert_eq!(
            matcher.validate_command("rm"),
            CommandValidation::Alias("remove".to_string())
        );
        // "instal" is an alias to "install", not a suggestion
        assert_eq!(
            matcher.validate_command("instal"),
            CommandValidation::Alias("install".to_string())
        );
        assert_eq!(
            matcher.validate_command("xyz123"),
            CommandValidation::Invalid
        );
    }

    #[test]
    fn test_multiple_suggestions() {
        let matcher = FuzzyMatcher::new();

        let suggestions = matcher.suggest_multiple("pac", 3);
        assert!(suggestions.contains(&"package".to_string()));

        let suggestions = matcher.suggest_multiple("rep", 3);
        assert!(suggestions.contains(&"repo".to_string()));
    }

    #[tokio::test]
    async fn test_fuzzy_match_validation_empty() {
        let config = Config::default();
        let cmd = FuzzyMatch::new(config.clone(), String::new());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Input cannot be empty")
        );
    }

    #[tokio::test]
    async fn test_fuzzy_match_validation_valid() {
        let config = Config::default();
        let cmd = FuzzyMatch::new(config.clone(), "instal".to_string());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_fuzzy_multimatch_validation_zero_limit() {
        let config = Config::default();
        let cmd = FuzzyMultiMatch::new(config.clone(), "test".to_string(), 0);
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Limit must be at least 1")
        );
    }

    #[tokio::test]
    async fn test_fuzzy_multimatch_validation_excessive_limit() {
        let config = Config::default();
        let cmd = FuzzyMultiMatch::new(config.clone(), "test".to_string(), 60);
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Limit cannot exceed 50")
        );
    }

    #[tokio::test]
    async fn test_fuzzy_multimatch_validation_valid() {
        let config = Config::default();
        let cmd = FuzzyMultiMatch::new(config.clone(), "instal".to_string(), 5);
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }
}