Skip to content

Commit 744757f

Browse files
committed
feat(nvim)!: switch to new native tool calling schema for codecompanion
1 parent fb3043d commit 744757f

File tree

1 file changed

+69
-71
lines changed

1 file changed

+69
-71
lines changed

lua/vectorcode/integrations/codecompanion.lua

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,16 @@ local make_tool = check_cli_wrap(function(opts)
8888
type(cb) == "function",
8989
"Please upgrade CodeCompanion.nvim to at least 13.5.0"
9090
)
91-
assert(vim.list_contains({ "ls", "query" }, action.command))
91+
if not (vim.list_contains({ "ls", "query" }, action.command)) then
92+
if action.options.query ~= nil then
93+
action.command = "query"
94+
else
95+
return {
96+
status = "error",
97+
data = "Need to specify the command (`ls` or `query`).",
98+
}
99+
end
100+
end
92101
if opts.auto_submit[action.command] then
93102
vim.schedule(function()
94103
vim.api.nvim_input("<Esc>")
@@ -107,7 +116,6 @@ local make_tool = check_cli_wrap(function(opts)
107116
and vim.uv.fs_stat(action.options.project_root).type == "directory"
108117
then
109118
vim.list_extend(args, { "--project_root", action.options.project_root })
110-
vim.list_extend(args, { "--absolute" })
111119
else
112120
agent.chat:add_message(
113121
{ role = "user", content = "INVALID PROJECT ROOT! USE THE LS COMMAND!" },
@@ -128,10 +136,12 @@ local make_tool = check_cli_wrap(function(opts)
128136
vim.list_extend(args, existing_files)
129137
end
130138
end
139+
vim.list_extend(args, { "--absolute" })
131140
logger.info(
132141
"CodeCompanion query tool called the runner with the following args: ",
133142
args
134143
)
144+
135145
job_runner.run_async(args, function(result, error)
136146
vim.schedule(function()
137147
if opts.auto_submit[action.command] then
@@ -173,56 +183,47 @@ local make_tool = check_cli_wrap(function(opts)
173183
end,
174184
},
175185
schema = {
176-
{
177-
tool = {
178-
_attr = { name = "vectorcode" },
179-
action = {
180-
command = "query",
181-
options = {
182-
query = { "keyword1", "keyword2" },
183-
count = 5,
186+
type = "function",
187+
["function"] = {
188+
name = "vectorcode",
189+
description = "Retrieves code documents using semantic search or lists indexed projects",
190+
parameters = {
191+
type = "object",
192+
properties = {
193+
command = {
194+
type = "string",
195+
enum = { "query", "ls" },
196+
description = "Action to perform: 'query' for semantic search or 'ls' to list projects",
184197
},
185-
},
186-
},
187-
},
188-
{
189-
tool = {
190-
_attr = { name = "vectorcode" },
191-
action = {
192-
command = "query",
193198
options = {
194-
query = { "keyword1" },
195-
count = 2,
199+
type = "object",
200+
properties = {
201+
query = {
202+
type = "array",
203+
items = { type = "string" },
204+
description = "Search keywords (required for 'query' command). Orthogornal keywords should be in distinct strings.",
205+
},
206+
count = {
207+
type = "integer",
208+
description = "Number of documents to retrieve, must be positive",
209+
},
210+
project_root = {
211+
type = "string",
212+
description = "Project path to search within (must be from 'ls' results)",
213+
},
214+
},
215+
required = { "query" },
216+
additionalProperties = false,
196217
},
197218
},
219+
required = { "command" },
220+
additionalProperties = false,
198221
},
199-
},
200-
{
201-
tool = {
202-
_attr = { name = "vectorcode" },
203-
action = {
204-
command = "query",
205-
options = {
206-
query = { "keyword1" },
207-
count = 3,
208-
project_root = "path/to/other/project",
209-
},
210-
},
211-
},
212-
},
213-
{
214-
tool = {
215-
_attr = { name = "vectorcode" },
216-
action = {
217-
command = "ls",
218-
},
219-
},
222+
strict = true,
220223
},
221224
},
222-
system_prompt = function(schema, xml2lua)
225+
system_prompt = function()
223226
local guidelines = {
224-
" - Ensure XML is **valid and follows the schema**",
225-
" - Make sure the tools xml block is **surrounded by ```xml**",
226227
" - 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",
227228
" - 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",
228229
" - Include one single command call for VectorCode each time. You may include multiple keywords in the command",
@@ -269,40 +270,37 @@ local make_tool = check_cli_wrap(function(opts)
269270
270271
1. **Purpose**: This gives you the ability to access the repository to find information that you may need to assist the user.
271272
272-
2. **Usage**: Return an XML markdown code block that retrieves relevant documents corresponding to the generated query.
273-
274-
3. **Key Points**:
273+
2. **Key Points**:
275274
%s
276275
277-
4. **Actions**:
278-
279-
a) **Query for 5 documents using 2 keywords: `keyword1` and `keyword2`**:
280-
281-
```xml
282-
%s
276+
3. Example Tool Call
277+
**Querying a project and retrieve the 10 most relevant files with keywords "keyword1" and "keyword2"**
283278
```
284-
285-
b) **Query for 2 documents using one keyword: `keyword1`**:
286-
287-
```xml
288-
%s
279+
{
280+
"_attr": "vectorcode",
281+
{
282+
"action": "query",
283+
"options": {
284+
"query": ["keyword1", "keyword2"],
285+
"count": 10,
286+
"project_root": "/path/to/project",
287+
}
288+
}
289+
}
289290
```
290-
c) **Query for 3 documents using one keyword: `keyword1` in a different project located at `path/to/other/project` (relative to current working directory)**:
291-
```xml
292-
%s
291+
292+
**Listing available projects**
293293
```
294-
d) **Get all indexed project**
295-
```xml
296-
%s
294+
{
295+
"_attr": "vectorcode",
296+
{
297+
"action": "ls"
298+
}
299+
}
297300
```
298301
299-
Remember:
300-
- Minimize explanations unless prompted. Focus on generating correct XML.]],
301-
table.concat(guidelines, "\n"),
302-
xml2lua.toXml({ tools = { schema[1] } }),
303-
xml2lua.toXml({ tools = { schema[2] } }),
304-
xml2lua.toXml({ tools = { schema[3] } }),
305-
xml2lua.toXml({ tools = { schema[4] } })
302+
]],
303+
table.concat(guidelines, "\n")
306304
)
307305
end,
308306
output = {

0 commit comments

Comments
 (0)