[feat] lifecycle hooks #417

Closed
opened 2026-02-16 17:26:39 -05:00 by yindo · 4 comments
Owner

Originally created by @monotykamary on GitHub (Jul 1, 2025).

Originally assigned to: @thdxr on GitHub.

Hooks are probably hidden gems for a client/server architecture CLI like opencode. It's bit different that the current event system hooks in that we can inject other behaviors (like inline linting) or pass messages to a UI framework.

Claude Code has these hooks:
PreToolUse: runs before a tool is called; can block or modify tool execution (e.g., validate or restrict commands).
PostToolUse: runs after a tool completes successfully; can provide feedback or take actions based on tool results.
Notification: runs when Claude Code sends notifications (e.g., when awaiting input or permission).
Stop: runs when Claude Code finishes responding; can block or allow the session to end.

Maybe a middle ground we can take advantage of for /share as well, as the websocket sends some simple key values:

// Initial message (just created)                                        
{ "key": "session/message/abc/msg1", "content": { "parts": [],           
"metadata": {...} }}                                                     
                                                                         
// After text starts streaming                                           
{ "key": "session/message/abc/msg1", "content": { "parts": [{"type":     
"text", "text": "I'll help..."}], "metadata": {...} }}                   
                                                                         
// After tool call starts                                                
{ "key": "session/message/abc/msg1", "content": { "parts": [..., {"type":
"tool-invocation", "state": "partial-call"}], "metadata": {...} }}       
                                                                         
// After tool completes                                                  
{ "key": "session/message/abc/msg1", "content": { "parts": [..., {"type":
"tool-invocation", "state": "result", "result": "..."}], "metadata":     
{"tool": {"call_123": {"time": {...}}}} }}                               
Originally created by @monotykamary on GitHub (Jul 1, 2025). Originally assigned to: @thdxr on GitHub. [Hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) are probably hidden gems for a client/server architecture CLI like opencode. It's bit different that the current event system hooks in that we can inject other behaviors (like inline linting) or pass messages to a UI framework. Claude Code has these hooks: • `PreToolUse`: runs before a tool is called; can block or modify tool execution (e.g., validate or restrict commands). • `PostToolUse`: runs after a tool completes successfully; can provide feedback or take actions based on tool results. • `Notification`: runs when Claude Code sends notifications (e.g., when awaiting input or permission). • `Stop`: runs when Claude Code finishes responding; can block or allow the session to end. Maybe a middle ground we can take advantage of for `/share` as well, as the websocket sends some simple key values: ``` // Initial message (just created) { "key": "session/message/abc/msg1", "content": { "parts": [], "metadata": {...} }} // After text starts streaming { "key": "session/message/abc/msg1", "content": { "parts": [{"type": "text", "text": "I'll help..."}], "metadata": {...} }} // After tool call starts { "key": "session/message/abc/msg1", "content": { "parts": [..., {"type": "tool-invocation", "state": "partial-call"}], "metadata": {...} }} // After tool completes { "key": "session/message/abc/msg1", "content": { "parts": [..., {"type": "tool-invocation", "state": "result", "result": "..."}], "metadata": {"tool": {"call_123": {"time": {...}}}} }} ```
yindo closed this issue 2026-02-16 17:26:39 -05:00
Author
Owner

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

we're likely going to have a plugin system where you can define these in typescript instead of the awkward hooks format claude code has

we actually have some experimental command based hooks here: https://raw.githubusercontent.com/sst/opencode/refs/heads/dev/packages/opencode/config.schema.json

@thdxr commented on GitHub (Jul 1, 2025): we're likely going to have a plugin system where you can define these in typescript instead of the awkward hooks format claude code has<br><br>we actually have some experimental command based hooks here: [https://raw.githubusercontent.com/sst/opencode/refs/heads/dev/packages/opencode/config.schema.json](https://raw.githubusercontent.com/sst/opencode/refs/heads/dev/packages/opencode/config.schema.json)
Author
Owner

@haydnhkim commented on GitHub (Jul 3, 2025):

It would be nice to have a hook that matches Claude Code's PreToolUse.

@haydnhkim commented on GitHub (Jul 3, 2025): It would be nice to have a hook that matches Claude Code's `PreToolUse`.
Author
Owner

@ghost91- commented on GitHub (Aug 6, 2025):

@monotykamary is this already solved by the new plugin functionality? Or are any particular hooks still missing?

@ghost91- commented on GitHub (Aug 6, 2025): @monotykamary is this already solved by the new [plugin](https://opencode.ai/docs/plugins/) functionality? Or are any particular hooks still missing?
Author
Owner

@monotykamary commented on GitHub (Aug 6, 2025):

Oh probably; this issue can probably be closed. Probably deserves a documentation update for all of the events that opencode has:

  • installation.updated
  • lsp.client.diagnostics
  • message.updated
  • message.removed
  • message.part.updated
  • message.part.removed
  • storage.write
  • file.edited
  • server.connected
  • permission.updated
  • permission.replied
  • session.updated
  • session.deleted
  • session.idle
  • session.error
  • file.watcher.updated
  • ide.installed
@monotykamary commented on GitHub (Aug 6, 2025): Oh probably; this issue can probably be closed. Probably deserves a documentation update for all of the events that opencode has: * `installation.updated` * `lsp.client.diagnostics` * `message.updated` * `message.removed` * `message.part.updated` * `message.part.removed` * `storage.write` * `file.edited` * `server.connected` * `permission.updated` * `permission.replied` * `session.updated` * `session.deleted` * `session.idle` * `session.error` * `file.watcher.updated` * `ide.installed`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#417