From 4133bd8c3184c74efd56a76c3fcae51fed556c7f Mon Sep 17 00:00:00 2001 From: User Date: Mon, 23 Feb 2026 17:36:10 +0800 Subject: [PATCH] fix: set Windows console code page to UTF-8 for CJK input --- packages/opencode/src/cli/cmd/tui/attach.ts | 3 ++- packages/opencode/src/cli/cmd/tui/thread.ts | 3 ++- packages/opencode/src/cli/cmd/tui/win32.ts | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/cli/cmd/tui/attach.ts b/packages/opencode/src/cli/cmd/tui/attach.ts index a2559cfce67..b66bfdf5395 100644 --- a/packages/opencode/src/cli/cmd/tui/attach.ts +++ b/packages/opencode/src/cli/cmd/tui/attach.ts @@ -1,7 +1,7 @@ import { cmd } from "../cmd" import { UI } from "@/cli/ui" import { tui } from "./app" -import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32" +import { win32DisableProcessedInput, win32InstallCtrlCGuard, win32SetUtf8CodePage } from "./win32" export const AttachCommand = cmd({ command: "attach ", @@ -40,6 +40,7 @@ export const AttachCommand = cmd({ const unguard = win32InstallCtrlCGuard() try { win32DisableProcessedInput() + win32SetUtf8CodePage() if (args.fork && !args.continue && !args.session) { UI.error("--fork requires --continue or --session") diff --git a/packages/opencode/src/cli/cmd/tui/thread.ts b/packages/opencode/src/cli/cmd/tui/thread.ts index 50f63c3dfbd..585cadb02a7 100644 --- a/packages/opencode/src/cli/cmd/tui/thread.ts +++ b/packages/opencode/src/cli/cmd/tui/thread.ts @@ -11,7 +11,7 @@ import { withNetworkOptions, resolveNetworkOptions } from "@/cli/network" import { Filesystem } from "@/util/filesystem" import type { Event } from "@opencode-ai/sdk/v2" import type { EventSource } from "./context/sdk" -import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32" +import { win32DisableProcessedInput, win32InstallCtrlCGuard, win32SetUtf8CodePage } from "./win32" declare global { const OPENCODE_WORKER_PATH: string @@ -87,6 +87,7 @@ export const TuiThreadCommand = cmd({ // Must be the very first thing — disables CTRL_C_EVENT before any Worker // spawn or async work so the OS cannot kill the process group. win32DisableProcessedInput() + win32SetUtf8CodePage() if (args.fork && !args.continue && !args.session) { UI.error("--fork requires --continue or --session") diff --git a/packages/opencode/src/cli/cmd/tui/win32.ts b/packages/opencode/src/cli/cmd/tui/win32.ts index 23e9f448574..4f0946c59db 100644 --- a/packages/opencode/src/cli/cmd/tui/win32.ts +++ b/packages/opencode/src/cli/cmd/tui/win32.ts @@ -2,6 +2,7 @@ import { dlopen, ptr } from "bun:ffi" const STD_INPUT_HANDLE = -10 const ENABLE_PROCESSED_INPUT = 0x0001 +const CP_UTF8 = 65001 const kernel = () => dlopen("kernel32.dll", { @@ -9,6 +10,8 @@ const kernel = () => GetConsoleMode: { args: ["ptr", "ptr"], returns: "i32" }, SetConsoleMode: { args: ["ptr", "u32"], returns: "i32" }, FlushConsoleInputBuffer: { args: ["ptr"], returns: "i32" }, + SetConsoleCP: { args: ["u32"], returns: "i32" }, + SetConsoleOutputCP: { args: ["u32"], returns: "i32" }, }) let k32: ReturnType | undefined @@ -23,6 +26,18 @@ function load() { } } +/** + * Set console code page to UTF-8 for proper Unicode input/output. + * This fixes character encoding issues when using CJK (Chinese, Japanese, Korean) + * and other non-ASCII input methods on Windows. + */ +export function win32SetUtf8CodePage() { + if (process.platform !== "win32") return + if (!load()) return + k32!.symbols.SetConsoleCP(CP_UTF8) + k32!.symbols.SetConsoleOutputCP(CP_UTF8) +} + /** * Clear ENABLE_PROCESSED_INPUT on the console stdin handle. */