-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathls_tool.lua
More file actions
95 lines (91 loc) · 2.76 KB
/
ls_tool.lua
File metadata and controls
95 lines (91 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---@module "codecompanion"
local vc_config = require("vectorcode.config")
local utils = require("vectorcode.utils")
local logger = vc_config.logger
---@type VectorCode.CodeCompanion.LsToolOpts
local default_ls_options = {
use_lsp = vc_config.get_user_config().async_backend == "lsp",
}
---@param opts VectorCode.CodeCompanion.LsToolOpts|{}|nil
---@return VectorCode.CodeCompanion.LsToolOpts
local get_ls_tool_opts = function(opts)
opts = vim.tbl_deep_extend("force", default_ls_options, opts or {})
logger.info(
string.format(
"Loading `vectorcode_ls` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end
---@param opts VectorCode.CodeCompanion.LsToolOpts
---@return CodeCompanion.Tools.Tool
return function(opts)
opts = get_ls_tool_opts(opts)
local job_runner =
require("vectorcode.integrations.codecompanion.common").initialise_runner(
opts.use_lsp
)
local tool_name = "vectorcode_ls"
---@type CodeCompanion.Tools.Tool|{}
return {
name = tool_name,
cmds = {
---@param tools CodeCompanion.Tools
---@return nil|{ status: string, data: string }
function(tools, _, _, cb)
job_runner.run_async({ "ls", "--pipe" }, function(result, error)
if vim.islist(result) and #result > 0 then
cb({ status = "success", data = result })
else
if type(error) == "table" then
error = utils.flatten_table_to_string(error, "Unknown error.")
end
cb({
status = "error",
data = error,
})
end
end, tools.chat.bufnr)
end,
},
schema = {
type = "function",
["function"] = {
name = tool_name,
description = [[
Retrieve a list of projects accessible via the VectorCode tools.
Where relevant, use paths from this tool as the `project_root` parameter in other vectorcode tools.
]],
parameters = {
-- make anthropic models happy.
type = "object",
properties = vim.empty_dict(),
required = {},
additionalProperties = false,
},
},
},
output = {
---@param tools CodeCompanion.Tools
---@param stdout VectorCode.LsResult[][]
success = function(_, tools, _, stdout)
stdout = stdout[#stdout]
local user_message
for i, col in ipairs(stdout) do
if i == 1 then
user_message =
string.format("**VectorCode `ls` Tool**: Found %d collections.", #stdout)
else
user_message = ""
end
tools.chat:add_tool_output(
tools.tool,
string.format("<collection>%s</collection>", col["project-root"]),
user_message
)
end
end,
},
}
end