@wwkit/harness 1.0.33 → 1.0.35

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.
Files changed (210) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +128 -128
  3. package/agents/scrum.md +206 -186
  4. package/agents/sprint.md +130 -121
  5. package/bin/index.js +291 -226
  6. package/commands/fix.md +87 -87
  7. package/commands/git-sync.md +217 -217
  8. package/commands/net-port.md +364 -364
  9. package/commands/pytest.md +10 -10
  10. package/commands/resume.md +104 -104
  11. package/commands/tl.md +11 -11
  12. package/commands/tsl.md +12 -0
  13. package/package.json +5 -4
  14. package/plugin.js +19 -19
  15. package/readme/development.md +39 -39
  16. package/readme/publish.md +41 -41
  17. package/readme/testing.md +44 -44
  18. package/scripts/postinstall.js +58 -58
  19. package/skills/better-agents/SKILL.md +300 -0
  20. package/skills/better-agents/references/evidence-map.md +102 -0
  21. package/skills/better-agents/references/format-spec.md +91 -0
  22. package/skills/better-agents/references/section-template.md +116 -0
  23. package/skills/better-skill/SKILL.md +319 -124
  24. package/skills/better-skill/references/new-mode.md +126 -0
  25. package/skills/blame-agents/SKILL.md +270 -0
  26. package/skills/blame-agents/references/check-agents.js +364 -0
  27. package/skills/blame-agents/references/scoring-rules.md +259 -0
  28. package/skills/blame-skill/SKILL.md +163 -201
  29. package/skills/blame-skill/references/scoring-rules.md +190 -0
  30. package/skills/blame-sprint-agent/SKILL.md +172 -0
  31. package/skills/blame-sprint-agent/references/scoring-rules.md +113 -0
  32. package/skills/blame-sprint-skill/SKILL.md +165 -0
  33. package/skills/blame-sprint-skill/references/scoring-rules.md +101 -0
  34. package/skills/extract/SKILL.md +119 -119
  35. package/skills/extract/references/build-xpath.js +30 -30
  36. package/skills/extract/references/clean-html.js +90 -90
  37. package/skills/extract/references/detail.md +54 -54
  38. package/skills/extract/references/detail.schema.json5 +48 -48
  39. package/skills/extract/references/extract-detail.js +71 -71
  40. package/skills/extract/references/extract-regex.js +81 -81
  41. package/skills/extract/references/extract-sample.js +48 -48
  42. package/skills/extract/references/list-rule-gen.md +109 -109
  43. package/skills/extract/references/list.md +134 -134
  44. package/skills/extract/references/list.schema.json5 +33 -33
  45. package/skills/extract/references/navi.md +44 -44
  46. package/skills/extract/references/navi.schema.json5 +23 -23
  47. package/skills/extract/references/text.md +40 -40
  48. package/skills/extract/references/text.schema.json5 +21 -21
  49. package/skills/extract/references/to-text.js +20 -20
  50. package/skills/extract/references/validate-schema.js +39 -39
  51. package/skills/jstest/SKILL.md +561 -561
  52. package/skills/jstest/references/case-create.md +327 -327
  53. package/skills/jstest/references/case-fix.md +273 -273
  54. package/skills/jstest/references/config.md +148 -148
  55. package/skills/jstest/references/coverage-analyze.md +247 -247
  56. package/skills/jstest/references/env-ensure.md +210 -210
  57. package/skills/jstest/references/execute.md +168 -168
  58. package/skills/jstest/references/sample.md +166 -166
  59. package/skills/jstest/references/scoring-rules.md +75 -75
  60. package/skills/jstest/references/src/jstest-sample/Calculator.js +80 -80
  61. package/skills/jstest/references/src/jstest-sample/ConfigManager.js +72 -72
  62. package/skills/jstest/references/src/jstest-sample/FileProcessor.js +57 -57
  63. package/skills/jstest/references/src/jstest-sample/OrderService.js +98 -98
  64. package/skills/jstest/references/src/jstest-sample/TokenGenerator.js +60 -60
  65. package/skills/jstest/references/src/jstest-sample/UserService.js +56 -56
  66. package/skills/jstest/references/src/jstest-sample/index.js +6 -6
  67. package/skills/jstest/references/suitability-check.md +232 -232
  68. package/skills/jstest/references/test-standards.md +288 -288
  69. package/skills/lint/SKILL.md +440 -440
  70. package/skills/lint/references/ai-fix.md +137 -137
  71. package/skills/lint/references/config-setup.md +102 -102
  72. package/skills/lint/references/config.md +65 -65
  73. package/skills/lint/references/detect-language.js +65 -65
  74. package/skills/lint/references/env-ensure.md +88 -88
  75. package/skills/lint/references/language-detect.md +75 -75
  76. package/skills/lint/references/languages/js.md +110 -110
  77. package/skills/lint/references/languages/py.md +89 -89
  78. package/skills/lint/references/rules-analyze.md +125 -125
  79. package/skills/lint/references/suitability-check.md +81 -81
  80. package/skills/lint/references/tool-fix.md +90 -90
  81. package/skills/pytest/SKILL.md +555 -555
  82. package/skills/pytest/references/case-create.md +324 -324
  83. package/skills/pytest/references/case-fix.md +271 -271
  84. package/skills/pytest/references/config.md +145 -145
  85. package/skills/pytest/references/coverage-analyze.md +223 -223
  86. package/skills/pytest/references/env-ensure.md +195 -195
  87. package/skills/pytest/references/execute.md +152 -152
  88. package/skills/pytest/references/sample.md +159 -159
  89. package/skills/pytest/references/scoring-rules.md +57 -57
  90. package/skills/pytest/references/src/pytest-sample/Calculator.py +67 -67
  91. package/skills/pytest/references/src/pytest-sample/ConfigManager.py +68 -68
  92. package/skills/pytest/references/src/pytest-sample/FileProcessor.py +53 -53
  93. package/skills/pytest/references/src/pytest-sample/OrderService.py +82 -82
  94. package/skills/pytest/references/src/pytest-sample/TokenGenerator.py +50 -50
  95. package/skills/pytest/references/src/pytest-sample/UserService.py +45 -45
  96. package/skills/pytest/references/suitability-check.md +220 -220
  97. package/skills/pytest/references/test-standards.md +244 -244
  98. package/skills/query/SKILL.md +71 -71
  99. package/skills/read-docs/SKILL.md +134 -134
  100. package/skills/read-docs/references/opencode/agents/cases.md +206 -206
  101. package/skills/read-docs/references/opencode/agents/design-pattern.md +47 -47
  102. package/skills/read-docs/references/opencode/agents/detail.md +191 -191
  103. package/skills/read-docs/references/opencode/agents/examples.md +100 -100
  104. package/skills/read-docs/references/opencode/agents/index.md +309 -309
  105. package/skills/read-docs/references/opencode/agents/parallel-dispatch.md +149 -149
  106. package/skills/read-docs/references/opencode/agents/subagent-internals.md +217 -217
  107. package/skills/read-docs/references/opencode/agents/workflow.md +161 -161
  108. package/skills/read-docs/references/opencode/cli/commands/acp.md +31 -31
  109. package/skills/read-docs/references/opencode/cli/commands/agent.md +16 -16
  110. package/skills/read-docs/references/opencode/cli/commands/attach.md +20 -20
  111. package/skills/read-docs/references/opencode/cli/commands/mcp.md +37 -37
  112. package/skills/read-docs/references/opencode/cli/commands/others.md +48 -48
  113. package/skills/read-docs/references/opencode/cli/commands/plugin.md +12 -12
  114. package/skills/read-docs/references/opencode/cli/commands/provider.md +43 -43
  115. package/skills/read-docs/references/opencode/cli/commands/run.md +81 -81
  116. package/skills/read-docs/references/opencode/cli/commands/serve.md +84 -84
  117. package/skills/read-docs/references/opencode/cli/commands/session.md +37 -37
  118. package/skills/read-docs/references/opencode/cli/commands/web.md +14 -14
  119. package/skills/read-docs/references/opencode/cli/env.md +38 -38
  120. package/skills/read-docs/references/opencode/cli/index.md +18 -18
  121. package/skills/read-docs/references/opencode/cli/tui.md +35 -35
  122. package/skills/read-docs/references/opencode/commands/examples.md +42 -42
  123. package/skills/read-docs/references/opencode/commands/index.md +185 -185
  124. package/skills/read-docs/references/opencode/config/provider.md +151 -151
  125. package/skills/read-docs/references/opencode/formatter/index.md +70 -70
  126. package/skills/read-docs/references/opencode/guide/config.md +419 -419
  127. package/skills/read-docs/references/opencode/guide/formatters.md +70 -70
  128. package/skills/read-docs/references/opencode/guide/index.md +37 -37
  129. package/skills/read-docs/references/opencode/guide/providers.md +31 -31
  130. package/skills/read-docs/references/opencode/guide/rules.md +63 -63
  131. package/skills/read-docs/references/opencode/plugins/examples.md +75 -75
  132. package/skills/read-docs/references/opencode/plugins/index.md +188 -188
  133. package/skills/read-docs/references/opencode/reference/index.md +118 -118
  134. package/skills/read-docs/references/opencode/rule/index.md +77 -77
  135. package/skills/read-docs/references/opencode/skills/detail.md +113 -113
  136. package/skills/read-docs/references/opencode/skills/examples.md +141 -141
  137. package/skills/read-docs/references/opencode/skills/index.md +126 -126
  138. package/skills/read-docs/references/opencode/skills/workflow.md +146 -146
  139. package/skills/read-docs/references/opencode/tests/agent.md +9 -9
  140. package/skills/read-docs/references/opencode/tests/config.md +59 -59
  141. package/skills/read-docs/references/opencode/tests/file.md +11 -11
  142. package/skills/read-docs/references/opencode/tests/serve.md +17 -17
  143. package/skills/read-docs/references/opencode/tests/session.md +30 -30
  144. package/skills/read-docs/references/opencode/tests/web.md +16 -16
  145. package/skills/read-docs/references/opencode/tools/arguments.md +305 -305
  146. package/skills/read-docs/references/opencode/tools/context.md +17 -17
  147. package/skills/read-docs/references/opencode/tools/custom.md +110 -110
  148. package/skills/read-docs/references/opencode/tools/detail.md +104 -104
  149. package/skills/read-docs/references/opencode/tools/examples.md +71 -71
  150. package/skills/read-docs/references/opencode/tools/index.md +56 -56
  151. package/skills/read-docs/references/opencode/tools/lsp.md +25 -25
  152. package/skills/read-docs/references/opencode/tools/mcp.md +132 -132
  153. package/skills/read-docs/references/opencode/train/README.md +134 -134
  154. package/skills/read-docs/references/opencode/train/agent-basic.md +771 -771
  155. package/skills/read-docs/references/opencode/train/command-basic.md +667 -667
  156. package/skills/read-docs/references/opencode/train/config-basic.md +508 -508
  157. package/skills/read-docs/references/opencode/train/index.md +163 -163
  158. package/skills/read-docs/references/opencode/train/practice.md +872 -872
  159. package/skills/read-docs/references/opencode/train/skill-basic.md +607 -607
  160. package/skills/read-docs/references/opencode/tui/commands/config.md +31 -31
  161. package/skills/read-docs/references/opencode/tui/commands/editor.md +46 -46
  162. package/skills/read-docs/references/opencode/tui/commands/index.md +124 -124
  163. package/skills/read-docs/references/opencode/tui/commands/init.md +4 -4
  164. package/skills/read-docs/references/opencode/tui/index.md +26 -26
  165. package/skills/read-docs/references/superpowers/bootstrap.md +107 -107
  166. package/skills/read-docs/references/superpowers/comparison.md +77 -77
  167. package/skills/read-docs/references/superpowers/context.md +71 -71
  168. package/skills/read-docs/references/superpowers/index.md +58 -58
  169. package/skills/read-docs/references/superpowers/parallel.md +49 -49
  170. package/skills/read-docs/references/superpowers/sdd.md +183 -183
  171. package/skills/read-docs/references/superpowers/skills.md +68 -68
  172. package/skills/read-docs/references/superpowers/workflow.md +83 -83
  173. package/skills/revise/SKILL.md +121 -121
  174. package/skills/revise/references/article.md +36 -36
  175. package/skills/revise/references/article.schema.json5 +40 -40
  176. package/skills/revise/references/gallery.md +34 -34
  177. package/skills/revise/references/gallery.schema.json5 +40 -40
  178. package/skills/revise/references/question.md +32 -32
  179. package/skills/revise/references/question.schema.json5 +31 -31
  180. package/skills/revise/references/status.md +31 -31
  181. package/skills/revise/references/status.schema.json5 +27 -27
  182. package/skills/revise/references/validate-schema.js +39 -39
  183. package/skills/step-extract/SKILL.md +205 -0
  184. package/skills/tsl/SKILL.md +393 -0
  185. package/skills/tsl/package.json +10 -0
  186. package/skills/tsl/references/global-review-rules.md +25 -0
  187. package/skills/tsl/references/prepare-prompt.md +150 -0
  188. package/skills/tsl/references/terminology-rules.md +93 -0
  189. package/skills/tsl/references/translation-guide.md +23 -0
  190. package/skills/tsl/references/translation-prompt.md +115 -0
  191. package/skills/tsl/references/translation-rules.md +48 -0
  192. package/skills/tsl/references/verification-rules.md +58 -0
  193. package/skills/tsl/scripts/check-boundaries.js +140 -0
  194. package/skills/tsl/scripts/chunk-text.js +144 -0
  195. package/skills/tsl/scripts/clean-text.js +121 -0
  196. package/skills/tsl/scripts/cleanup-glossary.js +160 -0
  197. package/skills/tsl/scripts/extract-pdf.js +94 -0
  198. package/skills/tsl/scripts/generate-review.js +179 -0
  199. package/skills/tsl/scripts/merge-result.js +164 -0
  200. package/skills/tsl/scripts/verify-chunk.js +170 -0
  201. package/skills/tsl-format/SKILL.md +298 -0
  202. package/skills/tsl-format/references/format-prompt.md +103 -0
  203. package/skills/tsl-format/references/format-rules.md +58 -0
  204. package/skills/tsl-format/scripts/chunk-bilingual.js +96 -0
  205. package/skills/tsl-format/scripts/merge-format.js +105 -0
  206. package/src/config.js +29 -29
  207. package/src/config.json5 +19 -19
  208. package/skills/new-skill/SKILL.md +0 -227
  209. package/skills/new-skill/references/template.md +0 -53
  210. package/skills/new-skill/references/workflow-patterns.md +0 -104
@@ -1,305 +1,305 @@
1
- # Arguments
2
-
3
- ## Zod
4
- TypeScript-first schema validation with static type inference: `https://zod.dev/`
5
-
6
- You can use tool.schema, which is just Zod, to define argument types.
7
- ```ts
8
- args: {
9
- query: tool.schema.string().describe("SQL query to execute")
10
- }
11
- ```
12
-
13
- You can also import Zod directly and return a plain object:
14
- ```ts
15
- import { z } from "zod"
16
-
17
- export default {
18
- description: "Tool description",
19
- args: {
20
- param: z.string().describe("Parameter description"),
21
- },
22
- async execute(args, context) {
23
- // Tool implementation
24
- return "result"
25
- },
26
- }
27
- ```
28
-
29
- ## Example
30
- Write a tool in Python
31
- You can write your tools in any language you want. Here’s an example that adds two numbers using Python.
32
-
33
- First, create the tool as a Python script:
34
- ```py
35
- .opencode/tools/add.py
36
-
37
- import sys
38
-
39
- a = int(sys.argv[1])
40
- b = int(sys.argv[2])
41
- print(a + b)
42
- ```
43
-
44
- Then create the tool definition that invokes it:
45
- ```ts
46
- // .opencode/tools/python-add.ts
47
- import { tool } from "@opencode-ai/plugin"
48
- import path from "path"
49
-
50
- export default tool({
51
- description: "Add two numbers using Python",
52
- args: {
53
- a: tool.schema.number().describe("First number"),
54
- b: tool.schema.number().describe("Second number"),
55
- },
56
- async execute(args, context) {
57
- const script = path.join(context.worktree, ".opencode/tools/add.py")
58
- const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
59
- return result.trim()
60
- },
61
- })
62
- ```
63
-
64
- ## Bun.$ Shell API
65
-
66
- OpenCode 内置 Bun 运行时,可在 Tool 中直接使用 `Bun.$` Shell API 执行命令。
67
-
68
- ### 基础用法
69
-
70
- ```ts
71
- import { tool } from "@opencode-ai/plugin"
72
-
73
- export default tool({
74
- description: "Execute shell commands",
75
- args: {
76
- cmd: tool.schema.string().describe("Command to execute"),
77
- },
78
- async execute(args, context) {
79
- // 获取文本输出
80
- const text = await Bun.$`echo ${args.cmd}`.text()
81
- return text.trim()
82
- },
83
- })
84
- ```
85
-
86
- ### 常用方法
87
-
88
- ```ts
89
- // 文本输出
90
- const text = await Bun.$`cat file.txt`.text()
91
-
92
- // JSON 输出
93
- const data = await Bun.$`echo '{"name":"test"}'`.json()
94
-
95
- // 二进制数据
96
- const buffer = await Bun.$`cat image.png`.arrayBuffer()
97
-
98
- // 只执行不获取输出
99
- await Bun.$`mkdir -p ${dir}`
100
-
101
- // 检查退出码
102
- const { exitCode, stdout } = await Bun.$`some-command`
103
- if (exitCode !== 0) {
104
- throw new Error(`Command failed with code ${exitCode}`)
105
- }
106
- ```
107
-
108
- ### 变量插值(自动转义)
109
-
110
- ```ts
111
- const file = "test.txt"
112
- const count = 10
113
-
114
- // 变量自动转义,防止注入
115
- await Bun.$`cat ${file}`.text()
116
- await Bun.$`head -n ${count} ${file}`.text()
117
-
118
- // 数组自动展开
119
- const files = ["a.txt", "b.txt", "c.txt"]
120
- await Bun.$`cat ${files}`.text() // => cat a.txt b.txt c.txt
121
- ```
122
-
123
- ### 错误处理
124
-
125
- ```ts
126
- // 捕获错误
127
- try {
128
- await Bun.$`some-command`.text()
129
- } catch (error) {
130
- console.error("Command failed:", error.message)
131
- }
132
-
133
- // 忽略错误
134
- const result = await Bun.$`some-command`.quiet()
135
- ```
136
-
137
- ### 管道操作
138
-
139
- ```ts
140
- // 管道
141
- const result = await Bun.$`cat file.txt | grep pattern`.text()
142
-
143
- // 流式处理
144
- const proc = Bun.$`cat large-file.txt`
145
- const reader = proc.stdout.getReader()
146
- ```
147
-
148
- ### 实用示例
149
-
150
- ```ts
151
- // Git 操作
152
- export default tool({
153
- description: "Get git status",
154
- args: {},
155
- async execute() {
156
- const status = await Bun.$`git status --short`.text()
157
- return status || "Working tree clean"
158
- },
159
- })
160
-
161
- // 文件操作
162
- export default tool({
163
- description: "Count lines in file",
164
- args: {
165
- file: tool.schema.string().describe("File path"),
166
- },
167
- async execute(args) {
168
- const count = await Bun.$`wc -l ${args.file}`.text()
169
- return `Total lines: ${count.trim()}`
170
- },
171
- })
172
-
173
- // 系统信息
174
- export default tool({
175
- description: "Get system info",
176
- args: {},
177
- async execute() {
178
- const [os, arch, cores] = await Promise.all([
179
- Bun.$`uname -s`.text(),
180
- Bun.$`uname -m`.text(),
181
- Bun.$`nproc`.text(),
182
- ])
183
- return { os: os.trim(), arch: arch.trim(), cores: cores.trim() }
184
- },
185
- })
186
-
187
- // 多行脚本
188
- export default tool({
189
- description: "Initialize project structure",
190
- args: {
191
- name: tool.schema.string().describe("Project name"),
192
- },
193
- async execute(args) {
194
- const result = await Bun.$`
195
- mkdir -p ${args.name}/src/components
196
- mkdir -p ${args.name}/tests
197
- cd ${args.name}
198
- touch src/index.ts
199
- touch tests/index.test.ts
200
- echo "Project ${args.name} initialized"
201
- `.text()
202
- return result.trim()
203
- },
204
- })
205
- ```
206
-
207
- ### 传递复杂对象给脚本
208
-
209
- 命令行参数本质是字符串数组,无法直接传递复杂对象。推荐通过 **stdin** 或 **临时文件** 传递 JSON。
210
-
211
- #### 方案1:stdin(推荐)
212
-
213
- Tool 通过 stdin 传递 JSON,Python 脚本从 stdin 读取,双方解耦。
214
-
215
- ```ts
216
- // Tool 定义
217
- export default tool({
218
- description: "Run Python with config",
219
- args: {
220
- config: tool.schema.object({
221
- name: tool.schema.string(),
222
- count: tool.schema.number(),
223
- options: tool.schema.object({
224
- debug: tool.schema.boolean(),
225
- }),
226
- }).describe("Configuration object"),
227
- },
228
- async execute(args) {
229
- const proc = Bun.$`python3 script.py`
230
- proc.stdin.write(JSON.stringify(args.config))
231
- const result = await proc.text()
232
- return result.trim()
233
- },
234
- })
235
- ```
236
-
237
- ```py
238
- # script.py
239
- import sys
240
- import json
241
-
242
- # 从 stdin 读取 JSON
243
- config = json.load(sys.stdin)
244
-
245
- print(f"Name: {config['name']}")
246
- print(f"Count: {config['count']}")
247
- print(f"Debug: {config['options']['debug']}")
248
- ```
249
-
250
- #### 方案2:临时文件
251
-
252
- Tool 写入临时文件,Python 从文件读取,适合大对象或需要持久化的场景。
253
-
254
- ```ts
255
- export default tool({
256
- description: "Run Python with config file",
257
- args: {
258
- config: tool.schema.object({
259
- name: tool.schema.string(),
260
- count: tool.schema.number(),
261
- }).describe("Configuration object"),
262
- },
263
- async execute(args) {
264
- // 写入临时文件
265
- const tmpFile = `/tmp/config-${Date.now()}.json`
266
- await Bun.write(tmpFile, JSON.stringify(args.config))
267
-
268
- // 执行 Python 脚本
269
- const result = await Bun.$`python3 script.py --config ${tmpFile}`.text()
270
-
271
- // 清理临时文件
272
- await Bun.$`rm ${tmpFile}`
273
-
274
- return result.trim()
275
- },
276
- })
277
- ```
278
-
279
- ```py
280
- # script.py
281
- import sys
282
- import json
283
- import argparse
284
-
285
- parser = argparse.ArgumentParser()
286
- parser.add_argument("--config", help="Config file path")
287
- args = parser.parse_args()
288
-
289
- # 从文件读取 JSON
290
- with open(args.config, 'r') as f:
291
- config = json.load(f)
292
-
293
- print(f"Name: {config['name']}")
294
- print(f"Count: {config['count']}")
295
- ```
296
-
297
- #### 方案对比
298
-
299
- | 方案 | 优点 | 缺点 | 适用场景 |
300
- |------|------|------|---------|
301
- | **stdin** | 解耦、简单、无需清理 | 无法持久化 | 小对象、一次性传递 |
302
- | **临时文件** | 可持久化、支持大对象 | 需要清理、文件管理 | 大对象、需要复用 |
303
-
304
- **推荐**:优先使用 stdin,简单且解耦。
305
- ```
1
+ # Arguments
2
+
3
+ ## Zod
4
+ TypeScript-first schema validation with static type inference: `https://zod.dev/`
5
+
6
+ You can use tool.schema, which is just Zod, to define argument types.
7
+ ```ts
8
+ args: {
9
+ query: tool.schema.string().describe("SQL query to execute")
10
+ }
11
+ ```
12
+
13
+ You can also import Zod directly and return a plain object:
14
+ ```ts
15
+ import { z } from "zod"
16
+
17
+ export default {
18
+ description: "Tool description",
19
+ args: {
20
+ param: z.string().describe("Parameter description"),
21
+ },
22
+ async execute(args, context) {
23
+ // Tool implementation
24
+ return "result"
25
+ },
26
+ }
27
+ ```
28
+
29
+ ## Example
30
+ Write a tool in Python
31
+ You can write your tools in any language you want. Here’s an example that adds two numbers using Python.
32
+
33
+ First, create the tool as a Python script:
34
+ ```py
35
+ .opencode/tools/add.py
36
+
37
+ import sys
38
+
39
+ a = int(sys.argv[1])
40
+ b = int(sys.argv[2])
41
+ print(a + b)
42
+ ```
43
+
44
+ Then create the tool definition that invokes it:
45
+ ```ts
46
+ // .opencode/tools/python-add.ts
47
+ import { tool } from "@opencode-ai/plugin"
48
+ import path from "path"
49
+
50
+ export default tool({
51
+ description: "Add two numbers using Python",
52
+ args: {
53
+ a: tool.schema.number().describe("First number"),
54
+ b: tool.schema.number().describe("Second number"),
55
+ },
56
+ async execute(args, context) {
57
+ const script = path.join(context.worktree, ".opencode/tools/add.py")
58
+ const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
59
+ return result.trim()
60
+ },
61
+ })
62
+ ```
63
+
64
+ ## Bun.$ Shell API
65
+
66
+ OpenCode 内置 Bun 运行时,可在 Tool 中直接使用 `Bun.$` Shell API 执行命令。
67
+
68
+ ### 基础用法
69
+
70
+ ```ts
71
+ import { tool } from "@opencode-ai/plugin"
72
+
73
+ export default tool({
74
+ description: "Execute shell commands",
75
+ args: {
76
+ cmd: tool.schema.string().describe("Command to execute"),
77
+ },
78
+ async execute(args, context) {
79
+ // 获取文本输出
80
+ const text = await Bun.$`echo ${args.cmd}`.text()
81
+ return text.trim()
82
+ },
83
+ })
84
+ ```
85
+
86
+ ### 常用方法
87
+
88
+ ```ts
89
+ // 文本输出
90
+ const text = await Bun.$`cat file.txt`.text()
91
+
92
+ // JSON 输出
93
+ const data = await Bun.$`echo '{"name":"test"}'`.json()
94
+
95
+ // 二进制数据
96
+ const buffer = await Bun.$`cat image.png`.arrayBuffer()
97
+
98
+ // 只执行不获取输出
99
+ await Bun.$`mkdir -p ${dir}`
100
+
101
+ // 检查退出码
102
+ const { exitCode, stdout } = await Bun.$`some-command`
103
+ if (exitCode !== 0) {
104
+ throw new Error(`Command failed with code ${exitCode}`)
105
+ }
106
+ ```
107
+
108
+ ### 变量插值(自动转义)
109
+
110
+ ```ts
111
+ const file = "test.txt"
112
+ const count = 10
113
+
114
+ // 变量自动转义,防止注入
115
+ await Bun.$`cat ${file}`.text()
116
+ await Bun.$`head -n ${count} ${file}`.text()
117
+
118
+ // 数组自动展开
119
+ const files = ["a.txt", "b.txt", "c.txt"]
120
+ await Bun.$`cat ${files}`.text() // => cat a.txt b.txt c.txt
121
+ ```
122
+
123
+ ### 错误处理
124
+
125
+ ```ts
126
+ // 捕获错误
127
+ try {
128
+ await Bun.$`some-command`.text()
129
+ } catch (error) {
130
+ console.error("Command failed:", error.message)
131
+ }
132
+
133
+ // 忽略错误
134
+ const result = await Bun.$`some-command`.quiet()
135
+ ```
136
+
137
+ ### 管道操作
138
+
139
+ ```ts
140
+ // 管道
141
+ const result = await Bun.$`cat file.txt | grep pattern`.text()
142
+
143
+ // 流式处理
144
+ const proc = Bun.$`cat large-file.txt`
145
+ const reader = proc.stdout.getReader()
146
+ ```
147
+
148
+ ### 实用示例
149
+
150
+ ```ts
151
+ // Git 操作
152
+ export default tool({
153
+ description: "Get git status",
154
+ args: {},
155
+ async execute() {
156
+ const status = await Bun.$`git status --short`.text()
157
+ return status || "Working tree clean"
158
+ },
159
+ })
160
+
161
+ // 文件操作
162
+ export default tool({
163
+ description: "Count lines in file",
164
+ args: {
165
+ file: tool.schema.string().describe("File path"),
166
+ },
167
+ async execute(args) {
168
+ const count = await Bun.$`wc -l ${args.file}`.text()
169
+ return `Total lines: ${count.trim()}`
170
+ },
171
+ })
172
+
173
+ // 系统信息
174
+ export default tool({
175
+ description: "Get system info",
176
+ args: {},
177
+ async execute() {
178
+ const [os, arch, cores] = await Promise.all([
179
+ Bun.$`uname -s`.text(),
180
+ Bun.$`uname -m`.text(),
181
+ Bun.$`nproc`.text(),
182
+ ])
183
+ return { os: os.trim(), arch: arch.trim(), cores: cores.trim() }
184
+ },
185
+ })
186
+
187
+ // 多行脚本
188
+ export default tool({
189
+ description: "Initialize project structure",
190
+ args: {
191
+ name: tool.schema.string().describe("Project name"),
192
+ },
193
+ async execute(args) {
194
+ const result = await Bun.$`
195
+ mkdir -p ${args.name}/src/components
196
+ mkdir -p ${args.name}/tests
197
+ cd ${args.name}
198
+ touch src/index.ts
199
+ touch tests/index.test.ts
200
+ echo "Project ${args.name} initialized"
201
+ `.text()
202
+ return result.trim()
203
+ },
204
+ })
205
+ ```
206
+
207
+ ### 传递复杂对象给脚本
208
+
209
+ 命令行参数本质是字符串数组,无法直接传递复杂对象。推荐通过 **stdin** 或 **临时文件** 传递 JSON。
210
+
211
+ #### 方案1:stdin(推荐)
212
+
213
+ Tool 通过 stdin 传递 JSON,Python 脚本从 stdin 读取,双方解耦。
214
+
215
+ ```ts
216
+ // Tool 定义
217
+ export default tool({
218
+ description: "Run Python with config",
219
+ args: {
220
+ config: tool.schema.object({
221
+ name: tool.schema.string(),
222
+ count: tool.schema.number(),
223
+ options: tool.schema.object({
224
+ debug: tool.schema.boolean(),
225
+ }),
226
+ }).describe("Configuration object"),
227
+ },
228
+ async execute(args) {
229
+ const proc = Bun.$`python3 script.py`
230
+ proc.stdin.write(JSON.stringify(args.config))
231
+ const result = await proc.text()
232
+ return result.trim()
233
+ },
234
+ })
235
+ ```
236
+
237
+ ```py
238
+ # script.py
239
+ import sys
240
+ import json
241
+
242
+ # 从 stdin 读取 JSON
243
+ config = json.load(sys.stdin)
244
+
245
+ print(f"Name: {config['name']}")
246
+ print(f"Count: {config['count']}")
247
+ print(f"Debug: {config['options']['debug']}")
248
+ ```
249
+
250
+ #### 方案2:临时文件
251
+
252
+ Tool 写入临时文件,Python 从文件读取,适合大对象或需要持久化的场景。
253
+
254
+ ```ts
255
+ export default tool({
256
+ description: "Run Python with config file",
257
+ args: {
258
+ config: tool.schema.object({
259
+ name: tool.schema.string(),
260
+ count: tool.schema.number(),
261
+ }).describe("Configuration object"),
262
+ },
263
+ async execute(args) {
264
+ // 写入临时文件
265
+ const tmpFile = `/tmp/config-${Date.now()}.json`
266
+ await Bun.write(tmpFile, JSON.stringify(args.config))
267
+
268
+ // 执行 Python 脚本
269
+ const result = await Bun.$`python3 script.py --config ${tmpFile}`.text()
270
+
271
+ // 清理临时文件
272
+ await Bun.$`rm ${tmpFile}`
273
+
274
+ return result.trim()
275
+ },
276
+ })
277
+ ```
278
+
279
+ ```py
280
+ # script.py
281
+ import sys
282
+ import json
283
+ import argparse
284
+
285
+ parser = argparse.ArgumentParser()
286
+ parser.add_argument("--config", help="Config file path")
287
+ args = parser.parse_args()
288
+
289
+ # 从文件读取 JSON
290
+ with open(args.config, 'r') as f:
291
+ config = json.load(f)
292
+
293
+ print(f"Name: {config['name']}")
294
+ print(f"Count: {config['count']}")
295
+ ```
296
+
297
+ #### 方案对比
298
+
299
+ | 方案 | 优点 | 缺点 | 适用场景 |
300
+ |------|------|------|---------|
301
+ | **stdin** | 解耦、简单、无需清理 | 无法持久化 | 小对象、一次性传递 |
302
+ | **临时文件** | 可持久化、支持大对象 | 需要清理、文件管理 | 大对象、需要复用 |
303
+
304
+ **推荐**:优先使用 stdin,简单且解耦。
305
+ ```
@@ -1,18 +1,18 @@
1
- # Context in a tool
2
- Tools receive context about the current session:
3
- ```ts
4
- // .opencode/tools/project.ts
5
- import { tool } from "@opencode-ai/plugin"
6
-
7
- export default tool({
8
- description: "Get project information",
9
- args: {},
10
- async execute(args, context) {
11
- // Access context information
12
- const { agent, sessionID, messageID, directory, worktree } = context
13
- return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
14
- },
15
- })
16
- ```
17
- - Use context.directory for the session working directory.
1
+ # Context in a tool
2
+ Tools receive context about the current session:
3
+ ```ts
4
+ // .opencode/tools/project.ts
5
+ import { tool } from "@opencode-ai/plugin"
6
+
7
+ export default tool({
8
+ description: "Get project information",
9
+ args: {},
10
+ async execute(args, context) {
11
+ // Access context information
12
+ const { agent, sessionID, messageID, directory, worktree } = context
13
+ return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
14
+ },
15
+ })
16
+ ```
17
+ - Use context.directory for the session working directory.
18
18
  - Use context.worktree for the git worktree root.