Skip to content

Commit e6d3f6b

Browse files
authored
refactor(nvim): Follow codecompanion refactoring. (#262)
1 parent 653374f commit e6d3f6b

File tree

6 files changed

+63
-102
lines changed

6 files changed

+63
-102
lines changed

lua/vectorcode/integrations/codecompanion/files_ls_tool.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ local default_opts = {
1010
}
1111

1212
---@param opts VectorCode.CodeCompanion.FilesLsToolOpts
13-
---@return CodeCompanion.Agent.Tool
13+
---@return CodeCompanion.Tools.Tool
1414
return function(opts)
1515
opts = vim.tbl_deep_extend("force", default_opts, opts or {})
1616
local job_runner =
1717
require("vectorcode.integrations.codecompanion.common").initialise_runner(
1818
opts.use_lsp
1919
)
2020
local tool_name = "vectorcode_files_ls"
21-
---@type CodeCompanion.Agent.Tool|{}
21+
---@type CodeCompanion.Tools.Tool|{}
2222
return {
2323
name = tool_name,
2424
cmds = {
25-
---@param agent CodeCompanion.Agent
25+
---@param tools CodeCompanion.Tools
2626
---@param action {project_root: string}
2727
---@return nil|{ status: string, data: string }
28-
function(agent, action, _, cb)
28+
function(tools, action, _, cb)
2929
local args = { "files", "ls", "--pipe" }
3030
if action ~= nil then
3131
action.project_root = action.project_root
@@ -50,7 +50,7 @@ return function(opts)
5050
data = error,
5151
})
5252
end
53-
end, agent.chat.bufnr)
53+
end, tools.chat.bufnr)
5454
end,
5555
},
5656
schema = {
@@ -70,9 +70,9 @@ return function(opts)
7070
},
7171
},
7272
output = {
73-
---@param agent CodeCompanion.Agent
73+
---@param tools CodeCompanion.Tools
7474
---@param stdout string[][]
75-
success = function(_, agent, _, stdout)
75+
success = function(_, tools, _, stdout)
7676
stdout = stdout[1]
7777
local user_message
7878
for i, col in ipairs(stdout) do
@@ -82,8 +82,8 @@ return function(opts)
8282
else
8383
user_message = ""
8484
end
85-
agent.chat:add_tool_output(
86-
agent.tool,
85+
tools.chat:add_tool_output(
86+
tools.tool,
8787
string.format("<path>%s</path>", col),
8888
user_message
8989
)

lua/vectorcode/integrations/codecompanion/files_rm_tool.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ local default_opts = {
1212
---@alias FilesRmArgs { paths: string[], project_root: string }
1313

1414
---@param opts VectorCode.CodeCompanion.FilesRmToolOpts
15-
---@return CodeCompanion.Agent.Tool
15+
---@return CodeCompanion.Tools
1616
return function(opts)
1717
opts = vim.tbl_deep_extend("force", default_opts, opts or {})
1818

1919
local tool_name = "vectorcode_files_rm"
2020
local job_runner = cc_common.initialise_runner(opts.use_lsp)
2121

22-
---@type CodeCompanion.Agent.Tool|{}
22+
---@type CodeCompanion.Tools|{}
2323
return {
2424
name = tool_name,
2525
schema = {
@@ -47,10 +47,10 @@ return function(opts)
4747
},
4848
},
4949
cmds = {
50-
---@param agent CodeCompanion.Agent
50+
---@param tools CodeCompanion.Tools
5151
---@param action VectoriseToolArgs
5252
---@return nil|{ status: string, data: string }
53-
function(agent, action, _, cb)
53+
function(tools, action, _, cb)
5454
local args = { "files", "rm", "--pipe" }
5555
local project_root = vim.fs.abspath(vim.fs.normalize(action.project_root or ""))
5656
if project_root ~= "" then
@@ -99,22 +99,22 @@ return function(opts)
9999
cb({ status = "error", data = { error = error, code = code } })
100100
end
101101
end,
102-
agent.chat.bufnr
102+
tools.chat.bufnr
103103
)
104104
end,
105105
},
106106
output = {
107-
---@param self CodeCompanion.Agent.Tool
107+
---@param self CodeCompanion.Tools.Tool
108108
prompt = function(self, _)
109109
return string.format(
110110
"Remove %d files from VectorCode database?",
111111
#self.args.paths
112112
)
113113
end,
114-
---@param self CodeCompanion.Agent.Tool
115-
---@param agent CodeCompanion.Agent
116-
success = function(self, agent, _, _)
117-
agent.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.")
114+
---@param self CodeCompanion.Tools.Tool
115+
---@param tools CodeCompanion.Tools
116+
success = function(self, tools, _, _)
117+
tools.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.")
118118
end,
119119
},
120120
}

lua/vectorcode/integrations/codecompanion/init.lua

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
---@module "codecompanion"
22

3-
local vc_config = require("vectorcode.config")
4-
local check_cli_wrap = vc_config.check_cli_wrap
5-
63
return {
74
chat = {
8-
---@param component_cb (fun(result:VectorCode.QueryResult):string)?
9-
make_slash_command = check_cli_wrap(function(component_cb)
10-
vim.deprecate(
11-
"vectorcode slash command",
12-
"vectorcode tool/extension",
13-
"0.8.0",
14-
"VectorCode",
15-
true
16-
)
17-
return {
18-
description = "Add relevant files from the codebase.",
19-
---@param chat CodeCompanion.Chat
20-
callback = function(chat)
21-
local codebase_prompt = ""
22-
local vc_cache = vc_config.get_cacher_backend()
23-
local bufnr = chat.context.bufnr
24-
if not vc_cache.buf_is_registered(bufnr) then
25-
return
26-
end
27-
codebase_prompt =
28-
"The following are relevant files from the repository. Use them as extra context."
29-
local query_result = vc_cache.make_prompt_component(bufnr, component_cb)
30-
local id = tostring(query_result.count) .. " file(s) from codebase"
31-
codebase_prompt = codebase_prompt .. query_result.content
32-
chat:add_message(
33-
{ content = codebase_prompt, role = "user" },
34-
{ visible = false, id = id }
35-
)
36-
chat.references:add({
37-
source = "VectorCode",
38-
name = "VectorCode",
39-
id = id,
40-
})
41-
end,
42-
}
43-
end),
44-
455
---@param subcommand sub_cmd
466
---@param opts VectorCode.CodeCompanion.ToolOpts
47-
---@return CodeCompanion.Agent.Tool
7+
---@return CodeCompanion.Tools.Tool
488
make_tool = function(subcommand, opts)
499
local has = require("codecompanion").has
5010
if has ~= nil and has("function-calling") then

lua/vectorcode/integrations/codecompanion/ls_tool.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ local get_ls_tool_opts = function(opts)
2525
end
2626

2727
---@param opts VectorCode.CodeCompanion.LsToolOpts
28-
---@return CodeCompanion.Agent.Tool
28+
---@return CodeCompanion.Tools.Tool
2929
return function(opts)
3030
opts = get_ls_tool_opts(opts)
3131
local job_runner =
3232
require("vectorcode.integrations.codecompanion.common").initialise_runner(
3333
opts.use_lsp
3434
)
3535
local tool_name = "vectorcode_ls"
36-
---@type CodeCompanion.Agent.Tool|{}
36+
---@type CodeCompanion.Tools.Tool|{}
3737
return {
3838
name = tool_name,
3939
cmds = {
40-
---@param agent CodeCompanion.Agent
40+
---@param tools CodeCompanion.Tools
4141
---@return nil|{ status: string, data: string }
42-
function(agent, _, _, cb)
42+
function(tools, _, _, cb)
4343
job_runner.run_async({ "ls", "--pipe" }, function(result, error)
4444
if vim.islist(result) and #result > 0 then
4545
cb({ status = "success", data = result })
@@ -52,7 +52,7 @@ return function(opts)
5252
data = error,
5353
})
5454
end
55-
end, agent.chat.bufnr)
55+
end, tools.chat.bufnr)
5656
end,
5757
},
5858
schema = {
@@ -73,9 +73,9 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe
7373
},
7474
},
7575
output = {
76-
---@param agent CodeCompanion.Agent
76+
---@param tools CodeCompanion.Tools
7777
---@param stdout VectorCode.LsResult[][]
78-
success = function(_, agent, _, stdout)
78+
success = function(_, tools, _, stdout)
7979
stdout = stdout[1]
8080
local user_message
8181
for i, col in ipairs(stdout) do
@@ -85,8 +85,8 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe
8585
else
8686
user_message = ""
8787
end
88-
agent.chat:add_tool_output(
89-
agent.tool,
88+
tools.chat:add_tool_output(
89+
tools.tool,
9090
string.format("<collection>%s</collection>", col["project-root"]),
9191
user_message
9292
)

lua/vectorcode/integrations/codecompanion/query_tool.lua

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ local result_tracker = {}
164164
---@param chat CodeCompanion.Chat
165165
---@return VectorCode.QueryResult[]
166166
local filter_results = function(results, chat)
167-
local existing_refs = chat.context_items or chat.refs or {}
167+
local existing_refs = chat.context_items or {}
168168

169169
existing_refs = vim
170170
.iter(existing_refs)
@@ -316,7 +316,7 @@ When summarising the code, pay extra attention on information related to the que
316316
end
317317

318318
---@param opts VectorCode.CodeCompanion.QueryToolOpts?
319-
---@return CodeCompanion.Agent.Tool
319+
---@return CodeCompanion.Tools.Tool
320320
return check_cli_wrap(function(opts)
321321
opts = get_query_tool_opts(opts)
322322
assert(
@@ -337,10 +337,10 @@ return check_cli_wrap(function(opts)
337337
return {
338338
name = tool_name,
339339
cmds = {
340-
---@param agent CodeCompanion.Agent
340+
---@param tools CodeCompanion.Tools
341341
---@param action QueryToolArgs
342342
---@return nil|{ status: string, data: string }
343-
function(agent, action, _, cb)
343+
function(tools, action, _, cb)
344344
logger.info(
345345
"CodeCompanion query tool called with the following arguments:\n",
346346
action
@@ -387,12 +387,14 @@ return check_cli_wrap(function(opts)
387387
end
388388
end
389389

390-
-- TODO: remove the `chat.refs` compatibility code when the upstream PR is released.
391-
local context_items = agent.chat.context_items or agent.chat.refs
392-
if opts.no_duplicate and context_items ~= nil and action.deduplicate then
390+
if
391+
opts.no_duplicate
392+
and tools.chat.context_items ~= nil
393+
and action.deduplicate
394+
then
393395
-- exclude files that has been added to the context
394396
local existing_files = { "--exclude" }
395-
for _, ref in pairs(context_items) do
397+
for _, ref in pairs(tools.chat.context_items) do
396398
if ref.source == cc_common.tool_result_source then
397399
table.insert(existing_files, ref.id)
398400
elseif type(ref.path) == "string" then
@@ -421,7 +423,7 @@ return check_cli_wrap(function(opts)
421423
if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.QueryResult[]
422424
local summary_opts = vim.deepcopy(opts.summarise) or {}
423425
if type(summary_opts.enabled) == "function" then
424-
summary_opts.enabled = summary_opts.enabled(agent.chat, result) --[[@as boolean]]
426+
summary_opts.enabled = summary_opts.enabled(tools.chat, result) --[[@as boolean]]
425427
end
426428

427429
if
@@ -431,7 +433,7 @@ return check_cli_wrap(function(opts)
431433
then
432434
-- NOTE: deduplication in summary mode prevents the model from requesting
433435
-- the same content without summarysation.
434-
result = filter_results(result, agent.chat)
436+
result = filter_results(result, tools.chat)
435437
end
436438

437439
local max_result = #result
@@ -457,7 +459,7 @@ return check_cli_wrap(function(opts)
457459
data = error,
458460
})
459461
end
460-
end, agent.chat.bufnr)
462+
end, tools.chat.bufnr)
461463
end,
462464
},
463465
schema = {
@@ -530,10 +532,10 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
530532
},
531533
},
532534
output = {
533-
---@param agent CodeCompanion.Agent
535+
---@param tools CodeCompanion.Tools
534536
---@param cmd QueryToolArgs
535537
---@param stderr table|string
536-
error = function(self, agent, cmd, stderr)
538+
error = function(self, tools, cmd, stderr)
537539
logger.error(
538540
("CodeCompanion tool with command %s thrown with the following error: %s"):format(
539541
vim.inspect(cmd),
@@ -543,21 +545,21 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
543545
stderr = cc_common.flatten_table_to_string(stderr)
544546
if string.find(stderr, "InvalidCollectionException") then
545547
if cmd.project_root then
546-
agent.chat:add_tool_output(
548+
tools.chat:add_tool_output(
547549
self,
548550
string.format(
549551
"`%s` hasn't been vectorised. Please use the `vectorcode_vectorise` tool or vectorise it from the CLI.",
550552
cmd.project_root
551553
)
552554
)
553555
else
554-
agent.chat:add_tool_output(
556+
tools.chat:add_tool_output(
555557
self,
556558
"Failed to query from the requested project. Please verify the available projects via the `vectorcode_ls` tool or run it from the CLI."
557559
)
558560
end
559561
else
560-
agent.chat:add_tool_output(
562+
tools.chat:add_tool_output(
561563
self,
562564
string.format(
563565
"**VectorCode `query` Tool**: Failed with error:\n```\n%s\n```",
@@ -566,17 +568,17 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
566568
)
567569
end
568570
end,
569-
---@param agent CodeCompanion.Agent
571+
---@param tools CodeCompanion.Tools
570572
---@param cmd QueryToolArgs
571573
---@param stdout VectorCode.CodeCompanion.QueryToolResult[]
572-
success = function(self, agent, cmd, stdout)
574+
success = function(self, tools, cmd, stdout)
573575
stdout = stdout[1]
574576
logger.info(
575577
("CodeCompanion tool with command %s finished."):format(vim.inspect(cmd))
576578
)
577579
if vim.tbl_isempty(stdout.raw_results) then
578580
logger.info("CodeCompanion query tool recieved empty result.")
579-
return agent.chat:add_tool_output(
581+
return tools.chat:add_tool_output(
580582
self,
581583
string.format(
582584
"`%s` tool returned empty result. Please retry without deduplication.",
@@ -585,7 +587,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
585587
"**VectorCode `query` Tool**: Retrieved 0 result. Retrying..."
586588
)
587589
end
588-
agent.chat:add_tool_output(
590+
tools.chat:add_tool_output(
589591
self,
590592
stdout.summary
591593
or table.concat(vim
@@ -597,10 +599,9 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
597599
string.format("**VectorCode Tool**: Retrieved %d %s(s)", stdout.count, mode)
598600
)
599601
if not opts.chunk_mode then
600-
local context = agent.chat.context or agent.chat.references
601602
for _, result in pairs(stdout.raw_results) do
602603
-- skip referencing because there will be multiple chunks with the same path (id).
603-
context:add({
604+
tools.chat.context:add({
604605
source = cc_common.tool_result_source,
605606
id = result.path,
606607
path = result.path,

0 commit comments

Comments
 (0)