Skip to content

Commit 5bac43b

Browse files
committed
fix(chat): acp model selection
1 parent 3465c5c commit 5bac43b

4 files changed

Lines changed: 35 additions & 16 deletions

File tree

lua/codecompanion/acp/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ function Connection:_apply_config_options(config_options)
907907
end
908908
end
909909

910+
log:debug("[acp::_apply_config_options] Available Models:\n%s", available)
911+
910912
self._models = {
911913
availableModels = available,
912914
currentModelId = opt.currentValue,

lua/codecompanion/interactions/chat/helpers/init.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ local api = vim.api
1010
local fmt = string.format
1111

1212
---Establishes the connection, authenticates, creates a session and links the buffer.
13-
---Runs asynchronously so the UI is not blocked during ACP initialization.
14-
---@param chat CodeCompanion.Chat The chat instance
13+
---@param chat CodeCompanion.Chat
14+
---@param cb? function
1515
---@return nil
16-
function M.create_acp_connection(chat)
16+
function M.create_acp_connection(chat, cb)
17+
---Run async so as not to block the UI
1718
local async_utils = require("codecompanion.utils.async")
1819

20+
local function call_cb()
21+
if cb then
22+
vim.schedule(cb)
23+
end
24+
end
25+
1926
async_utils.sync(function()
2027
local ACPHandler = require("codecompanion.interactions.chat.acp.handler")
2128
local handler = ACPHandler.new(chat)
2229

2330
if not handler:ensure_connection() then
24-
return
31+
return call_cb()
2532
end
2633

2734
handler:ensure_session()
35+
call_cb()
2836
end)()
2937
end
3038

lua/codecompanion/interactions/chat/init.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ end
699699

700700
---Change the adapter in the chat buffer
701701
---@param adapter string
702-
function Chat:change_adapter(adapter)
702+
---@param cb? function
703+
function Chat:change_adapter(adapter, cb)
703704
local function fire()
704705
return utils.fire("ChatAdapter", { bufnr = self.bufnr, adapter = adapters.make_safe(self.adapter) })
705706
end
@@ -708,8 +709,12 @@ function Chat:change_adapter(adapter)
708709
self.ui.adapter = self.adapter
709710

710711
if self.adapter.type == "acp" then
711-
helpers.create_acp_connection(self)
712+
helpers.create_acp_connection(self, cb)
712713
helpers.remove_mcp_tools(self)
714+
else
715+
if cb then
716+
vim.schedule(cb)
717+
end
713718
end
714719

715720
self:set_system_prompt()

lua/codecompanion/interactions/chat/keymaps/change_adapter.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function M.select_model(chat)
165165
local acp_models = M.list_acp_models(chat.acp_connection)
166166
models_list = acp_models and acp_models.availableModels or nil
167167
if not acp_models or not models_list then
168-
return log:debug("No models to select for the HTTP adapter")
168+
return log:debug("No models to select for the ACP adapter")
169169
end
170170
current_model = acp_models.currentModelId
171171
end
@@ -233,18 +233,22 @@ function M.callback(chat)
233233
return
234234
end
235235

236-
if current_adapter ~= selected_adapter then
237-
chat.acp_connection = nil -- Ensure we reset this
238-
chat:change_adapter(selected_adapter)
239-
end
236+
local function on_adapter_ready()
237+
-- Only force a system prompt update if the user isn't ignoring it. This
238+
-- occurs when a user has initiated a chat from the prompt library
239+
if not chat.opts.ignore_system_prompt then
240+
M.update_system_prompt(chat)
241+
end
240242

241-
-- Only force a system prompt update if the user isn't ignoring it. This
242-
-- occurs when a user has initiated a chat from the prompt library
243-
if not chat.opts.ignore_system_prompt then
244-
M.update_system_prompt(chat)
243+
return M.select_model(chat)
245244
end
246245

247-
return M.select_model(chat)
246+
if current_adapter ~= selected_adapter then
247+
chat.acp_connection = nil -- Ensure we reset this
248+
chat:change_adapter(selected_adapter, on_adapter_ready)
249+
else
250+
return on_adapter_ready()
251+
end
248252
end)
249253
end
250254

0 commit comments

Comments
 (0)