Skip to content

Commit 104f913

Browse files
authored
fix: #698 custom variables (#711)
Co-authored-by: Oli Morris <[email protected]>
1 parent 4f5b303 commit 104f913

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

lua/codecompanion/strategies/chat/variables/init.lua

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,36 @@ end
2626
---@param params? string
2727
---@return table
2828
local function resolve(chat, var_config, params)
29-
local splits = vim.split(var_config.callback, ".", { plain = true })
30-
local path = table.concat(splits, ".", 1, #splits - 1)
31-
local variable = splits[#splits]
32-
33-
local ok, module = pcall(require, "codecompanion." .. path .. "." .. variable)
34-
35-
local init = {
36-
Chat = chat,
37-
config = var_config,
38-
params = params,
39-
}
29+
if type(var_config.callback) == "string" then
30+
local splits = vim.split(var_config.callback, ".", { plain = true })
31+
local path = table.concat(splits, ".", 1, #splits - 1)
32+
local variable = splits[#splits]
33+
34+
local ok, module = pcall(require, "codecompanion." .. path .. "." .. variable)
35+
36+
local init = {
37+
Chat = chat,
38+
config = var_config,
39+
params = params,
40+
}
41+
42+
-- User is using a custom callback
43+
if not ok then
44+
log:trace("Calling variable: %s", path .. "." .. variable)
45+
return require(path .. "." .. variable).new(init):output()
46+
end
4047

41-
-- User is using a custom callback
42-
if not ok then
4348
log:trace("Calling variable: %s", path .. "." .. variable)
44-
return require(path .. "." .. variable).new(init):output()
49+
return module.new(init):output()
4550
end
4651

47-
log:trace("Calling variable: %s", path .. "." .. variable)
48-
return module.new(init):output()
52+
return require("codecompanion.strategies.chat.variables.user")
53+
.new({
54+
Chat = chat,
55+
config = var_config,
56+
params = params,
57+
})
58+
:output()
4959
end
5060

5161
---@class CodeCompanion.Variables
@@ -92,6 +102,8 @@ function Variables:parse(chat, message)
92102
local var_config = self.vars[var]
93103
log:debug("Variable found: %s", var)
94104

105+
var_config["name"] = var
106+
95107
if (var_config.opts and var_config.opts.contains_code) and not config.can_send_code() then
96108
log:warn("Sending of code has been disabled")
97109
goto continue
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local buf_utils = require("codecompanion.utils.buffers")
2+
local config = require("codecompanion.config")
3+
4+
---@class CodeCompanion.Variable.User: CodeCompanion.Variable
5+
local Variable = {}
6+
7+
---@param args CodeCompanion.VariableArgs
8+
function Variable.new(args)
9+
local self = setmetatable({
10+
Chat = args.Chat,
11+
config = args.config,
12+
params = args.params,
13+
}, { __index = Variable })
14+
15+
return self
16+
end
17+
18+
---Return the user's custom variable
19+
---@return nil
20+
function Variable:output()
21+
local id = "<var>" .. self.config.name .. "</var>"
22+
23+
self.Chat:add_message({
24+
role = config.constants.USER_ROLE,
25+
content = self.config.callback(),
26+
}, { reference = id, tag = "variable", visible = false })
27+
28+
self.Chat.References:add({
29+
bufnr = self.Chat.bufnr,
30+
id = id,
31+
source = "codecompanion.strategies.chat.variables.user",
32+
})
33+
end
34+
35+
return Variable

0 commit comments

Comments
 (0)