opencode-windows-encoding 2.1.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -6
- package/dist/utf8-encoding.d.ts +5 -4
- package/dist/utf8-encoding.js +43 -6
- package/dist/utf8-encoding.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,23 +3,35 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/opencode-windows-encoding)
|
|
4
4
|
[](https://www.gnu.org/licenses/agpl-3.0)
|
|
5
5
|
|
|
6
|
-
OpenCode plugin that fixes UTF-8 encoding issues when executing
|
|
6
|
+
OpenCode plugin that fixes UTF-8 encoding issues when executing shell commands on Windows. Single-file, zero npm runtime dependencies.
|
|
7
7
|
|
|
8
8
|
## The Problem
|
|
9
9
|
|
|
10
|
-
When OpenCode runs shell commands
|
|
10
|
+
When OpenCode runs shell commands on Windows, the console output encoding defaults to the system locale (e.g., GBK for zh-CN). This causes garbled text when LLM-generated commands produce UTF-8 output — breaking file paths, error messages, and all non-ASCII content.
|
|
11
11
|
|
|
12
12
|
## How It Works
|
|
13
13
|
|
|
14
|
-
This plugin hooks into OpenCode's `tool.execute.before` event and injects UTF-8 encoding configuration before every
|
|
14
|
+
This plugin hooks into OpenCode's `tool.execute.before` event, detects the configured shell from OpenCode's config (`config.shell`), and injects the matching UTF-8 encoding configuration before every shell command:
|
|
15
15
|
|
|
16
|
+
**PowerShell (`pwsh`):**
|
|
16
17
|
```powershell
|
|
17
|
-
[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;$OutputEncoding=[Text.Encoding]::UTF8;
|
|
18
|
+
[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;$OutputEncoding=[Text.Encoding]::UTF8;$env:PYTHONIOENCODING='utf-8';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Bash / POSIX shells (`bash`, `zsh`, `sh`, ...):**
|
|
22
|
+
```bash
|
|
23
|
+
export LC_ALL=C.UTF-8; export LANG=C.UTF-8; export PYTHONIOENCODING=utf-8;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Command Prompt (`cmd`):**
|
|
27
|
+
```bat
|
|
28
|
+
chcp 65001 >nul
|
|
18
29
|
```
|
|
19
30
|
|
|
20
31
|
### Key behaviors:
|
|
32
|
+
- **Shell auto-detection** — reads `config.shell` from OpenCode at startup; falls back to platform detection (`pwsh` on Windows, `bash` elsewhere) when unset or unavailable
|
|
21
33
|
- **Automatic injection** — applies to all `bash` and `shell` tool calls
|
|
22
|
-
- **Idempotent** — skips commands that already contain `OutputEncoding` to avoid duplication
|
|
34
|
+
- **Idempotent** — skips commands that already contain the shell's encoding marker (`OutputEncoding` / `LC_ALL` / `chcp`) to avoid duplication
|
|
23
35
|
- **`set` prefix aware** — preserves PowerShell `set VAR="value"` prefixes before injecting
|
|
24
36
|
- **Zero config** — works out of the box with no options
|
|
25
37
|
- **Debug logging off by default** — set `OPENCODE_UTF8_DEBUG=1` to enable diagnostic logging to `$TMP/utf8-plugin.log`
|
|
@@ -74,8 +86,8 @@ Restart OpenCode to apply. No `npm install`, no build step.
|
|
|
74
86
|
## Requirements
|
|
75
87
|
|
|
76
88
|
- **OpenCode** (any recent version with plugin support)
|
|
77
|
-
- **PowerShell 7+** (`pwsh`)
|
|
78
89
|
- **Windows** (this plugin is designed specifically for Windows encoding issues)
|
|
90
|
+
- **Any of**: PowerShell 7+ (`pwsh`), Bash, or Command Prompt (`cmd`)
|
|
79
91
|
|
|
80
92
|
## Development
|
|
81
93
|
|
package/dist/utf8-encoding.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { PluginInput } from '@opencode-ai/plugin';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* OpenCode Plugin — UTF-8 Encoding Fix for Windows
|
|
4
|
+
* OpenCode Plugin — UTF-8 Encoding Fix for Windows
|
|
5
5
|
*
|
|
6
6
|
* 单文件插件,可直接复制到 ~/.config/opencode/plugins/ 使用,无需 npm install。
|
|
7
7
|
*
|
|
8
|
-
* 工作原理:拦截所有 bash/shell
|
|
9
|
-
* UTF-8
|
|
8
|
+
* 工作原理:拦截所有 bash/shell 工具调用,根据 opencode 当前配置的 shell
|
|
9
|
+
* (pwsh / bash / cmd)在命令前注入对应的 UTF-8 编码配置,解决中文/非 ASCII
|
|
10
|
+
* 字符乱码问题。
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
declare const Utf8EncodingPlugin: (
|
|
13
|
+
declare const Utf8EncodingPlugin: (input: PluginInput) => Promise<{
|
|
13
14
|
"tool.execute.before": (input: {
|
|
14
15
|
tool: string;
|
|
15
16
|
sessionID: string;
|
package/dist/utf8-encoding.js
CHANGED
|
@@ -12,17 +12,54 @@ function flog(msg) {
|
|
|
12
12
|
} catch {
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
var
|
|
15
|
+
var ENC = {
|
|
16
|
+
pwsh: {
|
|
17
|
+
prefix: "[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;$OutputEncoding=[Text.Encoding]::UTF8;$env:PYTHONIOENCODING='utf-8';",
|
|
18
|
+
sep: "\n",
|
|
19
|
+
marker: "OutputEncoding"
|
|
20
|
+
},
|
|
21
|
+
bash: {
|
|
22
|
+
prefix: "export LC_ALL=C.UTF-8; export LANG=C.UTF-8; export PYTHONIOENCODING=utf-8;",
|
|
23
|
+
sep: "\n",
|
|
24
|
+
marker: "LC_ALL"
|
|
25
|
+
},
|
|
26
|
+
cmd: {
|
|
27
|
+
prefix: "chcp 65001 >nul",
|
|
28
|
+
sep: " & ",
|
|
29
|
+
marker: "chcp"
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function detectShellKind(shell) {
|
|
33
|
+
if (shell) {
|
|
34
|
+
const base = shell.replace(/\\/g, "/").split("/").pop().toLowerCase().replace(/\.exe$/, "");
|
|
35
|
+
if (base === "pwsh" || base === "powershell") return "pwsh";
|
|
36
|
+
if (base === "cmd") return "cmd";
|
|
37
|
+
return "bash";
|
|
38
|
+
}
|
|
39
|
+
return process.platform === "win32" ? "pwsh" : "bash";
|
|
40
|
+
}
|
|
41
|
+
async function resolveShellKind(client) {
|
|
42
|
+
try {
|
|
43
|
+
const res = await client.config.get();
|
|
44
|
+
const shell = res.data?.shell;
|
|
45
|
+
return detectShellKind(shell);
|
|
46
|
+
} catch {
|
|
47
|
+
return detectShellKind(void 0);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
16
50
|
function stripSetPrefixes(cmd) {
|
|
17
51
|
const m = cmd.match(/^((?:set\s+\w+="[^"]*"\s*&&\s*)+)/);
|
|
18
52
|
if (m) return { prefixes: m[1], cleanCmd: cmd.slice(m[1].length) };
|
|
19
53
|
return { prefixes: "", cleanCmd: cmd };
|
|
20
54
|
}
|
|
21
|
-
var Utf8EncodingPlugin = async (
|
|
55
|
+
var Utf8EncodingPlugin = async (input) => {
|
|
22
56
|
flog("=== LOADED ===");
|
|
57
|
+
const kind = await resolveShellKind(input.client);
|
|
58
|
+
const { prefix, sep, marker } = ENC[kind];
|
|
59
|
+
flog(`shell kind: ${kind}`);
|
|
23
60
|
return {
|
|
24
|
-
"tool.execute.before": async (
|
|
25
|
-
const tool = String(
|
|
61
|
+
"tool.execute.before": async (input2, output) => {
|
|
62
|
+
const tool = String(input2?.tool ?? "");
|
|
26
63
|
flog(`[tool.before] tool="${tool}"`);
|
|
27
64
|
if (tool !== "bash" && tool !== "shell") return;
|
|
28
65
|
const args = output.args;
|
|
@@ -37,11 +74,11 @@ var Utf8EncodingPlugin = async (_input) => {
|
|
|
37
74
|
}
|
|
38
75
|
const { prefixes, cleanCmd } = stripSetPrefixes(cmd);
|
|
39
76
|
flog(` orig: ${cleanCmd.slice(0, 120)}`);
|
|
40
|
-
if (cleanCmd.includes(
|
|
77
|
+
if (cleanCmd.includes(marker)) {
|
|
41
78
|
flog(" skip (idempotent)");
|
|
42
79
|
return;
|
|
43
80
|
}
|
|
44
|
-
args.command = prefixes +
|
|
81
|
+
args.command = prefixes + prefix + sep + cleanCmd;
|
|
45
82
|
flog(" INJECTED");
|
|
46
83
|
}
|
|
47
84
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utf8-encoding.ts"],"sourcesContent":["/**\n * OpenCode Plugin — UTF-8 Encoding Fix for Windows
|
|
1
|
+
{"version":3,"sources":["../src/utf8-encoding.ts"],"sourcesContent":["/**\n * OpenCode Plugin — UTF-8 Encoding Fix for Windows\n *\n * 单文件插件,可直接复制到 ~/.config/opencode/plugins/ 使用,无需 npm install。\n *\n * 工作原理:拦截所有 bash/shell 工具调用,根据 opencode 当前配置的 shell\n * (pwsh / bash / cmd)在命令前注入对应的 UTF-8 编码配置,解决中文/非 ASCII\n * 字符乱码问题。\n */\n\nimport { appendFileSync } from \"node:fs\"\nimport { tmpdir } from \"node:os\"\nimport { join } from \"node:path\"\nimport type { PluginInput } from \"@opencode-ai/plugin\"\n\n// 调试日志(写入临时目录,默认关闭,设 OPENCODE_UTF8_DEBUG=1 开启)\nconst DEBUG = process.env.OPENCODE_UTF8_DEBUG === \"1\"\nconst LOG = join(tmpdir(), \"utf8-plugin.log\")\nfunction flog(msg: string) {\n if (!DEBUG) return\n try { appendFileSync(LOG, `[${new Date().toISOString()}] ${msg}\\n`, \"utf8\") } catch {}\n}\n\ntype ShellKind = \"pwsh\" | \"bash\" | \"cmd\"\n\n// ── 各 shell 的 UTF-8 编码前缀与命令分隔符 ──\nconst ENC: Record<ShellKind, { prefix: string; sep: string; marker: string }> = {\n pwsh: {\n prefix:\n \"[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;\" +\n \"$OutputEncoding=[Text.Encoding]::UTF8;\" +\n \"$env:PYTHONIOENCODING='utf-8';\",\n sep: \"\\n\",\n marker: \"OutputEncoding\",\n },\n bash: {\n prefix: \"export LC_ALL=C.UTF-8; export LANG=C.UTF-8; export PYTHONIOENCODING=utf-8;\",\n sep: \"\\n\",\n marker: \"LC_ALL\",\n },\n cmd: {\n prefix: \"chcp 65001 >nul\",\n sep: \" & \",\n marker: \"chcp\",\n },\n}\n\n/** 由 config.shell 的值归一化为 shell 类型 */\nfunction detectShellKind(shell: string | undefined): ShellKind {\n if (shell) {\n const base = shell\n .replace(/\\\\/g, \"/\")\n .split(\"/\")\n .pop()!\n .toLowerCase()\n .replace(/\\.exe$/, \"\")\n if (base === \"pwsh\" || base === \"powershell\") return \"pwsh\"\n if (base === \"cmd\") return \"cmd\"\n return \"bash\" // bash / zsh / sh / dash / ksh 等 POSIX shell\n }\n // 未配置:按平台回退(opencode Windows 默认优先 pwsh)\n return process.platform === \"win32\" ? \"pwsh\" : \"bash\"\n}\n\n/** 读取 opencode 配置中的 shell,失败时按平台回退 */\nasync function resolveShellKind(client: PluginInput[\"client\"]): Promise<ShellKind> {\n try {\n const res = await client.config.get()\n const shell = (res.data as unknown as { shell?: string } | undefined)?.shell\n return detectShellKind(shell)\n } catch {\n return detectShellKind(undefined)\n }\n}\n\n/** 提取 opencode 在命令前追加的 set VAR=\"value\" && 前缀 */\nfunction stripSetPrefixes(cmd: string): { prefixes: string; cleanCmd: string } {\n const m = cmd.match(/^((?:set\\s+\\w+=\"[^\"]*\"\\s*&&\\s*)+)/)\n if (m) return { prefixes: m[1], cleanCmd: cmd.slice(m[1].length) }\n return { prefixes: \"\", cleanCmd: cmd }\n}\n\nexport const Utf8EncodingPlugin = async (input: PluginInput) => {\n flog(\"=== LOADED ===\")\n\n const kind = await resolveShellKind(input.client)\n const { prefix, sep, marker } = ENC[kind]\n flog(`shell kind: ${kind}`)\n\n return {\n \"tool.execute.before\": async (input: { tool: string; sessionID: string; callID: string }, output: { args: any }) => {\n const tool = String(input?.tool ?? \"\")\n flog(`[tool.before] tool=\"${tool}\"`)\n\n if (tool !== \"bash\" && tool !== \"shell\") return\n\n const args = output.args\n if (!args) { flog(\" no args\"); return }\n\n const cmd = args.command\n if (typeof cmd !== \"string\" || !cmd) {\n flog(` args keys: ${JSON.stringify(Object.keys(args))}`)\n return\n }\n\n const { prefixes, cleanCmd } = stripSetPrefixes(cmd)\n flog(` orig: ${cleanCmd.slice(0, 120)}`)\n\n // 防止重复注入\n if (cleanCmd.includes(marker)) { flog(\" skip (idempotent)\"); return }\n\n args.command = prefixes + prefix + sep + cleanCmd\n flog(\" INJECTED\")\n },\n }\n}\n\nexport default Utf8EncodingPlugin\n"],"mappings":";AAUA,SAAS,sBAAsB;AAC/B,SAAS,cAAc;AACvB,SAAS,YAAY;AAIrB,IAAM,QAAQ,QAAQ,IAAI,wBAAwB;AAClD,IAAM,MAAM,KAAK,OAAO,GAAG,iBAAiB;AAC5C,SAAS,KAAK,KAAa;AACzB,MAAI,CAAC,MAAO;AACZ,MAAI;AAAE,mBAAe,KAAK,KAAI,oBAAI,KAAK,GAAE,YAAY,CAAC,KAAK,GAAG;AAAA,GAAM,MAAM;AAAA,EAAE,QAAQ;AAAA,EAAC;AACvF;AAKA,IAAM,MAA0E;AAAA,EAC9E,MAAM;AAAA,IACJ,QACE;AAAA,IAGF,KAAK;AAAA,IACL,QAAQ;AAAA,EACV;AAAA,EACA,MAAM;AAAA,IACJ,QAAQ;AAAA,IACR,KAAK;AAAA,IACL,QAAQ;AAAA,EACV;AAAA,EACA,KAAK;AAAA,IACH,QAAQ;AAAA,IACR,KAAK;AAAA,IACL,QAAQ;AAAA,EACV;AACF;AAGA,SAAS,gBAAgB,OAAsC;AAC7D,MAAI,OAAO;AACT,UAAM,OAAO,MACV,QAAQ,OAAO,GAAG,EAClB,MAAM,GAAG,EACT,IAAI,EACJ,YAAY,EACZ,QAAQ,UAAU,EAAE;AACvB,QAAI,SAAS,UAAU,SAAS,aAAc,QAAO;AACrD,QAAI,SAAS,MAAO,QAAO;AAC3B,WAAO;AAAA,EACT;AAEA,SAAO,QAAQ,aAAa,UAAU,SAAS;AACjD;AAGA,eAAe,iBAAiB,QAAmD;AACjF,MA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