Skip to content

feat(tui): color markdown in submitted user messages - #55441

Closed
Nowaker wants to merge 3 commits into
anomalyco:v1from
Nowaker:message-markdown
Closed

Nowaker wants to merge 3 commits into
anomalyco:v1from
Nowaker:message-markdown

Conversation

@Nowaker

@Nowaker Nowaker commented Oct 14, 2026

Copy link
Copy Markdown

Note: I'm aware v1 is now the maintenance branch and new features go to v2 only. This PR is here for others to use, and I'll submit the v2 version once I've migrated my whole plugin system to it.

Issue for this PR

Closes #55439

Builds on the tool output coloring PR (#54968, itself on #54704), whose highlight cache, non-red tool-code palette and wrap-preserving StyledOutput it uses; their commits are the first two here. Review only the last commit.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The AI's replies are markdown; the prompts you sent are plain text. A prompt with a plan, headings or code reads as one grey block.

  • syntax.user_messages in tui.json (default false) runs the markdown grammar over a submitted message and colors headings of every level, numbered and bulleted lists, inline code, fenced code in its language, emphasis, links, quotes and tables.
  • Only colors change. The colors are set on the typed characters, so nothing is hidden (backticks and # are dimmed, not concealed) and the message wraps exactly as the plain text does.
  • People write markdown loosely, so what a strict parser finds by accident stays plain (forgivingMarkdown in util/user-markdown.ts): a line followed by --- or === is not a heading, four leading spaces are not a code block, a quote covers only lines that start with >, and * inside a word (2*3*4) is not emphasis.
  • The palette is the non-red tool-code palette from feat(tui): color the bash tool's command as shell code #54704, extended to the markdown slots: in the transcript red means failure. Messages over 100,000 characters stay plain.

How did you verify your code works?

  • bun typecheck in packages/tui; focused tests pass. user-text.test.tsx compares plain and colored frames at 40, 80 and 160 columns; user-markdown.test.ts runs the real markdown grammar and checks what is kept and what is dropped; tool-code-theme.test.ts checks no markdown slot is red in any built-in theme.
  • A real TUI at 80 and 160 columns, setting off and on: the screen text of the message is byte-identical; headings, list markers, inline code and table headers are colored; done followed by --- stays plain; no cell is red.

Screenshots / recordings

┃  ## Plan for today            <- heading color
┃  1. ble - run `bun test` first <- list marker, inline code colored
┃  2. blah
┃  - a bullet with **bold** word
┃  ### Third level
┃  | col | value |               <- table header colored
┃  done
┃  ---                           <- plain, not a heading

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

This PR is AI generated, but under direct supervision and on request of @Nowaker.

The bash tool's command was the one block of code in a session drawn as
plain text, while write, edit and apply_patch already go through
tree-sitter. It is now the same `<text>$ command</text>` as before, with
each token a colored span, so it wraps, indents and fits the block
exactly as the plain command does; only the colors differ.

The text appears plain at once and takes its colors when the parser
worker answers. Highlighting goes through a cache keyed by language and
content (packages/tui/src/util/highlight-cache.ts), so a remounted
transcript or a reopened session does not reparse every command.
Identical concurrent requests share one round trip, a failed one is
retried, and the cache holds at most 1000 entries or 4M characters.

In the transcript red means a failed or rejected tool call, so the
command is colored with a syntax style that never uses red
(packages/tui/src/theme/tool-code.ts): every syntax slot that comes out
red, or in the theme's error color, takes a non-red alternative, such as
the function color for variables, commands and builtins. A test checks
every built-in theme in dark and light.

nvim-treesitter's bash query narrows its number, constant and shebang
captures with `#lua-match?`, which web-tree-sitter does not evaluate, so
every word came back as a number and every variable as a constant.
packages/tui/src/util/bash-highlight.ts applies those patterns after the
worker answers. It also drops the query's `@spell` captures: the theme
colors spell as plain text, which turned every comment back to the text
color.

`syntax.bash: false` in tui.json restores the plain-text command.
Generic tool output was drawn as plain text, and nothing knows its
type: a plugin tool returns a string and an MCP text result carries no
content type, so a JSON result from `browser` read as a grey wall.

`syntax.tool_output` in tui.json maps a tool name or glob (`browser`,
`github_*`, `*`) to a language id, `auto` or `none`; an exact name wins
over globs and the most specific glob over the rest
(packages/tui/src/util/tool-output.ts). `auto` colors output that is a
JSON object or array: a bounded sniff (bracket pair, at most 100,000
characters, one parse) whose answer is cached by content. Only output
that is shown is sniffed or colored. Unset by default, so output stays
plain as upstream draws it.

The output is one text renderable whose content becomes the same text
as colored chunks when the parser worker answers, so it wraps exactly
as the plain text does (test/cli/tui/tool-output.test.tsx compares the
frames at 40, 80 and 160 columns), and it uses the non-red tool-code
palette, since red means a failed tool call.

The whole output is highlighted, once and cached, even while the block
is collapsed: a preview cut through the middle of a JSON value does not
parse, so the preview takes the whole output's highlights that fall
inside the part it shares with the output, and its ellipsis stays plain.
syntax.user_messages (default false) runs the markdown grammar over a
submitted message and colors headings at every level, numbered and
bulleted lists, inline code, fenced code in its language, emphasis,
links, quotes and tables.

The colors are set on the typed characters through StyledOutput, so
nothing is hidden and the message wraps exactly as the plain text
does. forgivingMarkdown (util/user-markdown.ts) drops what people write
by accident rather than as markdown: setext headings (a line followed
by --- or ===), indented code blocks, a quote's lazy continuation
lines, intra-word emphasis such as 2*3*4, and spell/nospell captures.

The palette is toolCodeTheme, extended to the markdown slots, so a
user message never draws in red, which in the transcript means failure.
Messages over 100,000 characters stay plain.
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Nowaker added a commit to Nowaker/opencode that referenced this pull request Oct 14, 2026
Feature record and session record for syntax.user_messages, syntax.blocks
and api.blocks.register: the Vibeterm route plugin rule (opencode-tools
0497c41), upstream issues anomalyco#55439/anomalyco#55440 and PRs anomalyco#55441/anomalyco#55442 against v1,
vt-292 on desktop and m4max with the gate passed, and the knob enforced
through Vibeterm settings on both hosts. The tool syntax coloring
timeline notes the palette slots and toolInputColors this shares.

AI-Tool: opencode 1.19.0-vt-285-dfb56f76c7
AI-Model: anthropic/claude-opus-5-5
AI-Variant: high
AI-Platform: linux
AI-Harness: Vibeterm 1e793a4-dirty
AI-Session-ID: ses_ec57be11affe3kxXJ4WyM3OwOY
@Nowaker

Nowaker commented Nov 2, 2026

Copy link
Copy Markdown
Author

Closing: this targets the v1 TUI, which upstream now maintains for critical fixes only, so it is not going to be merged. The branch stays on Nowaker/opencode if anyone wants to pick it up.

@Nowaker Nowaker closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant