Skip to content

Commit 03dc43a

Browse files
committed
refactor: consolidate to single-file plugin, zero npm dependencies
Merge src/index.ts into src/utf8-encoding.ts. Remove @opencode-ai/plugin dependency. The same file now works both as an npm package and as a standalone copy-to-plugins file.
1 parent c905404 commit 03dc43a

6 files changed

Lines changed: 53 additions & 38 deletions

File tree

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ Or with a specific version:
5353

5454
After adding the plugin, restart OpenCode. All subsequent shell commands will use UTF-8 encoding automatically.
5555

56+
## 本地使用(复制即用)
57+
58+
本项目是单文件插件,`src/utf8-encoding.ts` 可以直接复制到 OpenCode 插件目录使用,零依赖:
59+
60+
**PowerShell:**
61+
```powershell
62+
Copy-Item src/utf8-encoding.ts $env:USERPROFILE/.config/opencode/plugins/utf8-encoding.ts
63+
```
64+
65+
**Bash / WSL:**
66+
```bash
67+
cp src/utf8-encoding.ts ~/.config/opencode/plugins/utf8-encoding.ts
68+
```
69+
70+
重启 OpenCode 即生效。无需 `npm install`,无需构建。
71+
72+
`src/utf8-encoding.ts` 仅使用 Node.js 内置模块(`node:fs`, `node:os`, `node:path`),无任何 npm 依赖。
5673
## Requirements
5774

5875
- **OpenCode** (any recent version with plugin support)
@@ -75,24 +92,14 @@ npm run typecheck
7592
npm run dev
7693
```
7794

78-
### Local plugin testing
79-
80-
To test locally without publishing, reference the build output in your `opencode.jsonc`:
81-
82-
```jsonc
83-
{
84-
"plugin": [
85-
"/path/to/opencode-windows-encoding/dist/index.js"
86-
]
87-
}
88-
```
95+
### 本地开发测试
8996

90-
Or reference the TypeScript source directly:
97+
直接引用源码即可:
9198

9299
```jsonc
93100
{
94101
"plugin": [
95-
"/path/to/opencode-windows-encoding/src/index.ts"
102+
"/path/to/opencode-windows-encoding/src/utf8-encoding.ts"
96103
]
97104
}
98105
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "opencode-windows-encoding",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "OpenCode plugin to fix UTF-8 encoding issues in PowerShell on Windows",
55
"type": "module",
6-
"main": "./dist/index.js",
7-
"types": "./dist/index.d.ts",
6+
"main": "./dist/utf8-encoding.js",
7+
"types": "./dist/utf8-encoding.d.ts",
88
"exports": {
99
".": {
10-
"types": "./dist/index.d.ts",
11-
"import": "./dist/index.js"
10+
"types": "./dist/utf8-encoding.d.ts",
11+
"import": "./dist/utf8-encoding.js"
1212
}
1313
},
1414
"files": [
@@ -34,9 +34,7 @@
3434
"url": "git+https://github.com/Cle2ment/opencode-windows-encoding.git"
3535
},
3636
"license": "AGPL-3.0",
37-
"dependencies": {
38-
"@opencode-ai/plugin": "^1.17.0"
39-
},
37+
"dependencies": {},
4038
"devDependencies": {
4139
"@types/node": "^22.0.0",
4240
"tsup": "^8.0.0",

src/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/utf8-encoding.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
import type { Plugin } from "@opencode-ai/plugin"
1+
/**
2+
* OpenCode Plugin — UTF-8 Encoding Fix for Windows + PowerShell
3+
*
4+
* 单文件插件,可直接复制到 ~/.config/opencode/plugins/ 使用,无需 npm install。
5+
*
6+
* 工作原理:拦截所有 bash/shell 工具调用,在命令前注入 PowerShell
7+
* UTF-8 编码配置,解决 Windows 下中文/非 ASCII 字符乱码问题。
8+
*/
9+
210
import { appendFileSync } from "node:fs"
311
import { tmpdir } from "node:os"
412
import { join } from "node:path"
513

14+
// ── 调试日志(写入临时目录) ──
615
const LOG = join(tmpdir(), "utf8-plugin.log")
716
function flog(msg: string) {
817
try { appendFileSync(LOG, `[${new Date().toISOString()}] ${msg}\n`, "utf8") } catch {}
918
}
1019

11-
export const Utf8EncodingPlugin: Plugin = async () => {
12-
flog("=== LOADED ===")
20+
// ── PowerShell UTF-8 编码前缀 ──
21+
const UTF8_ENC =
22+
"[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;$OutputEncoding=[Text.Encoding]::UTF8;"
1323

14-
function stripSetPrefixes(cmd: string): { prefixes: string; cleanCmd: string } {
15-
const m = cmd.match(/^((?:set\s+\w+="[^"]*"\s*&&\s*)+)/)
16-
if (m) return { prefixes: m[1], cleanCmd: cmd.slice(m[1].length) }
17-
return { prefixes: "", cleanCmd: cmd }
18-
}
24+
/** 提取 opencode 在命令前追加的 set VAR="value" && 前缀 */
25+
function stripSetPrefixes(cmd: string): { prefixes: string; cleanCmd: string } {
26+
const m = cmd.match(/^((?:set\s+\w+="[^"]*"\s*&&\s*)+)/)
27+
if (m) return { prefixes: m[1], cleanCmd: cmd.slice(m[1].length) }
28+
return { prefixes: "", cleanCmd: cmd }
29+
}
1930

20-
const UTF8_ENC = "[Console]::OutputEncoding=[Console]::InputEncoding=[Text.Encoding]::UTF8;$OutputEncoding=[Text.Encoding]::UTF8;"
31+
export const Utf8EncodingPlugin = async () => {
32+
flog("=== LOADED ===")
2133

2234
return {
23-
// ── LLM 工具调用 (bash/shell) ──
24-
"tool.execute.before": async (input, output) => {
35+
"tool.execute.before": async (input: Record<string, unknown>, output: Record<string, unknown>) => {
2536
const tool = String(input?.tool ?? "")
2637
flog(`[tool.before] tool="${tool}"`)
2738

@@ -39,10 +50,11 @@ export const Utf8EncodingPlugin: Plugin = async () => {
3950
const { prefixes, cleanCmd } = stripSetPrefixes(cmd)
4051
flog(` orig: ${cleanCmd.slice(0, 120)}`)
4152

53+
// 防止重复注入
4254
if (cleanCmd.includes("OutputEncoding")) { flog(" skip (idempotent)"); return }
4355

4456
args.command = prefixes + UTF8_ENC + cleanCmd
45-
flog(` INJECTED`)
57+
flog(" INJECTED")
4658
},
4759
}
4860
}

tsup.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { defineConfig } from "tsup"
22

33
export default defineConfig({
4-
entry: ["src/index.ts"],
4+
entry: ["src/utf8-encoding.ts"],
55
format: ["esm"],
66
dts: true,
77
clean: true,
8-
external: ["@opencode-ai/plugin"],
98
target: "node18",
109
outDir: "dist",
1110
sourcemap: true,

0 commit comments

Comments
 (0)