create-opencode-plugin
Original:🇺🇸 English
Translated
1 scriptsChecked / no sensitive code detected
Create OpenCode plugins using the @opencode-ai/plugin SDK. Use for building custom tools, event hooks, auth providers, or tool execution interception. Use proactively when developing new plugins in .opencode/plugin/ or ~/.config/opencode/plugin/. Examples: - user: "Create a plugin to block dangerous commands" → implement tool execution before hook with blocking logic - user: "Add a custom tool for jira" → design tool schema and implementation using SDK context - user: "Show toast on file edit" → react to file edit events and display status message - user: "Build a custom auth provider" → implement auth flow for new model provider - user: "Intercept git commits" → add hook to validate commit messages before execution
8installs
Added on
NPX Install
npx skill4agent add igorwarzocha/opencode-workflows create-opencode-pluginTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Creating OpenCode Plugins
<critical> Re-read this file periodically during plugin development to refresh context and ensure you're following the correct procedure. </critical> <workflow>Procedure Overview
| Step | Action | Read |
|---|---|---|
| 1 | Verify SDK reference | Run extract script |
| 2 | Validate feasibility | This file |
| 3 | Design plugin | |
| 4 | Implement | |
| 5 | Add UI feedback | |
| 6 | Test | |
| 7 | Publish | |
Step 1: Verify SDK Reference (REQUIRED)
Before creating any plugin, MUST regenerate the API reference to ensure accuracy:
bash
bun run .opencode/skill/create-opencode-plugin/scripts/extract-plugin-api.tsThis generates:
- - All available hooks and signatures
references/hooks.md - - All event types and properties
references/events.md - - Tool creation patterns
references/tool-helper.md
Step 2: Validate Feasibility (REQUIRED)
MUST determine if the user's concept is achievable with available hooks.
Feasible as plugins:
- Intercepting/blocking tool calls
- Reacting to events (file edits, session completion, etc.)
- Adding custom tools for the LLM
- Modifying LLM parameters (temperature, etc.)
- Custom auth flows for providers
- Customizing session compaction
- Displaying status messages (toasts, inline)
NOT feasible (inform user):
- Modifying TUI rendering or layout
- Adding new built-in tools (requires OC source)
- Changing core agent behavior/prompts
- Intercepting assistant responses mid-stream
- Adding new keybinds or commands
- Modifying internal file read/write
- Adding new permission types
If not feasible, MUST inform user clearly. Suggest:
- OC core changes: contribute to
packages/opencode - MCP tools: use MCP server configuration
- Simple automation: use shell scripts
Step 3: Design Plugin
READ: for available hooks, for implementation patterns.
references/hooks.mdreferences/hook-patterns.mdREAD: for code architecture principles. MUST follow these design guidelines:
references/CODING-TS.MD- Modular structure: Split complex plugins into multiple focused files (types, utilities, hooks, tools)
- Single purpose: Each function does ONE thing well
- DRY: Extract common patterns into shared utilities immediately
- Small files: Keep individual files under 150 lines - split into smaller modules as needed
- No monoliths: MUST NOT put all plugin code in a single file
index.ts
Plugin Locations
| Scope | Path | Use Case |
|---|---|---|
| Project | | Team-shared, repo-specific |
| Global | | Personal, all projects |
Basic Structure
typescript
import type { Plugin } from "@opencode-ai/plugin"
export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
// Setup code runs once on load
return {
// Hook implementations - see references/hook-patterns.md
}
}Context Parameters
| Parameter | Type | Description |
|---|---|---|
| | Current project info (id, worktree, name) |
| SDK Client | OpenCode API client |
| | Bun shell for commands |
| | Current working directory |
| | Git worktree path |
Step 4: Implement
READ: for hook implementation examples.
references/hook-patterns.mdREAD: if adding custom tools (Zod schemas).
references/tool-helper.mdREAD: if using event hook (event types/properties).
references/events.mdREAD: for complete plugin examples.
references/examples.mdALWAYS READ: and follow modular design principles.
references/CODING-TS.MDPlugin Structure (Non-Monolithic)
For complex plugins, MUST use a modular directory structure:
.opencode/plugin/my-plugin/
├── index.ts # Entry point, exports Plugin
├── types.ts # TypeScript types/interfaces
├── utils.ts # Shared utilities
├── hooks/ # Hook implementations
│ ├── event.ts
│ └── tool-execute.ts
└── tools/ # Custom tool definitions
└── my-tool.tsExample modular index.ts:
typescript
import type { Plugin } from "@opencode-ai/plugin"
import { eventHooks } from "./hooks/event"
import { toolHooks } from "./hooks/tool-execute"
import { customTools } from "./tools"
export const MyPlugin: Plugin = async ({ project, client }) => {
return {
...eventHooks({ client }),
...toolHooks({ client }),
tool: customTools,
}
}Keep each file under 150 lines. Split as complexity grows.
Common Mistakes
| Mistake | Fix |
|---|---|
Using | Use |
| Wrong event property names | Check |
| Sync event handler | MUST use |
| Not throwing to block | |
| Forgetting TypeScript types | |
Step 5: Add UI Feedback (Optional)
Only if plugin needs user-visible notifications:
READ: for transient alerts (brief popups)
references/toast-notifications.mdREAD: for persistent inline status messages
references/ui-feedback.mdChoose based on:
| Need | Use |
|---|---|
| Brief alerts, warnings | Toast |
| Detailed stats, multi-line | Inline message |
| Config validation errors | Toast |
| Session completion notice | Toast or inline |
Step 6: Test
READ: for full testing procedure.
references/testing.mdQuick Test Steps
-
Create test folder with:
opencode.jsonjsonc{ "plugin": ["file:///path/to/your/plugin/index.ts"], } -
Verify plugin loads:bash
cd /path/to/test-folder opencode run hi -
Test interactively:bash
opencode -
SHOULD recommend specific tests based on hook type used.
Step 7: Publish (Optional)
READ: for npm publishing.
references/publishing.mdREAD: for version update toasts (for users with pinned versions).
</workflow>
references/update-notifications.md<reference_summary>
Reference Files Summary
| File | Purpose | When to Read |
|---|---|---|
| Hook signatures (auto-generated) | Step 3-4 |
| Event types (auto-generated) | Step 4 (if using events) |
| Zod tool schemas (auto-generated) | Step 4 (if custom tools) |
| Hook implementation examples | Step 3-4 |
| Code architecture principles | Step 3 (Design) |
| Complete plugin examples | Step 4 |
| Toast popup API | Step 5 (if toasts needed) |
| Inline message API | Step 5 (if inline needed) |
| Testing procedure | Step 6 |
| npm publishing | Step 7 |
| Version toast pattern | Step 7 (for npm plugins) |
</reference_summary>