Skip to content

Commit 7542cfd

Browse files
feat(hooks): Add done_thinking and permission_requested hooks to the
user config
1 parent d87f04e commit 7542cfd

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lua/opencode/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ M.defaults = {
189189
hooks = {
190190
on_file_edited = nil,
191191
on_session_loaded = nil,
192+
on_done_thinking = nil,
193+
on_permission_requested = nil,
192194
},
193195
}
194196

lua/opencode/core.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,24 @@ function M.initialize_current_model()
366366
return state.current_model
367367
end
368368

369+
local function on_job_count_change(_, new, old)
370+
local done_thinking = new == 0 and old > 0
371+
if config.hooks and config.hooks.on_done_thinking and done_thinking then
372+
pcall(config.hooks.on_done_thinking)
373+
end
374+
end
375+
376+
local function on_current_permission_change(_, new, old)
377+
local permission_requested = old == nil and new ~= nil
378+
if config.hooks and config.hooks.on_permission_requested and permission_requested then
379+
pcall(config.hooks.on_permission_requested)
380+
end
381+
end
382+
369383
function M.setup()
370384
state.subscribe('opencode_server', on_opencode_server)
385+
state.subscribe('user_message_count', on_job_count_change)
386+
state.subscribe('current_permission', on_current_permission_change)
371387

372388
vim.schedule(function()
373389
M.opencode_ok()

lua/opencode/types.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139
---@class OpencodeHooks
140140
---@field on_file_edited? fun(file: string): nil
141141
---@field on_session_loaded? fun(session: Session): nil
142+
---@field on_done_thinking? fun(): nil
143+
---@field on_permission_requested? fun(): nil
142144

143145
---@class OpencodeProviders
144146
---@field [string] string[]

0 commit comments

Comments
 (0)