-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathcompletion.lua
123 lines (112 loc) · 2.96 KB
/
completion.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
local SlashCommands = require("codecompanion.strategies.chat.slash_commands")
local config = require("codecompanion.config")
local strategy = require("codecompanion.strategies")
local trigger = {
agents = "@",
variables = "#",
slash_commands = "/",
}
local M = {}
---Return the slash commands to be used for completion
---@return table
function M.slash_commands()
local slash_commands = vim
.iter(config.strategies.chat.slash_commands)
:filter(function(name)
return name ~= "opts"
end)
:map(function(label, v)
return {
label = trigger.slash_commands .. label,
detail = v.description,
config = v,
type = "slash_command",
}
end)
:totable()
vim
.iter(pairs(config.prompt_library))
:filter(function(_, v)
return v.opts and v.opts.is_slash_cmd and v.strategy == "chat"
end)
:each(function(_, v)
table.insert(slash_commands, {
label = "/" .. v.opts.short_name,
detail = v.description,
config = v,
type = "slash_command",
from_prompt_library = true,
})
end)
return slash_commands
end
---Execute selected slash command
---@param selected table The selected item from the completion menu
---@param chat CodeCompanion.Chat
---@return nil
function M.slash_commands_execute(selected, chat)
if selected.from_prompt_library then
local prompts = strategy.evaluate_prompts(selected.config.prompts, selected.context)
vim.iter(prompts):each(function(prompt)
if prompt.role == config.constants.SYSTEM_ROLE then
chat:add_message(prompt, { visible = false })
elseif prompt.role == config.constants.USER_ROLE then
chat:add_buf_message(prompt)
end
end)
else
SlashCommands:execute(selected, chat)
end
end
---Return the tools to be used for completion
---@return table
function M.tools()
-- Add groups
local items = vim
.iter(config.strategies.chat.tools.groups)
:filter(function(label)
return label ~= "tools"
end)
:map(function(label, v)
return {
label = trigger.agents .. label,
name = label,
type = "agent",
callback = v.callback,
detail = v.description,
}
end)
:totable()
-- Add tools
vim
.iter(config.strategies.chat.tools)
:filter(function(label)
return label ~= "opts" and label ~= "groups"
end)
:each(function(label, v)
table.insert(items, {
label = trigger.agents .. label,
name = label,
type = "tool",
callback = v.callback,
detail = v.description,
})
end)
return items
end
---Return the variables to be used for completion
---@return table
function M.variables()
local variables = config.strategies.chat.variables
return vim
.iter(variables)
:map(function(label, data)
return {
label = trigger.variables .. label,
detail = data.description,
type = "variable",
}
end)
:totable()
end
return M