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
189 changes: 72 additions & 117 deletions lua/vectorcode/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
local M = {}

local vc_config = require("vectorcode.config")

local logger = vc_config.logger

local get_config = vc_config.get_user_config

local notify_opts = vc_config.notify_opts
local jobrunner = require("vectorcode.jobrunner.cmd")
local notify = vim.schedule_wrap(vim.notify)

M.query = vc_config.check_cli_wrap(
---This function wraps the `query` subcommand of the VectorCode CLI. When used without the `callback` parameter,
Expand Down Expand Up @@ -39,57 +38,33 @@ M.query = vc_config.check_cli_wrap(
notify_opts
)
end
local args = { "vectorcode", "query", "--pipe", "-n", tostring(opts.n_query) }
local bufnr = vim.api.nvim_get_current_buf()
local args = { "query", "--pipe", "-n", tostring(opts.n_query) }
if type(query_message) == "string" then
query_message = { query_message }
end
vim.list_extend(args, query_message)

if opts.exclude_this then
vim.list_extend(args, { "--exclude", vim.api.nvim_buf_get_name(0) })
vim.list_extend(args, { "--exclude", vim.api.nvim_buf_get_name(bufnr) })
end

logger.debug("vectorcode.query cmd args: ", args)
local decoded_response = {}
local job = vim.system(args, { text = true }, function(out)
logger.debug("vectorcode.query cmd output: ", out)
local raw_response
if out.code == 124 and out.signal == 9 then
-- killed due to timeout
raw_response = nil
if opts.notify then
vim.schedule(function()
vim.notify(
"VectorCode process killed due to timeout.",
vim.log.levels.WARN,
notify_opts
)
end)
end
else
raw_response = out.stdout
if callback == nil then
local result, err = jobrunner.run(args, timeout_ms, bufnr)
if err then
logger.warn(vim.inspect(err))
end

if raw_response ~= nil and raw_response ~= "" then
decoded_response =
vim.json.decode(raw_response, { object = true, array = true })
if opts.notify then
vim.notify(
("Retrieved %s documents."):format(tostring(#decoded_response)),
vim.log.levels.INFO,
notify_opts
)
end
if type(callback) == "function" then
callback(decoded_response)
logger.debug(result)
return result
else
jobrunner.run_async(args, function(result, error)
logger.debug(result)
callback(result)
if error then
logger.warn(vim.inspect(error))
end
logger.info("vectorcode.query result:\n", decoded_response)
end
end)

if callback == nil then
job:wait(timeout_ms)
return decoded_response
end, bufnr)
end
end
)
Expand Down Expand Up @@ -138,70 +113,67 @@ M.vectorise = vc_config.check_cli_wrap(
)
end)
end
local bufnr = vim.api.nvim_get_current_buf()
logger.debug("vectorcode.vectorise cmd args: ", args)
require("plenary.job")
:new({
command = "vectorcode",
args = args,
on_exit = function(job, return_code)
if get_config().notify then
if return_code == 0 then
vim.notify(
"Indexing successful.",
vim.log.levels.INFO,
{ title = "VectorCode" }
)
else
vim.notify(
"Indexing failed.",
vim.log.levels.WARN,
{ title = "VectorCode" }
)
end
end
end,
})
:start()
jobrunner.run_async(args, function(result, error)
if result then
if vc_config.get_user_config().notify then
vim.schedule_wrap(vim.notify)(
"Indexing successful.",
vim.log.levels.INFO,
notify_opts
)
end
logger.info("Vectorise result:", vim.inspect(result))
elseif error then
vim.schedule_wrap(vim.notify)(
string.format("Indexing failed:\n%s", vim.inspect(error)),
vim.log.levels.WARN,
notify_opts
)
logger.warn(vim.inspect(error))
else
vim.schedule_wrap(vim.notify)(
"Indexing failed.",
vim.log.levels.WARN,
notify_opts
)
end
end, bufnr)
end
)

---@param project_root string?
M.update = vc_config.check_cli_wrap(function(project_root)
logger.info("vectorcode.update: ", project_root)
local args = { "vectorcode", "update" }
local args = { "update" }
if
type(project_root) == "string"
and vim.uv.fs_stat(vim.fs.normalize(project_root)).type == "directory"
then
vim.list_extend(args, { "--project_root", project_root })
end
logger.debug("vectorcode.update cmd args: ", args)
vim.system(args, { stdout = nil, stderr = nil }, function(out)
logger.debug("vectorcode.update cmd out:\n", out)
if get_config().notify then
vim.schedule(function()
if out.code == 0 then
vim.notify(
"VectorCode embeddings has been updated.",
vim.log.levels.INFO,
notify_opts
)
else
vim.notify(
("Failed to update the embeddings due to the following error:\n%s"):format(
out.stderr
),
vim.log.levels.ERROR,
notify_opts
)
end
end)
jobrunner.run_async(args, function(result, error)
if result then
if vc_config.get_user_config().notify then
notify("Indexing successful.", vim.log.levels.INFO, notify_opts)
end
logger.info("Update result:", vim.inspect(result))
elseif error then
notify(
string.format("Update failed:\n%s", vim.inspect(error)),
vim.log.levels.WARN,
notify_opts
)
logger.warn(vim.inspect(error))
else
notify("Update failed.", vim.log.levels.WARN, notify_opts)
end
end)
end, vim.api.nvim_get_current_buf())

if get_config().notify then
vim.schedule(function()
vim.notify("Updating VectorCode embeddings...", vim.log.levels.INFO, notify_opts)
end)
notify("Updating VectorCode embeddings...", vim.log.levels.INFO, notify_opts)
end
end)

Expand All @@ -227,32 +199,15 @@ end

---@return string[]
M.prompts = vc_config.check_cli_wrap(function()
local vectorcode_prompts = {}
vim
.system({ "vectorcode", "prompts", "-p" }, {}, function(out)
local successful = false
if out.code == 0 then
local ok, result =
pcall(vim.json.decode, out.stdout, { array = true, object = true })

if ok then
for _, str in pairs(result) do
table.insert(vectorcode_prompts, str)
end
successful = true
end
end
if not successful then
vim.schedule_wrap(vim.notify)(
"Failed to run `vectorcode prompts`. Please ensure your vectorcode CLI is up to date.\n"
.. tostring(out.stderr),
vim.log.levels.ERROR,
notify_opts
)
end
end)
:wait()
return vectorcode_prompts
local result, error = jobrunner.run({ "prompts", "-p" }, -1, 0)
if result == nil or result == {} then
logger.warn(vim.inspect(error))
if vc_config.get_user_config().notify then
notify(vim.inspect(error))
end
return {}
end
return result
end)

M.setup = vc_config.setup
Expand Down
1 change: 1 addition & 0 deletions lua/vectorcode/jobrunner/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function runner.run_async(args, callback, bufnr)
on_exit = function(self, _, _)
jobs[self.pid] = nil
local result = self:result()
logger.debug(result)
local ok, decoded = pcall(vim.json.decode, table.concat(result, ""))
if callback ~= nil then
if ok then
Expand Down