plugins.mdx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Check out the [ecosystem](/docs/ecosystem) for community plugins.
  7. ---
  8. ## Create a plugin
  9. A plugin is a **JavaScript/TypeScript module** that exports one or more plugin
  10. functions. Each function receives a context object and returns a hooks object.
  11. ---
  12. ### Location
  13. Plugins are loaded from:
  14. 1. `.opencode/plugin` directory either in your project
  15. 2. Or, globally in `~/.config/opencode/plugin`
  16. ---
  17. ### Basic structure
  18. ```js title=".opencode/plugin/example.js"
  19. export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
  20. console.log("Plugin initialized!")
  21. return {
  22. // Hook implementations go here
  23. }
  24. }
  25. ```
  26. The plugin function receives:
  27. - `project`: The current project information.
  28. - `directory`: The current working directory.
  29. - `worktree`: The git worktree path.
  30. - `client`: An opencode SDK client for interacting with the AI.
  31. - `$`: Bun's [shell API](https://bun.com/docs/runtime/shell) for executing commands.
  32. ---
  33. ### TypeScript support
  34. For TypeScript plugins, you can import types from the plugin package:
  35. ```ts title="my-plugin.ts" {1}
  36. import type { Plugin } from "@opencode-ai/plugin"
  37. export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
  38. return {
  39. // Type-safe hook implementations
  40. }
  41. }
  42. ```
  43. ---
  44. ### Events
  45. Plugins can subscribe to events as seen below in the Examples section. Here is a list of the different events available.
  46. #### Command Events
  47. - `command.executed`
  48. #### File Events
  49. - `file.edited`
  50. - `file.watcher.updated`
  51. #### Installation Events
  52. - `installation.updated`
  53. #### LSP Events
  54. - `lsp.client.diagnostics`
  55. - `lsp.updated`
  56. #### Message Events
  57. - `message.part.removed`
  58. - `message.part.updated`
  59. - `message.removed`
  60. - `message.updated`
  61. #### Permission Events
  62. - `permission.replied`
  63. - `permission.updated`
  64. #### Server Events
  65. - `server.connected`
  66. #### Session Events
  67. - `session.created`
  68. - `session.compacted`
  69. - `session.deleted`
  70. - `session.diff`
  71. - `session.error`
  72. - `session.idle`
  73. - `session.status`
  74. - `session.updated`
  75. #### Todo Events
  76. - `todo.updated`
  77. #### Tool Events
  78. - `tool.execute.after`
  79. - `tool.execute.before`
  80. #### TUI Events
  81. - `tui.prompt.append`
  82. - `tui.command.execute`
  83. - `tui.toast.show`
  84. ---
  85. ## Examples
  86. Here are some examples of plugins you can use to extend opencode.
  87. ---
  88. ### Send notifications
  89. Send notifications when certain events occur:
  90. ```js title=".opencode/plugin/notification.js"
  91. export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
  92. return {
  93. event: async ({ event }) => {
  94. // Send notification on session completion
  95. if (event.type === "session.idle") {
  96. await $`osascript -e 'display notification "Session completed!" with title "opencode"'`
  97. }
  98. },
  99. }
  100. }
  101. ```
  102. We are using `osascript` to run AppleScript on macOS. Here we are using it to send notifications.
  103. ---
  104. ### .env protection
  105. Prevent opencode from reading `.env` files:
  106. ```javascript title=".opencode/plugin/env-protection.js"
  107. export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
  108. return {
  109. "tool.execute.before": async (input, output) => {
  110. if (input.tool === "read" && output.args.filePath.includes(".env")) {
  111. throw new Error("Do not read .env files")
  112. }
  113. },
  114. }
  115. }
  116. ```
  117. ---
  118. ### Custom tools
  119. Plugins can also add custom tools to opencode:
  120. ```ts title=".opencode/plugin/custom-tools.ts"
  121. import { type Plugin, tool } from "@opencode-ai/plugin"
  122. export const CustomToolsPlugin: Plugin = async (ctx) => {
  123. return {
  124. tool: {
  125. mytool: tool({
  126. description: "This is a custom tool",
  127. args: {
  128. foo: tool.schema.string(),
  129. },
  130. async execute(args, ctx) {
  131. return `Hello ${args.foo}!`
  132. },
  133. }),
  134. },
  135. }
  136. }
  137. ```
  138. The `tool` helper creates a custom tool that opencode can call. It takes a Zod schema function and returns a tool definition with:
  139. - `description`: What the tool does
  140. - `args`: Zod schema for the tool's arguments
  141. - `execute`: Function that runs when the tool is called
  142. Your custom tools will be available to opencode alongside built-in tools.