forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecompanion.lua
More file actions
349 lines (331 loc) · 11.1 KB
/
Copy pathcodecompanion.lua
File metadata and controls
349 lines (331 loc) · 11.1 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
---@module "codecompanion"
---@class VectorCode.CodeCompanion.ToolOpts
---@field max_num integer?
---@field default_num integer?
---@field include_stderr boolean?
---@field use_lsp boolean?
---@field auto_submit table<string, boolean>?
local vc_config = require("vectorcode.config")
local check_cli_wrap = vc_config.check_cli_wrap
local notify_opts = vc_config.notify_opts
---@param t table|string
---@return string
local function flatten_table_to_string(t)
if type(t) == "string" then
return t
end
return table.concat(vim.iter(t):flatten(math.huge):totable(), "\n")
end
local job_runner = nil
---@param use_lsp boolean
local function initialise_runner(use_lsp)
if job_runner == nil then
if use_lsp then
job_runner = require("vectorcode.jobrunner.lsp")
end
if job_runner == nil then
job_runner = require("vectorcode.jobrunner.cmd")
if use_lsp then
vim.schedule_wrap(vim.notify)(
"Failed to initialise the LSP runner. Falling back to cmd runner.",
vim.log.levels.WARN,
notify_opts
)
end
end
end
end
---@param opts VectorCode.CodeCompanion.ToolOpts?
---@return CodeCompanion.Agent.Tool
local make_tool = check_cli_wrap(function(opts)
if opts == nil or opts.use_lsp == nil then
opts = vim.tbl_deep_extend(
"force",
opts or {},
{ use_lsp = vc_config.get_user_config().async_backend == "lsp" }
)
end
opts = vim.tbl_deep_extend("force", {
max_num = -1,
default_num = 10,
include_stderr = false,
use_lsp = false,
auto_submit = { ls = false, query = false },
}, opts or {})
local capping_message = ""
if opts.max_num > 0 then
capping_message = (" - Request for at most %d documents"):format(opts.max_num)
end
return {
name = "vectorcode",
cmds = {
---@param agent CodeCompanion.Agent
---@param action table
---@param input table
---@return nil|{ status: string, msg: string }
function(agent, action, input, cb)
initialise_runner(opts.use_lsp)
assert(job_runner ~= nil)
assert(
type(cb) == "function",
"Please upgrade CodeCompanion.nvim to at least 13.5.0"
)
assert(vim.list_contains({ "ls", "query" }, action.command))
if opts.auto_submit[action.command] then
vim.schedule(function()
vim.api.nvim_input("<Esc>")
agent.chat.ui:lock_buf()
end)
end
if action.command == "query" then
local args = { "query", "--pipe", "-n", tostring(action.options.count) }
if type(action.options.query) == "string" then
action.options.query = { action.options.query }
end
vim.list_extend(args, action.options.query)
if action.options.project_root ~= nil then
if
vim.uv.fs_stat(action.options.project_root) ~= nil
and vim.uv.fs_stat(action.options.project_root).type == "directory"
then
vim.list_extend(args, { "--project_root", action.options.project_root })
vim.list_extend(args, { "--absolute" })
else
agent.chat:add_message(
{ role = "user", content = "INVALID PROJECT ROOT! USE THE LS COMMAND!" },
{ visible = false }
)
end
end
job_runner.run_async(args, function(result, error)
vim.schedule(function()
if opts.auto_submit[action.command] then
agent.chat.ui:unlock_buf()
end
end)
if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.Result[]
cb({ status = "success", data = result })
else
if type(error) == "table" then
error = flatten_table_to_string(error)
end
cb({
status = "error",
data = error,
})
end
end, agent.chat.bufnr)
elseif action.command == "ls" then
job_runner.run_async({ "ls", "--pipe" }, function(result, error)
vim.schedule(function()
if opts.auto_submit[action.command] then
agent.chat.ui:unlock_buf()
end
end)
if vim.islist(result) and #result > 0 then
cb({ status = "success", data = result })
else
if type(error) == "table" then
error = flatten_table_to_string(error)
end
cb({
status = "error",
data = error,
})
end
end, agent.chat.bufnr)
end
end,
},
schema = {
{
tool = {
_attr = { name = "vectorcode" },
action = {
command = "query",
options = {
query = { "keyword1", "keyword2" },
count = 5,
},
},
},
},
{
tool = {
_attr = { name = "vectorcode" },
action = {
command = "query",
options = {
query = { "keyword1" },
count = 2,
},
},
},
},
{
tool = {
_attr = { name = "vectorcode" },
action = {
command = "query",
options = {
query = { "keyword1" },
count = 3,
project_root = "path/to/other/project",
},
},
},
},
{
tool = {
_attr = { name = "vectorcode" },
action = {
command = "ls",
},
},
},
},
system_prompt = function(schema, xml2lua)
local guidelines = {
" - Ensure XML is **valid and follows the schema**",
" - Make sure the tools xml block is **surrounded by ```xml**",
" - The path of a retrieved file will be wrapped in `<path>` and `</path>` tags. Its content will be right after the `</path>` tag, wrapped by `<content>` and `</content>` tags",
" - If you used the tool, tell users that they may need to wait for the results and there will be a virtual text indicator showing the tool is still running",
" - Include one single command call for VectorCode each time. You may include multiple keywords in the command",
" - VectorCode is the name of this tool. Do not include it in the query unless the user explicitly asks",
" - Use the `ls` command to retrieve a list of indexed project and pick one that may be relevant, unless the user explicitly mentioned 'this project' (or in other equivalent expressions)",
" - **The project root option MUST be a valid path on the filesystem. It can only be one of the results from the `ls` command or from user input**",
capping_message,
(" - If the user did not specify how many documents to retrieve, **start with %d documents**"):format(
opts.default_num
),
" - If you decide to call VectorCode tool, do not output anything else. Once you have the results, provide answers based on the results and let the user decide whether to run the tool again",
}
vim.list_extend(
guidelines,
vim.tbl_map(function(line)
return " - " .. line
end, require("vectorcode").prompts())
)
return string.format(
[[### VectorCode, a repository indexing and query tool.
1. **Purpose**: This gives you the ability to access the repository to find information that you may need to assist the user.
2. **Usage**: Return an XML markdown code block that retrieves relevant documents corresponding to the generated query.
3. **Key Points**:
%s
4. **Actions**:
a) **Query for 5 documents using 2 keywords: `keyword1` and `keyword2`**:
```xml
%s
```
b) **Query for 2 documents using one keyword: `keyword1`**:
```xml
%s
```
c) **Query for 3 documents using one keyword: `keyword1` in a different project located at `path/to/other/project` (relative to current working directory)**:
```xml
%s
```
d) **Get all indexed project**
```xml
%s
```
Remember:
- Minimize explanations unless prompted. Focus on generating correct XML.]],
table.concat(guidelines, "\n"),
xml2lua.toXml({ tools = { schema[1] } }),
xml2lua.toXml({ tools = { schema[2] } }),
xml2lua.toXml({ tools = { schema[3] } }),
xml2lua.toXml({ tools = { schema[4] } })
)
end,
output = {
---@param agent CodeCompanion.Agent
---@param cmd table
---@param stderr table|string
error = function(agent, cmd, stderr)
stderr = flatten_table_to_string(stderr)
agent.chat:add_message({
role = "user",
content = string.format(
"VectorCode tool failed with the following error:\n",
stderr
),
}, { visible = false })
end,
---@param agent CodeCompanion.Agent
---@param cmd table
---@param stdout table
success = function(agent, cmd, stdout)
stdout = stdout[1]
if cmd.command == "query" then
for i, file in pairs(stdout) do
if opts.max_num < 0 or i <= opts.max_num then
agent.chat:add_message({
role = "user",
content = string.format(
[[Here is a file the VectorCode tool retrieved:
<path>
%s
</path>
<content>
%s
</content>
]],
file.path,
file.document
),
}, { visible = false })
end
end
elseif cmd.command == "ls" then
for _, col in pairs(stdout) do
agent.chat:add_message({
role = "user",
content = string.format(
"<collection>%s</collection>",
col["project-root"]
),
}, { visible = false })
end
end
if opts.auto_submit[cmd.command] then
agent.chat:submit()
end
end,
},
}
end)
return {
chat = {
---@param component_cb (fun(result:VectorCode.Result):string)?
make_slash_command = check_cli_wrap(function(component_cb)
return {
description = "Add relevant files from the codebase.",
---@param chat CodeCompanion.Chat
callback = function(chat)
local codebase_prompt = ""
local vc_cache = vc_config.get_cacher_backend()
local bufnr = chat.context.bufnr
if not vc_cache.buf_is_registered(bufnr) then
return
end
codebase_prompt =
"The following are relevant files from the repository. Use them as extra context."
local query_result = vc_cache.make_prompt_component(bufnr, component_cb)
local id = tostring(query_result.count) .. " file(s) from codebase"
codebase_prompt = codebase_prompt .. query_result.content
chat:add_message(
{ content = codebase_prompt, role = "user" },
{ visible = false, id = id }
)
chat.references:add({
source = "VectorCode",
name = "VectorCode",
id = id,
})
end,
}
end),
make_tool = make_tool,
},
}