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
216 lines (199 loc) · 7.21 KB
/
Copy pathcodecompanion.lua
File metadata and controls
216 lines (199 loc) · 7.21 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
---@alias tool_opts {max_num: integer?, default_num: integer?, include_stderr: boolean?}
local check_cli_wrap = require("vectorcode.config").check_cli_wrap
---@param opts tool_opts?
---@return CodeCompanion.Agent.Tool
local make_tool = check_cli_wrap(function(opts)
opts = vim.tbl_deep_extend(
"force",
{ max_num = -1, default_num = 10, include_stderr = false },
opts or {}
)
local xml2lua = require("codecompanion.utils.xml.xml2lua")
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 = { { "vectorcode", "query", "--pipe" } },
handlers = {
---@param self CodeCompanion.Agent
setup = function(self)
local tool = self.tool
local n_query = tool.request.action.count or opts.default_num
local keywords = tool.request.action.query
if type(keywords) == "string" then
keywords = { keywords }
end
vim.list_extend(tool.cmds[1], { "-n", tostring(n_query) })
vim.list_extend(tool.cmds[1], keywords)
if not opts.include_stderr then
vim.list_extend(tool.cmds[1], { "--no_stderr" })
end
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)
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**
- seperate 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
%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 = {
error = function(self, cmd, stderr)
if type(stderr) == "table" then
stderr = table.concat(vim.iter(stderr):flatten(math.huge):totable(), "\n")
end
if opts.include_stderr then
vim.notify(
stderr,
vim.log.levels.ERROR,
require("vectorcode.config").notify_opts
)
self.chat:add_message({
role = "user",
content = string.format(
[[After the VectorCode tool completed, there was an error:
<error>
%s
</error>
]],
stderr
),
}, { visible = false })
self.chat:add_message({
role = "user",
content = "I've shared the error message from the VectorCode tool with you.\n",
}, { visible = false })
else
self.chat:add_message({
role = "user",
content = "There was an error in the execution of the tool, but the user chose to ignore it.",
})
end
end,
success = function(self, cmd, stdout)
local retrievals = {}
if type(stdout) == "table" then
retrievals = vim.json.decode(
vim.iter(stdout):flatten(math.huge):totable()[1],
{ array = true, object = true }
)
end
for i, file in ipairs(retrievals) do
if opts.max_num < 0 or i <= opts.max_num then
self.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
self.chat:add_message({
role = "user",
content = "I've shared the content from the VectorCode tool with you.\n",
}, { visible = false })
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 ok, vc_config = pcall(require, "vectorcode.config")
if ok then
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,
}
end),
make_tool = make_tool,
},
}