every-other-token 4.3.2

Watch an LLM answer token by token: a CLI and web UI that intercepts the OpenAI or Anthropic stream, shows per-token confidence and perplexity from logprobs, and rewrites every other token live. Mock provider needs no API key.
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
//! Sandboxed code block extraction and simulation.
//!
//! Extracts fenced code blocks from Markdown text, simulates execution for
//! common patterns, and validates basic syntax properties.

use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Language
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Language {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    Go,
    Bash,
    Unknown(String),
}

impl std::fmt::Display for Language {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Language::Rust => write!(f, "rust"),
            Language::Python => write!(f, "python"),
            Language::JavaScript => write!(f, "javascript"),
            Language::TypeScript => write!(f, "typescript"),
            Language::Go => write!(f, "go"),
            Language::Bash => write!(f, "bash"),
            Language::Unknown(s) => write!(f, "{}", s),
        }
    }
}

// ---------------------------------------------------------------------------
// CodeBlock
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct CodeBlock {
    pub language: Language,
    pub code: String,
    pub start_line: usize,
    pub end_line: usize,
}

// ---------------------------------------------------------------------------
// ExecutionMode
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExecutionMode {
    Simulate,
    Extract,
    Validate,
}

// ---------------------------------------------------------------------------
// SimulationResult
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct SimulationResult {
    pub block: CodeBlock,
    pub output: String,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
    pub execution_ms: u64,
}

// ---------------------------------------------------------------------------
// CodeExtractor
// ---------------------------------------------------------------------------

pub struct CodeExtractor;

impl CodeExtractor {
    /// Extract all fenced code blocks from the given Markdown text.
    pub fn extract(text: &str) -> Vec<CodeBlock> {
        let mut blocks = Vec::new();
        let lines: Vec<&str> = text.lines().collect();
        let mut i = 0;

        while i < lines.len() {
            let line = lines[i].trim();
            if line.starts_with("```") {
                let tag = line.trim_start_matches('`').trim();
                let language = Self::detect_language(tag);
                let start_line = i + 1; // 1-based, line after the opening fence
                i += 1;

                let mut code_lines = Vec::new();
                while i < lines.len() {
                    let inner = lines[i];
                    if inner.trim() == "```" {
                        break;
                    }
                    code_lines.push(inner);
                    i += 1;
                }
                let end_line = i; // line of closing fence

                blocks.push(CodeBlock {
                    language,
                    code: code_lines.join("\n"),
                    start_line,
                    end_line,
                });
            }
            i += 1;
        }

        blocks
    }

    /// Detect programming language from a fence tag string.
    pub fn detect_language(tag: &str) -> Language {
        match tag.to_lowercase().trim() {
            "rust" | "rs" => Language::Rust,
            "python" | "py" => Language::Python,
            "javascript" | "js" => Language::JavaScript,
            "typescript" | "ts" => Language::TypeScript,
            "go" | "golang" => Language::Go,
            "bash" | "sh" | "shell" => Language::Bash,
            other => Language::Unknown(other.to_string()),
        }
    }
}

// ---------------------------------------------------------------------------
// CodeSimulator
// ---------------------------------------------------------------------------

pub struct CodeSimulator {
    pub mode: ExecutionMode,
}

impl CodeSimulator {
    pub fn new(mode: ExecutionMode) -> Self {
        Self { mode }
    }

    /// Simulate execution of a code block, producing plausible output.
    pub fn simulate(&self, block: &CodeBlock) -> SimulationResult {
        let start = std::time::Instant::now();
        let mut output = String::new();
        let mut errors = Vec::new();
        let warnings = Vec::new();

        match &block.language {
            Language::Rust => {
                if block.code.contains("panic!") {
                    errors.push("thread 'main' panicked".to_string());
                } else if block.code.contains("println!") {
                    output = Self::extract_rust_println(&block.code);
                } else {
                    output = "Execution simulated".to_string();
                }
            }
            Language::Python => {
                if block.code.contains("raise") {
                    errors.push("Traceback (most recent call last): RuntimeError".to_string());
                } else if block.code.contains("print(") {
                    output = Self::extract_python_print(&block.code);
                } else {
                    output = "Execution simulated".to_string();
                }
            }
            Language::Bash => {
                if block.code.contains("echo ") {
                    output = Self::extract_bash_echo(&block.code);
                } else if block.code.contains("ls") {
                    output = "[file list simulation]".to_string();
                } else {
                    output = "Execution simulated".to_string();
                }
            }
            _ => {
                output = "Execution simulated".to_string();
            }
        }

        SimulationResult {
            block: block.clone(),
            output,
            errors,
            warnings,
            execution_ms: start.elapsed().as_millis() as u64,
        }
    }

    fn extract_rust_println(code: &str) -> String {
        // Find println!("...") or println!("{}", ...) style
        let mut results = Vec::new();
        for line in code.lines() {
            let trimmed = line.trim();
            if let Some(start) = trimmed.find("println!(") {
                let after = &trimmed[start + 9..];
                // Extract the string literal
                if let Some(content) = Self::extract_string_arg(after) {
                    // Simplistic: just output the literal, stripping format specifiers
                    results.push(content);
                }
            }
        }
        results.join("\n")
    }

    fn extract_python_print(code: &str) -> String {
        let mut results = Vec::new();
        for line in code.lines() {
            let trimmed = line.trim();
            if let Some(start) = trimmed.find("print(") {
                let after = &trimmed[start + 6..];
                if let Some(content) = Self::extract_string_arg(after) {
                    results.push(content);
                }
            }
        }
        results.join("\n")
    }

    fn extract_bash_echo(code: &str) -> String {
        let mut results = Vec::new();
        for line in code.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("echo ") {
                let arg = trimmed[5..].trim().trim_matches('"').trim_matches('\'');
                results.push(arg.to_string());
            }
        }
        results.join("\n")
    }

    fn extract_string_arg(s: &str) -> Option<String> {
        // Find first " or ' and extract until the matching close
        let s = s.trim();
        if s.starts_with('"') {
            let inner = &s[1..];
            if let Some(end) = inner.find('"') {
                return Some(inner[..end].to_string());
            }
        } else if s.starts_with('\'') {
            let inner = &s[1..];
            if let Some(end) = inner.find('\'') {
                return Some(inner[..end].to_string());
            }
        }
        None
    }

    /// Validate basic syntax properties of a code block.
    pub fn validate_syntax(block: &CodeBlock) -> Vec<String> {
        let mut errors = Vec::new();
        let code = &block.code;

        // Check balanced braces
        let open_braces = code.chars().filter(|&c| c == '{').count();
        let close_braces = code.chars().filter(|&c| c == '}').count();
        if open_braces != close_braces {
            errors.push(format!(
                "Unbalanced braces: {} opening, {} closing",
                open_braces, close_braces
            ));
        }

        // Check balanced brackets
        let open_brackets = code.chars().filter(|&c| c == '[').count();
        let close_brackets = code.chars().filter(|&c| c == ']').count();
        if open_brackets != close_brackets {
            errors.push(format!(
                "Unbalanced brackets: {} opening, {} closing",
                open_brackets, close_brackets
            ));
        }

        // Check balanced parens
        let open_parens = code.chars().filter(|&c| c == '(').count();
        let close_parens = code.chars().filter(|&c| c == ')').count();
        if open_parens != close_parens {
            errors.push(format!(
                "Unbalanced parentheses: {} opening, {} closing",
                open_parens, close_parens
            ));
        }

        // Check for unclosed strings (odd number of unescaped double quotes)
        // Simple heuristic: count non-escaped "
        let quote_count = code.chars().filter(|&c| c == '"').count();
        if quote_count % 2 != 0 {
            errors.push("Possible unclosed string: odd number of double-quote characters".to_string());
        }

        errors
    }
}

// ---------------------------------------------------------------------------
// CodeReport
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct CodeReport {
    pub total_blocks: usize,
    pub by_language: HashMap<String, usize>,
    pub total_lines: usize,
    pub syntax_errors: usize,
}

/// Analyze a slice of code blocks and produce a summary report.
pub fn analyze_blocks(blocks: &[CodeBlock]) -> CodeReport {
    let mut by_language: HashMap<String, usize> = HashMap::new();
    let mut total_lines = 0usize;
    let mut syntax_errors = 0usize;

    for block in blocks {
        let lang_key = block.language.to_string();
        *by_language.entry(lang_key).or_insert(0) += 1;

        let lines = block.code.lines().count();
        total_lines += lines;

        let errs = CodeSimulator::validate_syntax(block);
        syntax_errors += errs.len();
    }

    CodeReport {
        total_blocks: blocks.len(),
        by_language,
        total_lines,
        syntax_errors,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    const SAMPLE_MD: &str = r#"
Some text here.

```rust
fn main() {
    println!("Hello, world!");
}
```

Another paragraph.

```python
def greet():
    print("Hello from Python")
```

```bash
echo "Hello from bash"
ls
```
"#;

    #[test]
    fn test_extract_finds_code_blocks() {
        let blocks = CodeExtractor::extract(SAMPLE_MD);
        assert_eq!(blocks.len(), 3);
    }

    #[test]
    fn test_language_detection() {
        assert_eq!(CodeExtractor::detect_language("rust"), Language::Rust);
        assert_eq!(CodeExtractor::detect_language("py"), Language::Python);
        assert_eq!(CodeExtractor::detect_language("js"), Language::JavaScript);
        assert_eq!(CodeExtractor::detect_language("ts"), Language::TypeScript);
        assert_eq!(CodeExtractor::detect_language("go"), Language::Go);
        assert_eq!(CodeExtractor::detect_language("sh"), Language::Bash);
        assert_eq!(
            CodeExtractor::detect_language("haskell"),
            Language::Unknown("haskell".to_string())
        );
    }

    #[test]
    fn test_simulate_rust_println() {
        let block = CodeBlock {
            language: Language::Rust,
            code: r#"fn main() { println!("Hello, world!"); }"#.to_string(),
            start_line: 1,
            end_line: 1,
        };
        let sim = CodeSimulator::new(ExecutionMode::Simulate);
        let result = sim.simulate(&block);
        assert!(result.output.contains("Hello, world!"));
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_simulate_python_print() {
        let block = CodeBlock {
            language: Language::Python,
            code: "print(\"Hello from Python\")".to_string(),
            start_line: 1,
            end_line: 1,
        };
        let sim = CodeSimulator::new(ExecutionMode::Simulate);
        let result = sim.simulate(&block);
        assert!(result.output.contains("Hello from Python"));
    }

    #[test]
    fn test_simulate_bash_echo() {
        let block = CodeBlock {
            language: Language::Bash,
            code: "echo \"Hello from bash\"".to_string(),
            start_line: 1,
            end_line: 1,
        };
        let sim = CodeSimulator::new(ExecutionMode::Simulate);
        let result = sim.simulate(&block);
        assert!(result.output.contains("Hello from bash"));
    }

    #[test]
    fn test_validate_catches_unbalanced_braces() {
        let block = CodeBlock {
            language: Language::Rust,
            code: "fn main() { let x = 1;".to_string(),
            start_line: 1,
            end_line: 1,
        };
        let errors = CodeSimulator::validate_syntax(&block);
        assert!(!errors.is_empty());
        assert!(errors[0].contains("brace"));
    }

    #[test]
    fn test_validate_balanced_code() {
        let block = CodeBlock {
            language: Language::Rust,
            code: "fn main() { let x = 1; }".to_string(),
            start_line: 1,
            end_line: 1,
        };
        let errors = CodeSimulator::validate_syntax(&block);
        assert!(errors.is_empty());
    }

    #[test]
    fn test_analyze_blocks() {
        let blocks = CodeExtractor::extract(SAMPLE_MD);
        let report = analyze_blocks(&blocks);
        assert_eq!(report.total_blocks, 3);
        assert!(report.by_language.contains_key("rust"));
        assert!(report.by_language.contains_key("python"));
        assert!(report.by_language.contains_key("bash"));
    }

    #[test]
    fn test_rust_panic_produces_error() {
        let block = CodeBlock {
            language: Language::Rust,
            code: r#"fn main() { panic!("something went wrong"); }"#.to_string(),
            start_line: 1,
            end_line: 1,
        };
        let sim = CodeSimulator::new(ExecutionMode::Simulate);
        let result = sim.simulate(&block);
        assert!(!result.errors.is_empty());
    }
}