sparrow-cli 0.5.0

A local-first Rust agent cockpit — route, run, replay, rewind
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
//! `sparrow demo` — A self-contained demo that codes a snake game.
//!
//! Shows live progress with module labels ("[Planner] Analyzing...",
//! "[Coder] Writing game.rs...", "[Verifier] Checking...") using colored
//! terminal output. Takes ~30 seconds and is designed to be shared on
//! social media to showcase Sparrow's swarm pipeline.

use std::io::{self, Write};
use std::time::Duration;

/// Run the self-contained demo.
///
/// Simulates Sparrow's Planner → Coder → Verifier pipeline by generating
/// a simple snake game in Rust, compiling it, and optionally running it.
pub async fn run_demo(_skills: Option<&dyn crate::capabilities::SkillLibrary>) -> anyhow::Result<()> {
    use crossterm::style::{Color, SetForegroundColor, ResetColor};
    use crossterm::ExecutableCommand;

    let mut stdout = io::stdout();

    // ── Header ────────────────────────────────────────────────────────
    println!();
    stdout.execute(SetForegroundColor(Color::Cyan))?;
    println!("══════════════════════════════════════════════════");
    println!("  🐦 SPARROW DEMO — Let's code a Snake game !");
    println!("══════════════════════════════════════════════════");
    stdout.execute(ResetColor)?;
    println!();

    // ── Phase 1: Planner ──────────────────────────────────────────────
    phase_header(&mut stdout, "Planner", "Analyse de la demande...", Color::Yellow)?;
    tokio::time::sleep(Duration::from_millis(500)).await;

    let plan = vec![
        "1. Créer un fichier game.rs avec la boucle de jeu",
        "2. Implémenter le serpent (position, direction, croissance)",
        "3. Génération de la pomme aléatoire",
        "4. Gestion des entrées clavier (flèches directionnelles)",
        "5. Détection de collision (murs + auto-collision)",
        "6. Affichage terminal avec crossterm",
    ];

    for step in &plan {
        print!("  ");
        stdout.execute(SetForegroundColor(Color::DarkYellow))?;
        print!("");
        stdout.execute(ResetColor)?;
        println!("{step}");
        io::stdout().flush().ok();
        tokio::time::sleep(Duration::from_millis(400)).await;
    }
    println!();

    // ── Phase 2: Coder ────────────────────────────────────────────────
    phase_header(&mut stdout, "Coder", "Écriture de game.rs...", Color::Green)?;
    tokio::time::sleep(Duration::from_millis(300)).await;

    let game_code = generate_snake_game_code();

    // Show code being "written" with typing effect (truncated for speed)
    let preview_lines: Vec<&str> = game_code.lines().take(8).collect();
    for line in &preview_lines {
        stdout.execute(SetForegroundColor(Color::DarkGreen))?;
        println!("{line}");
        io::stdout().flush().ok();
        tokio::time::sleep(Duration::from_millis(150)).await;
    }
    stdout.execute(SetForegroundColor(Color::DarkGreen))?;
    println!("  │ ... ({} lignes au total)", game_code.lines().count());
    stdout.execute(ResetColor)?;

    // Write the actual file
    let demo_dir = std::env::temp_dir().join("sparrow_demo");
    std::fs::create_dir_all(&demo_dir)?;
    let game_path = demo_dir.join("game.rs");
    std::fs::write(&game_path, &game_code)?;

    println!();
    stdout.execute(SetForegroundColor(Color::Green))?;
    println!("  ✓ Fichier écrit → {}", game_path.display());
    stdout.execute(ResetColor)?;
    println!();

    // ── Phase 3: Verifier ─────────────────────────────────────────────
    phase_header(&mut stdout, "Verifier", "Vérification du code...", Color::Magenta)?;
    tokio::time::sleep(Duration::from_millis(300)).await;

    // Try to compile the game
    let compile_result = compile_snake_game(&demo_dir, &game_path);
    match &compile_result {
        Ok(()) => {
            stdout.execute(SetForegroundColor(Color::Green))?;
            println!("  ✓ Compilation réussie !");
            stdout.execute(ResetColor)?;
        }
        Err(err) => {
            stdout.execute(SetForegroundColor(Color::Red))?;
            println!("  ✗ Compilation échouée : {err}");
            stdout.execute(ResetColor)?;
            println!("  → Le code source reste disponible dans {}", demo_dir.display());
        }
    }

    println!();

    // ── Summary ───────────────────────────────────────────────────────
    stdout.execute(SetForegroundColor(Color::Cyan))?;
    println!("══════════════════════════════════════════════════");
    println!("  🎉 Démo terminée !");
    println!("══════════════════════════════════════════════════");
    stdout.execute(ResetColor)?;
    println!();
    println!("  Planner  : {} étapes planifiées", plan.len());
    println!("  Coder    : {} lignes de code générées", game_code.lines().count());
    println!(
        "  Verifier : {}",
        if compile_result.is_ok() {
            "compilation OK ✓"
        } else {
            "compilation échouée ✗"
        }
    );
    println!();
    println!("  Code source : {}", game_path.display());
    println!();

    // ── Offer to run ──────────────────────────────────────────────────
    if compile_result.is_ok() {
        println!("🐍 Le jeu est prêt ! Veux-tu y jouer maintenant ?");
        print!("Lancer le snake game ? [O/n] ");
        io::stdout().flush().ok();

        let mut answer = String::new();
        io::stdin().read_line(&mut answer)?;

        if !matches!(answer.trim().to_lowercase().as_str(), "n" | "no" | "non") {
            run_compiled_game(&demo_dir)?;
        }
    }

    println!("\n✨ Merci d'avoir testé Sparrow !");
    println!("   → Partage : sparrow share");
    println!("   → Docs    : https://github.com/ucav/Sparrow\n");

    Ok(())
}

// ─── Phase header helper ─────────────────────────────────────────────────────

fn phase_header(
    stdout: &mut io::Stdout,
    label: &str,
    subtitle: &str,
    color: crossterm::style::Color,
) -> io::Result<()> {
    use crossterm::style::{Attribute, SetAttribute, SetForegroundColor, ResetColor};
    use crossterm::ExecutableCommand;

    stdout.execute(SetForegroundColor(color))?;
    stdout.execute(SetAttribute(Attribute::Bold))?;
    print!("[{label}]");
    stdout.execute(ResetColor)?;
    stdout.execute(SetAttribute(Attribute::Reset))?;
    println!(" {subtitle}");
    Ok(())
}

// ─── Snake game code generator ────────────────────────────────────────────────

/// Generate a complete, compilable snake game in Rust using crossterm.
fn generate_snake_game_code() -> String {
    r#"//! Snake Game — Généré par Sparrow Demo
//!
//! Un snake game minimaliste dans le terminal.
//! Contrôles : ← ↑ ↓ → (flèches directionnelles), q pour quitter.

use std::collections::VecDeque;
use std::io::{stdout, Write};
use std::time::{Duration, Instant};

use crossterm::cursor::{Hide, Show};
use crossterm::event::{self, Event, KeyCode};
use crossterm::style::{Color, Print, SetForegroundColor, ResetColor};
use crossterm::terminal::{self, Clear, ClearType};
use crossterm::{ExecutableCommand, QueueableCommand};
use rand::Rng;

const WIDTH: u16 = 40;
const HEIGHT: u16 = 20;
const TICK_MS: u64 = 100;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Direction {
    Up,
    Down,
    Left,
    Right,
}

impl Direction {
    fn opposite(self) -> Self {
        match self {
            Direction::Up => Direction::Down,
            Direction::Down => Direction::Up,
            Direction::Left => Direction::Right,
            Direction::Right => Direction::Left,
        }
    }
}

struct Game {
    snake: VecDeque<(u16, u16)>,
    direction: Direction,
    food: (u16, u16),
    score: u32,
    game_over: bool,
}

impl Game {
    fn new() -> Self {
        let mut rng = rand::thread_rng();
        let start_x = WIDTH / 2;
        let start_y = HEIGHT / 2;
        let mut snake = VecDeque::new();
        snake.push_back((start_x, start_y));
        snake.push_back((start_x - 1, start_y));
        snake.push_back((start_x - 2, start_y));

        let mut game = Self {
            snake,
            direction: Direction::Right,
            food: (rng.gen_range(1..WIDTH - 1), rng.gen_range(1..HEIGHT - 1)),
            score: 0,
            game_over: false,
        };
        game.spawn_food();
        game
    }

    fn spawn_food(&mut self) {
        let mut rng = rand::thread_rng();
        loop {
            let candidate = (rng.gen_range(1..WIDTH - 1), rng.gen_range(1..HEIGHT - 1));
            if !self.snake.contains(&candidate) {
                self.food = candidate;
                break;
            }
        }
    }

    fn tick(&mut self) {
        if self.game_over {
            return;
        }

        let head = *self.snake.front().unwrap();
        let new_head = match self.direction {
            Direction::Up => (head.0, head.1.wrapping_sub(1)),
            Direction::Down => (head.0, head.1 + 1),
            Direction::Left => (head.0.wrapping_sub(1), head.1),
            Direction::Right => (head.0 + 1, head.1),
        };

        // Wall collision
        if new_head.0 == 0 || new_head.0 >= WIDTH - 1 || new_head.1 == 0 || new_head.1 >= HEIGHT - 1
        {
            self.game_over = true;
            return;
        }

        // Self collision
        if self.snake.contains(&new_head) {
            self.game_over = true;
            return;
        }

        self.snake.push_front(new_head);

        if new_head == self.food {
            self.score += 1;
            self.spawn_food();
        } else {
            self.snake.pop_back();
        }
    }

    fn set_direction(&mut self, dir: Direction) {
        if dir != self.direction.opposite() {
            self.direction = dir;
        }
    }

    fn draw(&self, stdout: &mut std::io::Stdout) {
        stdout.queue(Clear(ClearType::All)).unwrap();

        // Draw top wall
        stdout.queue(crossterm::cursor::MoveTo(0, 0)).unwrap();
        stdout
            .queue(SetForegroundColor(Color::DarkBlue))
            .unwrap();
        for _ in 0..WIDTH {
            stdout.queue(Print("█")).unwrap();
        }

        // Draw body
        for y in 1..HEIGHT {
            stdout.queue(crossterm::cursor::MoveTo(0, y)).unwrap();
            stdout
                .queue(SetForegroundColor(Color::DarkBlue))
                .unwrap();
            stdout.queue(Print("█")).unwrap(); // left wall

            for x in 1..WIDTH - 1 {
                if self.snake.contains(&(x, y)) {
                    let is_head = self.snake.front() == Some(&(x, y));
                    stdout
                        .queue(SetForegroundColor(if is_head {
                            Color::Green
                        } else {
                            Color::DarkGreen
                        }))
                        .unwrap();
                    stdout.queue(Print(if is_head { "●" } else { "○" })).unwrap();
                } else if (x, y) == self.food {
                    stdout.queue(SetForegroundColor(Color::Red)).unwrap();
                    stdout.queue(Print("🍎")).unwrap();
                } else {
                    stdout.queue(Print(" ")).unwrap();
                }
            }

            stdout
                .queue(SetForegroundColor(Color::DarkBlue))
                .unwrap();
            stdout.queue(Print("█")).unwrap(); // right wall
        }

        // Draw bottom wall
        stdout.queue(crossterm::cursor::MoveTo(0, HEIGHT)).unwrap();
        stdout
            .queue(SetForegroundColor(Color::DarkBlue))
            .unwrap();
        for _ in 0..WIDTH {
            stdout.queue(Print("█")).unwrap();
        }

        // Score
        stdout
            .queue(crossterm::cursor::MoveTo(0, HEIGHT + 1))
            .unwrap();
        stdout.queue(ResetColor).unwrap();
        stdout
            .queue(Print(format!(
                "Score: {}  |  Flèches: diriger  |  q: quitter",
                self.score
            )))
            .unwrap();

        stdout.flush().unwrap();
    }
}

fn main() -> anyhow::Result<()> {
    let mut stdout = stdout();
    terminal::enable_raw_mode()?;
    stdout.execute(Hide)?;
    stdout.execute(Clear(ClearType::All))?;

    let mut game = Game::new();
    let mut last_tick = Instant::now();

    loop {
        // Handle input
        while event::poll(Duration::from_millis(0))? {
            if let Event::Key(key_event) = event::read()? {
                match key_event.code {
                    KeyCode::Up => game.set_direction(Direction::Up),
                    KeyCode::Down => game.set_direction(Direction::Down),
                    KeyCode::Left => game.set_direction(Direction::Left),
                    KeyCode::Right => game.set_direction(Direction::Right),
                    KeyCode::Char('q') | KeyCode::Char('Q') => {
                        terminal::disable_raw_mode()?;
                        stdout.execute(Show)?;
                        println!("\n👋 Score final : {}\n", game.score);
                        return Ok(());
                    }
                    _ => {}
                }
            }
        }

        // Game tick
        if last_tick.elapsed() >= Duration::from_millis(TICK_MS) {
            game.tick();
            last_tick = Instant::now();
            game.draw(&mut stdout);

            if game.game_over {
                stdout
                    .queue(crossterm::cursor::MoveTo(WIDTH / 2 - 5, HEIGHT / 2))
                    .unwrap();
                stdout.queue(SetForegroundColor(Color::Red)).unwrap();
                stdout.queue(Print("GAME OVER!")).unwrap();
                stdout.queue(ResetColor).unwrap();
                stdout.flush().unwrap();
                std::thread::sleep(Duration::from_secs(2));

                terminal::disable_raw_mode()?;
                stdout.execute(Show)?;
                println!("\n💀 Game Over ! Score final : {}\n", game.score);
                return Ok(());
            }
        }

        std::thread::sleep(Duration::from_millis(1));
    }
}
"#
    .to_string()
}

// ─── Compilation ─────────────────────────────────────────────────────────────

/// Try to compile the generated snake game.
fn compile_snake_game(demo_dir: &std::path::Path, game_path: &std::path::Path) -> anyhow::Result<()> {
    // Check if rustc is available
    let rustc_check = std::process::Command::new("rustc")
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status();

    if rustc_check.map_or(true, |s| !s.success()) {
        anyhow::bail!("rustc n'est pas installé. Installe Rust : https://rustup.rs");
    }

    // Create a minimal Cargo.toml for the demo (crossterm + rand deps)
    let cargo_toml = format!(
        r#"[package]
name = "sparrow-snake"
version = "0.1.0"
edition = "2021"

[dependencies]
crossterm = "0.28"
rand = "0.8"
anyhow = "1"
"#
    );
    std::fs::write(demo_dir.join("Cargo.toml"), cargo_toml)?;

    // Rename game.rs to main.rs in a src/ directory
    let src_dir = demo_dir.join("src");
    std::fs::create_dir_all(&src_dir)?;
    std::fs::copy(game_path, src_dir.join("main.rs"))?;

    // Run cargo build
    let output = std::process::Command::new("cargo")
        .args(["build", "--release"])
        .current_dir(demo_dir)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("Compilation échouée : {stderr}");
    }

    Ok(())
}

/// Run the compiled snake game.
fn run_compiled_game(demo_dir: &std::path::Path) -> anyhow::Result<()> {
    let binary = demo_dir.join("target/release/sparrow-snake");

    if !binary.exists() {
        anyhow::bail!("Binaire introuvable : {}", binary.display());
    }

    println!("\n🐍 Lancement du jeu... (q pour quitter)\n");

    let status = std::process::Command::new(&binary)
        .current_dir(demo_dir)
        .status()?;

    if !status.success() {
        anyhow::bail!("Le jeu a terminé avec une erreur.");
    }

    Ok(())
}