eryx 0.4.9

A Python sandbox with async callbacks powered by WebAssembly
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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Example showing trace events with a colorful terminal UI.
//!
//! This example demonstrates what trace events are emitted for different
//! Python code patterns, with a nice visual display showing execution progress.
//!
//! Run with: `cargo run --example trace_events --features embedded-runtime`

#![allow(clippy::expect_used)]

use std::future::Future;
use std::io::{Write, stdout};
use std::pin::Pin;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use async_trait::async_trait;
use crossterm::{
    cursor::{Hide, MoveToColumn, MoveUp, Show},
    execute,
    style::{Color, Print, ResetColor, SetForegroundColor, Stylize},
    terminal::{Clear, ClearType},
};
use eryx::{
    CallbackError, JsonSchema, Sandbox, TraceEvent, TraceEventKind, TraceHandler, TypedCallback,
};
use serde::Deserialize;
use serde_json::{Value, json};

// =============================================================================
// Test Callbacks
// =============================================================================

struct SucceedCallback;

impl TypedCallback for SucceedCallback {
    type Args = ();

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

    fn description(&self) -> &str {
        "Always succeeds"
    }

    fn invoke_typed(
        &self,
        _args: (),
    ) -> Pin<Box<dyn Future<Output = Result<Value, CallbackError>> + Send + '_>> {
        Box::pin(async move { Ok(json!({"status": "ok"})) })
    }
}

#[derive(Deserialize, JsonSchema)]
struct EchoArgs {
    message: String,
}

struct EchoCallback;

impl TypedCallback for EchoCallback {
    type Args = EchoArgs;

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

    fn description(&self) -> &str {
        "Echoes the message"
    }

    fn invoke_typed(
        &self,
        args: EchoArgs,
    ) -> Pin<Box<dyn Future<Output = Result<Value, CallbackError>> + Send + '_>> {
        Box::pin(async move { Ok(json!({"echoed": args.message})) })
    }
}

// =============================================================================
// Visual Trace Handler
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LineState {
    Pending,
    Current,
    Executed,
}

#[derive(Clone)]
struct VisualTraceHandler {
    script_lines: Arc<Vec<String>>,
    line_states: Arc<Mutex<Vec<LineState>>>,
    event_count: Arc<AtomicU32>,
    events: Arc<Mutex<Vec<(String, Color)>>>,
    delay_ms: u64,
    title: Arc<String>,
    num_lines: usize,
}

impl VisualTraceHandler {
    fn new(script: &str, delay_ms: u64, title: &str) -> Self {
        let lines: Vec<String> = script.lines().map(|s| s.to_string()).collect();
        let line_count = lines.len();
        // Total height: title + blank + script lines + blank + progress + blank + events header + 4 events + bottom
        let num_lines = 1 + 1 + line_count + 1 + 1 + 1 + 1 + 4 + 1;
        Self {
            script_lines: Arc::new(lines),
            line_states: Arc::new(Mutex::new(vec![LineState::Pending; line_count])),
            event_count: Arc::new(AtomicU32::new(0)),
            events: Arc::new(Mutex::new(Vec::new())),
            delay_ms,
            title: Arc::new(title.to_string()),
            num_lines,
        }
    }

    fn render(&self) -> std::io::Result<()> {
        let mut stdout = stdout();
        let states = self.line_states.lock().expect("lock line_states");
        let events = self.events.lock().expect("lock events");

        // Count executed lines for progress
        let executed = states.iter().filter(|s| **s == LineState::Executed).count();
        let current = states.iter().filter(|s| **s == LineState::Current).count();
        let total = states.len();
        let progress = if total > 0 {
            ((executed + current) as f64 / total as f64 * 100.0) as u32
        } else {
            0
        };

        // Box dimensions - inner_width is content between │ and │
        let inner_width: usize = 58;
        let code_width: usize = 42; // Width for code display

        // Title bar: ┌─ Title ─────...─┐ must have same width as └─────...─┘
        // Bottom is: └ + (inner_width × ─) + ┘
        // Title is:  ┌ + ─ + " Title " + (padding × ─) + ┐
        // So: 1 + title_text.len() + padding = inner_width
        let title_text = format!(" {} ", self.title);
        let title_padding = inner_width.saturating_sub(1 + title_text.len());
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print("┌─"),
            Print(&title_text),
            Print("".repeat(title_padding)),
            Print("\n"),
            ResetColor
        )?;

        // Blank line
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            Print(format!("{:inner_width$}", "")),
            Print("\n"),
            ResetColor
        )?;

        // Script display with line states
        // Format: "  X NN │ code..." where X is marker, NN is line num
        // Prefix "  X NN │ " = 9 chars, leaving (inner_width - 9) for code
        for (idx, line) in self.script_lines.iter().enumerate() {
            let state = states.get(idx).copied().unwrap_or(LineState::Pending);
            let line_num = idx + 1;

            // Truncate long lines to fit
            let display_line = if line.len() > code_width {
                format!("{}...", &line[..code_width - 3])
            } else {
                format!("{:<code_width$}", line)
            };

            execute!(stdout, SetForegroundColor(Color::Cyan), Print(""))?;

            match state {
                LineState::Pending => {
                    execute!(
                        stdout,
                        SetForegroundColor(Color::DarkGrey),
                        Print(format!("    {:2}{}", line_num, display_line)),
                        ResetColor
                    )?;
                }
                LineState::Current => {
                    execute!(
                        stdout,
                        SetForegroundColor(Color::Yellow),
                        Print(format!("{:2}{}", line_num, display_line).bold()),
                        ResetColor
                    )?;
                }
                LineState::Executed => {
                    execute!(
                        stdout,
                        SetForegroundColor(Color::Green),
                        Print(format!("{:2}{}", line_num, display_line)),
                        ResetColor
                    )?;
                }
            }

            // Pad to inner_width and close: prefix is 9, code is code_width
            let used = 9 + code_width;
            let padding = inner_width.saturating_sub(used);
            execute!(
                stdout,
                Print(format!("{:padding$}", "")),
                SetForegroundColor(Color::Cyan),
                Print("\n"),
                ResetColor
            )?;
        }

        // Blank line
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            Print(format!("{:inner_width$}", "")),
            Print("\n"),
            ResetColor
        )?;

        // Progress bar
        let bar_width = 30;
        let filled = (progress as usize * bar_width) / 100;
        let empty = bar_width - filled;

        let bar_color = if progress < 50 {
            Color::Yellow
        } else if progress < 100 {
            Color::Cyan
        } else {
            Color::Green
        };

        // "  Progress: " = 12, bar = 30, "  NNN%" = 6, total = 48
        let progress_padding = inner_width.saturating_sub(48);
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            ResetColor,
            Print("  Progress: "),
            SetForegroundColor(bar_color),
            Print("".repeat(filled)),
            SetForegroundColor(Color::DarkGrey),
            Print("".repeat(empty)),
            ResetColor,
            Print(format!("  {:3}%", progress)),
            Print(format!("{:progress_padding$}", "")),
            SetForegroundColor(Color::Cyan),
            Print("\n"),
            ResetColor
        )?;

        // Blank line
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            Print(format!("{:inner_width$}", "")),
            Print("\n"),
            ResetColor
        )?;

        // Recent events header: "  Recent Events:" = 16
        let header_padding = inner_width.saturating_sub(16);
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            ResetColor,
            Print("  "),
            Print("Recent Events:".bold()),
            Print(format!("{:header_padding$}", "")),
            SetForegroundColor(Color::Cyan),
            Print("\n"),
            ResetColor
        )?;

        // Recent events (last 4): "    " prefix = 4, text up to 50
        let event_text_width = 50;
        let event_padding = inner_width.saturating_sub(4 + event_text_width);
        let recent: Vec<_> = events.iter().rev().take(4).collect();
        for i in 0..4 {
            execute!(
                stdout,
                SetForegroundColor(Color::Cyan),
                Print(""),
                ResetColor,
                Print("    ")
            )?;

            if let Some((text, color)) = recent.get(3 - i) {
                let display_text = if text.len() > event_text_width {
                    format!("{}...", &text[..event_text_width - 3])
                } else {
                    format!("{:<event_text_width$}", text)
                };
                execute!(
                    stdout,
                    SetForegroundColor(*color),
                    Print(&display_text),
                    ResetColor
                )?;
            } else {
                execute!(stdout, Print(format!("{:event_text_width$}", "")))?;
            }

            execute!(
                stdout,
                Print(format!("{:event_padding$}", "")),
                SetForegroundColor(Color::Cyan),
                Print("\n"),
                ResetColor
            )?;
        }

        // Bottom border
        execute!(
            stdout,
            SetForegroundColor(Color::Cyan),
            Print(""),
            Print("".repeat(inner_width)),
            Print("\n"),
            ResetColor
        )?;

        stdout.flush()?;
        Ok(())
    }

    fn mark_all_executed(&self) {
        let mut states = self.line_states.lock().expect("lock line_states");
        for state in states.iter_mut() {
            if *state == LineState::Current || *state == LineState::Pending {
                *state = LineState::Executed;
            }
        }
    }
}

#[async_trait]
impl TraceHandler for VisualTraceHandler {
    async fn on_trace(&self, event: TraceEvent) {
        let count = self.event_count.fetch_add(1, Ordering::SeqCst) + 1;

        // Update line states
        if event.lineno > 0 {
            let mut states = self.line_states.lock().expect("lock line_states");
            let line_idx = (event.lineno - 1) as usize;

            // Mark previous current line as executed
            for state in states.iter_mut() {
                if *state == LineState::Current {
                    *state = LineState::Executed;
                }
            }

            // Mark new current line
            if line_idx < states.len() && matches!(event.event, TraceEventKind::Line) {
                states[line_idx] = LineState::Current;
            }
        }

        // Log the event with color
        let (desc, color) = match &event.event {
            TraceEventKind::Line => (
                format!("[{:2}] LINE {}", count, event.lineno),
                Color::Yellow,
            ),
            TraceEventKind::Call { function } => {
                (format!("[{:2}] CALL {}()", count, function), Color::Green)
            }
            TraceEventKind::Return { function } => {
                (format!("[{:2}] RETURN {}()", count, function), Color::Blue)
            }
            TraceEventKind::Exception { message } => {
                (format!("[{:2}] EXCEPTION: {}", count, message), Color::Red)
            }
            TraceEventKind::CallbackStart { name } => {
                (format!("[{:2}] → CALLBACK {}", count, name), Color::Magenta)
            }
            TraceEventKind::CallbackEnd { name, duration_ms } => (
                format!("[{:2}] ← CALLBACK {} ({}ms)", count, name, duration_ms),
                Color::Cyan,
            ),
        };

        self.events.lock().expect("lock events").push((desc, color));

        // Re-render in place: move cursor up, clear lines, then render
        let mut stdout = stdout();
        let _ = execute!(stdout, MoveUp(self.num_lines as u16));
        // Clear each line before re-rendering to avoid artifacts
        for _ in 0..self.num_lines {
            let _ = execute!(
                stdout,
                MoveToColumn(0),
                Clear(ClearType::CurrentLine),
                Print("\n")
            );
        }
        let _ = execute!(stdout, MoveUp(self.num_lines as u16));
        let _ = self.render();
        let _ = stdout.flush(); // Ensure output is visible immediately

        // Add a delay for visual effect (this is what makes it visible!)
        if self.delay_ms > 0 {
            thread::sleep(Duration::from_millis(self.delay_ms));
        }
    }
}

// =============================================================================
// Example Runner
// =============================================================================

async fn run_visual_example(
    title: &str,
    script: &str,
    callbacks: Vec<Box<dyn eryx::Callback>>,
    delay_ms: u64,
) -> anyhow::Result<String> {
    let handler = VisualTraceHandler::new(script, delay_ms, title);
    let mut stdout = stdout();

    // Initial render
    handler.render()?;

    // Build sandbox with embedded runtime for fast loading
    let sandbox = Sandbox::embedded()
        .with_trace_handler(handler.clone())
        .with_callbacks(callbacks)
        .build()?;
    let result = sandbox.execute(script).await?;

    // Mark all as executed and do final render
    handler.mark_all_executed();

    // Move up, clear, and render final state
    execute!(stdout, MoveUp(handler.num_lines as u16))?;
    for _ in 0..handler.num_lines {
        execute!(
            stdout,
            MoveToColumn(0),
            Clear(ClearType::CurrentLine),
            Print("\n")
        )?;
    }
    execute!(stdout, MoveUp(handler.num_lines as u16))?;
    handler.render()?;

    Ok(result.stdout)
}

fn print_header(text: &str) -> std::io::Result<()> {
    let mut stdout = stdout();
    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Cyan),
        Print("══════════════════════════════════════════════════════════════\n"),
        Print(format!("  {}\n", text).bold()),
        Print("══════════════════════════════════════════════════════════════"),
        ResetColor
    )?;
    println!();
    Ok(())
}

fn print_section(text: &str) -> std::io::Result<()> {
    let mut stdout = stdout();
    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Yellow),
        Print(format!("── {} ──", text).bold()),
        ResetColor
    )?;
    println!();
    println!();
    Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut stdout = stdout();

    // Hide cursor during execution
    execute!(stdout, Hide)?;

    // Ensure we show cursor on exit
    let result = run_demo().await;

    execute!(stdout, Show)?;

    result
}

async fn run_demo() -> anyhow::Result<()> {
    let mut stdout = stdout();

    // Clear screen for clean start
    execute!(stdout, Clear(ClearType::All))?;
    execute!(stdout, crossterm::cursor::MoveTo(0, 0))?;

    print_header("Eryx Trace Events - Visual Demo")?;

    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::DarkGrey),
        Print("This demo shows real-time execution tracing with colorful visualization.\n"),
        Print("Watch as each line is highlighted during execution!\n"),
        ResetColor
    )?;

    // Configurable delay (ms per event) - higher = slower, more visible progress
    let delay_ms = 200;

    // Example 1: Simple assignment
    print_section("Example 1: Simple Assignment")?;

    let output = run_visual_example(
        "Simple Assignment",
        "x = 42\ny = x * 2\nz = x + y",
        vec![],
        delay_ms,
    )
    .await?;

    if !output.is_empty() {
        execute!(
            stdout,
            Print("Output: ".bold()),
            SetForegroundColor(Color::White),
            Print(output.trim()),
            ResetColor,
            Print("\n")
        )?;
    }

    thread::sleep(Duration::from_millis(800));

    // Example 2: Loop
    print_section("Example 2: Loop Execution")?;

    let output = run_visual_example(
        "Loop",
        "total = 0\nfor i in range(3):\n    total += i\nprint(total)",
        vec![],
        delay_ms,
    )
    .await?;

    if !output.is_empty() {
        execute!(
            stdout,
            Print("Output: ".bold()),
            SetForegroundColor(Color::White),
            Print(output.trim()),
            ResetColor,
            Print("\n")
        )?;
    }

    thread::sleep(Duration::from_millis(800));

    // Example 3: Function definition and call
    print_section("Example 3: Function Call")?;

    let output = run_visual_example(
        "Function Call",
        "def greet(name):\n    msg = f'Hello, {name}!'\n    return msg\n\nresult = greet('World')\nprint(result)",
        vec![],
        delay_ms,
    )
    .await?;

    if !output.is_empty() {
        execute!(
            stdout,
            Print("Output: ".bold()),
            SetForegroundColor(Color::White),
            Print(output.trim()),
            ResetColor,
            Print("\n")
        )?;
    }

    thread::sleep(Duration::from_millis(800));

    // Example 4: Callback
    print_section("Example 4: Async Callback")?;

    let output = run_visual_example(
        "Async Callback",
        "result = await succeed()\nprint('Done!')",
        vec![Box::new(SucceedCallback)],
        delay_ms,
    )
    .await?;

    if !output.is_empty() {
        execute!(
            stdout,
            Print("Output: ".bold()),
            SetForegroundColor(Color::White),
            Print(output.trim()),
            ResetColor,
            Print("\n")
        )?;
    }

    thread::sleep(Duration::from_millis(800));

    // Example 5: Multiple callbacks
    print_section("Example 5: Multiple Callbacks")?;

    let output = run_visual_example(
        "Multiple Callbacks",
        "a = await echo(message='Hello')\nb = await succeed()\nprint('All done!')",
        vec![Box::new(EchoCallback), Box::new(SucceedCallback)],
        delay_ms,
    )
    .await?;

    if !output.is_empty() {
        execute!(
            stdout,
            Print("Output: ".bold()),
            SetForegroundColor(Color::White),
            Print(output.trim()),
            ResetColor,
            Print("\n")
        )?;
    }

    // Summary section
    print_section("Summary")?;

    execute!(stdout, Print("Key Points for UI Integration:\n".bold()))?;
    println!();

    execute!(
        stdout,
        SetForegroundColor(Color::Green),
        Print(""),
        ResetColor,
        SetForegroundColor(Color::Cyan),
        Print("TraceHandler.on_trace()"),
        ResetColor,
        Print(" is called in "),
        Print("real-time".bold()),
        Print(" during execution\n")
    )?;

    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Green),
        Print(""),
        ResetColor,
        Print("For line highlighting:\n"),
        SetForegroundColor(Color::DarkGrey),
        Print("      - Filter for "),
        SetForegroundColor(Color::Cyan),
        Print("TraceEventKind::Line"),
        SetForegroundColor(Color::DarkGrey),
        Print(" events\n"),
        Print("      - Ignore events with "),
        SetForegroundColor(Color::Yellow),
        Print("lineno=0"),
        SetForegroundColor(Color::DarkGrey),
        Print(" (module/callback events)\n"),
        Print("      - Use "),
        SetForegroundColor(Color::Cyan),
        Print("event.lineno"),
        SetForegroundColor(Color::DarkGrey),
        Print(" to highlight the current line\n"),
        ResetColor
    )?;

    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Green),
        Print(""),
        ResetColor,
        Print("For callback visualization:\n"),
        SetForegroundColor(Color::DarkGrey),
        Print("      - "),
        SetForegroundColor(Color::Magenta),
        Print("CallbackStart"),
        SetForegroundColor(Color::DarkGrey),
        Print("/"),
        SetForegroundColor(Color::Cyan),
        Print("CallbackEnd"),
        SetForegroundColor(Color::DarkGrey),
        Print(" show when callbacks execute\n"),
        Print("      - "),
        SetForegroundColor(Color::Cyan),
        Print("CallbackEnd"),
        SetForegroundColor(Color::DarkGrey),
        Print(" includes "),
        SetForegroundColor(Color::Yellow),
        Print("duration_ms"),
        SetForegroundColor(Color::DarkGrey),
        Print(" for performance tracking\n"),
        ResetColor
    )?;

    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Green),
        Print(""),
        ResetColor,
        Print("All trace events are also collected in "),
        SetForegroundColor(Color::Cyan),
        Print("result.trace[]"),
        ResetColor,
        Print("\n")
    )?;

    println!();
    execute!(
        stdout,
        SetForegroundColor(Color::Cyan),
        Print("══════════════════════════════════════════════════════════════\n"),
        SetForegroundColor(Color::Green),
        Print("  Demo Complete!".bold()),
        Print("\n"),
        SetForegroundColor(Color::Cyan),
        Print("══════════════════════════════════════════════════════════════"),
        ResetColor
    )?;
    println!();
    println!();

    Ok(())
}