Skip to content

Commit 8fcb0be

Browse files
author
Zhe Yu
committed
feat(nvim): Validate file paths in vectorise_tool and add error handler
1 parent fce1298 commit 8fcb0be

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

lua/vectorcode/integrations/codecompanion/vectorise_tool.lua

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv
5252
paths = {
5353
type = "array",
5454
items = { type = "string" },
55-
description = "Paths to the files to be vectorised",
55+
description = "Paths to the files to be vectorised. DO NOT use directories for this parameter.",
5656
},
5757
project_root = {
5858
type = "string",
@@ -91,23 +91,22 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv
9191
if project_root ~= "" then
9292
action.project_root = project_root
9393
end
94-
vim.list_extend(
95-
args,
96-
vim
97-
.iter(action.paths)
98-
:filter(
99-
---@param item string
100-
function(item)
101-
local stat = vim.uv.fs_stat(item)
102-
if stat and stat.type == "file" then
103-
return true
104-
else
105-
return false
106-
end
107-
end
108-
)
109-
:totable()
110-
)
94+
if
95+
vim.iter(action.paths):any(function(p)
96+
local stat = vim.uv.fs_stat(vim.fs.normalize(p))
97+
if stat and stat.type == "directory" then
98+
return true
99+
end
100+
return false
101+
end)
102+
then
103+
return {
104+
status = "error",
105+
data = "Please only supply paths to files, not directories.",
106+
}
107+
end
108+
109+
vim.list_extend(args, action.paths)
111110
job_runner.run_async(
112111
args,
113112
---@param result VectoriseResult
@@ -127,6 +126,22 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv
127126
prompt = function(self, _)
128127
return string.format("Vectorise %d files with VectorCode?", #self.args.paths)
129128
end,
129+
---@param self CodeCompanion.Tools.Tool
130+
---@param tools CodeCompanion.Tools
131+
---@param cmd VectoriseToolArgs
132+
error = function(self, tools, cmd, stderr)
133+
logger.error(
134+
("CodeCompanion tool with command %s thrown with the following error: %s"):format(
135+
vim.inspect(cmd),
136+
vim.inspect(stderr)
137+
)
138+
)
139+
stderr = cc_common.flatten_table_to_string(stderr)
140+
tools.chat:add_tool_output(
141+
self,
142+
string.format("**VectorCode Vectorise Tool: %s", stderr)
143+
)
144+
end,
130145
---@param self CodeCompanion.Agent.Tool
131146
---@param agent CodeCompanion.Agent
132147
---@param cmd VectoriseToolArgs

0 commit comments

Comments
 (0)