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
# Inferno CLI Architecture

**Version**: 0.4.0+
**Status**: Stable (Phase 2 Foundation)
**Migration**: Fully backward compatible

## Overview

The new CLI architecture provides a unified, extensible command system with:

- **Command Trait**: Standard interface for all commands
- **Command Pipeline**: Orchestrates execution with middleware
- **Middleware System**: Cross-cutting concerns (logging, metrics, validation)
- **Command Context**: Shared state and configuration
- **Structured Output**: Type-safe command results

## Quick Start

### Creating a Command

```rust
use inferno::interfaces::cli::{Command, CommandContext, CommandOutput};
use async_trait::async_trait;
use anyhow::Result;

pub struct MyCommand {
    arg1: String,
    arg2: i32,
}

#[async_trait]
impl Command for MyCommand {
    fn name(&self) -> &str {
        "my-command"
    }

    fn description(&self) -> &str {
        "Does something useful"
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        // Access configuration
        let models_dir = &ctx.config.models_dir;

        // Do work...

        // Return structured output
        Ok(CommandOutput::success("Command completed successfully"))
    }
}
```

### Using the Pipeline

```rust
use inferno::interfaces::cli::{CommandPipeline, LoggingMiddleware, MetricsMiddleware};

let pipeline = CommandPipeline::builder()
    .with_middleware(Box::new(LoggingMiddleware::new()))
    .with_middleware(Box::new(MetricsMiddleware::new()))
    .build();

let command = MyCommand {
    arg1: "value".to_string(),
    arg2: 42,
};

let mut ctx = CommandContext::new(config);
let output = pipeline.execute(&command, &mut ctx).await?;

if output.success {
    println!("{}", output.to_display());
} else {
    std::process::exit(output.exit_code);
}
```

## Architecture

### Components

1. **Command Trait** (`command.rs`)
   - Defines standard interface for all commands
   - Includes validation, execution, and metadata
   - Async-first with `#[async_trait]`

2. **CommandContext** (`context.rs`)
   - Shared context for command execution
   - Contains configuration, args, state
   - Execution metadata (ID, timing, metrics)

3. **CommandOutput** (`output.rs`)
   - Structured output from commands
   - Supports JSON and human-readable formats
   - Exit codes and severity levels

4. **Middleware** (`middleware/`)
   - Trait for pre/post execution hooks
   - Built-in: Logging, Metrics
   - Stackable and conditional

5. **CommandPipeline** (`pipeline.rs`)
   - Orchestrates command execution
   - Manages middleware lifecycle
   - Handles errors uniformly

### Execution Flow

```text
┌─────────────────────────────────────────────────────────┐
│                    CommandPipeline                      │
├─────────────────────────────────────────────────────────┤
│ 1. Run pre-execution middleware (logging, setup)        │
│ 2. Validate command arguments and context               │
│ 3. Execute command with shared context                  │
│ 4. Run post-execution middleware (metrics, cleanup)     │
│ 5. Handle errors and format output                      │
└─────────────────────────────────────────────────────────┘
```

## Key Features

### 1. Validation Before Execution

```rust
#[async_trait]
impl Command for MyCommand {
    async fn validate(&self, ctx: &CommandContext) -> Result<()> {
        if ctx.get_arg("required").is_none() {
            anyhow::bail!("Missing required argument");
        }
        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        // Validation passed, safe to execute
        Ok(CommandOutput::success("Done"))
    }
}
```

### 2. Structured Output

```rust
// Success with data
CommandOutput::success_with_data(
    "Found 5 models",
    json!({ "count": 5, "models": models })
)

// Warning (non-fatal)
CommandOutput::warning("No cache entries found", None)

// Error with exit code
CommandOutput::error("File not found", 1)
```

### 3. Command Context

```rust
async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
    // Access configuration
    let models_dir = &ctx.config.models_dir;

    // Check flags
    if ctx.is_verbose() {
        println!("Verbose mode enabled");
    }

    // Store state for middleware
    ctx.set_state("model_count", 42);

    // JSON output mode
    if ctx.json_output {
        return Ok(CommandOutput::data(json!({ "result": "..." })));
    }

    Ok(CommandOutput::success("Done"))
}
```

### 4. Middleware System

```rust
pub struct LoggingMiddleware;

#[async_trait]
impl Middleware for LoggingMiddleware {
    async fn before(&self, ctx: &mut CommandContext) -> Result<()> {
        tracing::info!("Starting command: {}", ctx.execution_id);
        Ok(())
    }

    async fn after(&self, ctx: &mut CommandContext, result: &Result<CommandOutput>) -> Result<()> {
        tracing::info!("Finished in {:?}", ctx.elapsed());
        Ok(())
    }
}
```

## Migration Guide

### From Old Style

**Old way (still works)**:
```rust
// src/cli/old_command.rs
pub async fn execute(args: OldCommandArgs, config: &Config) -> Result<()> {
    // Command logic
    Ok(())
}

// main.rs
Commands::OldCommand(args) => old_command::execute(args, &config).await?,
```

**New way (recommended)**:
```rust
// src/interfaces/cli/commands/new_command.rs
pub struct NewCommand {
    args: NewCommandArgs,
}

#[async_trait]
impl Command for NewCommand {
    fn name(&self) -> &str { "new-command" }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        // Command logic with context
        Ok(CommandOutput::success("Done"))
    }
}

// main.rs
Commands::NewCommand(args) => {
    let cmd = NewCommand { args };
    let mut ctx = CommandContext::new(config);
    let pipeline = CommandPipeline::builder()
        .with_middleware(Box::new(LoggingMiddleware::new()))
        .build();
    let output = pipeline.execute(&cmd, &mut ctx).await?;
    if !output.success {
        std::process::exit(output.exit_code);
    }
}
```

### Migration Steps

1. **Keep old command working** - No need to change immediately
2. **Add new command** - Implement Command trait
3. **Test both** - Ensure backward compatibility
4. **Gradual replacement** - Replace old commands incrementally
5. **Remove old** - Once all commands migrated

## Examples

### Simple Command

```rust
pub struct EchoCommand {
    message: String,
}

#[async_trait]
impl Command for EchoCommand {
    fn name(&self) -> &str { "echo" }
    fn description(&self) -> &str { "Echo a message" }

    async fn execute(&self, _ctx: &mut CommandContext) -> Result<CommandOutput> {
        println!("{}", self.message);
        Ok(CommandOutput::success("Echoed message"))
    }
}
```

### Command with Validation

```rust
pub struct ValidatingCommand {
    value: i32,
}

#[async_trait]
impl Command for ValidatingCommand {
    fn name(&self) -> &str { "validate" }
    fn description(&self) -> &str { "Validates input" }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        if self.value < 0 || self.value > 100 {
            anyhow::bail!("Value must be between 0 and 100");
        }
        Ok(())
    }

    async fn execute(&self, _ctx: &mut CommandContext) -> Result<CommandOutput> {
        Ok(CommandOutput::success_with_data(
            "Valid!",
            json!({ "value": self.value })
        ))
    }
}
```

### Command with Complex Logic

```rust
pub struct ModelListCommand;

#[async_trait]
impl Command for ModelListCommand {
    fn name(&self) -> &str { "models-list" }
    fn description(&self) -> &str { "List available models" }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        let model_manager = ModelManager::new(&ctx.config.models_dir);
        let models = model_manager.list_models().await?;

        if ctx.json_output {
            return Ok(CommandOutput::data(
                json!({ "models": models, "count": models.len() })
            ));
        }

        if models.is_empty() {
            return Ok(CommandOutput::warning(
                "No models found",
                Some(json!({ "models_dir": ctx.config.models_dir.display().to_string() }))
            ));
        }

        // Pretty print for human consumption
        println!("Found {} models:", models.len());
        for model in &models {
            println!("  - {}", model.name);
        }

        Ok(CommandOutput::success_with_data(
            format!("Found {} models", models.len()),
            json!({ "models": models, "count": models.len() })
        ))
    }
}
```

## Built-in Middleware

### LoggingMiddleware

Automatically logs command execution:
- Start time and execution ID
- Success/failure with duration
- Error details when commands fail

```rust
let pipeline = CommandPipeline::builder()
    .with_middleware(Box::new(LoggingMiddleware::new()))
    .build();
```

### MetricsMiddleware

Records command metrics:
- Execution duration
- Success/failure counts
- Exit codes

```rust
let pipeline = CommandPipeline::builder()
    .with_middleware(Box::new(MetricsMiddleware::new()))
    .build();
```

### Creating Custom Middleware

```rust
pub struct TimingMiddleware;

#[async_trait]
impl Middleware for TimingMiddleware {
    async fn before(&self, ctx: &mut CommandContext) -> Result<()> {
        ctx.set_state("timing_start", Instant::now());
        Ok(())
    }

    async fn after(&self, ctx: &mut CommandContext, result: &Result<CommandOutput>) -> Result<()> {
        if let Some(start) = ctx.get_state::<Instant>("timing_start") {
            let duration = start.elapsed();
            println!("Command took: {:?}", duration);
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "timing"
    }
}
```

## Testing

### Testing Commands

```rust
#[tokio::test]
async fn test_echo_command() {
    let cmd = EchoCommand {
        message: "test".to_string(),
    };

    let mut ctx = CommandContext::mock();
    let output = cmd.execute(&mut ctx).await.unwrap();

    assert!(output.success);
    assert_eq!(output.message, Some("Echoed message".to_string()));
}
```

### Testing with Pipeline

```rust
#[tokio::test]
async fn test_command_with_pipeline() {
    let pipeline = CommandPipeline::builder()
        .with_middleware(Box::new(LoggingMiddleware::new()))
        .build();

    let cmd = EchoCommand {
        message: "test".to_string(),
    };

    let mut ctx = CommandContext::mock();
    let output = pipeline.execute(&cmd, &mut ctx).await.unwrap();

    assert!(output.success);
}
```

### Testing Middleware

```rust
#[tokio::test]
async fn test_logging_middleware() {
    let middleware = LoggingMiddleware::new();
    let mut ctx = CommandContext::mock();

    middleware.before(&mut ctx).await.unwrap();
    let output = CommandOutput::success("test");
    middleware.after(&mut ctx, &Ok(output)).await.unwrap();
}
```

## Benefits

1. **Reduced Code Duplication**
   - Cross-cutting concerns handled once in middleware
   - Common validation patterns shared across commands
   - Consistent error handling

2. **Better Testing**
   - Commands testable in isolation
   - Mock context for unit tests
   - Middleware tested independently

3. **Enhanced Observability**
   - Automatic logging of all commands
   - Consistent metrics collection
   - Built-in audit trails

4. **Improved Error Handling**
   - Consistent error formatting
   - Better error messages with context
   - Centralized error recovery

5. **Better Developer Experience**
   - Clear patterns for new commands
   - Self-documenting via traits
   - Easier to understand command flow

## Performance

The new architecture adds minimal overhead:
- ~1-2ms per command execution (middleware)
- Zero-cost abstractions via traits
- Async-first for efficient I/O

Benchmark comparison (Phase 2):
- Old: ~12ms (command execution only)
- New: ~13-14ms (with logging + metrics middleware)
- Overhead: ~1-2ms (~8-16%)

## Future Enhancements

Planned for future phases:
- **Command Decorators**: @cache, @audit, @retry
- **Plugin System**: Dynamic command loading
- **Command Composition**: Pipelines of commands
- **Smart Validation**: Schema-based validation
- **Auto-completion**: Shell completion generation

## Troubleshooting

### Command Not Found

Ensure command is registered in `Commands` enum:
```rust
#[derive(Subcommand)]
pub enum Commands {
    MyCommand(my_command::MyCommandArgs),
}
```

### Middleware Not Running

Check middleware is added to pipeline:
```rust
let pipeline = CommandPipeline::builder()
    .with_middleware(Box::new(MyMiddleware))  // ← Add here
    .build();
```

### Context State Not Available

Ensure state is set before accessing:
```rust
ctx.set_state("key", value);  // Set
let value = ctx.get_state::<Type>("key");  // Get
```

## See Also

- **Examples**: `examples/cli_architecture.rs`
- **Planning Doc**: `.claude/plans/phase2-cli-architecture.md`
- **Core Config**: `src/core/config/README.md`
- **Legacy CLI**: `src/cli/` (old command files)