Skip to content

Commit 8fafa76

Browse files
committed
feat(nvim)!: switch to new native tool calling schema for codecompanion
1 parent 96ae4bc commit 8fafa76

File tree

1 file changed

+68
-71
lines changed

1 file changed

+68
-71
lines changed

lua/vectorcode/integrations/codecompanion.lua

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,16 @@ local make_tool = check_cli_wrap(function(opts)
8282
type(cb) == "function",
8383
"Please upgrade CodeCompanion.nvim to at least 13.5.0"
8484
)
85-
assert(vim.list_contains({ "ls", "query" }, action.command))
85+
if not (vim.list_contains({ "ls", "query" }, action.command)) then
86+
if action.options.query ~= nil then
87+
action.command = "query"
88+
else
89+
return {
90+
status = "error",
91+
data = "Need to specify the command (`ls` or `query`).",
92+
}
93+
end
94+
end
8695
if opts.auto_submit[action.command] then
8796
vim.schedule(function()
8897
vim.api.nvim_input("<Esc>")
@@ -101,7 +110,6 @@ local make_tool = check_cli_wrap(function(opts)
101110
and vim.uv.fs_stat(action.options.project_root).type == "directory"
102111
then
103112
vim.list_extend(args, { "--project_root", action.options.project_root })
104-
vim.list_extend(args, { "--absolute" })
105113
else
106114
agent.chat:add_message(
107115
{ role = "user", content = "INVALID PROJECT ROOT! USE THE LS COMMAND!" },
@@ -122,6 +130,7 @@ local make_tool = check_cli_wrap(function(opts)
122130
vim.list_extend(args, existing_files)
123131
end
124132
end
133+
vim.list_extend(args, { "--absolute" })
125134

126135
job_runner.run_async(args, function(result, error)
127136
vim.schedule(function()
@@ -164,56 +173,47 @@ local make_tool = check_cli_wrap(function(opts)
164173
end,
165174
},
166175
schema = {
167-
{
168-
tool = {
169-
_attr = { name = "vectorcode" },
170-
action = {
171-
command = "query",
172-
options = {
173-
query = { "keyword1", "keyword2" },
174-
count = 5,
176+
type = "function",
177+
["function"] = {
178+
name = "vectorcode",
179+
description = "Retrieves code documents using semantic search or lists indexed projects",
180+
parameters = {
181+
type = "object",
182+
properties = {
183+
command = {
184+
type = "string",
185+
enum = { "query", "ls" },
186+
description = "Action to perform: 'query' for semantic search or 'ls' to list projects",
175187
},
176-
},
177-
},
178-
},
179-
{
180-
tool = {
181-
_attr = { name = "vectorcode" },
182-
action = {
183-
command = "query",
184188
options = {
185-
query = { "keyword1" },
186-
count = 2,
189+
type = "object",
190+
properties = {
191+
query = {
192+
type = "array",
193+
items = { type = "string" },
194+
description = "Search keywords (required for 'query' command). Orthogornal keywords should be in distinct strings.",
195+
},
196+
count = {
197+
type = "integer",
198+
description = "Number of documents to retrieve, must be positive",
199+
},
200+
project_root = {
201+
type = "string",
202+
description = "Project path to search within (must be from 'ls' results)",
203+
},
204+
},
205+
required = { "query" },
206+
additionalProperties = false,
187207
},
188208
},
209+
required = { "command" },
210+
additionalProperties = false,
189211
},
190-
},
191-
{
192-
tool = {
193-
_attr = { name = "vectorcode" },
194-
action = {
195-
command = "query",
196-
options = {
197-
query = { "keyword1" },
198-
count = 3,
199-
project_root = "path/to/other/project",
200-
},
201-
},
202-
},
203-
},
204-
{
205-
tool = {
206-
_attr = { name = "vectorcode" },
207-
action = {
208-
command = "ls",
209-
},
210-
},
212+
strict = true,
211213
},
212214
},
213-
system_prompt = function(schema, xml2lua)
215+
system_prompt = function()
214216
local guidelines = {
215-
" - Ensure XML is **valid and follows the schema**",
216-
" - Make sure the tools xml block is **surrounded by ```xml**",
217217
" - 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",
218218
" - 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",
219219
" - Include one single command call for VectorCode each time. You may include multiple keywords in the command",
@@ -260,40 +260,37 @@ local make_tool = check_cli_wrap(function(opts)
260260
261261
1. **Purpose**: This gives you the ability to access the repository to find information that you may need to assist the user.
262262
263-
2. **Usage**: Return an XML markdown code block that retrieves relevant documents corresponding to the generated query.
264-
265-
3. **Key Points**:
263+
2. **Key Points**:
266264
%s
267265
268-
4. **Actions**:
269-
270-
a) **Query for 5 documents using 2 keywords: `keyword1` and `keyword2`**:
271-
272-
```xml
273-
%s
266+
3. Example Tool Call
267+
**Querying a project and retrieve the 10 most relevant files with keywords "keyword1" and "keyword2"**
274268
```
275-
276-
b) **Query for 2 documents using one keyword: `keyword1`**:
277-
278-
```xml
279-
%s
269+
{
270+
"_attr": "vectorcode",
271+
{
272+
"action": "query",
273+
"options": {
274+
"query": ["keyword1", "keyword2"],
275+
"count": 10,
276+
"project_root": "/path/to/project",
277+
}
278+
}
279+
}
280280
```
281-
c) **Query for 3 documents using one keyword: `keyword1` in a different project located at `path/to/other/project` (relative to current working directory)**:
282-
```xml
283-
%s
281+
282+
**Listing available projects**
284283
```
285-
d) **Get all indexed project**
286-
```xml
287-
%s
284+
{
285+
"_attr": "vectorcode",
286+
{
287+
"action": "ls"
288+
}
289+
}
288290
```
289291
290-
Remember:
291-
- Minimize explanations unless prompted. Focus on generating correct XML.]],
292-
table.concat(guidelines, "\n"),
293-
xml2lua.toXml({ tools = { schema[1] } }),
294-
xml2lua.toXml({ tools = { schema[2] } }),
295-
xml2lua.toXml({ tools = { schema[3] } }),
296-
xml2lua.toXml({ tools = { schema[4] } })
292+
]],
293+
table.concat(guidelines, "\n")
297294
)
298295
end,
299296
output = {

0 commit comments

Comments
 (0)