Skip to content

Commit 66533e8

Browse files
authored
fix(acp): slash commands in chat buffer (#2954)
Closes #2948
1 parent af6f43c commit 66533e8

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ local M = {}
99
local api = vim.api
1010
local fmt = string.format
1111

12-
---Create a new ACP connection for the given chat
12+
---Establishes the connection, authenticates, creates a session and links the buffer
1313
---@param chat CodeCompanion.Chat The chat instance
1414
---@return boolean
1515
function M.create_acp_connection(chat)
1616
local ACPHandler = require("codecompanion.interactions.chat.acp.handler")
1717
local handler = ACPHandler.new(chat)
18-
return handler:ensure_connection()
18+
19+
if not handler:ensure_connection() then
20+
return false
21+
end
22+
23+
return handler:ensure_session()
1924
end
2025

2126
---Format the given role without any separator

lua/codecompanion/interactions/chat/init.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,7 @@ function Chat:change_adapter(adapter)
731731
self.ui.adapter = self.adapter
732732

733733
if self.adapter.type == "acp" then
734-
-- Ensure the ACP connection and session are created so users can select a model
735734
helpers.create_acp_connection(self)
736-
if self.acp_connection then
737-
self.acp_connection:ensure_session()
738-
739-
local acp_commands = require("codecompanion.interactions.chat.acp.commands")
740-
acp_commands.link_buffer_to_session(self.bufnr, self.acp_connection.session_id)
741-
end
742-
743735
helpers.remove_mcp_tools(self)
744736
end
745737

lua/codecompanion/interactions/chat/slash_commands/builtin/command.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ function SlashCommand:execute()
5454
local command = Chat.adapter.commands[selected]
5555
if command then
5656
Chat.adapter.commands.selected = command
57-
Chat.acp_connection = nil
57+
58+
-- Disconnect the existing ACP process before starting a new one
59+
if Chat.acp_connection then
60+
pcall(function()
61+
Chat.acp_connection:disconnect()
62+
end)
63+
Chat.acp_connection = nil
64+
end
65+
5866
require("codecompanion.interactions.chat.helpers").create_acp_connection(Chat)
5967
utils.notify(string.format("Switched to `%s` command", selected))
6068
end

0 commit comments

Comments
 (0)