Skip to content

fix: Clipboard image paste fails with AI_DownloadError due to binary corruption and encoding mismatch #3816

Closed

Description

@frankdierolf

1. What's the bug/issue?

When pasting images from clipboard on Linux, the application fails with an AI_DownloadError when attempting to process the image data URL. This is caused by two bugs in the clipboard handling code:

Bug #1: Binary Data Corruption
The Linux clipboard code (Wayland and X11) uses .text() to read binary PNG data, which attempts to decode the binary image bytes as UTF-8 text. This completely corrupts the image data.

Bug #2: Encoding Mismatch
The clipboard code uses base64url encoding (URL-safe variant with - and _ characters) instead of standard base64 encoding (with + and / characters) required by the data URL specification (RFC 2397).

Location: packages/opencode/src/cli/cmd/tui/util/clipboard.ts lines 33-50

2. How to reproduce it

  1. Environment: Opencode v1.0.15, Ubuntu 25.04 (Omabub), in an Alacritty/Zellij terminal session
  2. Take a screenshot using the system shortcut, saving the image (approx. 800KB - 1.5MB PNG) to the clipboard
  3. In the opencode editor (e.g., in a ~sticky session), paste the image using the terminal's paste command/shortcut (e.g., Ctrl+V)

Resulting Error:

AI_DownloadError: Failed to download data:image/png;base64,77-9UE5HDQoaCgAAAA1JSERSAAAB77-9AAABDggGAAAAUe-_ve-_vWcAAAAEc0JJVAgICAh8CGTvv70AAAAZdEVYdFNvZnR3YXJlAGdub21lLXNjcmVlbnNob3Tvv70D77-9PgAAAC10RVh0Q3JlYXRpb24gVGltZQBNb24gMDMgTm92IDIwMjUgMTE6MzE6MTIgQU0gQ0VU77-9SC8eAAAgAElFTkTvv71CYO-_vQ: Error: failed to fetch the data URL
Image

3. What's the expected vs actual behavior?

Expected Behavior:
When an image is pasted from clipboard, it should be:

  1. Read as binary data (not decoded as text)
  2. Encoded using standard base64 (not base64url)
  3. Embedded in a proper data URL format: data:image/png;base64,<standard-base64-data>
  4. Successfully processed by the AI SDK

Actual Behavior:
The application corrupts the binary image data by decoding it as UTF-8 text, then encodes it with base64url instead of standard base64, resulting in an invalid data URL that the AI SDK cannot process.

4. What's the proposed fix?

In packages/opencode/src/cli/cmd/tui/util/clipboard.ts:

For Linux (Wayland, lines 33-35):

  • Change .text() to .arrayBuffer() to preserve binary data
  • Change toString('base64url') to toString('base64')
  • Add validation: if (wayland && wayland.byteLength > 0)

For Linux (X11, lines 37-39):

  • Change .text() to .arrayBuffer() to preserve binary data
  • Change toString('base64url') to toString('base64')
  • Add validation: if (x11 && x11.byteLength > 0)

For Windows (line 50):

  • Change toString('base64url') to toString('base64')

This aligns with how macOS correctly handles clipboard images (line 25) and ensures data URLs comply with RFC 2397.

Activity

  1. added
    opentuiThis relates to changes in v1.0, now that opencode uses opentui
    on Nov 3, 2025
  2. github-actions commented on Nov 3, 2025

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    However, your issue provides much more technical detail about the specific binary corruption and encoding mismatch problems in the Linux clipboard handling code, which makes it a valuable technical deep-dive that the other issues don't provide. The specific fixes you've identified for the .text()/.arrayBuffer() and base64url/base64 encoding issues are not covered in the other reports.

    Feel free to ignore if none of these address your specific case - your detailed technical analysis appears to be unique among the existing issues.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    opentuiThis relates to changes in v1.0, now that opencode uses opentui

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions