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
213 lines (198 loc) · 7.42 KB
/
Copy pathcodecompanion.lua
File metadata and controls
213 lines (198 loc) · 7.42 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
---@module "codecompanion"
---@alias tool_opts {max_num: integer?, default_num: integer?, include_stderr: boolean?, use_lsp: boolean}
local vc_config = require("vectorcode.config")
local check_cli_wrap = vc_config.check_cli_wrap
local notify_opts = vc_config.notify_opts
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 tool_opts?
---@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 },
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"
)
local args = { "query", "-n", tostring(action.count) }
if type(action.query) == "string" then
action.query = { action.query }
end
vim.list_extend(args, action.query)
job_runner.run_async(args, function(result, error)
if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.Result[]
cb({ status = "success", data = result })
else
cb({
status = "error",
data = table.concat(vim.iter(error):flatten(math.huge):totable(), "\n"),
})
end
end, agent.chat.bufnr)
end,
},
schema = {
{
tool = {
_attr = { name = "vectorcode" },
action = {
query = { "keyword1", "keyword2" },
count = 5,
},
},
},
{
tool = {
_attr = { name = "vectorcode" },
action = {
query = { "keyword1" },
count = 2,
},
},
},
},
system_prompt = function(schema, xml2lua)
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**:
- **Use at your discretion** when you feel you don't have enough information about the repository or project
- Ensure XML is **valid and follows the schema**
- **Don't escape** special characters
- Make sure the tools xml block is **surrounded by ```xml**
- separate phrases into distinct keywords when appropriate
- If a class, type or function has been imported from another file, this tool may be able to find its source. Add the name of the imported symbol to the query
- The embeddings are mostly generated from source code, so using keywords that may be present in source code may help with the retrieval
- 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
- Avoid retrieving one single file because the retrieval mechanism may not be very accurate
- When providing answers based on VectorCode results, try to give references such as paths to files and line ranges, unless you're told otherwise
- 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
- If the retrieval results do not contain the needed context, increase the file count so that the result will more likely contain the desired files
- If the returned paths are relative, they are relative to the root of the project directory
- Do not suggest edits to retrieved files that are outside of the current working directory, unless the user instructed otherwise
- If a query failed to retrieve desired results, a new attempt should use different keywords that are orthogonal to the previous ones but with similar meanings
%s
%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
```
Remember:
- Minimize explanations unless prompted. Focus on generating correct XML.]],
capping_message,
(" - If the user did not specify how many documents to retrieve, **start with %d documents**"):format(
opts.default_num
),
xml2lua.toXml({ tools = { schema[1] } }),
xml2lua.toXml({ tools = { schema[2] } })
)
end,
output = {
success = function(agent, cmd, stdout)
stdout = stdout[1]
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
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,
},
}