Skip to content

Commit 194f1da

Browse files
committed
wip
1 parent 842ce54 commit 194f1da

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

lua/codecompanion/interactions/chat/acp/handler.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Queue = require("codecompanion.utils.queue")
2+
23
local config = require("codecompanion.config")
34
local formatter = require("codecompanion.interactions.chat.acp.formatters")
45
local log = require("codecompanion.utils.log")
@@ -11,7 +12,7 @@ local watch = require("codecompanion.interactions.shared.watch")
1112
---@field reasoning table Reasoning output from the Agent
1213
---@field tools table<string, table> Cache of tool calls by their ID
1314
---@field ui_state table<string, table> Cache of tool call UI states (line_number, icon_id) by tool call ID
14-
---@field _permission {queue: CodeCompanion.Queue, active: boolean} Internal state for managing permission requests
15+
---@field _permission { queue: CodeCompanion.Queue, active: boolean } Internal state for managing permission requests
1516
local ACPHandler = {}
1617

1718
---@param chat CodeCompanion.Chat
@@ -190,7 +191,7 @@ function ACPHandler:create_and_send_prompt(payload)
190191
self:handle_permission_request(request)
191192
end)
192193
:on_complete(function()
193-
self:handle_completion()
194+
self:handle_complete()
194195
end)
195196
:on_error(function(error)
196197
self:handle_error(error)
@@ -201,6 +202,7 @@ end
201202

202203
---Handle incoming message chunks
203204
---@param content string
205+
---@return nil
204206
function ACPHandler:handle_message_chunk(content)
205207
table.insert(self.output, content)
206208
self.chat:add_buf_message(
@@ -211,6 +213,7 @@ end
211213

212214
---Handle incoming thought chunks
213215
---@param content string
216+
---@return nil
214217
function ACPHandler:handle_thought_chunk(content)
215218
table.insert(self.reasoning, content)
216219
if config.display.chat.show_reasoning then
@@ -286,12 +289,7 @@ function ACPHandler:process_tool_call(tool_call)
286289
self.ui_state[id] = { line_number = line_number, icon_id = icon_id }
287290
end
288291

289-
---Queue a permission request and present it when ready.
290-
---Agents may send multiple permission requests concurrently. Because the
291-
---approval UI uses buffer-local keymaps, presenting more than one at a time
292-
---would cause the second set of keymaps to overwrite the first, leaving
293-
---the earlier request unresolvable and the agent hanging. We queue them
294-
---and present one at a time.
292+
---Queue a permission request and process when ready
295293
---@param request table
296294
---@return nil
297295
function ACPHandler:handle_permission_request(request)
@@ -332,18 +330,18 @@ function ACPHandler:_process_next_permission()
332330
end, request.options or {}))
333331
)
334332

335-
-- Wrap respond so we present the next queued permission after the user decides
336-
local original_respond = request.respond
333+
-- Ensure that the next item in the queue is processed after the user's response
334+
local send_response = request.respond
337335
request.respond = function(option_id, canceled)
338-
original_respond(option_id, canceled)
336+
send_response(option_id, canceled)
339337
self._permission.active = false
340338
self:_process_next_permission()
341339
end
342340

343341
return require("codecompanion.interactions.chat.acp.request_permission").confirm(self.chat, request)
344342
end
345343

346-
---Reject all queued permission requests (e.g. on error or completion with pending items)
344+
---Clear any requests in the queue
347345
---@return nil
348346
function ACPHandler:_clear_permission_queue()
349347
while not self._permission.queue:is_empty() do
@@ -353,8 +351,9 @@ function ACPHandler:_clear_permission_queue()
353351
self._permission.active = false
354352
end
355353

356-
---Handle completion
357-
function ACPHandler:handle_completion()
354+
---Handle the prompt response when it's complete
355+
---@return nil
356+
function ACPHandler:handle_complete()
358357
self:_clear_permission_queue()
359358

360359
if not self.chat.status or self.chat.status == "" then
@@ -366,6 +365,7 @@ end
366365

367366
---Handle errors
368367
---@param error string
368+
---@return nil
369369
function ACPHandler:handle_error(error)
370370
self:_clear_permission_queue()
371371

tests/interactions/chat/acp/test_handler.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ T["ACPHandler"]["coordinates completion flow"] = function()
303303
handler:handle_message_chunk("Response part 1")
304304
handler:handle_message_chunk(" and part 2")
305305
handler:handle_thought_chunk("My reasoning")
306-
handler:handle_completion("end_turn")
306+
handler:handle_complete("end_turn")
307307
308308
return {
309309
status = chat.status,
@@ -835,7 +835,7 @@ T["ACPHandler"]["Permission Queue"]["clears queue on completion"] = function()
835835
836836
-- Simulate completion while requests are still queued
837837
chat.done = function() end
838-
handler:handle_completion()
838+
handler:handle_complete()
839839
840840
return {
841841
queue_empty = handler._permission.queue:is_empty(),

0 commit comments

Comments
 (0)