Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 修复 macOS 终端中文输入乱码 (#768)

DavidShenXD663ca0c26f08
diff --git a/apps/desktop/src/main/terminal/__tests__/ptyManager.test.ts b/apps/desktop/src/main/terminal/__tests__/ptyManager.test.ts
index b37d16a459..7835335033 100644
--- a/apps/desktop/src/main/terminal/__tests__/ptyManager.test.ts
+++ b/apps/desktop/src/main/terminal/__tests__/ptyManager.test.ts
@@ -121,7 +121,10 @@ let allSpawns: FakePty[] = [];
let dataPayloads: Array<{ target: WebContents; payload: DataPayload }> = [];
let exitPayloads: Array<{ target: WebContents; payload: ExitPayload }> = [];
-function makeManager(opts?: { resolveFallbackOwner?: (dead: WebContents) => WebContents | null }) {
+function makeManager(opts?: {
+ resolveFallbackOwner?: (dead: WebContents) => WebContents | null;
+ platform?: NodeJS.Platform;
+}) {
return new PtyManager({
spawn: (command, args, options) => {
const fake = makeFakePty({
@@ -144,6 +147,7 @@ function makeManager(opts?: { resolveFallbackOwner?: (dead: WebContents) => WebC
},
},
resolveFallbackOwner: opts?.resolveFallbackOwner,
+ platform: opts?.platform,
});
}
@@ -454,6 +458,118 @@ describe('PtyManager.spawn env scrubbing', () => {
// 这条 case 验证"剥永远生效",不是"opts.env 能塞回"。
expect(lastSpawn!.__spawnEnv.TERM_PROGRAM).toBeUndefined();
});
+
+ it('macOS 继承到空/C locale 时为 PTY 补 UTF-8 字符 locale', () => {
+ const original = {
+ LANG: process.env.LANG,
+ LC_ALL: process.env.LC_ALL,
+ LC_CTYPE: process.env.LC_CTYPE,
+ };
+ try {
+ process.env.LANG = '';
+ process.env.LC_ALL = '';
+ process.env.LC_CTYPE = 'C';
+
+ const mgr = makeManager({ platform: 'darwin' });
+ const owner = makeFakeWebContents() as unknown as WebContents;
+ mgr.create({ id: 't1', cwd: '/tmp', owner });
+
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('UTF-8');
+ expect(lastSpawn!.__spawnEnv.LC_ALL).toBe('');
+
+ delete process.env.LC_CTYPE;
+ process.env.LANG = 'C';
+ mgr.create({ id: 't2', cwd: '/tmp', owner });
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('UTF-8');
+ } finally {
+ for (const [key, value] of Object.entries(original)) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ });
+
+ it('调用方显式传入的 locale 在平台兜底之后生效', () => {
+ const original = {
+ LANG: process.env.LANG,
+ LC_ALL: process.env.LC_ALL,
+ LC_CTYPE: process.env.LC_CTYPE,
+ };
+ try {
+ process.env.LANG = '';
+ process.env.LC_ALL = '';
+ process.env.LC_CTYPE = 'C';
+
+ const mgr = makeManager({ platform: 'darwin' });
+ const owner = makeFakeWebContents() as unknown as WebContents;
+ mgr.create({
+ id: 't1',
+ cwd: '/tmp',
+ owner,
+ env: { LC_CTYPE: 'ja_JP.UTF-8' },
+ });
+
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('ja_JP.UTF-8');
+ } finally {
+ for (const [key, value] of Object.entries(original)) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ });
+
+ it('不覆盖用户显式设置的 LC_ALL 或非 C/POSIX 字符 locale', () => {
+ const original = {
+ LANG: process.env.LANG,
+ LC_ALL: process.env.LC_ALL,
+ LC_CTYPE: process.env.LC_CTYPE,
+ };
+ try {
+ process.env.LANG = '';
+ process.env.LC_ALL = 'C';
+ process.env.LC_CTYPE = 'C';
+
+ const mgr = makeManager({ platform: 'darwin' });
+ const owner = makeFakeWebContents() as unknown as WebContents;
+ mgr.create({ id: 't1', cwd: '/tmp', owner });
+ expect(lastSpawn!.__spawnEnv.LC_ALL).toBe('C');
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('C');
+
+ process.env.LC_ALL = '';
+ process.env.LC_CTYPE = 'en_US.UTF-8';
+ mgr.create({ id: 't2', cwd: '/tmp', owner });
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('en_US.UTF-8');
+ } finally {
+ for (const [key, value] of Object.entries(original)) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ });
+
+ it('Windows 不改变 C locale', () => {
+ const original = {
+ LANG: process.env.LANG,
+ LC_ALL: process.env.LC_ALL,
+ LC_CTYPE: process.env.LC_CTYPE,
+ };
+ try {
+ process.env.LANG = '';
+ process.env.LC_ALL = '';
+ process.env.LC_CTYPE = 'C';
+
+ const mgr = makeManager({ platform: 'win32' });
+ const owner = makeFakeWebContents() as unknown as WebContents;
+ mgr.create({ id: 't1', cwd: '/tmp', owner });
+
+ expect(lastSpawn!.__spawnEnv.LC_CTYPE).toBe('C');
+ } finally {
+ for (const [key, value] of Object.entries(original)) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ });
});
describe('PtyManager.disposeOwner (webContents destroyed)', () => {
diff --git a/apps/desktop/src/main/terminal/ptyManager.ts b/apps/desktop/src/main/terminal/ptyManager.ts
index 29cfc21cb2..784184d0c1 100644
--- a/apps/desktop/src/main/terminal/ptyManager.ts
+++ b/apps/desktop/src/main/terminal/ptyManager.ts
@@ -157,6 +157,8 @@ interface PtySession {
export interface PtyManagerDeps {
spawn?: PtySpawnFn;
sink: PtyEventSink;
+ /** 仅供测试注入平台;生产代码默认使用当前 Node 平台。 */
+ platform?: NodeJS.Platform;
/**
* owner webContents destroyed 时的接管者解析(典型:主窗 webContents)。
* 返回一个活着的、不同于 dead owner 的 webContents 时,该 owner 的 session
@@ -176,11 +178,13 @@ export class PtyManager {
private readonly trackedOwners = new WeakSet<WebContents>();
private readonly spawnFn: PtySpawnFn;
private readonly sink: PtyEventSink;
+ private readonly platform: NodeJS.Platform;
private readonly resolveFallbackOwner?: (deadOwner: WebContents) => WebContents | null;
constructor(deps: PtyManagerDeps) {
this.spawnFn = deps.spawn ?? defaultPtySpawn;
this.sink = deps.sink;
+ this.platform = deps.platform ?? process.platform;
this.resolveFallbackOwner = deps.resolveFallbackOwner;
}
@@ -368,11 +372,13 @@ export class PtyManager {
const rows = opts.rows && opts.rows > 0 ? opts.rows : DEFAULT_ROWS;
const resolved = resolveShellForCreate(opts.shellPref ?? null);
- // 合并 env:用户 env 覆盖 process.env,再注入 TERM,最后剥掉 host terminal app 标记。
+ // 合并 env:先复制父进程环境,做平台兜底,再应用调用方显式覆盖。
+ // ENV_KEYS_TO_STRIP 与 TERM 仍在下方强制执行,不允许 opts.env 改写。
const env: Record<string, string> = {};
for (const [k, v] of Object.entries(process.env)) {
if (typeof v === 'string') env[k] = v;
}
+ ensureMacTerminalUtf8Locale(env, this.platform);
if (opts.env) {
for (const [k, v] of Object.entries(opts.env)) {
if (typeof v === 'string') env[k] = v;
@@ -474,3 +480,22 @@ export class PtyManager {
}
}
}
+
+/**
+ * macOS Finder/Dock 启动的 GUI 进程可能继承到空/C locale。zsh 的行编辑器
+ * 需要 UTF-8 的 LC_CTYPE 才能正确识别中文等多字节输入;只设置 LANG 不够,
+ * 因为非空 LC_CTYPE 会覆盖 LANG。非空 LC_ALL 通常表示用户主动指定,保留其
+ * 语义;调用方通过 opts.env 传入的显式覆盖也在本函数之后生效。
+ */
+function ensureMacTerminalUtf8Locale(
+ env: Record<string, string>,
+ platform: NodeJS.Platform,
+): void {
+ if (platform !== 'darwin' || env.LC_ALL?.trim()) return;
+
+ const charLocale = env.LC_CTYPE?.trim() || env.LANG?.trim() || 'C';
+ const normalized = charLocale.toUpperCase();
+ if (normalized !== 'C' && normalized !== 'POSIX') return;
+
+ env.LC_CTYPE = 'UTF-8';
+}