plugins.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ---
  7. ## Create a plugin
  8. A plugin is a **JavaScript/TypeScript module** that exports one or more plugin
  9. functions. Each function receives a context object and returns a hooks object.
  10. ---
  11. ### Location
  12. Plugins are loaded from:
  13. 1. `.opencode/plugin` directory either in your project
  14. 2. Or, globally in `~/.config/opencode/plugin`
  15. ---
  16. ### Basic structure
  17. ```js title=".opencode/plugin/example.js"
  18. export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
  19. console.log("Plugin initialized!")
  20. return {
  21. // Hook implementations go here
  22. }
  23. }
  24. ```
  25. The plugin function receives:
  26. - `project`: The current project information.
  27. - `directory`: The current working directory.
  28. - `worktree`: The git worktree path.
  29. - `client`: An opencode SDK client for interacting with the AI.
  30. - `$`: Bun's [shell API](https://bun.com/docs/runtime/shell) for executing commands.
  31. ---
  32. ### TypeScript support
  33. For TypeScript plugins, you can import types from the plugin package:
  34. ```ts title="my-plugin.ts" {1}
  35. import type { Plugin } from "@opencode-ai/plugin"
  36. export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
  37. return {
  38. // Type-safe hook implementations
  39. }
  40. }
  41. ```
  42. ---
  43. ## Examples
  44. Here are some examples of plugins you can use to extend opencode.
  45. ---
  46. ### Send notifications
  47. Send notifications when certain events occur:
  48. ```js title=".opencode/plugin/notification.js"
  49. export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
  50. return {
  51. event: async ({ event }) => {
  52. // Send notification on session completion
  53. if (event.type === "session.idle") {
  54. await $`osascript -e 'display notification "Session completed!" with title "opencode"'`
  55. }
  56. },
  57. }
  58. }
  59. ```
  60. We are using `osascript` to run AppleScript on macOS. Here we are using it to send notifications.
  61. ---
  62. ### .env protection
  63. Prevent opencode from reading `.env` files:
  64. ```javascript title=".opencode/plugin/env-protection.js"
  65. export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
  66. return {
  67. "tool.execute.before": async (input, output) => {
  68. if (input.tool === "read" && output.args.filePath.includes(".env")) {
  69. throw new Error("Do not read .env files")
  70. }
  71. },
  72. }
  73. }
  74. ```
  75. ---
  76. ### Custom tools
  77. Plugins can also add custom tools to opencode:
  78. ```ts title=".opencode/plugin/custom-tools.ts"
  79. import type { Plugin, tool } from "@opencode-ai/plugin"
  80. export const CustomToolsPlugin: Plugin = async (ctx) => {
  81. return {
  82. tool: {
  83. mytool: tool({
  84. description: "This is a custom tool",
  85. args: {
  86. foo: tool.schema.string(),
  87. },
  88. async execute(args, ctx) {
  89. return `Hello ${args.foo}!`
  90. },
  91. }),
  92. },
  93. }
  94. }
  95. ```
  96. The `tool` helper creates a custom tool that opencode can call. It takes a Zod schema function and returns a tool definition with:
  97. - `description`: What the tool does
  98. - `args`: Zod schema for the tool's arguments
  99. - `execute`: Function that runs when the tool is called
  100. Your custom tools will be available to opencode alongside built-in tools.