Overview
OpenCode’s plugin system allows you to extend functionality by:- Creating custom tools for the AI agent
- Hooking into the chat lifecycle
- Modifying prompts and parameters
- Handling authentication for custom providers
- Responding to events
Installation
Install the plugin SDK:Basic Plugin
Create a plugin file (e.g.,my-plugin.ts):
Plugin Input
The plugin function receives a context object:Using the Context
Registering Plugins
Add your plugin toopencode.json:
Creating Tools
Tools are functions that the AI agent can call. Use thetool() helper to define them:
Tool Definition
Tool Context
Theexecute function receives a context object:
Tool Schema
Use Zod for argument validation:Hooks
Plugins can implement various hooks to customize behavior:Event Hook
Listen to all server events:Config Hook
Modify configuration:Chat Hooks
chat.message
Called when a new message is received:chat.params
Modify LLM parameters:chat.headers
Add custom headers to LLM requests:Permission Hook
Control permission requests:Command Hook
Run code before command execution:Tool Hooks
tool.execute.before
Called before tool execution:tool.execute.after
Called after tool execution:tool.definition
Modify tool definitions sent to the LLM:Shell Environment Hook
Customize shell environment:Auth Hook
Handle authentication for custom providers:Experimental Hooks
These hooks may change in future versions:experimental.chat.messages.transform
Transform messages before sending to LLM:experimental.chat.system.transform
Transform system prompt:experimental.session.compacting
Customize session compaction:Complete Example
Here’s a complete plugin with multiple features:Next Steps
Plugin Tools
Detailed tool creation guide
Plugin Examples
Real-world plugin examples