Skip to content

Commit e117663

Browse files
committed
fix(chat): hide tool context for ACP chats
1 parent f70759e commit e117663

3 files changed

Lines changed: 112 additions & 2 deletions

File tree

lua/codecompanion/interactions/chat/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ function Chat:change_adapter(adapter, cb)
719719
end
720720

721721
self:set_system_prompt()
722+
self.tool_registry:update_context_visibility()
722723
self:update_metadata()
723724
self:apply_settings()
724725
fire()

lua/codecompanion/interactions/chat/tool_registry.lua

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ local function group_id(name)
3232
return fmt("<group>%s</group>", name)
3333
end
3434

35+
---Determine if tool context should be visible for a chat adapter
36+
---@param chat CodeCompanion.Chat The chat buffer
37+
---@return boolean
38+
local function tool_context_visible(chat)
39+
return not (chat.adapter and chat.adapter.type == "acp")
40+
end
41+
3542
---@class CodeCompanion.Chat.ToolsArgs
3643
---@field chat CodeCompanion.Chat
3744
---@field ctx CodeCompanion.SystemPrompt.Context
@@ -56,6 +63,12 @@ end
5663
---@param opts? table Optional parameters for the context_item
5764
---@return nil
5865
local function add_context(chat, id, opts)
66+
opts = opts or {}
67+
if opts.visible == nil then
68+
opts.visible = tool_context_visible(chat)
69+
opts._tool_context_auto_visibility = true
70+
end
71+
5972
chat.context:add({
6073
source = "tool",
6174
name = "tool",
@@ -121,7 +134,8 @@ end
121134
function ToolRegistry:add_single_tool(tool, opts)
122135
opts = opts or {}
123136
if opts.visible == nil then
124-
opts.visible = true
137+
opts.visible = tool_context_visible(self.chat)
138+
opts._tool_context_auto_visibility = true
125139
end
126140

127141
local tool_config = opts.config or config.interactions.chat.tools[tool]
@@ -166,6 +180,25 @@ function ToolRegistry:add_single_tool(tool, opts)
166180
return self
167181
end
168182

183+
---Update automatically managed tool context visibility for the current adapter
184+
---@return CodeCompanion.Chat.ToolRegistry
185+
function ToolRegistry:update_context_visibility()
186+
local visible = tool_context_visible(self.chat)
187+
188+
for _, context in ipairs(self.chat.context_items or {}) do
189+
if context.source == "tool" and context.opts and context.opts._tool_context_auto_visibility then
190+
context.opts.visible = visible
191+
end
192+
end
193+
194+
if self.chat.bufnr and vim.api.nvim_buf_is_valid(self.chat.bufnr) then
195+
self.chat.context:clear_rendered()
196+
self.chat.context:render()
197+
end
198+
199+
return self
200+
end
201+
169202
---Add tools from a group to the chat buffer
170203
---@param group string The name of the group
171204
---@param opts? { config: table }
@@ -216,7 +249,7 @@ function ToolRegistry:add_group(group, opts)
216249
for _, tool in ipairs(group_config.tools) do
217250
local tool_cfg = tools_config[tool]
218251
if tool_cfg then
219-
if self:add_single_tool(tool, { config = tool_cfg, visible = not collapse_tools }) then
252+
if self:add_single_tool(tool, { config = tool_cfg, visible = collapse_tools and false or nil }) then
220253
table.insert(added_tools, tool)
221254
end
222255
end

tests/interactions/chat/tools/test_tool_registry.lua

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,82 @@ T["ToolRegistry"][":add"]["renders tool in the chat buffer"] = function()
6060
h.expect_contains("func", content)
6161
end
6262

63+
T["ToolRegistry"][":add"]["hides tool context for ACP chats"] = function()
64+
child.lua([[
65+
_G.chat = h.setup_chat_buffer({}, {
66+
name = "test_acp",
67+
config = {
68+
name = "test_acp",
69+
type = "acp",
70+
roles = { user = "user", assistant = "assistant" },
71+
handlers = {
72+
form_messages = function()
73+
return {}
74+
end,
75+
},
76+
},
77+
})
78+
_G.chat.tool_registry:add("func")
79+
_G.chat.context:render()
80+
_G.buf_lines = h.get_buf_lines(_G.chat.bufnr)
81+
]])
82+
83+
local lines = child.lua_get([[_G.buf_lines]])
84+
local content = table.concat(lines, "\n")
85+
86+
h.expect_tbl_contains("func", child.lua_get([[_G.chat.tool_registry.in_use]]))
87+
h.eq(nil, content:find("func", 1, true))
88+
end
89+
90+
T["ToolRegistry"][":add"]["hides tool group context for ACP chats"] = function()
91+
child.lua([[
92+
_G.chat = h.setup_chat_buffer({}, {
93+
name = "test_acp",
94+
config = {
95+
name = "test_acp",
96+
type = "acp",
97+
roles = { user = "user", assistant = "assistant" },
98+
handlers = {
99+
form_messages = function()
100+
return {}
101+
end,
102+
},
103+
},
104+
})
105+
_G.chat.tool_registry:add("senior_dev")
106+
_G.chat.context:render()
107+
_G.buf_lines = h.get_buf_lines(_G.chat.bufnr)
108+
]])
109+
110+
local lines = child.lua_get([[_G.buf_lines]])
111+
local content = table.concat(lines, "\n")
112+
113+
h.expect_tbl_contains("func", child.lua_get([[_G.chat.tool_registry.in_use]]))
114+
h.expect_tbl_contains("cmd", child.lua_get([[_G.chat.tool_registry.in_use]]))
115+
h.eq(nil, content:find("senior_dev", 1, true))
116+
h.eq(nil, content:find("func", 1, true))
117+
h.eq(nil, content:find("cmd", 1, true))
118+
end
119+
120+
T["ToolRegistry"][":add"]["updates tool context visibility when switching to ACP"] = function()
121+
child.lua([[
122+
_G.chat.tool_registry:add("senior_dev")
123+
_G.http_visible = _G.chat.context_items[1].opts.visible
124+
125+
_G.chat.adapter = { name = "test_acp", type = "acp" }
126+
_G.chat.tool_registry:update_context_visibility()
127+
_G.acp_visible = _G.chat.context_items[1].opts.visible
128+
129+
_G.chat.adapter = { name = "test_adapter", type = "http" }
130+
_G.chat.tool_registry:update_context_visibility()
131+
_G.http_again_visible = _G.chat.context_items[1].opts.visible
132+
]])
133+
134+
h.eq(true, child.lua_get([[_G.http_visible]]))
135+
h.eq(false, child.lua_get([[_G.acp_visible]]))
136+
h.eq(true, child.lua_get([[_G.http_again_visible]]))
137+
end
138+
63139
T["ToolRegistry"][":add"]["returns nil for unknown name"] = function()
64140
local result = child.lua_get([[_G.chat.tool_registry:add("nonexistent_tool_xyz")]])
65141

0 commit comments

Comments
 (0)