Skip to content

Commit 9724e13

Browse files
authored
fix(chat): add tools from prompt library slash commands (#3012)
1 parent fe792b3 commit 9724e13

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

doc/usage/prompt-library.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ Where `docs` is the `alias` of the prompt.
2222

2323
If your prompt library entries have an `alias` defined then you can invoke them using a slash command. In the cmd line `:CodeCompanion /<alias>` or `/<alias>` if you're in the chat buffer.
2424

25+
When invoked this way, any tools declared on the prompt are added to the current chat buffer before the prompt content is inserted.
26+

lua/codecompanion/interactions/chat/slash_commands/init.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ local log = require("codecompanion.utils.log")
55

66
local api = vim.api
77

8+
---Add prompt-declared tools to the current chat
9+
---@param chat CodeCompanion.Chat
10+
---@param prompt_config table
11+
local function add_prompt_tools(chat, prompt_config)
12+
local prompt_tools = prompt_config.tools or {}
13+
local tools_config = chat.tools.tools_config
14+
15+
vim.iter(prompt_tools):each(function(tool_name)
16+
if tools_config.groups and tools_config.groups[tool_name] then
17+
chat.tool_registry:add_group(tool_name, { config = tools_config })
18+
elseif tools_config[tool_name] then
19+
chat.tool_registry:add_single_tool(tool_name, { config = tools_config[tool_name] })
20+
end
21+
end)
22+
end
23+
824
---Resolve a path to the correct module
925
---@param path string The module or file path
1026
---@return table|nil
@@ -176,6 +192,8 @@ end
176192
---@return nil
177193
function SlashCommands.run(selected, chat)
178194
if selected.from_prompt_library then
195+
add_prompt_tools(chat, selected.config)
196+
179197
local context = selected.config.context
180198
if context then
181199
interactions.add_context(selected.config, chat)

tests/interactions/chat/test_context.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,43 @@ T["Context"]["Cannot be added twice with the same id"] = function()
6363
h.eq(1, child.lua_get([[#_G.chat.context_items]]), "Should only have 1 context item")
6464
end
6565

66+
T["Context"]["Prompt-library slash commands add a single tool"] = function()
67+
child.lua([[
68+
local SlashCommands = require("codecompanion.interactions.chat.slash_commands")
69+
70+
SlashCommands.run({
71+
from_prompt_library = true,
72+
config = {
73+
tools = { "weather" },
74+
prompts = {},
75+
},
76+
context = {},
77+
}, _G.chat)
78+
]])
79+
80+
local in_use = child.lua_get([[_G.chat.tool_registry.in_use]])
81+
h.expect_tbl_contains("weather", in_use)
82+
end
83+
84+
T["Context"]["Prompt-library slash commands add a tool group"] = function()
85+
child.lua([[
86+
local SlashCommands = require("codecompanion.interactions.chat.slash_commands")
87+
88+
SlashCommands.run({
89+
from_prompt_library = true,
90+
config = {
91+
tools = { "senior_dev" },
92+
prompts = {},
93+
},
94+
context = {},
95+
}, _G.chat)
96+
]])
97+
98+
local in_use = child.lua_get([[_G.chat.tool_registry.in_use]])
99+
h.expect_tbl_contains("func", in_use)
100+
h.expect_tbl_contains("cmd", in_use)
101+
end
102+
66103
T["Context"]["Can be deleted"] = function()
67104
child.lua([[
68105
-- Add context_items

0 commit comments

Comments
 (0)