Skip to content

fix(tui): use base64 encoding for PowerShell clipboard writes to preserve UTF-8#35124

Closed
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/wsl-clipboard-utf8
Closed

fix(tui): use base64 encoding for PowerShell clipboard writes to preserve UTF-8#35124
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/wsl-clipboard-utf8

Conversation

@annguyenNous

Copy link
Copy Markdown
Contributor

Problem

When using /copy in TUI on WSL2, non-ASCII text (CJK, emoji, accented characters) becomes garbled when pasted on the Windows side.

Root cause: writeClipboardText() pipes text to powershell.exe Set-Clipboard -Value $input via stdin. PowerShell reads stdin using the Windows system's default ANSI code page (e.g. CP936 for Chinese Windows), not UTF-8. This reinterprets UTF-8 bytes as ANSI, producing garbled output.

Example: 你好世界浣犲ソ涓栫晫

Fix

Encode text as base64 in Node.js and pass it as a PowerShell command argument instead of piping through stdin. PowerShell explicitly decodes from base64 using UTF-8:

Set-Clipboard -Value ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('BASE64')))

This bypasses the ANSI code page entirely since the text is passed as a command argument (which PowerShell handles in UTF-8) rather than through stdin.

Before vs After

Text Before (stdin pipe) After (base64 arg)
你好世界,测试中文 浣犲ソ涓栫晫锛屾祴璇曚腑鏂 你好世界,测试中文
café résumé caf茅 r茅sum茅 café résumé
🎉 Hello garbled 🎉 Hello

Changes

  • ui-tui/src/lib/clipboard.ts: PowerShell clipboard writes use base64-encoded UTF-8 via -Command argument instead of stdin piping. Other backends (pbcopy, wl-copy, xclip, xsel) continue using stdin as before.
  • ui-tui/src/__tests__/clipboard.test.ts: Updated existing tests to verify base64 encoding, added CJK-specific regression test.

Tests

All 19 clipboard tests pass (3 new assertions + 1 new test for CJK text).

Fixes #35107

…erve UTF-8

When writing text to the clipboard via PowerShell (WSL2 and native Windows),
the previous implementation piped text through stdin using `Set-Clipboard
-Value $input`. PowerShell reads stdin using the Windows system's default
ANSI code page (e.g. CP936 for Chinese Windows), causing all non-ASCII
characters (CJK, emoji, accented) to become garbled.

Fix: encode the text as base64 in Node.js and pass it as a command argument.
PowerShell decodes it from base64 using explicit UTF-8, bypassing the code
page issue entirely.

Fixes NousResearch#35107
@alt-glitch alt-glitch added type/bug Something isn't working comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists labels May 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing fix: #35122 also addresses #35107 by setting [Console]::InputEncoding to UTF-8. This PR's base64 approach bypasses stdin encoding entirely, which is more robust.

@teknium1

Copy link
Copy Markdown
Contributor

Merged via #35222 — your base64-via-argument mechanism was cherry-picked onto current main with your authorship preserved in git log (commit e6efa98). A small follow-up collapsed the per-entry psScript callback into a shared script + stdin flag. Thanks for the robust fix!

@teknium1 teknium1 closed this May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/copy in TUI produces garbled CJK text on WSL2

3 participants