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
Binary file modified images/codecompanion_chat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 46 additions & 37 deletions lua/codecompanion/_extensions/vectorcode/init.lua
Original file line number Diff line number Diff line change
@@ -1,72 +1,81 @@
---@module "codecompanion"

---@alias sub_cmd "ls"|"query"|"vectorise"

---@class VectorCode.CodeCompanion.ExtensionOpts
---@field add_tools boolean|string|nil
---@field tool_opts VectorCode.CodeCompanion.ToolOpts
---@field add_slash_command boolean
--- A table where the keys are the subcommand name (`ls`, `query`, `vectorise`)
--- and the values are their config options.
---@field tool_opts table<sub_cmd, VectorCode.CodeCompanion.ToolOpts>
--- Whether to add a tool group that contains all vectorcode tools.
---@field tool_group VectorCode.CodeCompanion.ToolGroupOpts

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

---@type VectorCode.CodeCompanion.ExtensionOpts|{}
local default_extension_opts = {
tool_opts = { ls = {}, query = {}, vectorise = {} },
tool_group = { enabled = true, collapse = true, extras = {} },
}

---@type sub_cmd[]
local valid_tools = { "ls", "query", "vectorise" }

---@type CodeCompanion.Extension
local M = {
---@param opts VectorCode.CodeCompanion.ExtensionOpts
setup = vc_config.check_cli_wrap(function(opts)
opts = vim.tbl_deep_extend(
"force",
{ add_tool = true, add_slash_command = false },
opts or {}
)
opts = vim.tbl_deep_extend("force", default_extension_opts, opts or {})
logger.info("Received codecompanion extension opts:\n", opts)
local cc_config = require("codecompanion.config").config
local cc_integration = require("vectorcode.integrations").codecompanion.chat
if opts.add_tool then
local tool_name = "vectorcode"
if type(opts.add_tool) == "string" then
tool_name = tostring(opts.add_tool)
end
for _, sub_cmd in pairs(valid_tools) do
local tool_name = string.format("vectorcode_%s", sub_cmd)
if cc_config.strategies.chat.tools[tool_name] ~= nil then
vim.notify(
"There's an existing tool named `vectorcode`. Please either remove it or rename it.",
string.format(
"There's an existing tool named `%s`. Please either remove it or rename it.",
tool_name
),
vim.log.levels.ERROR,
vc_config.notify_opts
)
logger.warn(
string.format(
"Not creating a tool because there is an existing tool named %s.",
"Not creating this tool because there is an existing tool named %s.",
tool_name
)
)
else
cc_config.strategies.chat.tools[tool_name] = {
description = "Run VectorCode to retrieve the project context.",
callback = cc_integration.make_tool(opts.tool_opts),
description = string.format("Run VectorCode %s tool", sub_cmd),
callback = cc_integration.make_tool(sub_cmd, opts.tool_opts[sub_cmd]),
}
logger.info(string.format("%s tool has been created.", tool_name))
end
end
if opts.add_slash_command then
local command_name = "vectorcode"
if type(opts.add_slash_command) == "string" then
command_name = tostring(opts.add_slash_command)

if opts.tool_group.enabled then
local included_tools = vim
.iter(valid_tools)
:map(function(s)
return "vectorcode_" .. s
end)
:totable()
if opts.tool_group.extras and not vim.tbl_isempty(opts.tool_group.extras) then
vim.list_extend(included_tools, opts.tool_group.extras)
end
if cc_config.strategies.chat.slash_commands[command_name] ~= nil then
vim.notify(
"There's an existing slash command named `vectorcode`. Please either remove it or rename it.",
vim.log.levels.ERROR,
vc_config.notify_opts
)
logger.warn(
string.format(
"Not creating a command because there is an existing slash command named %s.",
command_name
)
logger.info(
string.format(
"Loading the following tools into `vectorcode_toolbox` tool group:\n%s",
vim.inspect(included_tools)
)
else
cc_config.strategies.chat.slash_commands[command_name] =
cc_integration.make_slash_command()
logger.info(string.format("%s command has been created.", command_name))
end
)
cc_config.strategies.chat.tools.groups["vectorcode_toolbox"] = {
opts = { collapse_tools = opts.tool_group.collapse },
description = "Use VectorCode to automatically build and retrieve repository-level context.",
tools = included_tools,
}
end
end),
}
Expand Down
6 changes: 3 additions & 3 deletions lua/vectorcode/cacher/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ M.query_from_cache = vc_config.check_cli_wrap(
---of the array is in the format of `{path="path/to/your/code.lua", document="document content"}`.
---@param bufnr integer?
---@param opts {notify: boolean}?
---@return VectorCode.Result[]
---@return VectorCode.QueryResult[]
function(bufnr, opts)
local result = {}
if bufnr == 0 or bufnr == nil then
Expand All @@ -282,7 +282,7 @@ M.query_from_cache = vc_config.check_cli_wrap(
end
)

---@alias ComponentCallback fun(result:VectorCode.Result):string
---@alias ComponentCallback fun(result:VectorCode.QueryResult):string

---Compile the retrieval results into a string.
---@param bufnr integer
Expand All @@ -296,7 +296,7 @@ function M.make_prompt_component(bufnr, component_cb)
return { content = "", count = 0 }
end
if component_cb == nil then
---@type fun(result:VectorCode.Result):string
---@type fun(result:VectorCode.QueryResult):string
component_cb = function(result)
return "<|file_sep|>" .. result.path .. "\n" .. result.document
end
Expand Down
4 changes: 2 additions & 2 deletions lua/vectorcode/cacher/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ M.query_from_cache = vc_config.check_cli_wrap(
---of the array is in the format of `{path="path/to/your/code.lua", document="document content"}`.
---@param bufnr integer?
---@param opts {notify: boolean}?
---@return VectorCode.Result[]
---@return VectorCode.QueryResult[]
function(bufnr, opts)
local result = {}
if bufnr == 0 or bufnr == nil then
Expand Down Expand Up @@ -329,7 +329,7 @@ function M.make_prompt_component(bufnr, component_cb)
return { content = "", count = 0 }
end
if component_cb == nil then
---@type fun(result:VectorCode.Result):string
---@type fun(result:VectorCode.QueryResult):string
component_cb = function(result)
return "<|file_sep|>" .. result.path .. "\n" .. result.document
end
Expand Down
4 changes: 2 additions & 2 deletions lua/vectorcode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ M.query = vc_config.check_cli_wrap(
---callback).
---@param query_message string|string[] Query message(s) to send to the `vecctorcode query` command
---@param opts VectorCode.QueryOpts? A table of config options. If nil, the default config or `setup` config will be used.
---@param callback fun(result:VectorCode.Result[])? Use the result async style.
---@return VectorCode.Result[]? An array of results.
---@param callback fun(result:VectorCode.QueryResult[])? Use the result async style.
---@return VectorCode.QueryResult[]? An array of results.
function(query_message, opts, callback)
logger.info("vectorcode.query: ", query_message, opts, callback)
opts = vim.tbl_deep_extend("force", vc_config.get_query_opts(), opts or {})
Expand Down
55 changes: 46 additions & 9 deletions lua/vectorcode/integrations/codecompanion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ local vc_config = require("vectorcode.config")
local notify_opts = vc_config.notify_opts
local logger = vc_config.logger

---@type VectorCode.CodeCompanion.ToolOpts
local default_options = {
---@type VectorCode.CodeCompanion.QueryToolOpts
local default_query_options = {
max_num = { chunk = -1, document = -1 },
default_num = { chunk = 50, document = 10 },
include_stderr = false,
use_lsp = false,
ls_on_start = false,
no_duplicate = true,
chunk_mode = false,
}

---@type VectorCode.CodeCompanion.LsToolOpts
local default_ls_options = { use_lsp = false }

---@type VectorCode.CodeCompanion.VectoriseToolOpts
local default_vectorise_options = { use_lsp = false }

return {
tool_result_source = "VectorCodeToolResult",
---@param t table|string
Expand All @@ -25,17 +30,43 @@ return {
return table.concat(vim.iter(t):flatten(math.huge):totable(), "\n")
end,

---@param opts VectorCode.CodeCompanion.ToolOpts|{}|nil
---@return VectorCode.CodeCompanion.ToolOpts
get_tool_opts = function(opts)
---@param opts VectorCode.CodeCompanion.LsToolOpts|{}|nil
---@return VectorCode.CodeCompanion.LsToolOpts
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.VectoriseToolOpts|{}|nil
---@return VectorCode.CodeCompanion.VectoriseToolOpts
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.QueryToolOpts|{}|nil
---@return VectorCode.CodeCompanion.QueryToolOpts
get_query_tool_opts = function(opts)
if opts == nil or opts.use_lsp == nil then
opts = vim.tbl_deep_extend(
"force",
opts or {},
{ use_lsp = vc_config.get_user_config().async_backend == "lsp" }
)
end
opts = vim.tbl_deep_extend("force", default_options, opts)
opts = vim.tbl_deep_extend("force", default_query_options, opts)
if type(opts.default_num) == "table" then
if opts.chunk_mode then
opts.default_num = opts.default_num.chunk
Expand All @@ -48,7 +79,7 @@ return {
)
end
if type(opts.max_num) == "table" then
if opts.chunk_mode then
if opts._ then
opts.max_num = opts.max_num.chunk
else
opts.max_num = opts.max_num.document
Expand All @@ -58,10 +89,16 @@ return {
"max_num should be an integer or a table: {chunk: integer, document: integer}"
)
end
logger.info(
string.format(
"Loading `vectorcode_query` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end,

---@param result VectorCode.Result
---@param result VectorCode.QueryResult
---@return string
process_result = function(result)
local llm_message
Expand Down
Loading