Skip to content

feat(tui): render local Markdown images in the transcript - #54986

Closed
jlongster wants to merge 1 commit into
devfrom
tui-markdown-images
Closed

jlongster wants to merge 1 commit into
devfrom
tui-markdown-images

Conversation

@jlongster

@jlongster jlongster commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #

Type of change

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

What does this PR do?

The desktop app already shows ![alt](path) images in assistant Markdown. The TUI only showed the alt text as a link. This PR makes the TUI show local images too, using the same path rules as desktop.

  • localImagePath moves from session-ui to @opencode/util/markdown-image. A new imageReadRequest builds the file read the desktop already did: relative paths read from the session directory, absolute paths from their own parent folder. Desktop now calls both shared functions, with no behavior change.
  • In the TUI, the session view gets a small image loader. It reads files with client.api.file.read, so it also works with a remote server. Reads are cached per directory and path, so streaming rebuilds don't fetch again.
  • The loader only accepts PNG, JPEG, WebP and GIF.
  • The plugin Markdown renderNode is wrapped for assistant text and compaction summaries:
    • A paragraph made only of images shows just the images.
    • A paragraph with text keeps its text and shows the images below it.
    • Images are fitted to the column width, capped at a third of the terminal height (16 rows max), and refit on resize.
    • If an image can't be read or decoded, the alt text shows instead, as it did before.
    • Clicking an image opens the existing image preview dialog.
  • It only applies when session.image_preview is on and Markdown is in rendered mode.
  • Remote http(s) images, linked images, mini mode and the btw panel are not changed.

Paragraphs without images, and code-block plugin renderers, go through the same paths as before. The TUI already uses internalBlockMode="top-level", so a renderer that isn't code-block-only doesn't change how blocks are grouped.

How did you verify your code works?

  • bun run check passed.
  • New packages/tui/test/markdown-image.test.tsx covers four cases:
    • relative and absolute paths send the right read request and get fitted;
    • text and remote images are unchanged;
    • a failed read shows the alt text;
    • clicking an image opens the preview.
  • The image tests moved to packages/util/src/markdown-image.test.ts, with new cases for imageReadRequest.
  • Ran the existing plugin-markdown, markdown-streaming and group-view TUI tests.
  • In a test renderer with the block-character fallback: the image drew left-aligned between its text paragraphs and refit when the width shrank.
  • Ran the dev TUI against a live session. The images got correctly sized, left-aligned areas below their paragraphs. The test terminal's screenshot export didn't draw image pixels, so I couldn't check the final look.

Screenshots / recordings

Block-character fallback in the test renderer, at 60 and 14 columns:

Here is the lake:
▟▜▀▀▘▙▀▀▖▀▚▟▛▀▀▀▟▀
▛▙▄▄▞▙▗▌▙▙▜▟▐▄▖▖▜▄
▐▞▌▜▖▟▜▄▗▛▀▀▄▙▄▄▄▄
▜▖▄▟▄▄▀▀▙▄▄▄▄▄▝▀▀▀
▛▞▛▜▛▜▟▘▀▜▀▀▀▙▚▖▖▝
▀▞▜▐▘▄▄▗▜▖▄▖▖▟▄▄▄▄
After

Checklist

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

Added by the opencode bot (AI-generated).

Related: #52492

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as described. I ran the TUI from source with the simulated model replying with an absolute-path PNG between two paragraphs. On the base it shows only the alt text as a link. With this PR the image draws fitted and left-aligned between the paragraphs (screenshots below). The new and moved tests and the plugin-markdown tests pass, and typecheck is clean in tui, util, session-ui and app.

Public API: unchanged. session.image_preview gets only a new description, and localImagePath/imageReadRequest move into the internal @opencode/util package.

One point worth fixing, inline: the per-session read cache never invalidates, so failed and stale reads stick for as long as the session view is open. Small thing: routes/session/index.tsx also includes three formatting-only hunks (BlockTool, ShellDisplay, Read) that aren't part of this feature.

Related: #52492

Before

After

const cached = files.get(key)
if (cached) return { key, bytes: cached }
const bytes = input.read(imageReadRequest(directory, path), { signal: controller.signal })
// Failed reads stay cached so a streaming paragraph does not retry them on every update.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The read is cached permanently per directory + path, including failures. That breaks two common cases:

  • The model often writes the image reference before the tool call that creates the file ("I'll render it to chart.png: ", then a write or bash call). The first render caches the rejected read, so the alt text stays until the session view is remounted.
  • When an agent regenerates chart.png at the same path, the transcript keeps showing the old bytes.

Could failed reads be dropped from files once the stream settles, or the cache be keyed on something that changes when the message completes? That would let a later render retry. The map also holds every image's bytes for the life of the session view with no eviction.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant