Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lua/vectorcode/integrations/codecompanion/files_ls_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ local default_opts = {
}

---@param opts VectorCode.CodeCompanion.FilesLsToolOpts
---@return CodeCompanion.Agent.Tool
---@return CodeCompanion.Tools.Tool
return function(opts)
opts = vim.tbl_deep_extend("force", default_opts, opts or {})
local job_runner =
require("vectorcode.integrations.codecompanion.common").initialise_runner(
opts.use_lsp
)
local tool_name = "vectorcode_files_ls"
---@type CodeCompanion.Agent.Tool|{}
---@type CodeCompanion.Tools.Tool|{}
return {
name = tool_name,
cmds = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param action {project_root: string}
---@return nil|{ status: string, data: string }
function(agent, action, _, cb)
function(tools, action, _, cb)
local args = { "files", "ls", "--pipe" }
if action ~= nil then
action.project_root = action.project_root
Expand All @@ -50,7 +50,7 @@ return function(opts)
data = error,
})
end
end, agent.chat.bufnr)
end, tools.chat.bufnr)
end,
},
schema = {
Expand All @@ -70,9 +70,9 @@ return function(opts)
},
},
output = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param stdout string[][]
success = function(_, agent, _, stdout)
success = function(_, tools, _, stdout)
stdout = stdout[1]
local user_message
for i, col in ipairs(stdout) do
Expand All @@ -82,8 +82,8 @@ return function(opts)
else
user_message = ""
end
agent.chat:add_tool_output(
agent.tool,
tools.chat:add_tool_output(
tools.tool,
string.format("<path>%s</path>", col),
user_message
)
Expand Down
20 changes: 10 additions & 10 deletions lua/vectorcode/integrations/codecompanion/files_rm_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ local default_opts = {
---@alias FilesRmArgs { paths: string[], project_root: string }

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

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

---@type CodeCompanion.Agent.Tool|{}
---@type CodeCompanion.Tools|{}
return {
name = tool_name,
schema = {
Expand Down Expand Up @@ -47,10 +47,10 @@ return function(opts)
},
},
cmds = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param action VectoriseToolArgs
---@return nil|{ status: string, data: string }
function(agent, action, _, cb)
function(tools, action, _, cb)
local args = { "files", "rm", "--pipe" }
local project_root = vim.fs.abspath(vim.fs.normalize(action.project_root or ""))
if project_root ~= "" then
Expand Down Expand Up @@ -99,22 +99,22 @@ return function(opts)
cb({ status = "error", data = { error = error, code = code } })
end
end,
agent.chat.bufnr
tools.chat.bufnr
)
end,
},
output = {
---@param self CodeCompanion.Agent.Tool
---@param self CodeCompanion.Tools.Tool
prompt = function(self, _)
return string.format(
"Remove %d files from VectorCode database?",
#self.args.paths
)
end,
---@param self CodeCompanion.Agent.Tool
---@param agent CodeCompanion.Agent
success = function(self, agent, _, _)
agent.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.")
---@param self CodeCompanion.Tools.Tool
---@param tools CodeCompanion.Tools
success = function(self, tools, _, _)
tools.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.")
end,
},
}
Expand Down
42 changes: 1 addition & 41 deletions lua/vectorcode/integrations/codecompanion/init.lua
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
---@module "codecompanion"

local vc_config = require("vectorcode.config")
local check_cli_wrap = vc_config.check_cli_wrap

return {
chat = {
---@param component_cb (fun(result:VectorCode.QueryResult):string)?
make_slash_command = check_cli_wrap(function(component_cb)
vim.deprecate(
"vectorcode slash command",
"vectorcode tool/extension",
"0.8.0",
"VectorCode",
true
)
return {
description = "Add relevant files from the codebase.",
---@param chat CodeCompanion.Chat
callback = function(chat)
local codebase_prompt = ""
local vc_cache = vc_config.get_cacher_backend()
local bufnr = chat.context.bufnr
if not vc_cache.buf_is_registered(bufnr) then
return
end
codebase_prompt =
"The following are relevant files from the repository. Use them as extra context."
local query_result = vc_cache.make_prompt_component(bufnr, component_cb)
local id = tostring(query_result.count) .. " file(s) from codebase"
codebase_prompt = codebase_prompt .. query_result.content
chat:add_message(
{ content = codebase_prompt, role = "user" },
{ visible = false, id = id }
)
chat.references:add({
source = "VectorCode",
name = "VectorCode",
id = id,
})
end,
}
end),

---@param subcommand sub_cmd
---@param opts VectorCode.CodeCompanion.ToolOpts
---@return CodeCompanion.Agent.Tool
---@return CodeCompanion.Tools.Tool
make_tool = function(subcommand, opts)
local has = require("codecompanion").has
if has ~= nil and has("function-calling") then
Expand Down
18 changes: 9 additions & 9 deletions lua/vectorcode/integrations/codecompanion/ls_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ local get_ls_tool_opts = function(opts)
end

---@param opts VectorCode.CodeCompanion.LsToolOpts
---@return CodeCompanion.Agent.Tool
---@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.Agent.Tool|{}
---@type CodeCompanion.Tools.Tool|{}
return {
name = tool_name,
cmds = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@return nil|{ status: string, data: string }
function(agent, _, _, cb)
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 })
Expand All @@ -52,7 +52,7 @@ return function(opts)
data = error,
})
end
end, agent.chat.bufnr)
end, tools.chat.bufnr)
end,
},
schema = {
Expand All @@ -73,9 +73,9 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe
},
},
output = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param stdout VectorCode.LsResult[][]
success = function(_, agent, _, stdout)
success = function(_, tools, _, stdout)
stdout = stdout[1]
local user_message
for i, col in ipairs(stdout) do
Expand All @@ -85,8 +85,8 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe
else
user_message = ""
end
agent.chat:add_tool_output(
agent.tool,
tools.chat:add_tool_output(
tools.tool,
string.format("<collection>%s</collection>", col["project-root"]),
user_message
)
Expand Down
45 changes: 23 additions & 22 deletions lua/vectorcode/integrations/codecompanion/query_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ local result_tracker = {}
---@param chat CodeCompanion.Chat
---@return VectorCode.QueryResult[]
local filter_results = function(results, chat)
local existing_refs = chat.context_items or chat.refs or {}
local existing_refs = chat.context_items or {}

existing_refs = vim
.iter(existing_refs)
Expand Down Expand Up @@ -316,7 +316,7 @@ When summarising the code, pay extra attention on information related to the que
end

---@param opts VectorCode.CodeCompanion.QueryToolOpts?
---@return CodeCompanion.Agent.Tool
---@return CodeCompanion.Tools.Tool
return check_cli_wrap(function(opts)
opts = get_query_tool_opts(opts)
assert(
Expand All @@ -337,10 +337,10 @@ return check_cli_wrap(function(opts)
return {
name = tool_name,
cmds = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param action QueryToolArgs
---@return nil|{ status: string, data: string }
function(agent, action, _, cb)
function(tools, action, _, cb)
logger.info(
"CodeCompanion query tool called with the following arguments:\n",
action
Expand Down Expand Up @@ -387,12 +387,14 @@ return check_cli_wrap(function(opts)
end
end

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

if
Expand All @@ -431,7 +433,7 @@ return check_cli_wrap(function(opts)
then
-- NOTE: deduplication in summary mode prevents the model from requesting
-- the same content without summarysation.
result = filter_results(result, agent.chat)
result = filter_results(result, tools.chat)
end

local max_result = #result
Expand All @@ -457,7 +459,7 @@ return check_cli_wrap(function(opts)
data = error,
})
end
end, agent.chat.bufnr)
end, tools.chat.bufnr)
end,
},
schema = {
Expand Down Expand Up @@ -530,10 +532,10 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
},
},
output = {
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param cmd QueryToolArgs
---@param stderr table|string
error = function(self, agent, cmd, stderr)
error = function(self, tools, cmd, stderr)
logger.error(
("CodeCompanion tool with command %s thrown with the following error: %s"):format(
vim.inspect(cmd),
Expand All @@ -543,21 +545,21 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
stderr = cc_common.flatten_table_to_string(stderr)
if string.find(stderr, "InvalidCollectionException") then
if cmd.project_root then
agent.chat:add_tool_output(
tools.chat:add_tool_output(
self,
string.format(
"`%s` hasn't been vectorised. Please use the `vectorcode_vectorise` tool or vectorise it from the CLI.",
cmd.project_root
)
)
else
agent.chat:add_tool_output(
tools.chat:add_tool_output(
self,
"Failed to query from the requested project. Please verify the available projects via the `vectorcode_ls` tool or run it from the CLI."
)
end
else
agent.chat:add_tool_output(
tools.chat:add_tool_output(
self,
string.format(
"**VectorCode `query` Tool**: Failed with error:\n```\n%s\n```",
Expand All @@ -566,17 +568,17 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
)
end
end,
---@param agent CodeCompanion.Agent
---@param tools CodeCompanion.Tools
---@param cmd QueryToolArgs
---@param stdout VectorCode.CodeCompanion.QueryToolResult[]
success = function(self, agent, cmd, stdout)
success = function(self, tools, cmd, stdout)
stdout = stdout[1]
logger.info(
("CodeCompanion tool with command %s finished."):format(vim.inspect(cmd))
)
if vim.tbl_isempty(stdout.raw_results) then
logger.info("CodeCompanion query tool recieved empty result.")
return agent.chat:add_tool_output(
return tools.chat:add_tool_output(
self,
string.format(
"`%s` tool returned empty result. Please retry without deduplication.",
Expand All @@ -585,7 +587,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
"**VectorCode `query` Tool**: Retrieved 0 result. Retrying..."
)
end
agent.chat:add_tool_output(
tools.chat:add_tool_output(
self,
stdout.summary
or table.concat(vim
Expand All @@ -597,10 +599,9 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
string.format("**VectorCode Tool**: Retrieved %d %s(s)", stdout.count, mode)
)
if not opts.chunk_mode then
local context = agent.chat.context or agent.chat.references
for _, result in pairs(stdout.raw_results) do
-- skip referencing because there will be multiple chunks with the same path (id).
context:add({
tools.chat.context:add({
source = cc_common.tool_result_source,
id = result.path,
path = result.path,
Expand Down
Loading
Loading