Plugins allow you to extend OpenCode by hooking into various events and customizing behavior. You can create plugins to add new features, integrate with external services, or modify OpenCode’s default behavior. For examples, check out the plugins created by the community.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/anomalyco/opencode/llms.txt
Use this file to discover all available pages before exploring further.
Use a plugin
There are two ways to load plugins.From local files
Place JavaScript or TypeScript files in the plugin directory..opencode/plugins/- Project-level plugins~/.config/opencode/plugins/- Global plugins
From npm
Specify npm packages in your config file.opencode.json
How plugins are installed
npm plugins are installed automatically using Bun at startup. Packages and their dependencies are cached in~/.cache/opencode/node_modules/.
Local plugins are loaded directly from the plugin directory. To use external packages, you must create a package.json within your config directory (see Dependencies), or publish the plugin to npm and add it to your config.
Load order
Plugins are loaded from all sources and all hooks run in sequence. The load order is:- Global config (
~/.config/opencode/opencode.json) - Project config (
opencode.json) - Global plugin directory (
~/.config/opencode/plugins/) - Project plugin directory (
.opencode/plugins/)
Create a plugin
A plugin is a JavaScript/TypeScript module that exports one or more plugin functions. Each function receives a context object and returns a hooks object.Dependencies
Local plugins and custom tools can use external npm packages. Add apackage.json to your config directory with the dependencies you need.
.opencode/package.json
bun install at startup to install these. Your plugins and tools can then import them.
.opencode/plugins/my-plugin.ts
Basic structure
.opencode/plugins/example.js
project: The current project information.directory: The current working directory.worktree: The git worktree path.client: An opencode SDK client for interacting with the AI.$: Bun’s shell API for executing commands.
TypeScript support
For TypeScript plugins, you can import types from the plugin package:my-plugin.ts
Plugin API
Context
The plugin function receives aPluginInput context object:
client: SDK client for interacting with OpenCode’s APIproject: Current project metadatadirectory: Current working directory for the sessionworktree: Git worktree root pathserverUrl: OpenCode server URL$: Bun’s shell interface for executing commands
Hooks
Plugins return aHooks object with event handlers. All hooks are optional.
Chat Hooks
chat.message: Modify messages before they’re sent to the LLM
chat.params: Modify LLM parameters (temperature, topP, topK)
chat.headers: Add custom headers to LLM requests
Tool Hooks
tool.execute.before: Intercept tool calls before execution
tool.execute.after: Modify tool results after execution
tool.definition: Modify tool definitions sent to the LLM
tool: Register custom tools
Command Hooks
command.execute.before: Modify commands before execution
Permission Hooks
permission.ask: Override permission decisions
Shell Hooks
shell.env: Inject environment variables into shell execution
Session Hooks
experimental.session.compacting: Customize session compaction
Event Hook
event: Listen to all OpenCode events
Events
Plugins can subscribe to events using theevent hook. Here is a list of the different events available.
Command Events
command.executed
File Events
file.editedfile.watcher.updated
Installation Events
installation.updated
LSP Events
lsp.client.diagnosticslsp.updated
Message Events
message.part.removedmessage.part.updatedmessage.removedmessage.updated
Permission Events
permission.askedpermission.replied
Server Events
server.connected
Session Events
session.createdsession.compactedsession.deletedsession.diffsession.errorsession.idlesession.statussession.updated
Todo Events
todo.updated
Shell Events
shell.env
Tool Events
tool.execute.aftertool.execute.before
TUI Events
tui.prompt.appendtui.command.executetui.toast.show
Examples
Here are some examples of plugins you can use to extend opencode.Send notifications
Send notifications when certain events occur:.opencode/plugins/notification.js
osascript to run AppleScript on macOS. Here we are using it to send notifications.
If you’re using the OpenCode desktop app, it can send system notifications automatically when a response is ready or when a session errors.
.env protection
Prevent opencode from reading.env files:
.opencode/plugins/env-protection.js
Inject environment variables
Inject environment variables into all shell execution (AI tools and user terminals):.opencode/plugins/inject-env.js
Custom tools
Plugins can also add custom tools to opencode:.opencode/plugins/custom-tools.ts
tool helper creates a custom tool that opencode can call. It takes a Zod schema function and returns a tool definition with:
description: What the tool doesargs: Zod schema for the tool’s argumentsexecute: Function that runs when the tool is called
Logging
Useclient.app.log() instead of console.log for structured logging:
.opencode/plugins/my-plugin.ts
debug, info, warn, error. See SDK documentation for details.
Compaction hooks
Customize the context included when a session is compacted:.opencode/plugins/compaction.ts
experimental.session.compacting hook fires before the LLM generates a continuation summary. Use it to inject domain-specific context that the default compaction prompt would miss.
You can also replace the compaction prompt entirely by setting output.prompt:
.opencode/plugins/custom-compaction.ts
output.prompt is set, it completely replaces the default compaction prompt. The output.context array is ignored in this case.