Hooks support? #1025

Closed
opened 2026-02-16 17:29:07 -05:00 by yindo · 12 comments
Owner

Originally created by @codeagencybe on GitHub (Jul 31, 2025).

Originally assigned to: @thdxr on GitHub.

Is there an option to use hooks with opencode?

I want the agent to call a script after a task ends to automatically commit and push everything and open a PR.

Currently, it works by giving the instructions to the agent, but it consumes more and a lot of tokens. Also the output is not always consistent. Sometimes it doesn't set the assignee or labels, sometimes it randomly does nothing at all and I have to follow prompt to run the GitHub stuff.

And it's not efficient to let AI handle a simple task that can be run programmatically.

What is the best way to solve this with opencode?

Originally created by @codeagencybe on GitHub (Jul 31, 2025). Originally assigned to: @thdxr on GitHub. Is there an option to use hooks with opencode? I want the agent to call a script after a task ends to automatically commit and push everything and open a PR. Currently, it works by giving the instructions to the agent, but it consumes more and a lot of tokens. Also the output is not always consistent. Sometimes it doesn't set the assignee or labels, sometimes it randomly does nothing at all and I have to follow prompt to run the GitHub stuff. And it's not efficient to let AI handle a simple task that can be run programmatically. What is the best way to solve this with opencode?
yindo closed this issue 2026-02-16 17:29:07 -05:00
Author
Owner

@fahmi-grande commented on GitHub (Jul 31, 2025):

Up for this. Claude Code hooks can do so much things right now. especially for this PreToolUse

@fahmi-grande commented on GitHub (Jul 31, 2025): Up for this. Claude Code hooks can do so much things right now. especially for this `PreToolUse`
Author
Owner

@thdxr commented on GitHub (Aug 1, 2025):

we'll have something better than hooks - likely tomorrow

@thdxr commented on GitHub (Aug 1, 2025): we'll have something better than hooks - likely tomorrow
Author
Owner

@SquallLeonhart13 commented on GitHub (Aug 1, 2025):

better than hooks? Bold statement
I added claude likes hooks to the best of my (claude) ability no promises. Testing now with our new platform that helps capture hook event data to make a principle engineer, we are in early beta if you guys want to check us out https://github.com/principle-md/opencode-with-hooks/releases/tag/v0.4.0
https://principle-md.com

@SquallLeonhart13 commented on GitHub (Aug 1, 2025): better than hooks? Bold statement I added claude likes hooks to the best of my (claude) ability no promises. Testing now with our new platform that helps capture hook event data to make a principle engineer, we are in early beta if you guys want to check us out https://github.com/principle-md/opencode-with-hooks/releases/tag/v0.4.0 https://principle-md.com
Author
Owner

@ofonseca-pyming commented on GitHub (Aug 4, 2025):

With plugins you can extend beyond the execution of user-defined shell commands:
https://opencode.ai/docs/plugins/

@ofonseca-pyming commented on GitHub (Aug 4, 2025): With plugins you can extend beyond the execution of user-defined shell commands: https://opencode.ai/docs/plugins/
Author
Owner

@rmoriz commented on GitHub (Aug 8, 2025):

Did someone get the plugin approach to work? even the examples (notification) won't work for me. Neither in a project, nor globally (v0.4.1)

@rmoriz commented on GitHub (Aug 8, 2025): Did someone get the plugin approach to work? even the examples (notification) won't work for me. Neither in a project, nor globally (v0.4.1)
Author
Owner

@asafyish commented on GitHub (Aug 9, 2025):

I also tried using simple plugins, nothing works.
I have a plugin in ~/.config/opencode/plugin - but it doesn't load, no errors in the logs.

@asafyish commented on GitHub (Aug 9, 2025): I also tried using simple plugins, nothing works. I have a plugin in ~/.config/opencode/plugin - but it doesn't load, no errors in the logs.
Author
Owner

@darrenhinde commented on GitHub (Aug 9, 2025):

Also having the same issue, tried both in project and the global level.

@darrenhinde commented on GitHub (Aug 9, 2025): Also having the same issue, tried both in project and the global level.
Author
Owner

@rmoriz commented on GitHub (Aug 9, 2025):

let's use opencode to debug 😎
https://opencode.ai/s/U09ntSBv

Based on my analysis of the opencode plugin system, I've identified several issues that could prevent plugins from loading or working properly:
Issues Found:

  1. Plugin Hook Structure Mismatch in Documentation vs. Implementation

    Documentation shows tool.execute.before as nested hooks in /packages/web/src/content/docs/docs/plugins.mdx:96-106
    But implementation expects flat hook names like "tool.execute.before" in /packages/opencode/src/plugin/index.ts:25-36

  2. Missing Error Handling in Plugin Loading

    Plugin loading in /packages/opencode/src/plugin/index.ts:20-35 lacks error handling
    If a plugin fails to load or export properly, it could silently fail or crash the loading process

  3. Inconsistent Plugin Discovery Paths

    Global plugins search in ~/.config/opencode/plugin/.ts via /packages/opencode/src/config/config.ts:108
    Project plugins search in .opencode/plugin/
    .ts via /packages/opencode/src/config/config.ts:109
    But documentation mentions .opencode/plugin directory, not specific file patterns

  4. Plugin Configuration Loading Issues

    Config loading at /packages/opencode/src/config/config.ts:475-482 tries to resolve plugin paths but catches errors silently
    This could mask actual plugin loading failures

  5. Event System Integration

    Plugin initialization in /packages/opencode/src/plugin/index.ts:59-68 subscribes to all bus events
    But there's no verification that plugins actually get called properly

  6. No Plugin Validation or Loading Logs

    Missing debug logging to help troubleshoot plugin loading issues
    No validation that exported functions match the Plugin interface

Main Problem Areas:

config.ts:105-111 - Plugin discovery and path resolution
plugin/index.ts:20-35 - Plugin loading loop with no error handling
plugin/index.ts:42-57 - Hook triggering mechanism
plugin/index.ts:59-68 - Event subscription for plugins

The most likely cause of user reports is that plugins are failing to load due to path resolution issues or silent failures in the loading process, with no debugging information to help users understand what went wrong.

@rmoriz commented on GitHub (Aug 9, 2025): let's use opencode to debug 😎 https://opencode.ai/s/U09ntSBv > Based on my analysis of the opencode plugin system, I've identified several issues that could prevent plugins from loading or working properly: > Issues Found: > > 1. Plugin Hook Structure Mismatch in Documentation vs. Implementation > > Documentation shows tool.execute.before as nested hooks in /packages/web/src/content/docs/docs/plugins.mdx:96-106 > But implementation expects flat hook names like "tool.execute.before" in /packages/opencode/src/plugin/index.ts:25-36 > > 2. Missing Error Handling in Plugin Loading > > Plugin loading in /packages/opencode/src/plugin/index.ts:20-35 lacks error handling > If a plugin fails to load or export properly, it could silently fail or crash the loading process > > 3. Inconsistent Plugin Discovery Paths > > Global plugins search in ~/.config/opencode/plugin/*.ts via /packages/opencode/src/config/config.ts:108 > Project plugins search in .opencode/plugin/*.ts via /packages/opencode/src/config/config.ts:109 > But documentation mentions .opencode/plugin directory, not specific file patterns > > 4. Plugin Configuration Loading Issues > > Config loading at /packages/opencode/src/config/config.ts:475-482 tries to resolve plugin paths but catches errors silently > This could mask actual plugin loading failures > > 5. Event System Integration > > Plugin initialization in /packages/opencode/src/plugin/index.ts:59-68 subscribes to all bus events > But there's no verification that plugins actually get called properly > > 6. No Plugin Validation or Loading Logs > > Missing debug logging to help troubleshoot plugin loading issues > No validation that exported functions match the Plugin interface > > Main Problem Areas: > > config.ts:105-111 - Plugin discovery and path resolution > plugin/index.ts:20-35 - Plugin loading loop with no error handling > plugin/index.ts:42-57 - Hook triggering mechanism > plugin/index.ts:59-68 - Event subscription for plugins > > The most likely cause of user reports is that plugins are failing to load due to path resolution issues or silent failures in the loading process, with no debugging information to help users understand what went wrong.
Author
Owner

@rmoriz commented on GitHub (Aug 9, 2025):

this works for me:

Write .opencode/plugin/say-notification.ts

import type { Plugin } from "@opencode-ai/plugin"

export const SayNotificationPlugin: Plugin = async ({ app, client, $ }) => {
  console.log("Say Notification Plugin initialized!")

  return {
    event: async ({ event }) => {
      // Trigger when session becomes idle (user stops interacting)
      if (event.type === "session.idle") {
        try {
          // Use macOS say command to announce session completion
          await $`say "Your opencode session is now idle and ready for new input"`
        } catch (error) {
          console.error("Failed to use say command:", error)
        }
      }
    },
  }
}

prompt:

can you create an example plugin in typescript that calls macos' "say" command with a message when the session is idle?
rmoriz (11:46 PM)

I'll create an example TypeScript plugin that uses macOS's say command when a session becomes idle.
claude-sonnet-4 (11:46 PM)
@rmoriz commented on GitHub (Aug 9, 2025): this works for me: Write .opencode/plugin/say-notification.ts ```typescript import type { Plugin } from "@opencode-ai/plugin" export const SayNotificationPlugin: Plugin = async ({ app, client, $ }) => { console.log("Say Notification Plugin initialized!") return { event: async ({ event }) => { // Trigger when session becomes idle (user stops interacting) if (event.type === "session.idle") { try { // Use macOS say command to announce session completion await $`say "Your opencode session is now idle and ready for new input"` } catch (error) { console.error("Failed to use say command:", error) } } }, } } ``` prompt: ``` can you create an example plugin in typescript that calls macos' "say" command with a message when the session is idle? rmoriz (11:46 PM) I'll create an example TypeScript plugin that uses macOS's say command when a session becomes idle. claude-sonnet-4 (11:46 PM) ```
Author
Owner

@rmoriz commented on GitHub (Aug 9, 2025):

TODO:

JavaScript plugins are supported, but there's a discrepancy in the current implementation. The config only searches for .ts files, but the
documentation shows JavaScript examples. This might be one of the issues causing plugin loading problems.

Here's the JavaScript version I created. The main differences from TypeScript:

• No type imports needed
• Same functionality and structure
• Works with Bun's native JavaScript support

However, I noticed the config currently only searches for .ts files in /packages/opencode/src/config/config.ts:108-109. This should
probably
be updated to also include .js files to match the documentation.

The JavaScript plugin should work if you manually add it to your config or if the search pattern gets updated to include .js files.
claude-sonnet-4 (11:55 PM)

@rmoriz commented on GitHub (Aug 9, 2025): TODO: > JavaScript plugins are supported, but there's a discrepancy in the current implementation. The config only searches for .ts files, but the > documentation shows JavaScript examples. This might be one of the issues causing plugin loading problems. > > Here's the JavaScript version I created. The main differences from TypeScript: > > • No type imports needed > • Same functionality and structure > • Works with Bun's native JavaScript support > > **However, I noticed the config currently only searches for .ts files in /packages/opencode/src/config/config.ts:108-109. This should > probably > be updated to also include .js files to match the documentation.** > > The JavaScript plugin should work if you manually add it to your config or if the search pattern gets updated to include .js files. > claude-sonnet-4 (11:55 PM) >
Author
Owner

@asafyish commented on GitHub (Aug 10, 2025):

The problem I had was indeed js file instead of ts.

@asafyish commented on GitHub (Aug 10, 2025): The problem I had was indeed js file instead of ts.
Author
Owner

@rekram1-node commented on GitHub (Aug 29, 2025):

Closing since js files are supported and docs have been updated: https://opencode.ai/docs/plugins/

@rekram1-node commented on GitHub (Aug 29, 2025): Closing since js files are supported and docs have been updated: https://opencode.ai/docs/plugins/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1025