Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/opencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ M.defaults = {
hooks = {
on_file_edited = nil,
on_session_loaded = nil,
on_done_thinking = nil,
on_permission_requested = nil,
},
}

Expand Down
16 changes: 16 additions & 0 deletions lua/opencode/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,24 @@ function M.initialize_current_model()
return state.current_model
end

local function on_job_count_change(_, new, old)
local done_thinking = new == 0 and old > 0
if config.hooks and config.hooks.on_done_thinking and done_thinking then
pcall(config.hooks.on_done_thinking)
end
end

local function on_current_permission_change(_, new, old)
local permission_requested = old == nil and new ~= nil
if config.hooks and config.hooks.on_permission_requested and permission_requested then
pcall(config.hooks.on_permission_requested)
end
end

function M.setup()
state.subscribe('opencode_server', on_opencode_server)
state.subscribe('user_message_count', on_job_count_change)
state.subscribe('current_permission', on_current_permission_change)

vim.schedule(function()
M.opencode_ok()
Expand Down
11 changes: 11 additions & 0 deletions lua/opencode/server_job.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ end
--- @return Promise<T> promise A promise that resolves with the result or rejects with an error
function M.call_api(url, method, body)
local call_promise = Promise.new()

local function is_user_message()
return method == 'POST' and url:match("/message$") ~= nil
end

state.job_count = state.job_count + 1
if is_user_message() then
state.user_message_count = state.user_message_count + 1
end

local request_entry = { nil, call_promise }
table.insert(M.requests, request_entry)
Expand All @@ -35,6 +43,9 @@ function M.call_api(url, method, body)
local function remove_from_requests()
for i, entry in ipairs(M.requests) do
if entry == request_entry then
if is_user_message() then
state.user_message_count = state.user_message_count - 1
Comment thread
guillaumeboehm marked this conversation as resolved.
Outdated
end
table.remove(M.requests, i)
break
end
Expand Down
2 changes: 2 additions & 0 deletions lua/opencode/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
---@field cost number
---@field tokens_count number
---@field job_count number
---@field user_message_count number
---@field opencode_server OpencodeServer|nil
---@field api_client OpencodeApiClient
---@field event_manager EventManager|nil
Expand Down Expand Up @@ -80,6 +81,7 @@ local _state = {
tokens_count = 0,
-- job
job_count = 0,
user_message_count = 0,
opencode_server = nil,
api_client = nil,
event_manager = nil,
Expand Down
2 changes: 2 additions & 0 deletions lua/opencode/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
---@class OpencodeHooks
---@field on_file_edited? fun(file: string): nil
---@field on_session_loaded? fun(session: Session): nil
---@field on_done_thinking? fun(): nil
---@field on_permission_requested? fun(): nil

---@class OpencodeProviders
---@field [string] string[]
Expand Down