1111import java .io .ByteArrayOutputStream ;
1212import java .io .File ;
1313import java .io .IOException ;
14+ import java .nio .charset .StandardCharsets ;
1415import java .util .Objects ;
1516
1617/**
@@ -77,8 +78,10 @@ public OpenCodeCliResult execute(String... args) {
7778
7879 try {
7980 int exitCode = executor .execute (cmd );
80- String out = stdout .toString ().trim ();
81- String err = stderr .toString ().trim ();
81+ // 显式 UTF-8 解码:toString() 走平台默认字符集,GBK 默认字符集的
82+ // Windows 上会把 opencode 的 UTF-8 输出解成乱码。
83+ String out = decodeUtf8 (stdout );
84+ String err = decodeUtf8 (stderr );
8285 if (config .getDebug ().allows (HttpLogLevel .BASIC )) {
8386 log .debug ("OpenCode CLI executed: exitCode={}, stdoutLength={}, stderrLength={}" ,
8487 exitCode , out .length (), err .length ());
@@ -92,6 +95,22 @@ public OpenCodeCliResult execute(String... args) {
9295 }
9396 }
9497
98+ /**
99+ * 按 UTF-8 解码缓冲区内容。{@code ByteArrayOutputStream.toString(Charset)}
100+ * 是 Java 10+ API,JDK 8 线退化为 {@code toString("UTF-8")};UTF-8 在所有
101+ * JVM 上保证存在,{@code UnsupportedEncodingException} 分支实际不可达。
102+ *
103+ * @param buffer 子进程输出缓冲
104+ * @return 解码后的文本
105+ */
106+ private static String decodeUtf8 (ByteArrayOutputStream buffer ) {
107+ try {
108+ return buffer .toString ("UTF-8" );
109+ } catch (java .io .UnsupportedEncodingException e ) {
110+ return buffer .toString ();
111+ }
112+ }
113+
95114 /**
96115 * 探测 CLI 是否可用(执行 {@code opencode --version})。
97116 *
0 commit comments