Skip to content

Commit cd79d07

Browse files
authored
fix(nvim): Handle relative paths for codecompanion tools (#267)
* fix(nvim): Handle relative paths for query tool results * fix(nvim): Fix relative paths for codecompanion tools * fix(nvim): Clarify relative path descriptions in tools * doc
1 parent 55b4d03 commit cd79d07

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

lua/vectorcode/integrations/codecompanion/common.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ return {
4242
end
4343
return job_runner
4444
end,
45+
46+
---Convert `path` to a relative path if it's within the current project.
47+
---When `base` is `nil`, this function will attempt to find a project root
48+
---or use `cwd`.
49+
---@param path string
50+
---@param base? string
51+
---@return string
52+
cleanup_path = function(path, base)
53+
base = base or vim.fs.root(0, { ".vectorcode", ".git" }) or vim.uv.cwd() or "."
54+
return vim.fs.relpath(base, path) or path
55+
end,
4556
}

lua/vectorcode/integrations/codecompanion/files_ls_tool.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ return function(opts)
5757
type = "function",
5858
["function"] = {
5959
name = tool_name,
60-
description = "Retrieve a list of files that have been added to the database for a given project.",
60+
description = [[
61+
Retrieve a list of files that have been added to the database for a given project.
62+
**ABSOLUTE PATHS** in the results indicate that the files are OUTSIDE of the current working directories and you can **ONLY** access them via the VectorCode tools.
63+
**RELATIVE PATHS** in the results indicate that the files are INSIDE the current project. You can use VectorCode tools or any other tools that the user provided to interact with them. They are relative to the project root.
64+
]],
6165
parameters = {
6266
type = "object",
6367
properties = {
@@ -73,7 +77,7 @@ return function(opts)
7377
---@param tools CodeCompanion.Tools
7478
---@param stdout string[][]
7579
success = function(_, tools, _, stdout)
76-
stdout = stdout[1]
80+
stdout = stdout[#stdout]
7781
local user_message
7882
for i, col in ipairs(stdout) do
7983
if i == 1 then
@@ -84,7 +88,7 @@ return function(opts)
8488
end
8589
tools.chat:add_tool_output(
8690
tools.tool,
87-
string.format("<path>%s</path>", col),
91+
string.format("<path>%s</path>", cc_common.cleanup_path(col)),
8892
user_message
8993
)
9094
end

lua/vectorcode/integrations/codecompanion/ls_tool.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe
7676
---@param tools CodeCompanion.Tools
7777
---@param stdout VectorCode.LsResult[][]
7878
success = function(_, tools, _, stdout)
79-
stdout = stdout[1]
79+
stdout = stdout[#stdout]
8080
local user_message
8181
for i, col in ipairs(stdout) do
8282
if i == 1 then

lua/vectorcode/integrations/codecompanion/query_tool.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ When summarising the code, pay extra attention on information related to the que
315315
end
316316
end
317317

318+
---@param results VectorCode.QueryResult[]
319+
---@return VectorCode.QueryResult[]
320+
local function cleanup_paths(results)
321+
local cwd = vim.fs.root(0, { ".vectorcode", ".git" }) or vim.uv.cwd()
322+
if cwd then
323+
results = vim
324+
.iter(results)
325+
:map(
326+
---@param res VectorCode.QueryResult
327+
function(res)
328+
res.path = cc_common.cleanup_path(res.path)
329+
return res
330+
end
331+
)
332+
:totable()
333+
end
334+
return results
335+
end
336+
318337
---@param opts VectorCode.CodeCompanion.QueryToolOpts?
319338
---@return CodeCompanion.Tools.Tool
320339
return check_cli_wrap(function(opts)
@@ -436,6 +455,8 @@ return check_cli_wrap(function(opts)
436455
result = filter_results(result, tools.chat)
437456
end
438457

458+
result = cleanup_paths(result)
459+
439460
local max_result = #result
440461
if opts.max_num > 0 then
441462
max_result = math.min(tonumber(opts.max_num) or 1, max_result)
@@ -477,6 +498,8 @@ Make use of the line numbers (NOT THE XML TAGS) when you're quoting the source c
477498
Include one single command call for VectorCode each time.
478499
You may include multiple keywords in the command.
479500
**The project root option MUST be a valid path on the filesystem. It can only be one of the results from the `vectorcode_ls` tool or from user input**
501+
**ABSOLUTE PATHS** in the results indicate that the files are OUTSIDE of the current working directories and you can **ONLY** access them via the VectorCode tools.
502+
**RELATIVE PATHS** in the results indicate that the files are INSIDE the current project. You can use VectorCode tools or any other tools that the user provided to interact with them. They are relative to the project root.
480503
]],
481504
parameters = {
482505
type = "object",
@@ -572,7 +595,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
572595
---@param cmd QueryToolArgs
573596
---@param stdout VectorCode.CodeCompanion.QueryToolResult[]
574597
success = function(self, tools, cmd, stdout)
575-
stdout = stdout[1]
598+
stdout = stdout[#stdout]
576599
logger.info(
577600
("CodeCompanion tool with command %s finished."):format(vim.inspect(cmd))
578601
)

lua/vectorcode/integrations/codecompanion/vectorise_tool.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv
147147
---@param cmd VectoriseToolArgs
148148
---@param stdout VectorCode.VectoriseResult[]
149149
success = function(self, tools, cmd, stdout)
150-
stdout = stdout[1]
150+
stdout = stdout[#stdout]
151151
tools.chat:add_tool_output(
152152
self,
153153
string.format(

0 commit comments

Comments
 (0)