Skip to content

Commit f3a5766

Browse files
committed
fix(nvim): fix the ls command for cmd runner
1 parent 4e9cfca commit f3a5766

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

lua/vectorcode/integrations/codecompanion.lua

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
---@module "codecompanion"
22

3-
---@alias tool_opts {max_num: integer?, default_num: integer?, include_stderr: boolean?, use_lsp: boolean}
3+
---@class VectorCode.CodeCompanion.ToolOpts
4+
---@field max_num integer?
5+
---@field default_num integer?
6+
---@field include_stderr boolean?
7+
---@field use_lsp boolean?
8+
---@field auto_submit table<string, boolean>?
49

510
local vc_config = require("vectorcode.config")
611
local check_cli_wrap = vc_config.check_cli_wrap
712
local notify_opts = vc_config.notify_opts
813

14+
---@param t table|string
15+
---@return string
16+
local function flatten_table_to_string(t)
17+
if type(t) == "string" then
18+
return t
19+
end
20+
return table.concat(vim.iter(t):flatten(math.huge):totable(), "\n")
21+
end
22+
923
local job_runner = nil
1024
---@param use_lsp boolean
1125
local function initialise_runner(use_lsp)
@@ -25,7 +39,8 @@ local function initialise_runner(use_lsp)
2539
end
2640
end
2741
end
28-
---@param opts tool_opts?
42+
43+
---@param opts VectorCode.CodeCompanion.ToolOpts?
2944
---@return CodeCompanion.Agent.Tool
3045
local make_tool = check_cli_wrap(function(opts)
3146
if opts == nil or opts.use_lsp == nil then
@@ -35,11 +50,13 @@ local make_tool = check_cli_wrap(function(opts)
3550
{ use_lsp = vc_config.get_user_config().async_backend == "lsp" }
3651
)
3752
end
38-
opts = vim.tbl_deep_extend(
39-
"force",
40-
{ max_num = -1, default_num = 10, include_stderr = false, use_lsp = false },
41-
opts or {}
42-
)
53+
opts = vim.tbl_deep_extend("force", {
54+
max_num = -1,
55+
default_num = 10,
56+
include_stderr = false,
57+
use_lsp = false,
58+
auto_submit = { ls = false, query = false },
59+
}, opts or {})
4360
local capping_message = ""
4461
if opts.max_num > 0 then
4562
capping_message = (" - Request for at most %d documents"):format(opts.max_num)
@@ -66,19 +83,29 @@ local make_tool = check_cli_wrap(function(opts)
6683
action.options.query = { action.options.query }
6784
end
6885
vim.list_extend(args, action.options.query)
69-
if
70-
action.options.project_root ~= nil
71-
and vim.uv.fs_stat(action.options.project_root).type == "directory"
72-
then
73-
vim.list_extend(args, { "--project_root", action.options.project_root })
86+
if action.options.project_root ~= nil then
87+
if
88+
vim.uv.fs_stat(action.options.project_root) ~= nil
89+
and vim.uv.fs_stat(action.options.project_root).type == "directory"
90+
then
91+
vim.list_extend(args, { "--project_root", action.options.project_root })
92+
else
93+
agent.chat:add_message(
94+
{ role = "user", content = "INVALID PROJECT ROOT! USE THE LS COMMAND!" },
95+
{ visible = false }
96+
)
97+
end
7498
end
7599
job_runner.run_async(args, function(result, error)
76100
if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.Result[]
77101
cb({ status = "success", data = result })
78102
else
103+
if type(error) == "table" then
104+
error = flatten_table_to_string(error)
105+
end
79106
cb({
80107
status = "error",
81-
data = table.concat(vim.iter(error):flatten(math.huge):totable(), "\n"),
108+
data = error,
82109
})
83110
end
84111
end, agent.chat.bufnr)
@@ -87,9 +114,12 @@ local make_tool = check_cli_wrap(function(opts)
87114
if vim.islist(result) and #result > 0 then
88115
cb({ status = "success", data = result })
89116
else
117+
if type(error) == "table" then
118+
error = flatten_table_to_string(error)
119+
end
90120
cb({
91121
status = "error",
92-
data = table.concat(vim.iter(error):flatten(math.huge):totable(), "\n"),
122+
data = error,
93123
})
94124
end
95125
end, agent.chat.bufnr)
@@ -168,9 +198,9 @@ local make_tool = check_cli_wrap(function(opts)
168198
- If the retrieval results do not contain the needed context, increase the file count so that the result will more likely contain the desired files
169199
- If the returned paths are relative, they are relative to the root of the project directory
170200
- Do not suggest edits to retrieved files that are outside of the current working directory, unless the user instructed otherwise
201+
- Use the `ls` command to retrieve a list of indexed project and pick one that may be relevant, unless the user explicitly mentioned "this project" (or in other equivalent expressions)
171202
- If a query failed to retrieve desired results, a new attempt should use different keywords that are orthogonal to the previous ones but with similar meanings
172-
- When asked about information in other project, use the `ls` command to see if there's any other indexed project that might help
173-
- DO NOT MAKE UP A PATH. ONLY USE PROJECT ROOTS RETURNED BY THE LS COMMAND OR PROVIDED BY THE USER
203+
- **The project root option MUST be a valid path on the filesystem. It can only be one of the results from the `ls` command or from user input**
174204
%s
175205
%s
176206
@@ -209,6 +239,22 @@ Remember:
209239
)
210240
end,
211241
output = {
242+
---@param agent CodeCompanion.Agent
243+
---@param cmd table
244+
---@param stderr table|string
245+
error = function(agent, cmd, stderr)
246+
stderr = flatten_table_to_string(stderr)
247+
agent.chat:add_message({
248+
role = "user",
249+
content = string.format(
250+
"VectorCode tool failed with the following error:\n",
251+
stderr
252+
),
253+
}, { visible = false })
254+
end,
255+
---@param agent CodeCompanion.Agent
256+
---@param cmd table
257+
---@param stdout table
212258
success = function(agent, cmd, stdout)
213259
stdout = stdout[1]
214260
if cmd.command == "query" then
@@ -232,13 +278,19 @@ Remember:
232278
end
233279
end
234280
elseif cmd.command == "ls" then
235-
for _, path in pairs(stdout) do
281+
for _, col in pairs(stdout) do
236282
agent.chat:add_message({
237283
role = "user",
238-
content = string.format("<collection>%s</collection>", path),
284+
content = string.format(
285+
"<collection>%s</collection>",
286+
col["project-root"]
287+
),
239288
}, { visible = false })
240289
end
241290
end
291+
if opts.auto_submit[cmd.command] then
292+
agent.chat:submit()
293+
end
242294
end,
243295
},
244296
}

0 commit comments

Comments
 (0)