plugins.mdx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. ---
  2. title: Plugins
  3. description: Write your own plugins to extend OpenCode.
  4. ---
  5. 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.
  6. For examples, check out the [plugins](/docs/ecosystem#plugins) created by the community.
  7. ---
  8. ## Use a plugin
  9. There are two ways to load plugins.
  10. ---
  11. ### From local files
  12. Place JavaScript or TypeScript files in the plugin directory.
  13. - `.opencode/plugins/` - Project-level plugins
  14. - `~/.config/opencode/plugins/` - Global plugins
  15. Files in these directories are automatically loaded at startup.
  16. ---
  17. ### From npm
  18. Specify npm packages in your config file.
  19. ```json title="opencode.json"
  20. {
  21. "$schema": "https://opencode.ai/config.json",
  22. "plugin": ["opencode-helicone-session", "opencode-wakatime", "@my-org/custom-plugin"]
  23. }
  24. ```
  25. Both regular and scoped npm packages are supported.
  26. Browse available plugins in the [ecosystem](/docs/ecosystem#plugins).
  27. ---
  28. ### How plugins are installed
  29. **npm plugins** are installed automatically using Bun at startup. Packages and their dependencies are cached in `~/.cache/opencode/node_modules/`.
  30. **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](#dependencies)), or publish the plugin to npm and [add it to your config](/docs/config#plugins).
  31. ---
  32. ### Load order
  33. Plugins are loaded from all sources and all hooks run in sequence. The load order is:
  34. 1. Global config (`~/.config/opencode/opencode.json`)
  35. 2. Project config (`opencode.json`)
  36. 3. Global plugin directory (`~/.config/opencode/plugins/`)
  37. 4. Project plugin directory (`.opencode/plugins/`)
  38. Duplicate npm packages with the same name and version are loaded once. However, a local plugin and an npm plugin with similar names are both loaded separately.
  39. ---
  40. ## Create a plugin
  41. A plugin is a **JavaScript/TypeScript module** that exports one or more plugin
  42. functions. Each function receives a context object and returns a hooks object.
  43. ---
  44. ### Dependencies
  45. Local plugins and custom tools can use external npm packages. Add a `package.json` to your config directory with the dependencies you need.
  46. ```json title=".opencode/package.json"
  47. {
  48. "dependencies": {
  49. "shescape": "^2.1.0"
  50. }
  51. }
  52. ```
  53. OpenCode runs `bun install` at startup to install these. Your plugins and tools can then import them.
  54. ```ts title=".opencode/plugins/my-plugin.ts"
  55. import { escape } from "shescape"
  56. export const MyPlugin = async (ctx) => {
  57. return {
  58. "tool.execute.before": async (input, output) => {
  59. if (input.tool === "bash") {
  60. output.args.command = escape(output.args.command)
  61. }
  62. },
  63. }
  64. }
  65. ```
  66. ---
  67. ### Basic structure
  68. ```js title=".opencode/plugins/example.js"
  69. export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
  70. console.log("Plugin initialized!")
  71. return {
  72. // Hook implementations go here
  73. }
  74. }
  75. ```
  76. The plugin function receives:
  77. - `project`: The current project information.
  78. - `directory`: The current working directory.
  79. - `worktree`: The git worktree path.
  80. - `client`: An opencode SDK client for interacting with the AI.
  81. - `$`: Bun's [shell API](https://bun.com/docs/runtime/shell) for executing commands.
  82. ---
  83. ### TypeScript support
  84. For TypeScript plugins, you can import types from the plugin package:
  85. ```ts title="my-plugin.ts" {1}
  86. import type { Plugin } from "@opencode-ai/plugin"
  87. export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
  88. return {
  89. // Type-safe hook implementations
  90. }
  91. }
  92. ```
  93. ---
  94. ### Events
  95. Plugins can subscribe to events as seen below in the Examples section. Here is a list of the different events available.
  96. #### Command Events
  97. - `command.executed`
  98. #### File Events
  99. - `file.edited`
  100. - `file.watcher.updated`
  101. #### Installation Events
  102. - `installation.updated`
  103. #### LSP Events
  104. - `lsp.client.diagnostics`
  105. - `lsp.updated`
  106. #### Message Events
  107. - `message.part.removed`
  108. - `message.part.updated`
  109. - `message.removed`
  110. - `message.updated`
  111. #### Permission Events
  112. - `permission.asked`
  113. - `permission.replied`
  114. #### Server Events
  115. - `server.connected`
  116. #### Session Events
  117. - `session.created`
  118. - `session.compacted`
  119. - `session.deleted`
  120. - `session.diff`
  121. - `session.error`
  122. - `session.idle`
  123. - `session.status`
  124. - `session.updated`
  125. #### Todo Events
  126. - `todo.updated`
  127. #### Tool Events
  128. - `tool.execute.after`
  129. - `tool.execute.before`
  130. #### TUI Events
  131. - `tui.prompt.append`
  132. - `tui.command.execute`
  133. - `tui.toast.show`
  134. ---
  135. ## Examples
  136. Here are some examples of plugins you can use to extend opencode.
  137. ---
  138. ### Send notifications
  139. Send notifications when certain events occur:
  140. ```js title=".opencode/plugins/notification.js"
  141. export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
  142. return {
  143. event: async ({ event }) => {
  144. // Send notification on session completion
  145. if (event.type === "session.idle") {
  146. await $`osascript -e 'display notification "Session completed!" with title "opencode"'`
  147. }
  148. },
  149. }
  150. }
  151. ```
  152. We are using `osascript` to run AppleScript on macOS. Here we are using it to send notifications.
  153. :::note
  154. If you’re using the OpenCode desktop app, it can send system notifications automatically when a response is ready or when a session errors.
  155. :::
  156. ---
  157. ### .env protection
  158. Prevent opencode from reading `.env` files:
  159. ```javascript title=".opencode/plugins/env-protection.js"
  160. export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
  161. return {
  162. "tool.execute.before": async (input, output) => {
  163. if (input.tool === "read" && output.args.filePath.includes(".env")) {
  164. throw new Error("Do not read .env files")
  165. }
  166. },
  167. }
  168. }
  169. ```
  170. ---
  171. ### Custom tools
  172. Plugins can also add custom tools to opencode:
  173. ```ts title=".opencode/plugins/custom-tools.ts"
  174. import { type Plugin, tool } from "@opencode-ai/plugin"
  175. export const CustomToolsPlugin: Plugin = async (ctx) => {
  176. return {
  177. tool: {
  178. mytool: tool({
  179. description: "This is a custom tool",
  180. args: {
  181. foo: tool.schema.string(),
  182. },
  183. async execute(args, ctx) {
  184. return `Hello ${args.foo}!`
  185. },
  186. }),
  187. },
  188. }
  189. }
  190. ```
  191. The `tool` helper creates a custom tool that opencode can call. It takes a Zod schema function and returns a tool definition with:
  192. - `description`: What the tool does
  193. - `args`: Zod schema for the tool's arguments
  194. - `execute`: Function that runs when the tool is called
  195. Your custom tools will be available to opencode alongside built-in tools.
  196. ---
  197. ### Logging
  198. Use `client.app.log()` instead of `console.log` for structured logging:
  199. ```ts title=".opencode/plugins/my-plugin.ts"
  200. export const MyPlugin = async ({ client }) => {
  201. await client.app.log({
  202. service: "my-plugin",
  203. level: "info",
  204. message: "Plugin initialized",
  205. extra: { foo: "bar" },
  206. })
  207. }
  208. ```
  209. Levels: `debug`, `info`, `warn`, `error`. See [SDK documentation](https://opencode.ai/docs/sdk) for details.
  210. ---
  211. ### Compaction hooks
  212. Customize the context included when a session is compacted:
  213. ```ts title=".opencode/plugins/compaction.ts"
  214. import type { Plugin } from "@opencode-ai/plugin"
  215. export const CompactionPlugin: Plugin = async (ctx) => {
  216. return {
  217. "experimental.session.compacting": async (input, output) => {
  218. // Inject additional context into the compaction prompt
  219. output.context.push(`
  220. ## Custom Context
  221. Include any state that should persist across compaction:
  222. - Current task status
  223. - Important decisions made
  224. - Files being actively worked on
  225. `)
  226. },
  227. }
  228. }
  229. ```
  230. The `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.
  231. You can also replace the compaction prompt entirely by setting `output.prompt`:
  232. ```ts title=".opencode/plugins/custom-compaction.ts"
  233. import type { Plugin } from "@opencode-ai/plugin"
  234. export const CustomCompactionPlugin: Plugin = async (ctx) => {
  235. return {
  236. "experimental.session.compacting": async (input, output) => {
  237. // Replace the entire compaction prompt
  238. output.prompt = `
  239. You are generating a continuation prompt for a multi-agent swarm session.
  240. Summarize:
  241. 1. The current task and its status
  242. 2. Which files are being modified and by whom
  243. 3. Any blockers or dependencies between agents
  244. 4. The next steps to complete the work
  245. Format as a structured prompt that a new agent can use to resume work.
  246. `
  247. },
  248. }
  249. }
  250. ```
  251. When `output.prompt` is set, it completely replaces the default compaction prompt. The `output.context` array is ignored in this case.