[PR #3817] fix: correct clipboard image encoding and binary handling #10773

Closed
opened 2026-02-16 18:15:31 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/3817

State: closed
Merged: Yes


Summary

Fixes clipboard image paste failures on Linux and Windows (#3816) by addressing two critical bugs in clipboard.ts:

Bug #1: Binary Data Corruption (Linux)

  • Problem: Wayland and X11 clipboard reads used .text() on binary PNG data, corrupting it via UTF-8 decoding
  • Fix: Changed to .arrayBuffer() to preserve binary data integrity
  • Added: byteLength > 0 validation for empty buffer checks

Bug #2: Encoding Mismatch (All platforms)

  • Problem: Used base64url encoding (URL-safe with - and _) instead of standard base64 (with + and /)
  • Fix: Changed to standard base64 encoding as required by RFC 2397 data URL specification
  • Platforms affected: Linux (Wayland, X11) and Windows

Changes

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

  • Lines 33-35: Wayland clipboard - .text().arrayBuffer(), base64urlbase64
  • Lines 37-39: X11 clipboard - .text().arrayBuffer(), base64urlbase64
  • Line 50: Windows clipboard - base64urlbase64

These changes align Linux/Windows clipboard handling with the correct approach already used in macOS (line 25).

Test Plan

  1. Environment: Ubuntu/Linux with Wayland or X11
  2. Take a screenshot (saves to clipboard)
  3. Open opencode TUI
  4. Paste image using Ctrl+V
  5. Submit message with image
  6. Verify no AI_DownloadError occurs
  7. Verify AI can read and process the image

Technical Details

Before (broken):

const wayland = await $\`wl-paste -t image/png\`.nothrow().text()
return { data: Buffer.from(wayland).toString("base64url"), ... }

After (fixed):

const wayland = await $\`wl-paste -t image/png\`.nothrow().arrayBuffer()
if (wayland && wayland.byteLength > 0) {
  return { data: Buffer.from(wayland).toString("base64"), ... }
}

Fixes #3816

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/3817 **State:** closed **Merged:** Yes --- ## Summary Fixes clipboard image paste failures on Linux and Windows (#3816) by addressing two critical bugs in clipboard.ts: ### Bug #1: Binary Data Corruption (Linux) - **Problem:** Wayland and X11 clipboard reads used `.text()` on binary PNG data, corrupting it via UTF-8 decoding - **Fix:** Changed to `.arrayBuffer()` to preserve binary data integrity - **Added:** `byteLength > 0` validation for empty buffer checks ### Bug #2: Encoding Mismatch (All platforms) - **Problem:** Used `base64url` encoding (URL-safe with `-` and `_`) instead of standard `base64` (with `+` and `/`) - **Fix:** Changed to standard `base64` encoding as required by RFC 2397 data URL specification - **Platforms affected:** Linux (Wayland, X11) and Windows ## Changes **File:** `packages/opencode/src/cli/cmd/tui/util/clipboard.ts` - Lines 33-35: Wayland clipboard - `.text()` → `.arrayBuffer()`, `base64url` → `base64` - Lines 37-39: X11 clipboard - `.text()` → `.arrayBuffer()`, `base64url` → `base64` - Line 50: Windows clipboard - `base64url` → `base64` These changes align Linux/Windows clipboard handling with the correct approach already used in macOS (line 25). ## Test Plan 1. **Environment:** Ubuntu/Linux with Wayland or X11 2. Take a screenshot (saves to clipboard) 3. Open opencode TUI 4. Paste image using Ctrl+V 5. Submit message with image 6. Verify no `AI_DownloadError` occurs 7. Verify AI can read and process the image ## Technical Details **Before (broken):** ```typescript const wayland = await $\`wl-paste -t image/png\`.nothrow().text() return { data: Buffer.from(wayland).toString("base64url"), ... } ``` **After (fixed):** ```typescript const wayland = await $\`wl-paste -t image/png\`.nothrow().arrayBuffer() if (wayland && wayland.byteLength > 0) { return { data: Buffer.from(wayland).toString("base64"), ... } } ``` Fixes #3816
yindo added the pull-request label 2026-02-16 18:15:31 -05:00
yindo closed this issue 2026-02-16 18:15:31 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10773