opencode-gbk-tools 0.1.8 → 0.1.9

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 CHANGED
@@ -1,158 +1,140 @@
1
1
  # opencode-gbk-tools
2
2
 
3
- 为 OpenCode 提供一套面向 `GBK` / `GB18030` 文本工程的自定义工具与专用 agent。
3
+ 为 OpenCode 提供一套面向 `GBK` / `GB18030` 编码文本文件的自定义工具与专用 agent。
4
4
 
5
- ## 功能
5
+ 解决 OpenCode 内置工具无法正确读写 GBK 编码文件的问题。
6
6
 
7
- - `gbk_read`
8
- - `gbk_write`
9
- - `gbk_edit`
10
- - `gbk-engine` agent
7
+ ---
11
8
 
12
- ## 安装
13
-
14
- 推荐方式:直接作为 OpenCode npm plugin 加载工具。
15
-
16
- 最方便的一键接入:
17
-
18
- ```bash
19
- npx opencode-gbk-tools setup --project
20
- ```
9
+ ## 提供的工具
21
10
 
22
- 这条命令会自动:
11
+ | 工具 | 用途 |
12
+ |------|------|
13
+ | `gbk_read` | 读取 GBK/GB18030 文件,支持分页、尾部预览 |
14
+ | `gbk_write` | 写入或追加内容到 GBK 文件(`append=true` 支持追加) |
15
+ | `gbk_edit` | 精确替换 GBK 文件中的指定文本块 |
16
+ | `gbk_search` | 在 GBK 文件中搜索关键词,返回行号和上下文 |
17
+ | `gbk-engine` | 专用 agent,引导 AI 正确使用上述工具 |
23
18
 
24
- - 在当前项目写入或更新 `opencode.json`
25
- - 自动加入 `plugin: ["opencode-gbk-tools"]`
26
- - 安装 `.opencode/agents/gbk-engine.md`
19
+ ---
27
20
 
28
- 如果你想装到用户全局 OpenCode 配置:
21
+ ## 安装
29
22
 
30
23
  ```bash
31
- npx opencode-gbk-tools setup
32
- ```
33
-
34
- `opencode.json`:
35
-
36
- ```json
37
- {
38
- "$schema": "https://opencode.ai/config.json",
39
- "plugin": ["opencode-gbk-tools"]
40
- }
24
+ npx opencode-gbk-tools install
41
25
  ```
42
26
 
43
- 这样会直接注册:
27
+ 安装完成后**重启 OpenCode**,工具立即生效。
44
28
 
45
- - `gbk_read`
46
- - `gbk_write`
47
- - `gbk_edit`
29
+ ---
48
30
 
49
- 如果你还需要预置 `gbk-engine` agent,请继续使用下面的 CLI 安装方式把 agent 文件写入本地配置目录。
31
+ ## 卸载
50
32
 
51
- 推荐优先使用 `setup`,`install` 更适合手动拆分安装流程。
33
+ ### 一键卸载(对应一键安装)
52
34
 
53
- 一次性使用:
35
+ 如果当初是用 `npx opencode-gbk-tools install` 全局安装的,执行:
54
36
 
55
37
  ```bash
56
- npx opencode-gbk-tools install
38
+ npx opencode-gbk-tools uninstall
57
39
  ```
58
40
 
59
- 全局安装后使用:
41
+ ---
60
42
 
61
- ```bash
62
- npm install -g opencode-gbk-tools
63
- opencode-gbk install
64
- opencode-gbk setup
65
- ```
43
+ ## 工具使用说明
66
44
 
67
- 安装到当前项目:
45
+ ### gbk_read — 读取文件
68
46
 
69
- ```bash
70
- opencode-gbk install --project
47
+ ```
48
+ gbk_read(filePath="文件路径")
49
+ gbk_read(filePath="文件路径", offset=100, limit=50) # 从第100行读取50行
50
+ gbk_read(filePath="文件路径", tail=true, limit=30) # 读取最后30行
71
51
  ```
72
52
 
73
- ## 目录
74
-
75
- - 全局安装目标:`~/.config/opencode`
76
- - 项目安装目标:当前命令执行目录下的 `.opencode`
53
+ - 返回带行号的内容,格式为 `行号: 内容`
54
+ - `limit` 建议不超过 500 行,大文件请先用 `gbk_search` 定位
77
55
 
78
- ## CLI
56
+ ### gbk_search — 搜索内容
79
57
 
80
- ```bash
81
- opencode-gbk setup
82
- opencode-gbk setup --project
58
+ ```
59
+ gbk_search(filePath="文件路径", pattern="搜索关键词")
60
+ gbk_search(filePath="文件路径", pattern="[@标签名]", contextLines=5)
61
+ ```
83
62
 
84
- opencode-gbk install
85
- opencode-gbk install --project
86
- opencode-gbk install --force
63
+ - 返回匹配行号及上下文,是处理大文件的第一步
87
64
 
88
- opencode-gbk uninstall
89
- opencode-gbk uninstall --project
65
+ ### gbk_edit — 编辑文件
90
66
 
91
- opencode-gbk doctor
92
- opencode-gbk doctor --project
67
+ ```
68
+ gbk_edit(filePath="文件路径", oldString="原文内容", newString="新内容")
69
+ gbk_edit(filePath="文件路径", oldString="原文", newString="新文", startLine=100, endLine=200)
93
70
  ```
94
71
 
95
- ## 卸载
96
-
97
- 如果你是通过 `setup` 或手动修改 `opencode.json` / `opencode.jsonc` 启用 npm plugin:
98
-
99
- - 删除 `plugin` 数组中的 `"opencode-gbk-tools"`
100
- - 如果不再需要 `gbk-engine`,再删除对应 agent 文件
101
- - 项目级:`.opencode/agents/gbk-engine.md`
102
- - 全局:`~/.config/opencode/agents/gbk-engine.md`
72
+ **重要:`oldString` 必须是文件的原始内容,不能包含 `gbk_read` 输出的行号前缀。**
103
73
 
104
- 如果你是通过 CLI 安装 agent 或工具文件:
74
+ 错误示范(包含行号前缀,会失败):
75
+ ```
76
+ oldString="3787: SENDMSG 0 内容"
77
+ ```
105
78
 
106
- ```bash
107
- opencode-gbk uninstall
108
- opencode-gbk uninstall --project
79
+ 正确示范(纯文件内容):
80
+ ```
81
+ oldString="SENDMSG 0 内容"
109
82
  ```
110
83
 
111
- 说明:
84
+ ### gbk_write — 写入文件
112
85
 
113
- - `opencode-gbk uninstall` 会卸载全局安装到 `~/.config/opencode` 下的文件
114
- - `opencode-gbk uninstall --project` 会卸载当前项目 `.opencode/` 下的文件
115
- - `uninstall` 只会删除通过本包 manifest 记录过的文件,不会自动改写你的 `opencode.json` / `opencode.jsonc` plugin 配置
86
+ ```
87
+ gbk_write(filePath="文件路径", content="内容", overwrite=true) # 覆盖写入
88
+ gbk_write(filePath="文件路径", content="\r\n新增内容", append=true) # 追加到末尾
89
+ ```
116
90
 
117
- ## Agent
91
+ - `append=true`:在文件末尾追加内容,文件不存在时自动创建
92
+ - `overwrite=true`:覆盖整个文件
118
93
 
119
- 安装后会生成 `gbk-engine`。
94
+ ---
120
95
 
121
- 说明:
96
+ ## 大文件操作建议
122
97
 
123
- - npm `plugin` 方式负责注册 `gbk_read`、`gbk_write`、`gbk_edit`
124
- - `setup` 会自动写入 `plugin` 配置并安装 `gbk-engine`
125
- - `gbk-engine` 仍通过 CLI 安装到 `.opencode/agents/` 或 `~/.config/opencode/agents/`
126
- - 如果你只需要工具,不需要 agent,只配置 `plugin` 即可
98
+ 对于行数超过 500 行的大文件,推荐以下工作流:
127
99
 
128
- 它的目标是:
100
+ ```
101
+ 1. gbk_search(pattern="要找的内容") → 获取行号
102
+ 2. gbk_read(offset=行号-5, limit=20) → 读取目标区域
103
+ 3. gbk_edit(oldString=..., newString=..., startLine=..., endLine=...) → 精确编辑
104
+ ```
129
105
 
130
- - 文件内容读取必须走 `gbk_read`
131
- - 文件内容写入必须走 `gbk_write`
132
- - 文件内容修改必须走 `gbk_edit`
133
- - 内置 `read`、`edit`、`grep` 被限制
106
+ ---
134
107
 
135
108
  ## 已知限制
136
109
 
137
110
  - 只支持文本文件,不支持二进制文件
138
- - 首版不做自动编码识别
139
- - 只支持 `gbk` 和 `gb18030`
140
- - `edit: deny` 在 OpenCode 中会一起限制内置 `write`、`patch`、`multiedit`
111
+ - 只支持 `gbk` 和 `gb18030` 编码
141
112
  - 对无法映射的字符沿用 `iconv-lite` 默认替代行为
142
113
 
143
- ## 大文件建议
114
+ ---
144
115
 
145
- - `gbk_read` 支持 `tail` 预览最后 N 行
146
- - `gbk_read` 返回 `fileSize`、`newlineStyle`、`streamed`、`truncated`
147
- - `gbk_edit` 支持 `startLine/endLine`
148
- - `gbk_edit` 支持 `startAnchor/endAnchor`
149
- - 大文件会自动走更省内存的流式路径
116
+ ## 常见问题
150
117
 
151
- ## 发布
118
+ **Q:安装后 OpenCode 里看不到工具?**
119
+ A:重启 OpenCode。工具在 OpenCode 启动时加载,安装后需要重启才生效。
152
120
 
153
- ```bash
154
- npm run check
155
- npm test
156
- npm run build
157
- npm pack --dry-run
158
- ```
121
+ **Q:`npx` 提示找不到命令?**
122
+ A:请先安装 Node.js(https://nodejs.org/),版本需要 18 或以上。
123
+
124
+ **Q:Windows 路径在哪?**
125
+ A:全局安装目录为 `C:\Users\你的用户名\.config\opencode\tools\`
126
+
127
+ **Q:gbk_edit 提示"未找到需要替换的文本"?**
128
+ A:检查 `oldString` 是否包含了行号前缀(如 `"3787: "`),去掉行号前缀后重试。若要在文件末尾追加内容,请使用 `gbk_write [append=true]`。
129
+
130
+ ---
131
+
132
+ ## 版本历史
133
+
134
+ | 版本 | 说明 |
135
+ |------|------|
136
+ | 0.1.9 | `gbk_edit` 修复精确匹配路径写入 CRLF 文件时换行风格变 mixed 的 bug |
137
+ | 0.1.8 | `gbk_write` 新增 `append=true` 追加模式 |
138
+ | 0.1.7 | `gbk_edit` 自动剥离行号前缀后重试匹配 |
139
+ | 0.1.6 | 新增 `gbk_search` 搜索工具 |
140
+ | 0.1.5 | 初始版本 |
@@ -16724,7 +16724,9 @@ async function replaceGbkFileText(input) {
16724
16724
  } else if (occurrencesBefore > 1) {
16725
16725
  throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${normalizedInput.oldString}`);
16726
16726
  }
16727
- const replaced = replaceAll ? scope.selectedText.split(normalizedInput.oldString).join(normalizedInput.newString) : scope.selectedText.replace(normalizedInput.oldString, normalizedInput.newString);
16727
+ const fileNewlineStyle = detectNewlineStyle(current.content);
16728
+ const alignedNewString = fileNewlineStyle === "crlf" ? normalizedInput.newString.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : fileNewlineStyle === "lf" ? normalizedInput.newString.replace(/\r\n/g, "\n") : normalizedInput.newString;
16729
+ const replaced = replaceAll ? scope.selectedText.split(normalizedInput.oldString).join(alignedNewString) : scope.selectedText.replace(normalizedInput.oldString, alignedNewString);
16728
16730
  const outputText = `${current.content.slice(0, scope.rangeStart)}${replaced}${current.content.slice(scope.rangeEnd)}`;
16729
16731
  const buffer = import_iconv_lite.default.encode(outputText, current.encoding);
16730
16732
  await fs2.writeFile(current.filePath, buffer);
@@ -16934,7 +16934,9 @@ async function replaceGbkFileText(input) {
16934
16934
  } else if (occurrencesBefore > 1) {
16935
16935
  throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${normalizedInput.oldString}`);
16936
16936
  }
16937
- const replaced = replaceAll ? scope.selectedText.split(normalizedInput.oldString).join(normalizedInput.newString) : scope.selectedText.replace(normalizedInput.oldString, normalizedInput.newString);
16937
+ const fileNewlineStyle = detectNewlineStyle(current.content);
16938
+ const alignedNewString = fileNewlineStyle === "crlf" ? normalizedInput.newString.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : fileNewlineStyle === "lf" ? normalizedInput.newString.replace(/\r\n/g, "\n") : normalizedInput.newString;
16939
+ const replaced = replaceAll ? scope.selectedText.split(normalizedInput.oldString).join(alignedNewString) : scope.selectedText.replace(normalizedInput.oldString, alignedNewString);
16938
16940
  const outputText = `${current.content.slice(0, scope.rangeStart)}${replaced}${current.content.slice(scope.rangeEnd)}`;
16939
16941
  const buffer = import_iconv_lite.default.encode(outputText, current.encoding);
16940
16942
  await fs2.writeFile(current.filePath, buffer);
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "manifestVersion": 1,
3
3
  "packageName": "opencode-gbk-tools",
4
- "packageVersion": "0.1.8",
4
+ "packageVersion": "0.1.9",
5
5
  "artifacts": [
6
6
  {
7
7
  "relativePath": "tools/gbk_edit.js",
8
8
  "kind": "tool",
9
- "expectedHash": "68d8398eae66592166de62e1bdce35ac51a6c22f27e6b48eca83c62370bc44f5",
9
+ "expectedHash": "3b8f229a82519dcca21f5b9140bffc112d781390a0c47335a968f1e853ed1dc2",
10
10
  "hashAlgorithm": "sha256"
11
11
  },
12
12
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-gbk-tools",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "GBK/GB18030 custom tools and agent for OpenCode",
5
5
  "type": "module",
6
6
  "license": "MIT",