@wwkit/harness 1.0.33 → 1.0.34

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 (202) 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-skill/SKILL.md +192 -124
  20. package/skills/blame-skill/SKILL.md +148 -201
  21. package/skills/blame-skill/references/scoring-rules.md +190 -0
  22. package/skills/blame-sprint-agent/SKILL.md +167 -0
  23. package/skills/blame-sprint-agent/references/scoring-rules.md +113 -0
  24. package/skills/blame-sprint-skill/SKILL.md +154 -0
  25. package/skills/blame-sprint-skill/references/scoring-rules.md +101 -0
  26. package/skills/extract/SKILL.md +119 -119
  27. package/skills/extract/references/build-xpath.js +30 -30
  28. package/skills/extract/references/clean-html.js +90 -90
  29. package/skills/extract/references/detail.md +54 -54
  30. package/skills/extract/references/detail.schema.json5 +48 -48
  31. package/skills/extract/references/extract-detail.js +71 -71
  32. package/skills/extract/references/extract-regex.js +81 -81
  33. package/skills/extract/references/extract-sample.js +48 -48
  34. package/skills/extract/references/list-rule-gen.md +109 -109
  35. package/skills/extract/references/list.md +134 -134
  36. package/skills/extract/references/list.schema.json5 +33 -33
  37. package/skills/extract/references/navi.md +44 -44
  38. package/skills/extract/references/navi.schema.json5 +23 -23
  39. package/skills/extract/references/text.md +40 -40
  40. package/skills/extract/references/text.schema.json5 +21 -21
  41. package/skills/extract/references/to-text.js +20 -20
  42. package/skills/extract/references/validate-schema.js +39 -39
  43. package/skills/jstest/SKILL.md +561 -561
  44. package/skills/jstest/references/case-create.md +327 -327
  45. package/skills/jstest/references/case-fix.md +273 -273
  46. package/skills/jstest/references/config.md +148 -148
  47. package/skills/jstest/references/coverage-analyze.md +247 -247
  48. package/skills/jstest/references/env-ensure.md +210 -210
  49. package/skills/jstest/references/execute.md +168 -168
  50. package/skills/jstest/references/sample.md +166 -166
  51. package/skills/jstest/references/scoring-rules.md +75 -75
  52. package/skills/jstest/references/src/jstest-sample/Calculator.js +80 -80
  53. package/skills/jstest/references/src/jstest-sample/ConfigManager.js +72 -72
  54. package/skills/jstest/references/src/jstest-sample/FileProcessor.js +57 -57
  55. package/skills/jstest/references/src/jstest-sample/OrderService.js +98 -98
  56. package/skills/jstest/references/src/jstest-sample/TokenGenerator.js +60 -60
  57. package/skills/jstest/references/src/jstest-sample/UserService.js +56 -56
  58. package/skills/jstest/references/src/jstest-sample/index.js +6 -6
  59. package/skills/jstest/references/suitability-check.md +232 -232
  60. package/skills/jstest/references/test-standards.md +288 -288
  61. package/skills/lint/SKILL.md +440 -440
  62. package/skills/lint/references/ai-fix.md +137 -137
  63. package/skills/lint/references/config-setup.md +102 -102
  64. package/skills/lint/references/config.md +65 -65
  65. package/skills/lint/references/detect-language.js +65 -65
  66. package/skills/lint/references/env-ensure.md +88 -88
  67. package/skills/lint/references/language-detect.md +75 -75
  68. package/skills/lint/references/languages/js.md +110 -110
  69. package/skills/lint/references/languages/py.md +89 -89
  70. package/skills/lint/references/rules-analyze.md +125 -125
  71. package/skills/lint/references/suitability-check.md +81 -81
  72. package/skills/lint/references/tool-fix.md +90 -90
  73. package/skills/new-skill/SKILL.md +193 -227
  74. package/skills/pytest/SKILL.md +555 -555
  75. package/skills/pytest/references/case-create.md +324 -324
  76. package/skills/pytest/references/case-fix.md +271 -271
  77. package/skills/pytest/references/config.md +145 -145
  78. package/skills/pytest/references/coverage-analyze.md +223 -223
  79. package/skills/pytest/references/env-ensure.md +195 -195
  80. package/skills/pytest/references/execute.md +152 -152
  81. package/skills/pytest/references/sample.md +159 -159
  82. package/skills/pytest/references/scoring-rules.md +57 -57
  83. package/skills/pytest/references/src/pytest-sample/Calculator.py +67 -67
  84. package/skills/pytest/references/src/pytest-sample/ConfigManager.py +68 -68
  85. package/skills/pytest/references/src/pytest-sample/FileProcessor.py +53 -53
  86. package/skills/pytest/references/src/pytest-sample/OrderService.py +82 -82
  87. package/skills/pytest/references/src/pytest-sample/TokenGenerator.py +50 -50
  88. package/skills/pytest/references/src/pytest-sample/UserService.py +45 -45
  89. package/skills/pytest/references/suitability-check.md +220 -220
  90. package/skills/pytest/references/test-standards.md +244 -244
  91. package/skills/query/SKILL.md +71 -71
  92. package/skills/read-docs/SKILL.md +134 -134
  93. package/skills/read-docs/references/opencode/agents/cases.md +206 -206
  94. package/skills/read-docs/references/opencode/agents/design-pattern.md +47 -47
  95. package/skills/read-docs/references/opencode/agents/detail.md +191 -191
  96. package/skills/read-docs/references/opencode/agents/examples.md +100 -100
  97. package/skills/read-docs/references/opencode/agents/index.md +309 -309
  98. package/skills/read-docs/references/opencode/agents/parallel-dispatch.md +149 -149
  99. package/skills/read-docs/references/opencode/agents/subagent-internals.md +217 -217
  100. package/skills/read-docs/references/opencode/agents/workflow.md +161 -161
  101. package/skills/read-docs/references/opencode/cli/commands/acp.md +31 -31
  102. package/skills/read-docs/references/opencode/cli/commands/agent.md +16 -16
  103. package/skills/read-docs/references/opencode/cli/commands/attach.md +20 -20
  104. package/skills/read-docs/references/opencode/cli/commands/mcp.md +37 -37
  105. package/skills/read-docs/references/opencode/cli/commands/others.md +48 -48
  106. package/skills/read-docs/references/opencode/cli/commands/plugin.md +12 -12
  107. package/skills/read-docs/references/opencode/cli/commands/provider.md +43 -43
  108. package/skills/read-docs/references/opencode/cli/commands/run.md +81 -81
  109. package/skills/read-docs/references/opencode/cli/commands/serve.md +84 -84
  110. package/skills/read-docs/references/opencode/cli/commands/session.md +37 -37
  111. package/skills/read-docs/references/opencode/cli/commands/web.md +14 -14
  112. package/skills/read-docs/references/opencode/cli/env.md +38 -38
  113. package/skills/read-docs/references/opencode/cli/index.md +18 -18
  114. package/skills/read-docs/references/opencode/cli/tui.md +35 -35
  115. package/skills/read-docs/references/opencode/commands/examples.md +42 -42
  116. package/skills/read-docs/references/opencode/commands/index.md +185 -185
  117. package/skills/read-docs/references/opencode/config/provider.md +151 -151
  118. package/skills/read-docs/references/opencode/formatter/index.md +70 -70
  119. package/skills/read-docs/references/opencode/guide/config.md +419 -419
  120. package/skills/read-docs/references/opencode/guide/formatters.md +70 -70
  121. package/skills/read-docs/references/opencode/guide/index.md +37 -37
  122. package/skills/read-docs/references/opencode/guide/providers.md +31 -31
  123. package/skills/read-docs/references/opencode/guide/rules.md +63 -63
  124. package/skills/read-docs/references/opencode/plugins/examples.md +75 -75
  125. package/skills/read-docs/references/opencode/plugins/index.md +188 -188
  126. package/skills/read-docs/references/opencode/reference/index.md +118 -118
  127. package/skills/read-docs/references/opencode/rule/index.md +77 -77
  128. package/skills/read-docs/references/opencode/skills/detail.md +113 -113
  129. package/skills/read-docs/references/opencode/skills/examples.md +141 -141
  130. package/skills/read-docs/references/opencode/skills/index.md +126 -126
  131. package/skills/read-docs/references/opencode/skills/workflow.md +146 -146
  132. package/skills/read-docs/references/opencode/tests/agent.md +9 -9
  133. package/skills/read-docs/references/opencode/tests/config.md +59 -59
  134. package/skills/read-docs/references/opencode/tests/file.md +11 -11
  135. package/skills/read-docs/references/opencode/tests/serve.md +17 -17
  136. package/skills/read-docs/references/opencode/tests/session.md +30 -30
  137. package/skills/read-docs/references/opencode/tests/web.md +16 -16
  138. package/skills/read-docs/references/opencode/tools/arguments.md +305 -305
  139. package/skills/read-docs/references/opencode/tools/context.md +17 -17
  140. package/skills/read-docs/references/opencode/tools/custom.md +110 -110
  141. package/skills/read-docs/references/opencode/tools/detail.md +104 -104
  142. package/skills/read-docs/references/opencode/tools/examples.md +71 -71
  143. package/skills/read-docs/references/opencode/tools/index.md +56 -56
  144. package/skills/read-docs/references/opencode/tools/lsp.md +25 -25
  145. package/skills/read-docs/references/opencode/tools/mcp.md +132 -132
  146. package/skills/read-docs/references/opencode/train/README.md +134 -134
  147. package/skills/read-docs/references/opencode/train/agent-basic.md +771 -771
  148. package/skills/read-docs/references/opencode/train/command-basic.md +667 -667
  149. package/skills/read-docs/references/opencode/train/config-basic.md +508 -508
  150. package/skills/read-docs/references/opencode/train/index.md +163 -163
  151. package/skills/read-docs/references/opencode/train/practice.md +872 -872
  152. package/skills/read-docs/references/opencode/train/skill-basic.md +607 -607
  153. package/skills/read-docs/references/opencode/tui/commands/config.md +31 -31
  154. package/skills/read-docs/references/opencode/tui/commands/editor.md +46 -46
  155. package/skills/read-docs/references/opencode/tui/commands/index.md +124 -124
  156. package/skills/read-docs/references/opencode/tui/commands/init.md +4 -4
  157. package/skills/read-docs/references/opencode/tui/index.md +26 -26
  158. package/skills/read-docs/references/superpowers/bootstrap.md +107 -107
  159. package/skills/read-docs/references/superpowers/comparison.md +77 -77
  160. package/skills/read-docs/references/superpowers/context.md +71 -71
  161. package/skills/read-docs/references/superpowers/index.md +58 -58
  162. package/skills/read-docs/references/superpowers/parallel.md +49 -49
  163. package/skills/read-docs/references/superpowers/sdd.md +183 -183
  164. package/skills/read-docs/references/superpowers/skills.md +68 -68
  165. package/skills/read-docs/references/superpowers/workflow.md +83 -83
  166. package/skills/revise/SKILL.md +121 -121
  167. package/skills/revise/references/article.md +36 -36
  168. package/skills/revise/references/article.schema.json5 +40 -40
  169. package/skills/revise/references/gallery.md +34 -34
  170. package/skills/revise/references/gallery.schema.json5 +40 -40
  171. package/skills/revise/references/question.md +32 -32
  172. package/skills/revise/references/question.schema.json5 +31 -31
  173. package/skills/revise/references/status.md +31 -31
  174. package/skills/revise/references/status.schema.json5 +27 -27
  175. package/skills/revise/references/validate-schema.js +39 -39
  176. package/skills/step-extract/SKILL.md +205 -0
  177. package/skills/tsl/SKILL.md +393 -0
  178. package/skills/tsl/package.json +10 -0
  179. package/skills/tsl/references/global-review-rules.md +25 -0
  180. package/skills/tsl/references/prepare-prompt.md +150 -0
  181. package/skills/tsl/references/terminology-rules.md +93 -0
  182. package/skills/tsl/references/translation-guide.md +23 -0
  183. package/skills/tsl/references/translation-prompt.md +115 -0
  184. package/skills/tsl/references/translation-rules.md +48 -0
  185. package/skills/tsl/references/verification-rules.md +58 -0
  186. package/skills/tsl/scripts/check-boundaries.js +140 -0
  187. package/skills/tsl/scripts/chunk-text.js +144 -0
  188. package/skills/tsl/scripts/clean-text.js +121 -0
  189. package/skills/tsl/scripts/cleanup-glossary.js +160 -0
  190. package/skills/tsl/scripts/extract-pdf.js +94 -0
  191. package/skills/tsl/scripts/generate-review.js +179 -0
  192. package/skills/tsl/scripts/merge-result.js +164 -0
  193. package/skills/tsl/scripts/verify-chunk.js +170 -0
  194. package/skills/tsl-format/SKILL.md +298 -0
  195. package/skills/tsl-format/references/format-prompt.md +103 -0
  196. package/skills/tsl-format/references/format-rules.md +58 -0
  197. package/skills/tsl-format/scripts/chunk-bilingual.js +96 -0
  198. package/skills/tsl-format/scripts/merge-format.js +105 -0
  199. package/src/config.js +29 -29
  200. package/src/config.json5 +19 -19
  201. package/skills/new-skill/references/template.md +0 -53
  202. package/skills/new-skill/references/workflow-patterns.md +0 -104
@@ -1,111 +1,111 @@
1
- # 自定义工具
2
-
3
- 自定义工具是你创建的函数,LLM 可以在对话中调用。
4
-
5
- ## 创建工具
6
-
7
- 工具定义为 TypeScript 或 JavaScript 文件,但工具定义可以调用任何语言编写的脚本。
8
-
9
- ## 位置
10
-
11
- - 本地:`.opencode/tools/` 目录
12
- - 全局:`~/.config/opencode/tools/` 目录
13
-
14
- ## 使用 tool() 辅助函数
15
-
16
- ```js
17
- import { tool } from "@opencode-ai/plugin"
18
-
19
- export default tool({
20
- description: "Query the project database",
21
- args: {
22
- query: tool.schema.string().describe("SQL query to execute"),
23
- },
24
- async execute(args) {
25
- return `Executed query: ${args.query}`
26
- },
27
- })
28
- ```
29
-
30
- 文件名即为工具名。
31
-
32
- ## 单文件多工具
33
-
34
- 每个导出成为独立工具,命名为 `<filename>_<exportname>`:
35
-
36
- ```js
37
- import { tool } from "@opencode-ai/plugin"
38
-
39
- export const add = tool({
40
- description: "Add two numbers",
41
- args: {
42
- a: tool.schema.number().describe("First number"),
43
- b: tool.schema.number().describe("Second number"),
44
- },
45
- async execute(args) {
46
- return (args.a + args.b).toString()
47
- },
48
- })
49
-
50
- export const multiply = tool({
51
- description: "Multiply two numbers",
52
- args: {
53
- a: tool.schema.number().describe("First number"),
54
- b: tool.schema.number().describe("Second number"),
55
- },
56
- async execute(args) {
57
- return (args.a * args.b).toString()
58
- },
59
- })
60
- ```
61
-
62
- 创建两个工具:`math_add` 和 `math_multiply`。
63
-
64
- ### 参数类型
65
-
66
- 使用 `tool.schema`(基于 Zod)定义参数类型:
67
-
68
- ```js
69
- args: {
70
- query: tool.schema.string().describe("SQL query to execute")
71
- }
72
- ```
73
-
74
- 也可以直接导入 Zod:
75
-
76
- ```js
77
- import { z } from "zod"
78
-
79
- export default {
80
- description: "Tool description",
81
- args: {
82
- param: z.string().describe("Parameter description"),
83
- },
84
- async execute(args, context) {
85
- return "result"
86
- },
87
- }
88
- ```
89
-
90
- ### 上下文信息
91
-
92
- 工具接收当前会话的上下文:
93
-
94
- ```js
95
- import { tool } from "@opencode-ai/plugin"
96
-
97
- export default tool({
98
- description: "Get project information",
99
- args: {},
100
- async execute(args, context) {
101
- const { agent, sessionID, messageID, directory, worktree } = context
102
- return `Agent: ${agent}, Session: ${sessionID}, Directory: ${directory}`
103
- },
104
- })
105
- ```
106
-
107
- ## 更多内容
108
-
109
- - [工具详解](/tools/detail) — 常用类型、取消处理、依赖管理、输出限制
110
- - [工具示例](/tools/examples) — HTTP API 封装等示例
1
+ # 自定义工具
2
+
3
+ 自定义工具是你创建的函数,LLM 可以在对话中调用。
4
+
5
+ ## 创建工具
6
+
7
+ 工具定义为 TypeScript 或 JavaScript 文件,但工具定义可以调用任何语言编写的脚本。
8
+
9
+ ## 位置
10
+
11
+ - 本地:`.opencode/tools/` 目录
12
+ - 全局:`~/.config/opencode/tools/` 目录
13
+
14
+ ## 使用 tool() 辅助函数
15
+
16
+ ```js
17
+ import { tool } from "@opencode-ai/plugin"
18
+
19
+ export default tool({
20
+ description: "Query the project database",
21
+ args: {
22
+ query: tool.schema.string().describe("SQL query to execute"),
23
+ },
24
+ async execute(args) {
25
+ return `Executed query: ${args.query}`
26
+ },
27
+ })
28
+ ```
29
+
30
+ 文件名即为工具名。
31
+
32
+ ## 单文件多工具
33
+
34
+ 每个导出成为独立工具,命名为 `<filename>_<exportname>`:
35
+
36
+ ```js
37
+ import { tool } from "@opencode-ai/plugin"
38
+
39
+ export const add = tool({
40
+ description: "Add two numbers",
41
+ args: {
42
+ a: tool.schema.number().describe("First number"),
43
+ b: tool.schema.number().describe("Second number"),
44
+ },
45
+ async execute(args) {
46
+ return (args.a + args.b).toString()
47
+ },
48
+ })
49
+
50
+ export const multiply = tool({
51
+ description: "Multiply two numbers",
52
+ args: {
53
+ a: tool.schema.number().describe("First number"),
54
+ b: tool.schema.number().describe("Second number"),
55
+ },
56
+ async execute(args) {
57
+ return (args.a * args.b).toString()
58
+ },
59
+ })
60
+ ```
61
+
62
+ 创建两个工具:`math_add` 和 `math_multiply`。
63
+
64
+ ### 参数类型
65
+
66
+ 使用 `tool.schema`(基于 Zod)定义参数类型:
67
+
68
+ ```js
69
+ args: {
70
+ query: tool.schema.string().describe("SQL query to execute")
71
+ }
72
+ ```
73
+
74
+ 也可以直接导入 Zod:
75
+
76
+ ```js
77
+ import { z } from "zod"
78
+
79
+ export default {
80
+ description: "Tool description",
81
+ args: {
82
+ param: z.string().describe("Parameter description"),
83
+ },
84
+ async execute(args, context) {
85
+ return "result"
86
+ },
87
+ }
88
+ ```
89
+
90
+ ### 上下文信息
91
+
92
+ 工具接收当前会话的上下文:
93
+
94
+ ```js
95
+ import { tool } from "@opencode-ai/plugin"
96
+
97
+ export default tool({
98
+ description: "Get project information",
99
+ args: {},
100
+ async execute(args, context) {
101
+ const { agent, sessionID, messageID, directory, worktree } = context
102
+ return `Agent: ${agent}, Session: ${sessionID}, Directory: ${directory}`
103
+ },
104
+ })
105
+ ```
106
+
107
+ ## 更多内容
108
+
109
+ - [工具详解](/tools/detail) — 常用类型、取消处理、依赖管理、输出限制
110
+ - [工具示例](/tools/examples) — HTTP API 封装等示例
111
111
  - [MCP 服务器](/tools/mcp) — 通过 MCP 添加外部工具
@@ -1,104 +1,104 @@
1
- # 工具详解
2
-
3
- ## 常用参数类型
4
-
5
- ```js
6
- import { tool } from "@opencode-ai/plugin"
7
- export default tool({
8
- description: "Demo of parameter types",
9
- args: {
10
- // 字符串
11
- name: tool.schema.string().describe("User name"),
12
- // 可选参数
13
- email: tool.schema.string().email().optional().describe("Optional email"),
14
- // 带默认值
15
- limit: tool.schema.number().default(10).describe("Max results"),
16
- // 枚举
17
- status: tool.schema.enum(["pending", "done"]).describe("Task status"),
18
- // 布尔
19
- verbose: tool.schema.boolean().describe("Enable verbose output"),
20
- // 数组
21
- tags: tool.schema.array(tool.schema.string()).describe("List of tags"),
22
- // 对象
23
- config: tool.schema.object({
24
- host: tool.schema.string(),
25
- port: tool.schema.number(),
26
- }).describe("Server config"),
27
- },
28
- async execute(args) {
29
- return JSON.stringify(args, null, 2)
30
- },
31
- })
32
- ```
33
-
34
- ## 取消操作
35
-
36
- 当用户取消操作(如按 Ctrl+C)时,abort 信号会被触发。长时间运行的工具应监听此信号:
37
-
38
- ```js
39
- // .opencode/tool/long-task.ts
40
- import { tool } from "@opencode-ai/plugin"
41
- export default tool({
42
- description: "A long-running task",
43
- args: {},
44
- async execute(args, context) {
45
- // 检查是否已取消
46
- if (context.abort.aborted) {
47
- return "Task cancelled"
48
- }
49
- // 传递给支持 AbortSignal 的 API
50
- const response = await fetch("https://api.example.com/data", {
51
- signal: context.abort,
52
- })
53
- return await response.text()
54
- },
55
- })
56
- ```
57
-
58
- ## 依赖管理
59
-
60
- 自定义工具可以使用外部 npm 包,在配置目录添加 `package.json` 声明依赖:
61
-
62
- ```json
63
- {
64
- "dependencies": {
65
- "node-fetch": "^3.0.0",
66
- "cheerio": "^1.0.0"
67
- }
68
- }
69
- ```
70
-
71
- OpenCode 启动时自动运行 `bun install` 安装依赖:
72
-
73
- ```js
74
- // .opencode/tool/scraper.ts
75
- import { tool } from "@opencode-ai/plugin"
76
- import * as cheerio from "cheerio"
77
- export default tool({
78
- description: "Extract text from a webpage",
79
- args: {
80
- url: tool.schema.string().url().describe("URL to scrape"),
81
- },
82
- async execute(args, context) {
83
- const response = await fetch(args.url, { signal: context.abort })
84
- const html = await response.text()
85
- const $ = cheerio.load(html)
86
- return $("body").text().trim()
87
- },
88
- })
89
- ```
90
-
91
- ## 输出限制
92
-
93
- 工具返回值会被自动截断以避免上下文溢出:
94
-
95
- | 限制 | 值 |
96
- |------|-----|
97
- | 最大行数 | 2000 行 |
98
- | 最大字节 | 50 KB |
99
-
100
- 超出限制时,OpenCode 会在末尾添加 `...N lines truncated...` 提示。
101
-
102
- ## 查看工具列表
103
-
104
- 启动 OpenCode 后,使用 `/tools` 命令查看所有可用工具列表,确认自定义工具出现在列表中。
1
+ # 工具详解
2
+
3
+ ## 常用参数类型
4
+
5
+ ```js
6
+ import { tool } from "@opencode-ai/plugin"
7
+ export default tool({
8
+ description: "Demo of parameter types",
9
+ args: {
10
+ // 字符串
11
+ name: tool.schema.string().describe("User name"),
12
+ // 可选参数
13
+ email: tool.schema.string().email().optional().describe("Optional email"),
14
+ // 带默认值
15
+ limit: tool.schema.number().default(10).describe("Max results"),
16
+ // 枚举
17
+ status: tool.schema.enum(["pending", "done"]).describe("Task status"),
18
+ // 布尔
19
+ verbose: tool.schema.boolean().describe("Enable verbose output"),
20
+ // 数组
21
+ tags: tool.schema.array(tool.schema.string()).describe("List of tags"),
22
+ // 对象
23
+ config: tool.schema.object({
24
+ host: tool.schema.string(),
25
+ port: tool.schema.number(),
26
+ }).describe("Server config"),
27
+ },
28
+ async execute(args) {
29
+ return JSON.stringify(args, null, 2)
30
+ },
31
+ })
32
+ ```
33
+
34
+ ## 取消操作
35
+
36
+ 当用户取消操作(如按 Ctrl+C)时,abort 信号会被触发。长时间运行的工具应监听此信号:
37
+
38
+ ```js
39
+ // .opencode/tool/long-task.ts
40
+ import { tool } from "@opencode-ai/plugin"
41
+ export default tool({
42
+ description: "A long-running task",
43
+ args: {},
44
+ async execute(args, context) {
45
+ // 检查是否已取消
46
+ if (context.abort.aborted) {
47
+ return "Task cancelled"
48
+ }
49
+ // 传递给支持 AbortSignal 的 API
50
+ const response = await fetch("https://api.example.com/data", {
51
+ signal: context.abort,
52
+ })
53
+ return await response.text()
54
+ },
55
+ })
56
+ ```
57
+
58
+ ## 依赖管理
59
+
60
+ 自定义工具可以使用外部 npm 包,在配置目录添加 `package.json` 声明依赖:
61
+
62
+ ```json
63
+ {
64
+ "dependencies": {
65
+ "node-fetch": "^3.0.0",
66
+ "cheerio": "^1.0.0"
67
+ }
68
+ }
69
+ ```
70
+
71
+ OpenCode 启动时自动运行 `bun install` 安装依赖:
72
+
73
+ ```js
74
+ // .opencode/tool/scraper.ts
75
+ import { tool } from "@opencode-ai/plugin"
76
+ import * as cheerio from "cheerio"
77
+ export default tool({
78
+ description: "Extract text from a webpage",
79
+ args: {
80
+ url: tool.schema.string().url().describe("URL to scrape"),
81
+ },
82
+ async execute(args, context) {
83
+ const response = await fetch(args.url, { signal: context.abort })
84
+ const html = await response.text()
85
+ const $ = cheerio.load(html)
86
+ return $("body").text().trim()
87
+ },
88
+ })
89
+ ```
90
+
91
+ ## 输出限制
92
+
93
+ 工具返回值会被自动截断以避免上下文溢出:
94
+
95
+ | 限制 | 值 |
96
+ |------|-----|
97
+ | 最大行数 | 2000 行 |
98
+ | 最大字节 | 50 KB |
99
+
100
+ 超出限制时,OpenCode 会在末尾添加 `...N lines truncated...` 提示。
101
+
102
+ ## 查看工具列表
103
+
104
+ 启动 OpenCode 后,使用 `/tools` 命令查看所有可用工具列表,确认自定义工具出现在列表中。
@@ -1,71 +1,71 @@
1
- # 工具示例
2
-
3
- ## HTTP API 封装
4
-
5
- 封装 JIRA API 调用:
6
-
7
- ```js
8
- // .opencode/tool/jira.ts
9
- import { tool } from "@opencode-ai/plugin"
10
- export const getIssue = tool({
11
- description: "Get JIRA issue details by key",
12
- args: {
13
- key: tool.schema.string().describe("Issue key, e.g. PROJ-123"),
14
- },
15
- async execute(args, context) {
16
- const response = await fetch(
17
- `https://your-company.atlassian.net/rest/api/3/issue/${args.key}`,
18
- {
19
- headers: {
20
- Authorization: `Basic ${btoa("email@example.com:API_TOKEN")}`,
21
- Accept: "application/json",
22
- },
23
- signal: context.abort,
24
- }
25
- )
26
- if (!response.ok) {
27
- throw new Error(`Failed to fetch issue: ${response.status}`)
28
- }
29
- const issue = await response.json()
30
- return JSON.stringify(issue, null, 2)
31
- },
32
- })
33
- ```
34
-
35
- ::: warning
36
- 生产环境中,API Token 应从环境变量读取而非硬编码。
37
- :::
38
-
39
- ## Python 脚本集成
40
-
41
- 先用 Python 编写脚本:
42
-
43
- ```py
44
- import sys
45
-
46
- a = int(sys.argv[1])
47
- b = int(sys.argv[2])
48
- print(a + b)
49
- ```
50
-
51
- 然后创建工具定义调用它:
52
-
53
- ```js
54
- import { tool } from "@opencode-ai/plugin"
55
- import path from "path"
56
-
57
- export default tool({
58
- description: "Add two numbers using Python",
59
- args: {
60
- a: tool.schema.number().describe("First number"),
61
- b: tool.schema.number().describe("Second number"),
62
- },
63
- async execute(args, context) {
64
- const script = path.join(context.worktree, ".opencode/tools/add.py")
65
- const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
66
- return result.trim()
67
- },
68
- })
69
- ```
70
-
71
- 这里使用 `Bun.$` 工具运行 Python 脚本。
1
+ # 工具示例
2
+
3
+ ## HTTP API 封装
4
+
5
+ 封装 JIRA API 调用:
6
+
7
+ ```js
8
+ // .opencode/tool/jira.ts
9
+ import { tool } from "@opencode-ai/plugin"
10
+ export const getIssue = tool({
11
+ description: "Get JIRA issue details by key",
12
+ args: {
13
+ key: tool.schema.string().describe("Issue key, e.g. PROJ-123"),
14
+ },
15
+ async execute(args, context) {
16
+ const response = await fetch(
17
+ `https://your-company.atlassian.net/rest/api/3/issue/${args.key}`,
18
+ {
19
+ headers: {
20
+ Authorization: `Basic ${btoa("email@example.com:API_TOKEN")}`,
21
+ Accept: "application/json",
22
+ },
23
+ signal: context.abort,
24
+ }
25
+ )
26
+ if (!response.ok) {
27
+ throw new Error(`Failed to fetch issue: ${response.status}`)
28
+ }
29
+ const issue = await response.json()
30
+ return JSON.stringify(issue, null, 2)
31
+ },
32
+ })
33
+ ```
34
+
35
+ ::: warning
36
+ 生产环境中,API Token 应从环境变量读取而非硬编码。
37
+ :::
38
+
39
+ ## Python 脚本集成
40
+
41
+ 先用 Python 编写脚本:
42
+
43
+ ```py
44
+ import sys
45
+
46
+ a = int(sys.argv[1])
47
+ b = int(sys.argv[2])
48
+ print(a + b)
49
+ ```
50
+
51
+ 然后创建工具定义调用它:
52
+
53
+ ```js
54
+ import { tool } from "@opencode-ai/plugin"
55
+ import path from "path"
56
+
57
+ export default tool({
58
+ description: "Add two numbers using Python",
59
+ args: {
60
+ a: tool.schema.number().describe("First number"),
61
+ b: tool.schema.number().describe("Second number"),
62
+ },
63
+ async execute(args, context) {
64
+ const script = path.join(context.worktree, ".opencode/tools/add.py")
65
+ const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
66
+ return result.trim()
67
+ },
68
+ })
69
+ ```
70
+
71
+ 这里使用 `Bun.$` 工具运行 Python 脚本。