aprender-shell 0.41.0

AI-powered shell completion trained on your history
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

/// Publish model to Hugging Face Hub (GH-100)
fn cmd_publish(model_path: &str, repo_id: &str, commit_msg: &str, create: bool, private: bool) {
    use aprender::format::model_card::{ModelCard, TrainingDataInfo};

    let path = expand_path(model_path);

    // Load model to get metadata
    let model = match MarkovModel::load(&path) {
        Ok(m) => m,
        Err(e) => {
            eprintln!("❌ Failed to load model: {e}");
            std::process::exit(1);
        }
    };

    // Read model file bytes
    let model_bytes = match std::fs::read(&path) {
        Ok(b) => b,
        Err(e) => {
            eprintln!("❌ Failed to read model file: {e}");
            std::process::exit(1);
        }
    };

    // Generate model card
    let model_id = format!(
        "aprender-shell-markov-{}gram-{}",
        model.ngram_size(),
        chrono_lite_date()
    );

    let card = ModelCard::new(&model_id, "1.0.0")
        .with_name("Shell Completion Model")
        .with_description(
            "Markov chain model trained on shell command history for intelligent tab completion",
        )
        .with_architecture("MarkovModel")
        .with_license("MIT")
        .with_param_count(model.ngram_count() as u64)
        .with_hyperparameter("ngram_size", model.ngram_size())
        .with_metric("vocab_size", model.vocab_size())
        .with_metric("ngram_count", model.ngram_count())
        .with_training_data(
            TrainingDataInfo::new("shell_history").with_samples(model.total_commands() as u64),
        );

    println!("📤 Publishing to Hugging Face Hub...\n");
    println!("  Repository: {repo_id}");
    println!("  Model:      {}", path.display());
    println!("  Size:       {} bytes", model_bytes.len());
    println!("  N-gram:     {}", model.ngram_size());
    println!("  Vocab:      {} commands", model.vocab_size());
    println!();

    // Check for HF_TOKEN
    if std::env::var("HF_TOKEN").is_err() {
        eprintln!("⚠️  HF_TOKEN environment variable not set.\n");
        eprintln!("To publish to Hugging Face Hub:");
        eprintln!("  1. Create a token at https://huggingface.co/settings/tokens");
        eprintln!("  2. Export it: export HF_TOKEN=hf_xxxxx");
        eprintln!();
        eprintln!("📁 Saving model card locally instead...");

        // Save locally
        let local_dir = path.parent().unwrap_or(std::path::Path::new("."));
        let readme_path = local_dir.join("README.md");
        let card_content = card.to_huggingface();

        if let Err(e) = std::fs::write(&readme_path, &card_content) {
            eprintln!("❌ Failed to write README.md: {e}");
            std::process::exit(1);
        }

        println!("✅ Model card saved to: {}", readme_path.display());
        println!();
        println!("💡 Upload manually with:");
        println!(
            "   huggingface-cli upload {repo_id} {} model.apr",
            path.display()
        );
        println!(
            "   huggingface-cli upload {repo_id} {} README.md",
            readme_path.display()
        );
        return;
    }

    // Prepare for HF Hub upload
    println!("🔑 Using HF_TOKEN for authentication");
    println!("  Create repo: {create}");
    println!("  Private:     {private}");
    println!("  Commit:      {commit_msg}");
    println!();

    // Note: Full HTTP upload requires additional implementation
    // For now, prepare files and show instructions
    let local_dir = path.parent().unwrap_or(std::path::Path::new("."));
    let readme_path = local_dir.join("README.md");
    let card_content = card.to_huggingface();

    if let Err(e) = std::fs::write(&readme_path, &card_content) {
        eprintln!("❌ Failed to write README.md: {e}");
        std::process::exit(1);
    }

    println!("✅ Model card generated: {}", readme_path.display());
    println!();
    println!("📋 Model Card Preview:");
    println!("───────────────────────────────────────────");
    for line in card_content.lines().take(20) {
        println!("  {line}");
    }
    println!("  ...");
    println!("───────────────────────────────────────────");
    println!();
    println!("🚀 Upload with huggingface-cli:");
    println!(
        "   huggingface-cli repo create {repo_id} --type model{}",
        if private { " --private" } else { "" }
    );
    println!(
        "   huggingface-cli upload {repo_id} {} model.apr --commit-message \"{commit_msg}\"",
        path.display()
    );
    println!(
        "   huggingface-cli upload {repo_id} {} README.md",
        readme_path.display()
    );
}

// =============================================================================
// Daemon/Stream Mode Commands (GH-95)
// =============================================================================

/// Load a model for stream mode, handling password and error exits.
fn load_stream_model(path: &std::path::Path, use_password: bool) -> MarkovModel {
    if use_password {
        let password =
            rpassword::prompt_password("🔐 Model password: ").unwrap_or_else(|_| String::new());
        MarkovModel::load_encrypted(path, &password).unwrap_or_else(|e| {
            eprintln!("❌ Failed to load encrypted model: {e}");
            std::process::exit(1);
        })
    } else {
        load_model_graceful(path).unwrap_or_else(|e| {
            eprintln!("{e}");
            std::process::exit(1);
        })
    }
}

/// Write filtered suggestions to stdout in the requested format.
fn write_suggestions(
    stdout: &mut std::io::Stdout,
    filtered: &[(String, f32)],
    format: &str,
) {
    use std::io::Write;
    match format {
        "json" => {
            let json_suggestions: Vec<_> = filtered
                .iter()
                .map(|(s, score)| format!(r#"{{"suggestion":"{}","score":{:.4}}}"#, s, score))
                .collect();
            writeln!(stdout, "[{}]", json_suggestions.join(",")).ok();
        }
        "tab" => {
            let tab_line: Vec<_> = filtered.iter().map(|(s, _)| s.as_str()).collect();
            writeln!(stdout, "{}", tab_line.join("\t")).ok();
        }
        _ => {
            for (suggestion, _) in filtered {
                writeln!(stdout, "{suggestion}").ok();
            }
        }
    }
}

/// Stream mode: read prefixes from stdin, output suggestions to stdout
///
/// Model is loaded once and kept in memory for sub-millisecond latency.
/// Each line of input is treated as a prefix, with suggestions output immediately.
///
/// # Protocol
/// - Input: One prefix per line (UTF-8)
/// - Output: Suggestions in specified format, followed by empty line
/// - Special: Empty line or "QUIT" terminates
fn cmd_stream(model_path: &str, count: usize, format: &str, use_password: bool) {
    use std::io::{BufRead, Write};

    let path = expand_path(model_path);
    let model = load_stream_model(&path, use_password);

    eprintln!(
        "🚀 Stream mode ready (model: {} commands)",
        model.total_commands()
    );
    eprintln!("   Enter prefixes, one per line. Empty line or 'QUIT' to exit.");

    let stdin = std::io::stdin();
    let mut stdout = std::io::stdout();

    for line in stdin.lock().lines() {
        let prefix = match line {
            Ok(p) => p,
            Err(_) => break,
        };

        if prefix.is_empty() || prefix.eq_ignore_ascii_case("QUIT") {
            break;
        }

        let prefix = match sanitize_prefix(&prefix) {
            Ok(p) => p,
            Err(_) => {
                writeln!(stdout).ok();
                continue;
            }
        };

        let suggestions = model.suggest(&prefix, count);
        let filtered = filter_sensitive_suggestions(suggestions);
        write_suggestions(&mut stdout, &filtered, format);

        writeln!(stdout).ok();
        stdout.flush().ok();
    }

    eprintln!("👋 Stream mode exiting");
}

/// Load a model for daemon/stream use, handling password and error exits.
#[cfg(unix)]
fn load_daemon_model(path: &std::path::Path, use_password: bool) -> MarkovModel {
    if use_password {
        let password =
            rpassword::prompt_password("🔐 Model password: ").unwrap_or_else(|_| String::new());
        match MarkovModel::load_encrypted(path, &password) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("❌ Failed to load encrypted model: {e}");
                std::process::exit(1);
            }
        }
    } else {
        match load_model_graceful(path) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("{e}");
                std::process::exit(1);
            }
        }
    }
}

/// Result of handling a single daemon request.
#[cfg(unix)]
enum DaemonAction {
    /// Continue accepting connections.
    Continue,
    /// Shut down the daemon.
    Shutdown,
}

/// Handle a single daemon request (special commands or suggestion generation).
#[cfg(unix)]
fn handle_daemon_request(
    prefix: &str,
    stream: &mut std::os::unix::net::UnixStream,
    model: &MarkovModel,
    count: usize,
    start_time: std::time::Instant,
    request_count: u64,
) -> DaemonAction {
    use std::io::Write;

    match prefix.to_uppercase().as_str() {
        "PING" => {
            writeln!(stream, "PONG").ok();
            writeln!(stream).ok();
            return DaemonAction::Continue;
        }
        "QUIT" | "SHUTDOWN" => {
            writeln!(stream, "OK").ok();
            eprintln!("👋 Daemon shutting down (received QUIT)");
            return DaemonAction::Shutdown;
        }
        "STATS" => {
            let uptime = start_time.elapsed().as_secs();
            writeln!(stream, "requests: {request_count}").ok();
            writeln!(stream, "uptime_secs: {uptime}").ok();
            writeln!(stream, "model_commands: {}", model.total_commands()).ok();
            writeln!(stream, "model_ngrams: {}", model.ngram_count()).ok();
            writeln!(stream).ok();
            return DaemonAction::Continue;
        }
        "" => {
            writeln!(stream).ok();
            return DaemonAction::Continue;
        }
        _ => {}
    }

    // Validate and get suggestions
    let suggestions = match sanitize_prefix(prefix) {
        Ok(p) => {
            let raw = model.suggest(&p, count);
            filter_sensitive_suggestions(raw)
        }
        Err(_) => vec![],
    };

    // Send suggestions
    for (suggestion, _) in &suggestions {
        writeln!(stream, "{suggestion}").ok();
    }
    writeln!(stream).ok(); // Empty line terminates response

    DaemonAction::Continue
}

/// Daemon mode: Unix socket server for sub-ms suggestions
///
/// Starts a server that listens on a Unix socket and responds to suggestion requests.
/// Model is loaded once at startup for maximum performance.
///
/// # Protocol (line-based)
/// - Client sends: prefix\n
/// - Server responds: suggestion1\nsuggestion2\n...\n\n (empty line terminates)
/// - Special commands: PING, QUIT, STATS
#[cfg(unix)]
fn cmd_daemon(
    model_path: &str,
    socket_path: &std::path::Path,
    count: usize,
    use_password: bool,
    foreground: bool,
) {
    use std::io::{BufRead, BufReader};
    use std::os::unix::net::UnixListener;

    let path = expand_path(model_path);

    // Remove stale socket if exists
    if socket_path.exists() {
        if let Err(e) = std::fs::remove_file(socket_path) {
            eprintln!("⚠️  Could not remove stale socket: {e}");
        }
    }

    // Load model
    let model = load_daemon_model(&path, use_password);

    // Bind socket
    let listener = match UnixListener::bind(socket_path) {
        Ok(l) => l,
        Err(e) => {
            eprintln!("❌ Failed to bind socket '{}': {e}", socket_path.display());
            eprintln!("   Hint: Check permissions or use a different path");
            std::process::exit(1);
        }
    };

    if foreground {
        eprintln!("🚀 Daemon running in foreground");
    } else {
        println!("🚀 Daemon started");
    }
    println!("   Socket: {}", socket_path.display());
    println!("   Model:  {} commands", model.total_commands());
    println!("   PID:    {}", std::process::id());

    // Write PID file for daemon management
    let pid_path = socket_path.with_extension("pid");
    if let Err(e) = std::fs::write(&pid_path, std::process::id().to_string()) {
        eprintln!("⚠️  Could not write PID file: {e}");
    }

    let mut request_count = 0u64;
    let start_time = std::time::Instant::now();

    // Accept connections
    for stream in listener.incoming() {
        let mut stream = match stream {
            Ok(s) => s,
            Err(e) => {
                eprintln!("⚠️  Connection error: {e}");
                continue;
            }
        };

        let mut reader = BufReader::new(stream.try_clone().expect("clone stream for reader"));

        let mut line = String::new();
        if reader.read_line(&mut line).is_err() {
            continue;
        }

        let prefix = line.trim();
        request_count += 1;

        match handle_daemon_request(prefix, &mut stream, &model, count, start_time, request_count)
        {
            DaemonAction::Shutdown => break,
            DaemonAction::Continue => {}
        }
    }

    // Cleanup
    let _ = std::fs::remove_file(socket_path);
    let _ = std::fs::remove_file(&pid_path);
}

#[cfg(not(unix))]
fn cmd_daemon(
    _model_path: &str,
    _socket_path: &std::path::Path,
    _count: usize,
    _use_password: bool,
    _foreground: bool,
) {
    eprintln!("❌ Daemon mode is only supported on Unix systems");
    eprintln!("   Use 'aprender-shell stream' for cross-platform streaming mode");
    std::process::exit(1);
}

/// Stop the running daemon
fn cmd_daemon_stop(socket_path: &std::path::Path) {
    #[cfg(unix)]
    {
        use std::io::{BufRead, BufReader, Write};
        use std::os::unix::net::UnixStream;

        if !socket_path.exists() {
            eprintln!("❌ Daemon not running (socket not found)");
            std::process::exit(1);
        }

        let mut stream = match UnixStream::connect(socket_path) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("❌ Could not connect to daemon: {e}");
                std::process::exit(1);
            }
        };

        writeln!(stream, "QUIT").ok();
        stream.flush().ok();

        let mut reader = BufReader::new(&stream);
        let mut response = String::new();
        reader.read_line(&mut response).ok();

        if response.trim() == "OK" {
            println!("✅ Daemon stopped");
        } else {
            eprintln!("⚠️  Unexpected response: {response}");
        }
    }

    #[cfg(not(unix))]
    {
        let _ = socket_path;
        eprintln!("❌ Daemon mode is only supported on Unix systems");
        std::process::exit(1);
    }
}