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
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
//! Document text extraction and structure detection.

use std::collections::HashMap;

/// The detected format of a document.
#[derive(Debug, Clone, PartialEq)]
pub enum DocumentFormat {
    PlainText,
    Markdown,
    Html,
    Json,
    Csv,
    Code(String),
    Unknown,
}

/// A section of a parsed document.
#[derive(Debug, Clone)]
pub struct DocumentSection {
    pub title: Option<String>,
    pub content: String,
    /// Heading depth: 0 = body text, 1-6 = heading level.
    pub level: u8,
    pub line_start: usize,
    pub line_end: usize,
}

/// The result of parsing a document.
#[derive(Debug, Clone)]
pub struct ParsedDocument {
    pub format: DocumentFormat,
    pub sections: Vec<DocumentSection>,
    pub metadata: HashMap<String, String>,
    pub word_count: usize,
    pub char_count: usize,
    pub detected_language: Option<String>,
}

/// Stateless document parser.
pub struct DocumentParser;

impl DocumentParser {
    /// Heuristically detect the format of the given text.
    pub fn detect_format(text: &str) -> DocumentFormat {
        let trimmed = text.trim();

        // JSON: starts with { or [
        if (trimmed.starts_with('{') && trimmed.ends_with('}'))
            || (trimmed.starts_with('[') && trimmed.ends_with(']'))
        {
            return DocumentFormat::Json;
        }

        // HTML: contains common HTML tags
        if trimmed.contains("</") && (trimmed.contains("<html") || trimmed.contains("<div") || trimmed.contains("<p>") || trimmed.contains("<span")) {
            return DocumentFormat::Html;
        }

        // Code fence (Markdown code block at top level)
        if trimmed.starts_with("```") {
            let lang = trimmed
                .lines()
                .next()
                .unwrap_or("")
                .trim_start_matches('`')
                .trim()
                .to_string();
            return DocumentFormat::Code(lang);
        }

        // Markdown: has ATX headers or emphasis
        let has_md_header = text.lines().any(|l| l.starts_with('#'));
        let has_md_emphasis = text.contains("**") || text.contains("__");
        let has_code_fence = text.contains("```");
        if has_md_header || has_md_emphasis || has_code_fence {
            return DocumentFormat::Markdown;
        }

        // CSV: multiple lines with consistent comma counts
        let lines: Vec<&str> = text.lines().take(5).collect();
        if lines.len() >= 2 {
            let counts: Vec<usize> = lines.iter().map(|l| l.matches(',').count()).collect();
            let first = counts[0];
            if first > 0 && counts.iter().all(|&c| c == first) {
                return DocumentFormat::Csv;
            }
        }

        DocumentFormat::PlainText
    }

    /// Parse a Markdown document into sections.
    pub fn parse_markdown(text: &str) -> ParsedDocument {
        let mut sections: Vec<DocumentSection> = Vec::new();
        let mut metadata: HashMap<String, String> = HashMap::new();

        let lines: Vec<&str> = text.lines().collect();
        let mut i = 0;

        // YAML front matter
        if lines.first().copied() == Some("---") {
            i = 1;
            while i < lines.len() && lines[i] != "---" {
                let line = lines[i];
                if let Some(colon) = line.find(':') {
                    let key = line[..colon].trim().to_string();
                    let value = line[colon + 1..].trim().to_string();
                    metadata.insert(key, value);
                }
                i += 1;
            }
            if i < lines.len() {
                i += 1; // skip closing ---
            }
        }

        let mut current_title: Option<String> = None;
        let mut current_level: u8 = 0;
        let mut current_content = String::new();
        let mut section_start = i;

        while i < lines.len() {
            let line = lines[i];
            if let Some(header) = line.strip_prefix('#') {
                // Count '#' characters
                let hashes = line.chars().take_while(|&c| c == '#').count() as u8;
                let title = line.trim_start_matches('#').trim().to_string();

                // Save previous section
                if !current_content.trim().is_empty() || current_title.is_some() {
                    sections.push(DocumentSection {
                        title: current_title.clone(),
                        content: current_content.trim().to_string(),
                        level: current_level,
                        line_start: section_start,
                        line_end: i,
                    });
                }

                current_title = Some(title);
                current_level = hashes;
                current_content = String::new();
                section_start = i;
                let _ = header; // suppress unused warning
            } else {
                current_content.push_str(line);
                current_content.push('\n');
            }
            i += 1;
        }

        // Final section
        if !current_content.trim().is_empty() || current_title.is_some() {
            sections.push(DocumentSection {
                title: current_title,
                content: current_content.trim().to_string(),
                level: current_level,
                line_start: section_start,
                line_end: lines.len(),
            });
        }

        let wc = Self::word_count(text);
        let cc = text.chars().count();
        let lang = Self::detect_language(text);

        ParsedDocument {
            format: DocumentFormat::Markdown,
            sections,
            metadata,
            word_count: wc,
            char_count: cc,
            detected_language: lang,
        }
    }

    /// Parse an HTML document into sections.
    pub fn parse_html(text: &str) -> ParsedDocument {
        let mut sections: Vec<DocumentSection> = Vec::new();
        let mut metadata: HashMap<String, String> = HashMap::new();

        // Extract <title>
        if let Some(start) = text.find("<title>") {
            if let Some(end) = text.find("</title>") {
                let title = &text[start + 7..end];
                metadata.insert("title".to_string(), title.trim().to_string());
            }
        }

        // Extract headings h1-h6
        let plain = Self::strip_html_tags(text);
        let lines: Vec<&str> = text.lines().collect();

        for (level, tag) in &[
            (1u8, "h1"),
            (2, "h2"),
            (3, "h3"),
            (4, "h4"),
            (5, "h5"),
            (6, "h6"),
        ] {
            let open = format!("<{}>", tag);
            let close = format!("</{}>", tag);
            let mut search = text;
            let mut offset = 0;
            while let Some(start) = search.find(&open) {
                let abs_start = offset + start;
                let after_open = abs_start + open.len();
                if let Some(end_rel) = text[after_open..].find(&close) {
                    let heading_text = &text[after_open..after_open + end_rel];
                    let heading_text = Self::strip_html_tags(heading_text);
                    let line_start = text[..abs_start].matches('\n').count();
                    sections.push(DocumentSection {
                        title: Some(heading_text),
                        content: String::new(),
                        level: *level,
                        line_start,
                        line_end: line_start + 1,
                    });
                    offset = after_open + end_rel + close.len();
                    search = &text[offset..];
                } else {
                    break;
                }
            }
        }

        // Sort sections by line_start
        sections.sort_by_key(|s| s.line_start);

        // Add a body section with plain text
        if sections.is_empty() {
            sections.push(DocumentSection {
                title: None,
                content: plain.trim().to_string(),
                level: 0,
                line_start: 0,
                line_end: lines.len(),
            });
        }

        let wc = Self::word_count(&plain);
        let cc = plain.chars().count();
        let lang = Self::detect_language(&plain);

        ParsedDocument {
            format: DocumentFormat::Html,
            sections,
            metadata,
            word_count: wc,
            char_count: cc,
            detected_language: lang,
        }
    }

    /// Parse a JSON document, extracting string values and keys as content.
    pub fn parse_json(text: &str) -> ParsedDocument {
        let mut content = String::new();
        let mut in_string = false;
        let mut escape = false;
        let mut current_string = String::new();

        for ch in text.chars() {
            if escape {
                current_string.push(ch);
                escape = false;
                continue;
            }
            match ch {
                '\\' if in_string => escape = true,
                '"' => {
                    if in_string {
                        // end of string
                        content.push_str(&current_string);
                        content.push(' ');
                        current_string.clear();
                        in_string = false;
                    } else {
                        in_string = true;
                    }
                }
                _ if in_string => current_string.push(ch),
                _ => {}
            }
        }

        let wc = Self::word_count(&content);
        let cc = content.chars().count();
        let lang = Self::detect_language(&content);

        let section = DocumentSection {
            title: Some("JSON Content".to_string()),
            content: content.trim().to_string(),
            level: 0,
            line_start: 0,
            line_end: text.lines().count(),
        };

        ParsedDocument {
            format: DocumentFormat::Json,
            sections: vec![section],
            metadata: HashMap::new(),
            word_count: wc,
            char_count: cc,
            detected_language: lang,
        }
    }

    /// Parse a CSV document, treating the header row as section titles.
    pub fn parse_csv(text: &str) -> ParsedDocument {
        let mut sections: Vec<DocumentSection> = Vec::new();
        let mut lines = text.lines().enumerate();

        let headers: Vec<String> = if let Some((_, header_line)) = lines.next() {
            header_line.split(',').map(|s| s.trim().to_string()).collect()
        } else {
            return ParsedDocument {
                format: DocumentFormat::Csv,
                sections: vec![],
                metadata: HashMap::new(),
                word_count: 0,
                char_count: 0,
                detected_language: None,
            };
        };

        let mut rows: Vec<Vec<String>> = Vec::new();
        for (_, line) in lines {
            let row: Vec<String> = line.split(',').map(|s| s.trim().to_string()).collect();
            rows.push(row);
        }

        for (col_idx, header) in headers.iter().enumerate() {
            let col_content: Vec<String> = rows
                .iter()
                .filter_map(|r| r.get(col_idx).cloned())
                .filter(|s| !s.is_empty())
                .collect();
            let content = col_content.join(", ");
            sections.push(DocumentSection {
                title: Some(header.clone()),
                content,
                level: 1,
                line_start: 0,
                line_end: rows.len() + 1,
            });
        }

        let all_content: String = sections.iter().map(|s| s.content.as_str()).collect::<Vec<_>>().join(" ");
        let wc = Self::word_count(&all_content);
        let cc = text.chars().count();
        let lang = Self::detect_language(&all_content);

        ParsedDocument {
            format: DocumentFormat::Csv,
            sections,
            metadata: HashMap::new(),
            word_count: wc,
            char_count: cc,
            detected_language: lang,
        }
    }

    /// Auto-detect format and dispatch to the appropriate parser.
    pub fn parse(text: &str) -> ParsedDocument {
        match Self::detect_format(text) {
            DocumentFormat::Markdown => Self::parse_markdown(text),
            DocumentFormat::Html => Self::parse_html(text),
            DocumentFormat::Json => Self::parse_json(text),
            DocumentFormat::Csv => Self::parse_csv(text),
            _ => {
                // Plain text: one section
                let wc = Self::word_count(text);
                let cc = text.chars().count();
                let lang = Self::detect_language(text);
                let lines = text.lines().count();
                ParsedDocument {
                    format: DocumentFormat::PlainText,
                    sections: vec![DocumentSection {
                        title: None,
                        content: text.to_string(),
                        level: 0,
                        line_start: 0,
                        line_end: lines,
                    }],
                    metadata: HashMap::new(),
                    word_count: wc,
                    char_count: cc,
                    detected_language: lang,
                }
            }
        }
    }

    /// Extract all URLs (http/https) and Markdown-style links from text.
    pub fn extract_links(text: &str) -> Vec<String> {
        let mut links: Vec<String> = Vec::new();

        // Markdown links: [text](url)
        let mut rest = text;
        while let Some(open) = rest.find("](") {
            let after = &rest[open + 2..];
            if let Some(close) = after.find(')') {
                let url = &after[..close];
                if url.starts_with("http://") || url.starts_with("https://") || url.starts_with('/') {
                    links.push(url.to_string());
                }
                rest = &after[close + 1..];
            } else {
                break;
            }
        }

        // Bare URLs
        let words = text.split_whitespace();
        for word in words {
            let word = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '/' && c != ':' && c != '.' && c != '-' && c != '_' && c != '?' && c != '=' && c != '&' && c != '#' && c != '%');
            if word.starts_with("http://") || word.starts_with("https://") {
                if !links.contains(&word.to_string()) {
                    links.push(word.to_string());
                }
            }
        }

        links
    }

    /// Extract code blocks as (language, code) pairs.
    pub fn extract_code_blocks(text: &str) -> Vec<(String, String)> {
        let mut blocks: Vec<(String, String)> = Vec::new();
        let mut rest = text;

        while let Some(start) = rest.find("```") {
            let after_fence = &rest[start + 3..];
            // Language identifier is on the first line after ```
            let newline = after_fence.find('\n').unwrap_or(after_fence.len());
            let lang = after_fence[..newline].trim().to_string();
            let code_start = newline + 1;
            if code_start >= after_fence.len() {
                break;
            }
            let code_rest = &after_fence[code_start..];
            if let Some(end) = code_rest.find("```") {
                let code = code_rest[..end].to_string();
                blocks.push((lang, code));
                rest = &code_rest[end + 3..];
            } else {
                break;
            }
        }

        blocks
    }

    /// Simple heuristic language detection based on common words.
    pub fn detect_language(text: &str) -> Option<String> {
        let lower = text.to_lowercase();
        let words: Vec<&str> = lower.split_whitespace().collect();

        let score = |list: &[&str]| -> usize {
            words.iter().filter(|w| list.contains(w)).count()
        };

        let english = ["the", "is", "are", "was", "were", "and", "or", "in", "of", "to", "a", "an", "it", "that", "this"];
        let french = ["le", "la", "les", "est", "sont", "et", "ou", "dans", "de", "du", "un", "une", "il", "elle", "que"];
        let spanish = ["el", "la", "los", "las", "es", "son", "y", "o", "en", "de", "un", "una", "que", "se", "no"];
        let german = ["der", "die", "das", "ist", "sind", "und", "oder", "in", "von", "zu", "ein", "eine", "er", "sie", "es"];
        // Chinese: look for CJK characters
        let chinese_chars = text.chars().filter(|&c| c >= '\u{4E00}' && c <= '\u{9FFF}').count();

        let scores = [
            ("English", score(&english)),
            ("French", score(&french)),
            ("Spanish", score(&spanish)),
            ("German", score(&german)),
        ];

        if chinese_chars > 5 {
            return Some("Chinese".to_string());
        }

        let best = scores.iter().max_by_key(|&&(_, s)| s);
        if let Some(&(lang, s)) = best {
            if s > 0 {
                return Some(lang.to_string());
            }
        }
        None
    }

    /// Count words in text.
    pub fn word_count(text: &str) -> usize {
        text.split_whitespace().count()
    }

    /// Strip HTML tags from a string.
    pub fn strip_html_tags(html: &str) -> String {
        let mut result = String::new();
        let mut in_tag = false;
        for ch in html.chars() {
            match ch {
                '<' => in_tag = true,
                '>' => {
                    in_tag = false;
                    result.push(' ');
                }
                _ if !in_tag => result.push(ch),
                _ => {}
            }
        }
        // Collapse multiple spaces
        let mut out = String::new();
        let mut prev_space = false;
        for ch in result.chars() {
            if ch.is_whitespace() {
                if !prev_space {
                    out.push(' ');
                }
                prev_space = true;
            } else {
                out.push(ch);
                prev_space = false;
            }
        }
        out
    }

    /// Split text into sentences.
    pub fn split_sentences(text: &str) -> Vec<String> {
        let mut sentences: Vec<String> = Vec::new();
        let mut current = String::new();

        for ch in text.chars() {
            current.push(ch);
            if ch == '.' || ch == '!' || ch == '?' {
                let trimmed = current.trim().to_string();
                if !trimmed.is_empty() {
                    sentences.push(trimmed);
                }
                current.clear();
            }
        }

        let trimmed = current.trim().to_string();
        if !trimmed.is_empty() {
            sentences.push(trimmed);
        }

        sentences
    }
}

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

    #[test]
    fn test_detect_format_markdown() {
        let md = "# Hello\n\nSome **bold** text.";
        assert_eq!(DocumentParser::detect_format(md), DocumentFormat::Markdown);
    }

    #[test]
    fn test_detect_format_json() {
        let json = r#"{"key": "value"}"#;
        assert_eq!(DocumentParser::detect_format(json), DocumentFormat::Json);
    }

    #[test]
    fn test_word_count() {
        assert_eq!(DocumentParser::word_count("hello world foo"), 3);
    }

    #[test]
    fn test_strip_html_tags() {
        let html = "<h1>Title</h1><p>Body text.</p>";
        let plain = DocumentParser::strip_html_tags(html);
        assert!(plain.contains("Title"));
        assert!(plain.contains("Body text."));
    }

    #[test]
    fn test_extract_links() {
        let text = "Visit [docs](https://example.com) for more info. Also see https://rust-lang.org";
        let links = DocumentParser::extract_links(text);
        assert!(!links.is_empty());
    }

    #[test]
    fn test_detect_language_english() {
        let text = "The quick brown fox is and the lazy dog was in the field";
        assert_eq!(DocumentParser::detect_language(text), Some("English".to_string()));
    }

    #[test]
    fn test_parse_markdown() {
        let md = "# Title\n\nBody text\n\n## Section\n\nMore content.";
        let doc = DocumentParser::parse_markdown(md);
        assert_eq!(doc.format, DocumentFormat::Markdown);
        assert!(!doc.sections.is_empty());
    }

    #[test]
    fn test_extract_code_blocks() {
        let md = "Some text\n```rust\nfn main() {}\n```\nMore text";
        let blocks = DocumentParser::extract_code_blocks(md);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].0, "rust");
    }

    #[test]
    fn test_split_sentences() {
        let text = "Hello world. How are you? I am fine!";
        let sents = DocumentParser::split_sentences(text);
        assert_eq!(sents.len(), 3);
    }
}