From a000397160c45fe6d1d210ee3aa47ec2d8f4f300 Mon Sep 17 00:00:00 2001 From: QiuYang Shen <1741258352@qq.com> Date: Sun, 13 Jul 2025 19:00:52 +0800 Subject: [PATCH] avoid garbled Chinese when call runTerminalCommandTool in windows --- core/package.json | 1 + .../implementations/runTerminalCommand.ts | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/package.json b/core/package.json index 9c9af5851..d5dc5ed65 100644 --- a/core/package.json +++ b/core/package.json @@ -78,6 +78,7 @@ "handlebars": "^4.7.8", "http-proxy-agent": "^7.0.1", "https-proxy-agent": "^7.0.3", + "iconv-lite": "^0.6.3", "ignore": "^5.3.1", "is-localhost-ip": "^2.0.0", "jinja-js": "0.1.8", diff --git a/core/tools/implementations/runTerminalCommand.ts b/core/tools/implementations/runTerminalCommand.ts index 3d3ac85cb..3336a1006 100644 --- a/core/tools/implementations/runTerminalCommand.ts +++ b/core/tools/implementations/runTerminalCommand.ts @@ -1,5 +1,22 @@ +import iconv from "iconv-lite"; import childProcess from "node:child_process"; import util from "node:util"; +// Automatically decode the buffer according to the platform to avoid garbled Chinese +function getDecodedOutput(data: Buffer): string { + if (process.platform === "win32") { + try { + let out = iconv.decode(data, "utf-8"); + if (/�/.test(out)) { + out = iconv.decode(data, "gbk"); + } + return out; + } catch { + return iconv.decode(data, "gbk"); + } + } else { + return data.toString(); + } +} import { fileURLToPath } from "node:url"; import { ToolImpl } from "."; @@ -77,7 +94,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { // Skip if this process has been backgrounded if (isProcessBackgrounded(toolCallId)) return; - const newOutput = data.toString(); + const newOutput = getDecodedOutput(data); terminalOutput += newOutput; // Send partial output to UI @@ -103,7 +120,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { // Skip if this process has been backgrounded if (isProcessBackgrounded(toolCallId)) return; - const newOutput = data.toString(); + const newOutput = getDecodedOutput(data); terminalOutput += newOutput; // Send partial output to UI, status is not required