Repository navigation
feat(infinity-agent-cli): render streamed Markdown in the TUI - #196
Draft
MingweiSamuel wants to merge 4 commits into
Draft
MingweiSamuel wants to merge 4 commits into
MingweiSamuel wants to merge 4 commits into
Conversation
Adds a new `infinity-markdown` crate — a streaming Markdown renderer — and wires it into the CLI so model output is styled as it arrives instead of showing raw `**`, `#`, `` ` `` markers. ## `crates/infinity-markdown` (new) * `Renderer::push(&str) -> Vec<Segment>` / `finish()`: feed text in arbitrary chunks; get back runs of text with a `Style` (bold, italic, underline, strikethrough, dim, 16-color fg) and an optional `href`. * Decides what each character means with at most one character of lookahead, so text streams with essentially no delay. Inline styles are *optimistic*: switched on at an opening delimiter (using CommonMark flanking rules, which only need the previous/next char) and off at the closer or end of line — an unclosed `**` styles the rest of its line rather than ever buffering a paragraph. * Only two constructs are held back: link text (the URL follows it) and block prefixes (`- `, `1. `, `# `, `---`, fence lines) for the few chars needed to classify the line. * Supported: headings, bullet/ordered/task lists (`•`, `☐`, `☑`), block quotes (`│` gutter), thematic breaks, fenced code blocks (framed with dim rules and the language label, contents verbatim), `*`/`_` emphasis, strong, `~~strike~~`, code spans, backslash escapes, links/images (text + dim ` (url)` when they differ). Unknown syntax passes through. * `Theme` makes the per-construct styles configurable; `set_width` controls rule/fence width. * Tests include a chunking-transparency check: per-char and every two-way split of several documents must render identically to a single push. ## `crates/infinity-agent-cli` * New `markdown` module maps `infinity_markdown::Style` to ratatui styles and prints segments via `print_spans_above` (so only SGR sequences reach the viewport's `OutputTracker`). * `terminal.rs`: root-thread `TextChunk`s go through the renderer; `end_stream` and `ResponseDone` call `finish()` to flush held text. Child-thread text (status line) and ACP output are untouched. * New TUI snapshot test `markdown_streams_rendered` with chunk boundaries inside `**` and `](`. Not in this change (next stages): tables (box drawing), OSC 8 hyperlinks (needs `OutputTracker` to pass OSC through), a raw-markdown toggle. Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Follow-up to the streaming Markdown rendering: italics/bold stayed on after the closing delimiter until the end of the line. ## Root cause `InlineViewport::write_spans` computed each span's modifiers as a delta on the *previous span* (`next = mods; next.insert(add); next.remove(sub)`), so a plain span after an italic one never removed `ITALIC`. Span styles are meant to be absolute (ratatui patches each span onto the line style, not onto its neighbour). The bug was latent: until now no caller ever turned a modifier off mid-line. Fix: `next = span.style.add_modifier - span.style.sub_modifier`. ## Tests * `inline_viewport::tests`: byte-level checks that `write_spans` emits the SGR "off" codes (`23`, `22`) between spans. * Test harness: new `Emulator::row_attrs` (vt100 + alacritty) and `TuiHarness::screen_with_attrs`, rendering a per-cell attribute grid (`B`old/`I`talic/`U`nderline/`S`trike/`D`im/`C`olored) under the screen — the text-only snapshots could not see this class of bug. * `tui_snapshots::markdown_styles_switch_off`: streams a response mixing emphasis with plain text, lists, quotes, headings, links and an unclosed `*`, and snapshots the attribute grid. Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Stage 2 of Markdown rendering in the CLI: pipe tables are drawn as boxed grids instead of raw `| a | b |` text. ## Behaviour * A line starting with `|` is held; if the next line is a delimiter row (`|---|:-:|--:|`) the table starts, otherwise the line is released as text. The candidate is rejected as early as the first character that can't belong to a delimiter row. * Rows accumulate while lines start with `|`. The table is drawn on the first non-row line, a blank line, or `finish()` — including the common case of a message ending in a table row with no trailing newline. * Cells get full inline rendering (bold, code, links); header cells use `Theme::table_header` (bold), borders `Theme::table_border`. * Column widths are the natural (widest cell) widths, shrunk widest-first to fit the terminal; cell text then word-wraps inside its column (long words split by character; wide chars measured with `unicode-width`). Delimiter-row alignment (left/center/right) is honoured. * Ragged rows are padded/truncated to the header's column count; `\|` is a literal pipe. If even one column of content per cell doesn't fit, the source lines are shown as text. ## Code * New `table.rs`: `parse_row`, `parse_delimiter_row`, `column_widths`, `wrap_cell`, `pad_line`, `border` (pure functions, unit-tested). * `render.rs`: `Line::TableCandidate` / `Line::Table` states, `render_table`, `emit_table_row`, and `render_inline` (renders a cell's inline Markdown in isolation by swapping out the output/inline state). `finish()` now also handles a lone held `|` line. * Tests: renderer-level tables (alignment, wrapping, ragged rows, fallback, termination variants, hold/release timing) plus per-char chunking transparency; CLI snapshot `markdown_tables` streams two tables in awkward chunks and checks the attribute grid. Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Rows whose cells wrap were hard to tell apart; an empty row (`│ │ │`) is now emitted between consecutive body rows. The header separator and outer borders are unchanged, and single-row tables look the same as before. * `render.rs`: `emit_table_blank_row`, called between body rows in `render_table`. * Updated renderer tests and the `markdown_tables` CLI snapshots. Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Deploying infinity with
|
| Latest commit: |
5f72807
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://75a7989b.infinity-dc7.pages.dev |
| Branch Preview URL: | https://mingwei-tui-markdown.infinity-dc7.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Model output in the CLI was printed as raw Markdown (
**bold**,# headings,| tables |). This adds a streaming Markdown renderer and uses it to style agent text as it arrives.New crate:
infinity-markdownA streaming Markdown → styled-segments renderer with no dependencies beyond
unicode-width.Renderer::push(&str) -> Vec<Segment>/finish(): feed text in arbitrary chunks (boundaries may fall inside**,](, etc.); get back runs of text with aStyle(bold, italic, underline, strikethrough, dim, 16-color fg) and an optionalhref.**styles the rest of its line rather than ever buffering a paragraph.-,1.,#,---, fences), link text (the URL follows it), and tables (column widths depend on every cell).•,☐,☑), block quotes (│gutter), thematic breaks, fenced code blocks (framed with dim rules + language label, contents verbatim),*/_emphasis, strong,~~strike~~, code spans, backslash escapes, links/images (text + dim(url)when they differ), and GFM tables. Unknown syntax passes through verbatim.\|is a literal pipe. A|line is released as text as soon as the next line can't be a delimiter row. If even one column of content per cell doesn't fit, the source lines are shown as text.Thememakes per-construct styles configurable;set_widthcontrols rule/fence/table width.infinity-agent-climarkdownmodule mapsinfinity_markdown::Styleto ratatui styles and prints segments viaprint_spans_above(only SGR reaches the viewport'sOutputTracker).terminal.rs: root-threadTextChunks go through the renderer;end_streamandResponseDonecallfinish()to flush held text (e.g. a table at the end of a message). Child-thread status text and ACP output are untouched.inline_viewport::write_spans: each span's modifiers were computed as a delta on the previous span (next = mods; next.insert(add)), so a plain span after an italic one never emitted italic-off; styles accumulated until the end-of-print reset. Span styles are now absolute (add_modifier - sub_modifier), matching ratatui's own semantics. This was latent until the renderer became the first caller to turn a modifier off mid-line.Tests
write_spans: byte-level checks that SGR off-codes (23,22) are emitted between spans.Emulator::row_attrs(vt100 + alacritty) andTuiHarness::screen_with_attrs, rendering a per-cell attribute grid (B/I/U/S/D/C) under the screen so on/off styling is verifiable. New snapshots:markdown_streams_rendered,markdown_styles_switch_off,markdown_tables.Not in this PR
Segment.hrefis already populated; needsOutputTrackerto pass OSC strings through).a | b/--|--).