diff --git a/lua/vectorcode/integrations/codecompanion/common.lua b/lua/vectorcode/integrations/codecompanion/common.lua index ee17d5ab..89840022 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -42,4 +42,15 @@ return { end return job_runner end, + + ---Convert `path` to a relative path if it's within the current project. + ---When `base` is `nil`, this function will attempt to find a project root + ---or use `cwd`. + ---@param path string + ---@param base? string + ---@return string + cleanup_path = function(path, base) + base = base or vim.fs.root(0, { ".vectorcode", ".git" }) or vim.uv.cwd() or "." + return vim.fs.relpath(base, path) or path + end, } diff --git a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua index e5a940ff..7dbe0fd6 100644 --- a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua @@ -57,7 +57,11 @@ return function(opts) type = "function", ["function"] = { name = tool_name, - description = "Retrieve a list of files that have been added to the database for a given project.", + description = [[ +Retrieve a list of files that have been added to the database for a given project. +**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. +**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. + ]], parameters = { type = "object", properties = { @@ -73,7 +77,7 @@ return function(opts) ---@param tools CodeCompanion.Tools ---@param stdout string[][] success = function(_, tools, _, stdout) - stdout = stdout[1] + stdout = stdout[#stdout] local user_message for i, col in ipairs(stdout) do if i == 1 then @@ -84,7 +88,7 @@ return function(opts) end tools.chat:add_tool_output( tools.tool, - string.format("%s", col), + string.format("%s", cc_common.cleanup_path(col)), user_message ) end diff --git a/lua/vectorcode/integrations/codecompanion/ls_tool.lua b/lua/vectorcode/integrations/codecompanion/ls_tool.lua index 32d77367..788120fd 100644 --- a/lua/vectorcode/integrations/codecompanion/ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/ls_tool.lua @@ -76,7 +76,7 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe ---@param tools CodeCompanion.Tools ---@param stdout VectorCode.LsResult[][] success = function(_, tools, _, stdout) - stdout = stdout[1] + stdout = stdout[#stdout] local user_message for i, col in ipairs(stdout) do if i == 1 then diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index 296c32ec..85386082 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -315,6 +315,25 @@ When summarising the code, pay extra attention on information related to the que end end +---@param results VectorCode.QueryResult[] +---@return VectorCode.QueryResult[] +local function cleanup_paths(results) + local cwd = vim.fs.root(0, { ".vectorcode", ".git" }) or vim.uv.cwd() + if cwd then + results = vim + .iter(results) + :map( + ---@param res VectorCode.QueryResult + function(res) + res.path = cc_common.cleanup_path(res.path) + return res + end + ) + :totable() + end + return results +end + ---@param opts VectorCode.CodeCompanion.QueryToolOpts? ---@return CodeCompanion.Tools.Tool return check_cli_wrap(function(opts) @@ -436,6 +455,8 @@ return check_cli_wrap(function(opts) result = filter_results(result, tools.chat) end + result = cleanup_paths(result) + local max_result = #result if opts.max_num > 0 then 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 Include one single command call for VectorCode each time. You may include multiple keywords in the command. **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** +**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. +**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. ]], parameters = { type = "object", @@ -572,7 +595,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES ---@param cmd QueryToolArgs ---@param stdout VectorCode.CodeCompanion.QueryToolResult[] success = function(self, tools, cmd, stdout) - stdout = stdout[1] + stdout = stdout[#stdout] logger.info( ("CodeCompanion tool with command %s finished."):format(vim.inspect(cmd)) ) diff --git a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua index 887c3b8e..2e7b2138 100644 --- a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua @@ -147,7 +147,7 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv ---@param cmd VectoriseToolArgs ---@param stdout VectorCode.VectoriseResult[] success = function(self, tools, cmd, stdout) - stdout = stdout[1] + stdout = stdout[#stdout] tools.chat:add_tool_output( self, string.format(