Skip to content

Hooks support? #1473

Closed
Closed

Description

@codeagencybe

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?

Activity

  1. fahmi-grande commented on Aug 1, 2025

    @fahmi-grande

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

  2. self-assigned this
    on Aug 1, 2025
  3. thdxr commented on Aug 1, 2025

    @thdxr
    Member

    we'll have something better than hooks - likely tomorrow

  4. SquallLeonhart13 commented on Aug 1, 2025

    @SquallLeonhart13

    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

  5. ofonseca-pyming commented on Aug 4, 2025

    @ofonseca-pyming

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

  6. rmoriz commented on Aug 8, 2025

    @rmoriz
    Contributor

    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)

  7. asafyish commented on Aug 9, 2025

    @asafyish

    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.

  8. darrenhinde commented on Aug 9, 2025

    @darrenhinde

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

  9. rmoriz commented on Aug 9, 2025

    @rmoriz
    Contributor

    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.

  10. rmoriz commented on Aug 9, 2025

    @rmoriz
    Contributor

    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)
    
  11. rmoriz commented on Aug 9, 2025

    @rmoriz
    Contributor

    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)

  12. asafyish commented on Aug 10, 2025

    @asafyish

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

  13. rekram1-node commented on Aug 29, 2025

    @rekram1-node
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions