chromewright 0.8.0

Browser automation MCP server via Chrome DevTools Protocol (CDP)
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
//! Ratatui projection: nested landmark frames around semantic content.
//!
//! Meaningful landmarks render as nested Ratatui [`Block`] widgets (bordered
//! frames titled with the landmark role) directly around their children. This
//! is not an inspector/sidebar tree. A separate line helper remains available
//! for lightweight inspection; the widget/buffer path uses real Blocks.

use crate::semantic::component::{LandmarkRole, SemanticComponent, SemanticKind};
use crate::semantic::document::SemanticDocument;
use crate::semantic::render::bounds::{RenderError, RenderedOutput, bound_output};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Widget};

/// Deterministic Ratatui view of a [`SemanticDocument`].
///
/// Nested landmarks become real bordered [`Block`]s titled with the landmark
/// role. Content uses normal text; opaque refs are not shown in the visual
/// frame (they remain available via other projections and later TUI chrome).
#[derive(Debug, Clone, Copy)]
pub struct SemanticRatatuiView<'a> {
    document: &'a SemanticDocument,
}

impl<'a> SemanticRatatuiView<'a> {
    /// Borrow a document for widget or line-oriented Ratatui projection.
    pub fn new(document: &'a SemanticDocument) -> Self {
        Self { document }
    }

    /// Flatten the document into display lines (lightweight inspection helper).
    ///
    /// This path uses textual markers and is **not** the widget layout path.
    /// Prefer [`render_ratatui_buffer`] / [`Widget::render`] for real Blocks.
    pub fn lines(&self) -> Vec<String> {
        let mut lines = Vec::new();
        if !self.document.document.title.is_empty() {
            lines.push(self.document.document.title.clone());
            lines.push(String::new());
        }
        for root in &self.document.roots {
            push_component_lines(root, 0, &mut lines);
        }
        lines
    }
}

impl Widget for SemanticRatatuiView<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        let chrome = Block::default().borders(Borders::ALL).title(format!(
            "{} · rev {}",
            truncate_title(&self.document.document.title, area.width as usize),
            truncate_title(&self.document.document.revision, 24)
        ));
        let inner = chrome.inner(area);
        chrome.render(area, buf);
        render_stack(&self.document.roots, inner, buf);
    }
}

/// Render the document into a fixed-size buffer and return cell text rows.
///
/// Uses the widget path (nested landmark [`Block`]s), not the line helper.
pub fn render_ratatui_buffer(
    document: &SemanticDocument,
    width: u16,
    height: u16,
) -> Result<Vec<String>, RenderError> {
    let area = Rect::new(0, 0, width.max(1), height.max(1));
    let mut buffer = Buffer::empty(area);
    SemanticRatatuiView::new(document).render(area, &mut buffer);
    Ok(buffer_to_lines(&buffer, area))
}

/// Line-oriented projection used by golden tests (no absolute terminal required).
pub fn render_ratatui_lines(document: &SemanticDocument) -> Result<RenderedOutput, RenderError> {
    let view = SemanticRatatuiView::new(document);
    let mut content = view.lines().join("\n");
    if !content.ends_with('\n') {
        content.push('\n');
    }
    Ok(bound_output(
        content,
        &document.document.document_id,
        &document.document.revision,
    ))
}

/// Extract printable rows from a ratatui buffer for assertions.
pub fn buffer_to_lines(buffer: &Buffer, area: Rect) -> Vec<String> {
    let mut rows = Vec::with_capacity(area.height as usize);
    for y in area.top()..area.bottom() {
        let mut row = String::with_capacity(area.width as usize);
        for x in area.left()..area.right() {
            let cell = &buffer[(x, y)];
            let symbol = cell.symbol();
            if symbol.is_empty() {
                row.push(' ');
            } else {
                // ratatui may return multi-byte symbols for box-drawing.
                row.push_str(symbol);
            }
        }
        // Trim trailing spaces for stable golden comparisons.
        let trimmed = row.trim_end().to_string();
        rows.push(trimmed);
    }
    // Drop trailing empty rows for stability.
    while rows.last().is_some_and(|r| r.is_empty()) {
        rows.pop();
    }
    rows
}

/// Vertically stack siblings within `area`, rendering landmarks as Blocks.
fn render_stack(components: &[SemanticComponent], area: Rect, buf: &mut Buffer) {
    if area.width == 0 || area.height == 0 || components.is_empty() {
        return;
    }

    let heights: Vec<u16> = components.iter().map(estimate_height).collect();
    let total_needed: u32 = heights.iter().map(|&h| u32::from(h)).sum();
    let available = u32::from(area.height);

    // Scale heights down proportionally when content exceeds the viewport.
    let scaled: Vec<u16> = if total_needed <= available || total_needed == 0 {
        heights
    } else {
        let mut remaining_budget = available;
        let mut remaining_need = total_needed;
        heights
            .iter()
            .enumerate()
            .map(|(i, &h)| {
                if i + 1 == heights.len() {
                    remaining_budget as u16
                } else {
                    let share = u32::from(h)
                        .checked_mul(remaining_budget)
                        .and_then(|n| n.checked_div(remaining_need))
                        .unwrap_or(0);
                    let share = share.max(if h > 0 { 1 } else { 0 }).min(remaining_budget);
                    remaining_budget = remaining_budget.saturating_sub(share);
                    remaining_need = remaining_need.saturating_sub(u32::from(h));
                    share as u16
                }
            })
            .collect()
    };

    let mut y = area.y;
    let bottom = area.bottom();
    for (component, height) in components.iter().zip(scaled) {
        if y >= bottom || height == 0 {
            break;
        }
        let h = height.min(bottom.saturating_sub(y));
        let child_area = Rect {
            x: area.x,
            y,
            width: area.width,
            height: h,
        };
        render_component(component, child_area, buf);
        y = y.saturating_add(h);
    }
}

fn render_component(component: &SemanticComponent, area: Rect, buf: &mut Buffer) {
    if area.width == 0 || area.height == 0 {
        return;
    }

    match component.kind {
        SemanticKind::Landmark => {
            let role = landmark_label(component);
            let block = Block::default().borders(Borders::ALL).title(role);
            let inner = block.inner(area);
            block.render(area, buf);
            render_stack(&component.children, inner, buf);
        }
        SemanticKind::Group => {
            // Groups are not landmark frames; stack children, optionally label.
            if let Some(label) = component.label.as_deref().filter(|s| !s.is_empty())
                && area.height >= 1
            {
                let label_area = Rect {
                    x: area.x,
                    y: area.y,
                    width: area.width,
                    height: 1,
                };
                Paragraph::new(Line::from(Span::raw(format!("[{label}]")))).render(label_area, buf);
                let rest = Rect {
                    x: area.x,
                    y: area.y.saturating_add(1),
                    width: area.width,
                    height: area.height.saturating_sub(1),
                };
                render_stack(&component.children, rest, buf);
                return;
            }
            render_stack(&component.children, area, buf);
        }
        _ => {
            let lines = content_lines(component);
            let text_lines: Vec<Line> = lines
                .into_iter()
                .map(|s| Line::from(Span::raw(s)))
                .collect();
            Paragraph::new(text_lines).render(area, buf);
        }
    }
}

fn estimate_height(component: &SemanticComponent) -> u16 {
    match component.kind {
        SemanticKind::Landmark => {
            // Top + bottom border plus children.
            let children: u16 = component
                .children
                .iter()
                .map(estimate_height)
                .fold(0u16, |acc, h| acc.saturating_add(h));
            children.saturating_add(2).max(3)
        }
        SemanticKind::Group => {
            let label = u16::from(component.label.as_ref().is_some_and(|s| !s.is_empty()));
            let children: u16 = component
                .children
                .iter()
                .map(estimate_height)
                .fold(0u16, |acc, h| acc.saturating_add(h));
            children.saturating_add(label).max(1)
        }
        _ => {
            let n = content_lines(component).len();
            (n as u16).max(1)
        }
    }
}

fn content_lines(component: &SemanticComponent) -> Vec<String> {
    let mut lines = Vec::new();
    push_content_only(component, &mut lines);
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

/// Emit non-landmark content lines (landmarks are handled as Blocks).
fn push_content_only(component: &SemanticComponent, lines: &mut Vec<String>) {
    match component.kind {
        SemanticKind::Landmark => {
            // Should not be called for landmarks in the widget path; defensive.
            for child in &component.children {
                push_content_only(child, lines);
            }
        }
        SemanticKind::Heading => {
            let level = component.attrs.heading_level.unwrap_or(2).clamp(1, 6);
            let hashes = "#".repeat(level as usize);
            lines.push(format!("{} {}", hashes, inline_display(component)));
        }
        SemanticKind::Text => {
            let text = inline_display(component);
            if !text.is_empty() {
                lines.push(text);
            }
        }
        SemanticKind::List => {
            let ordered = component.attrs.ordered.unwrap_or(false);
            let mut index = 1usize;
            for child in &component.children {
                if child.kind == SemanticKind::ListItem {
                    let marker = if ordered {
                        format!("{index}.")
                    } else {
                        "-".to_string()
                    };
                    lines.push(format!("{} {}", marker, inline_display(child)));
                    for nested in &child.children {
                        if nested.kind == SemanticKind::List {
                            // Indent nested list lines.
                            let mut nested_lines = Vec::new();
                            push_content_only(nested, &mut nested_lines);
                            for line in nested_lines {
                                lines.push(format!("  {line}"));
                            }
                        }
                    }
                    index += 1;
                } else {
                    push_content_only(child, lines);
                }
            }
        }
        SemanticKind::ListItem => {
            lines.push(format!("- {}", inline_display(component)));
        }
        SemanticKind::Link => {
            let label = display_text(component);
            let href = component.attrs.href.as_deref().unwrap_or("");
            lines.push(format!("[{label}]({href})"));
        }
        SemanticKind::Image => {
            let alt = component
                .attrs
                .alt
                .as_deref()
                .or(component.label.as_deref())
                .unwrap_or("");
            let src = component.attrs.src.as_deref().unwrap_or("");
            lines.push(format!("![{alt}]({src})"));
        }
        SemanticKind::Input => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            let input_type = component.attrs.input_type.as_deref().unwrap_or("text");
            lines.push(format!("[input {input_type} name={name} value={value}]"));
        }
        SemanticKind::Textarea => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            lines.push(format!("[textarea name={name} value={value}]"));
        }
        SemanticKind::Select => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            lines.push(format!("[select name={name} value={value}]"));
        }
        SemanticKind::Button => {
            let label = display_text(component);
            lines.push(format!("[button {label}]"));
        }
        SemanticKind::Group => {
            if let Some(label) = component.label.as_deref().filter(|s| !s.is_empty()) {
                lines.push(format!("[{label}]"));
            }
            for child in &component.children {
                push_content_only(child, lines);
            }
        }
    }
}

// --- line helper (inspection only; not the widget Block path) ---

fn push_component_lines(component: &SemanticComponent, depth: usize, lines: &mut Vec<String>) {
    match component.kind {
        SemanticKind::Landmark => {
            let role = landmark_label(component);
            let indent = "  ".repeat(depth);
            lines.push(format!("{indent}┌─ {role}"));
            for child in &component.children {
                push_component_lines(child, depth + 1, lines);
            }
            lines.push(format!("{indent}└─ /{role}"));
        }
        SemanticKind::Heading => {
            let level = component.attrs.heading_level.unwrap_or(2).clamp(1, 6);
            let hashes = "#".repeat(level as usize);
            let text = inline_display(component);
            lines.push(format!("{}{} {}", "  ".repeat(depth), hashes, text));
        }
        SemanticKind::Text => {
            let text = inline_display(component);
            if !text.is_empty() {
                lines.push(format!("{}{}", "  ".repeat(depth), text));
            }
        }
        SemanticKind::List => {
            let ordered = component.attrs.ordered.unwrap_or(false);
            let mut index = 1usize;
            for child in &component.children {
                if child.kind == SemanticKind::ListItem {
                    let marker = if ordered {
                        format!("{index}.")
                    } else {
                        "-".to_string()
                    };
                    let text = inline_display(child);
                    lines.push(format!("{}{} {}", "  ".repeat(depth), marker, text));
                    for nested in &child.children {
                        if nested.kind == SemanticKind::List {
                            push_component_lines(nested, depth + 1, lines);
                        }
                    }
                    index += 1;
                } else {
                    push_component_lines(child, depth, lines);
                }
            }
        }
        SemanticKind::ListItem => {
            let text = inline_display(component);
            lines.push(format!("{}- {}", "  ".repeat(depth), text));
        }
        SemanticKind::Link => {
            let label = display_text(component);
            let href = component.attrs.href.as_deref().unwrap_or("");
            lines.push(format!("{}[{label}]({href})", "  ".repeat(depth)));
        }
        SemanticKind::Image => {
            let alt = component
                .attrs
                .alt
                .as_deref()
                .or(component.label.as_deref())
                .unwrap_or("");
            let src = component.attrs.src.as_deref().unwrap_or("");
            lines.push(format!("{}![{alt}]({src})", "  ".repeat(depth)));
        }
        SemanticKind::Input => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            let input_type = component.attrs.input_type.as_deref().unwrap_or("text");
            lines.push(format!(
                "{}[input {input_type} name={name} value={value}]",
                "  ".repeat(depth)
            ));
        }
        SemanticKind::Textarea => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            lines.push(format!(
                "{}[textarea name={name} value={value}]",
                "  ".repeat(depth)
            ));
        }
        SemanticKind::Select => {
            let name = component.attrs.name.as_deref().unwrap_or("");
            let value = component.attrs.value.as_deref().unwrap_or("");
            lines.push(format!(
                "{}[select name={name} value={value}]",
                "  ".repeat(depth)
            ));
        }
        SemanticKind::Button => {
            let label = display_text(component);
            lines.push(format!("{}[button {label}]", "  ".repeat(depth)));
        }
        SemanticKind::Group => {
            let indent = "  ".repeat(depth);
            if let Some(label) = component.label.as_deref().filter(|s| !s.is_empty()) {
                lines.push(format!("{indent}[{label}]"));
            }
            for child in &component.children {
                push_component_lines(child, depth, lines);
            }
        }
    }
}

fn inline_display(component: &SemanticComponent) -> String {
    if component.children.is_empty() {
        return display_text(component).to_string();
    }
    let mut parts = Vec::new();
    for child in &component.children {
        match child.kind {
            SemanticKind::Text => {
                if child.children.is_empty() {
                    if let Some(t) = child.text.as_deref() {
                        parts.push(t.to_string());
                    }
                } else {
                    parts.push(inline_display(child));
                }
            }
            SemanticKind::Link => {
                let label = display_text(child);
                let href = child.attrs.href.as_deref().unwrap_or("");
                parts.push(format!("[{label}]({href})"));
            }
            SemanticKind::Image => {
                let alt = child.attrs.alt.as_deref().unwrap_or("");
                let src = child.attrs.src.as_deref().unwrap_or("");
                parts.push(format!("![{alt}]({src})"));
            }
            SemanticKind::List => {}
            _ => {
                let t = display_text(child);
                if !t.is_empty() {
                    parts.push(t.to_string());
                }
            }
        }
    }
    join_inline_parts(&parts)
}

fn join_inline_parts(parts: &[String]) -> String {
    let mut out = String::new();
    for part in parts {
        if part.is_empty() {
            continue;
        }
        if out.is_empty() {
            out.push_str(part);
            continue;
        }
        let need_space = match (out.chars().next_back(), part.chars().next()) {
            (Some(a), Some(b)) if !a.is_whitespace() && !b.is_whitespace() => {
                !matches!(a, '[' | '(') && !matches!(b, ']' | ')' | ',' | '.' | ';' | ':')
            }
            _ => false,
        };
        if need_space {
            out.push(' ');
        }
        out.push_str(part);
    }
    out
}

fn display_text(component: &SemanticComponent) -> &str {
    component
        .text
        .as_deref()
        .or(component.label.as_deref())
        .or(component.attrs.value.as_deref())
        .or(component.attrs.alt.as_deref())
        .unwrap_or("")
}

fn landmark_label(component: &SemanticComponent) -> &'static str {
    match component.attrs.landmark {
        Some(LandmarkRole::Main) => "main",
        Some(LandmarkRole::Aside) => "aside",
        Some(LandmarkRole::Header) => "header",
        Some(LandmarkRole::Nav) => "nav",
        Some(LandmarkRole::Section) => "section",
        Some(LandmarkRole::Footer) => "footer",
        None => "landmark",
    }
}

fn truncate_title(value: &str, max_chars: usize) -> String {
    if max_chars == 0 {
        return String::new();
    }
    let count = value.chars().count();
    if count <= max_chars {
        return value.to_string();
    }
    if max_chars <= 1 {
        return "".to_string();
    }
    let mut out: String = value.chars().take(max_chars - 1).collect();
    out.push('');
    out
}