-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathvectorise_tool.lua
More file actions
166 lines (158 loc) · 5.31 KB
/
vectorise_tool.lua
File metadata and controls
166 lines (158 loc) · 5.31 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---@module "codecompanion"
local cc_common = require("vectorcode.integrations.codecompanion.common")
local vc_config = require("vectorcode.config")
local utils = require("vectorcode.utils")
local logger = vc_config.logger
---@alias VectoriseToolArgs { paths: string[], project_root: string? }
---@alias VectoriseResult { add: integer, update: integer, removed: integer }
---@type VectorCode.CodeCompanion.VectoriseToolOpts
local default_vectorise_options = {
use_lsp = vc_config.get_user_config().async_backend == "lsp",
}
---@param opts VectorCode.CodeCompanion.VectoriseToolOpts|{}|nil
---@return VectorCode.CodeCompanion.VectoriseToolOpts
local get_vectorise_tool_opts = function(opts)
opts = vim.tbl_deep_extend("force", default_vectorise_options, opts or {})
logger.info(
string.format(
"Loading `vectorcode_vectorise` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end
---@param opts VectorCode.CodeCompanion.VectoriseToolOpts|{}|nil
---@return CodeCompanion.Tools
return function(opts)
opts = get_vectorise_tool_opts(opts)
local tool_name = "vectorcode_vectorise"
local job_runner = cc_common.initialise_runner(opts.use_lsp)
---@type CodeCompanion.Tools|{}
return {
name = tool_name,
schema = {
type = "function",
["function"] = {
name = tool_name,
description = [[
Vectorise files in a project so that they'll be available from the `vectorcode_query` tool.
The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitive.
]],
parameters = {
type = "object",
properties = {
paths = {
type = "array",
items = { type = "string" },
description = "Paths to the files to be vectorised. DO NOT use directories for this parameter. You may use wildcard here if the user instructed to do so.",
},
project_root = {
type = "string",
description = [[
The project that the files belong to.
The value should be one of the following:
- One of the paths from the `vectorcode_ls` tool;
- User input;
- `null` (omit this parameter), which means the current project, if found.
]],
},
},
required = { "paths" },
},
},
},
cmds = {
---@param tools CodeCompanion.Tools
---@param action VectoriseToolArgs
---@return nil|{ status: string, data: string }
function(tools, action, _, cb)
local args = { "vectorise", "--pipe" }
action = utils.fix_nil(action)
if action.project_root then
local project_root = vim.fs.abspath(vim.fs.normalize(action.project_root))
if utils.is_directory(project_root) then
vim.list_extend(args, { "--project_root", project_root })
else
return { status = "error", data = "Invalid path " .. project_root }
end
end
if
vim.iter(action.paths):any(function(p)
return utils.is_directory(p)
end)
then
return {
status = "error",
data = "Please only supply paths to files as the `paths` parameter, not directories.",
}
end
vim.list_extend(args, action.paths)
job_runner.run_async(
args,
---@param result VectoriseResult
function(result, error, code, _)
if result then
cb({ status = "success", data = result })
else
cb({ status = "error", data = { error = error, code = code } })
end
end,
tools.chat.bufnr
)
end,
},
output = {
---@param self CodeCompanion.Tools.Tool
prompt = function(self, _)
return string.format("Vectorise %d files with VectorCode?", #self.args.paths)
end,
---@param self CodeCompanion.Tools.Tool
---@param tools CodeCompanion.Tools
---@param cmd VectoriseToolArgs
error = function(self, tools, cmd, stderr)
logger.error(
("CodeCompanion tool with command %s thrown with the following error: %s"):format(
vim.inspect(cmd),
vim.inspect(stderr)
)
)
stderr = utils.flatten_table_to_string(stderr, "Unknown error.")
tools.chat:add_tool_output(
self,
string.format("**VectorCode `vectorise` Tool: %s", stderr)
)
end,
---@param self CodeCompanion.Tools.Tool
---@param tools CodeCompanion.Tools
---@param cmd VectoriseToolArgs
---@param stdout VectorCode.VectoriseResult[]
success = function(self, tools, cmd, stdout)
stdout = stdout[#stdout]
tools.chat:add_tool_output(
self,
string.format(
[[**VectorCode `vectorise` Tool**:
- New files added: %d
- Existing files updated: %d
- Orphaned files removed: %d
- Up-to-date files skipped: %d
- Failed to decode: %d
]],
stdout.add or 0,
stdout.update or 0,
stdout.removed or 0,
stdout.skipped or 0,
stdout.failed or 0
)
)
if cmd.project_root and cmd.project_root then
tools.chat:add_tool_output(
self,
string.format("\nThe files were added to `%s`", cmd.project_root),
""
)
end
end,
},
}
end