11local Queue = require (" codecompanion.utils.queue" )
2+
23local config = require (" codecompanion.config" )
34local formatter = require (" codecompanion.interactions.chat.acp.formatters" )
45local 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
1516local 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 )
201202
202203--- Handle incoming message chunks
203204--- @param content string
205+ --- @return nil
204206function ACPHandler :handle_message_chunk (content )
205207 table.insert (self .output , content )
206208 self .chat :add_buf_message (
211213
212214--- Handle incoming thought chunks
213215--- @param content string
216+ --- @return nil
214217function 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 }
287290end
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
297295function 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 )
344342end
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
348346function 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
354352end
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
366365
367366--- Handle errors
368367--- @param error string
368+ --- @return nil
369369function ACPHandler :handle_error (error )
370370 self :_clear_permission_queue ()
371371
0 commit comments