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

Closed
opened 2026-02-16 17:36:06 -05:00 by yindo · 1 comment
Owner

Originally created by @frankdierolf on GitHub (Nov 3, 2025).

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.

Originally created by @frankdierolf on GitHub (Nov 3, 2025). ### 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 ``` <img width="956" height="1079" alt="Image" src="https://github.com/user-attachments/assets/2099bc64-a957-44ea-952c-7328ba1eef0c" /> ### 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.
yindo added the opentui label 2026-02-16 17:36:06 -05:00
yindo closed this issue 2026-02-16 17:36:06 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Nov 3, 2025):

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

  • #1429: General feature request for supporting pasting images from the clipboard
  • #2878: Similar issue with pasting images not working on WSL2 Wezterm (though that one shows no behavior at all)
  • #1828: Previous bug with pasting images from clipboard (closed)

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.

@github-actions[bot] commented on GitHub (Nov 3, 2025): This issue might be a duplicate of existing issues. Please check: - #1429: General feature request for supporting pasting images from the clipboard - #2878: Similar issue with pasting images not working on WSL2 Wezterm (though that one shows no behavior at all) - #1828: Previous bug with pasting images from clipboard (closed) 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 in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#2524