Coding & Refactoringlow risk
opencode-build-tool
Build custom tools and plugins using the OpenCode SDK packages. Use when creating custom tools, implementing tool schemas, building plugins, managing OpenCode sessions, using opencode-ai/sdk or opencode-ai/plugin, creating a tool.ts file, writing an execute handler, using tool.schema, ToolContext, createOpencode, plugin hooks, custom auth, automating OpenCode from a script, building a CI tool, or integrating external APIs. Keywords: Zod schema, execute handler, event streaming, session management, abort signal, tool discovery, multiple tools per file, tool factory.
pantheon-org/tekhne·skills/agentic-harness/opencode-toolkit/build-tool/SKILL.md
37/ 100推薦值
匯入這個 Skill
選擇你的 coding agent,複製專案級或個人級安裝指令。
匯入目前專案.agents/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a codex -y匯入個人環境~/.agents/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a codex -g -y匯入目前專案.claude/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a claude-code -y匯入個人環境~/.claude/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a claude-code -g -y匯入目前專案.agents/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a github-copilot -y匯入個人環境~/.copilot/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a github-copilot -g -y匯入目前專案.agents/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a cursor -y匯入個人環境~/.cursor/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a cursor -g -y匯入目前專案.agents/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a gemini-cli -y匯入個人環境~/.gemini/skills/build-tool
npx skills add https://github.com/pantheon-org/tekhne/tree/4a79b500f771a61b6b4bf63751e038649d6535bc/skills/agentic-harness/opencode-toolkit/build-tool -a gemini-cli -g -yNative Gemini CLI
gemini skills install https://github.com/pantheon-org/tekhne.git --scope workspace --path skills/agentic-harness/opencode-toolkit/build-tool⚠ 安裝指令使用開源 skills CLI。執行前請檢查來源、腳本與權限。
Skill 指令
在 GitHub 查看原始檔案 ↗# OpenCode SDK Development
## Quick Start: Custom Tool in 3 Steps
1. Create `.opencode/tool/my-tool.ts`:
```typescript
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Brief description of what the tool does",
args: {
query: tool.schema.string().describe("Search query")
},
async execute({ query }, { abort }) {
return `Result for: ${query}`
}
})
```
1. Restart OpenCode — tools are auto-discovered.
1. Filename = tool name: `my-tool.ts` → `my-tool`.
## Mindset
- `@opencode-ai/plugin` — **Tools + Hooks**: Add LLM-callable tools, intercept execution, react to events
- `@opencode-ai/sdk` — **Client API**: Control OpenCode from external scripts, session management
**When to use**: Adding new capabilities the agent doesn't have, automating OpenCode from CI/scripts, or integrating external APIs as first-class tools.
**When NOT to use**: Built-in tool exists (`client.tool.list()`). Need shell (use `$` from Bun). Logic belongs in system prompt (`AGENTS.md`). Need MCP.
> Full decision guide in [advanced-patterns.md](references/advanced-patterns.md).
## Tool File Locations
```bash
.opencode/tool/*.ts # project-specific
~/.config/opencode/tool/*.ts # global
```
Multiple named exports per file → multiple tools: `filename_exportname`
## Anti-Patterns
NEVER return non-string from `execute`. WHY: The tool runtime expects a string response; returning an object throws at runtime.
```typescript
// BAD - throws at runtime
async execute({ query }) { return { results: [] } }
// GOOD - serialize first
async execute({ query }) { return JSON.stringify({ results: [] }) }
```
NEVER `import { tool } from "@opencode-ai/sdk"`. WHY: The SDK package has no `tool()` export. The tool factory is only in `@opencode-ai/plugin`.
```typescript
// BAD - import error at runtime
import { tool } from "@opencode-ai/sdk"
// GOOD
import { tool } from "@opencode-ai/plugin"
```
NEVER ignore the `abort` signal in long-running operations. WHY: Ignoring abort leaves processes dangling after the user cancels, consuming resources.
```typescript
// BAD - dangling fetch after cancel
async execute({ url }) { return await fetch(url).then(r => r.text()) }
// GOOD - propagate abort
async execute({ url }, { abort }) { return await fetch(url, { signal: abort }).then(r => r.text()) }
```
## Reference Files
- [advanced-patterns.md](references/advanced-patterns.md) — patterns, anti-patterns, SDK examples; [sdk-api.md](references/sdk-api.md) — SDK client API; [plugin-api.md](references/plugin-api.md) — plugin hooks
- [basic-tool.ts](references/basic-tool.ts) — simple tool; [full-plugin.ts](references/full-plugin.ts) — complete plugin
Run `bun run opencode run "list tools"` to verify your tool is discovered and named correctly.
```yaml
---
description: custom tools opencode SDK plugin execute handler abort signal Zod schema session management event streaming
---
```
## Eval Scenarios
- [Scenario 0: Create a custom todo-search tool with schema validation](evals/scenario-0/task.md)
- [Scenario 1: Use SDK client to create session and send prompt](evals/scenario-1/task.md)
- [Scenario 2: Implement abort signal handling in long-running tools](evals/scenario-2/task.md)