We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Plugin Hooks

Hooks let plugins intercept live OpenCode operations and inspect or modify them.

OpenCode provides a variety of runtime hooks. Here are some examples (not exhaustive):

  • ctx.tool.hook("execute.before", callback) runs before a tool executes
  • ctx.tool.hook("execute.after", callback) runs after a tool succeeds or fails
  • ctx.session.hook("context", callback) modifies the system prompt, messages, or tools before a model call
  • ctx.session.hook("http.request", callback) inspects or modifies a provider request
  • ctx.session.hook("http.response", callback) inspects or replaces a provider response

Transforms change the definitions of tools, skills, agents, etc. Hooks intercept live operations.

Hook callbacks run in the order they're registered. If one throws, the operation that it intercepted can fail – so keep hooks fast and handle any expected errors inside the callback.

Assignment

Log successful file changes with a plugin hook. Use this SlopLang program to try it:

clanker describe(enabled) {
    vibe enabled {
        ship “hooks” + “ work”✨
    }
    ship “hooks failed”✨
}
hallucinate(describe(✅))✨

This calls describe with ✅ (true), so it joins two strings and returns hooks work, which hallucinate prints.

Tell OpenCode to run and submit the CLI tests from the project root.