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

local vc_config = require("vectorcode.config")
local utils = require("vectorcode.utils")
local logger = vc_config.logger
local get_config = vc_config.get_user_config
local notify_opts = vc_config.notify_opts
Expand Down Expand Up @@ -60,7 +61,7 @@ M.query = vc_config.check_cli_wrap(
else
jobrunner.run_async(args, function(result, error)
logger.debug(result)
callback(result)
callback(result or {})
if error then
logger.warn(vim.inspect(error))
end
Expand Down Expand Up @@ -191,8 +192,8 @@ function M.check(check_item, stdout_cb)
return_code = code
if type(stdout_cb) == "function" then
stdout_cb({
stdout = table.concat(vim.iter(result):flatten(math.huge):totable()),
stderr = table.concat(vim.iter(error):flatten(math.huge):totable()),
stdout = utils.flatten_table_to_string(result),
stderr = utils.flatten_table_to_string(error),
code = code,
signal = signal,
})
Expand Down
28 changes: 9 additions & 19 deletions lua/vectorcode/integrations/codecompanion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,17 @@ local TOOL_RESULT_SOURCE = "VectorCodeToolResult"
return {
tool_result_source = TOOL_RESULT_SOURCE,

---@param t table|string
---@param t table|string|nil
---@return string
flatten_table_to_string = function(t)
if type(t) == "string" then
return t
end

-- Handle empty tables or tables with empty strings
local flattened = vim
.iter(t)
:flatten(math.huge)
:filter(function(item)
return type(item) == "string" and vim.trim(item) ~= ""
end)
:totable()

if #flattened == 0 then
return "Unknown error occurred"
end

return table.concat(flattened, "\n")
vim.deprecate(
"vectorcode.integrations.codecompanion.common.flatten_table_to_string",
"vectorcode.utils.flatten_table_to_string",
"1.0.0",
"vectorcode",
true
)
return require("vectorcode.utils").flatten_table_to_string(t)
end,

---@param use_lsp boolean
Expand Down
3 changes: 2 additions & 1 deletion lua/vectorcode/integrations/codecompanion/files_ls_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

local cc_common = require("vectorcode.integrations.codecompanion.common")
local vc_config = require("vectorcode.config")
local utils = require("vectorcode.utils")

local default_opts = {
use_lsp = vc_config.get_user_config().async_backend == "lsp",
Expand Down Expand Up @@ -43,7 +44,7 @@ return function(opts)
cb({ status = "success", data = result })
else
if type(error) == "table" then
error = cc_common.flatten_table_to_string(error)
error = utils.flatten_table_to_string(error)
end
cb({
status = "error",
Expand Down
4 changes: 2 additions & 2 deletions lua/vectorcode/integrations/codecompanion/ls_tool.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@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

---@type VectorCode.CodeCompanion.LsToolOpts
Expand Down Expand Up @@ -45,7 +45,7 @@ return function(opts)
cb({ status = "success", data = result })
else
if type(error) == "table" then
error = cc_common.flatten_table_to_string(error)
error = utils.flatten_table_to_string(error)
end
cb({
status = "error",
Expand Down
3 changes: 1 addition & 2 deletions lua/vectorcode/integrations/codecompanion/prompts/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function M.register_prompt(opts)

assert(type(opts.name) == "string", "`name` cannot be `nil`.")

local cc_common = require("vectorcode.integrations.codecompanion.common")
local constants = require("codecompanion.config").config.constants
local prompts = {}

Expand Down Expand Up @@ -132,7 +131,7 @@ Here's my question:
vc_config.notify_opts
)
elseif err ~= nil then
err = cc_common.flatten_table_to_string(err)
err = utils.flatten_table_to_string(err)
if err ~= "" then
vim.schedule_wrap(vim.notify)(
err,
Expand Down
12 changes: 9 additions & 3 deletions lua/vectorcode/integrations/codecompanion/query_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local cc_config = require("codecompanion.config").config
local cc_schema = require("codecompanion.schema")
local http_client = require("codecompanion.http")
local vc_config = require("vectorcode.config")
local utils = require("vectorcode.utils")
local check_cli_wrap = vc_config.check_cli_wrap
local logger = vc_config.logger

Expand Down Expand Up @@ -463,9 +464,14 @@ return check_cli_wrap(function(opts)
)

job_runner.run_async(args, function(result, error, code)
local err_string = cc_common.flatten_table_to_string(error)
local err_string = utils.flatten_table_to_string(error)

if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.QueryResult[]
if
result ~= nil
and 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(tools.chat, result) --[[@as boolean]]
Expand Down Expand Up @@ -598,7 +604,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
vim.inspect(stderr)
)
)
stderr = cc_common.flatten_table_to_string(stderr)
stderr = utils.flatten_table_to_string(stderr)
if string.find(stderr, "InvalidCollectionException") then
if cmd.project_root then
tools.chat:add_tool_output(
Expand Down
3 changes: 2 additions & 1 deletion lua/vectorcode/integrations/codecompanion/vectorise_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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? }
Expand Down Expand Up @@ -129,7 +130,7 @@ The value should be one of the following:
vim.inspect(stderr)
)
)
stderr = cc_common.flatten_table_to_string(stderr)
stderr = utils.flatten_table_to_string(stderr)
tools.chat:add_tool_output(
self,
string.format("**VectorCode `vectorise` Tool: %s", stderr)
Expand Down
4 changes: 2 additions & 2 deletions lua/vectorcode/jobrunner/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local utils = require("vectorcode.utils")

---@alias VectorCode.JobRunner.Callback fun(result: table, error: table, code:integer, signal: integer?)
---@alias VectorCode.JobRunner.Callback fun(result: table|nil, error: table|nil, code:integer, signal: integer?)

--- A class for calling vectorcode commands that aims at providing a unified API for both LSP and command-line backend.
--- Implementations exist for both direct command-line execution (`cmd.lua`) and LSP (`lsp.lua`).
Expand All @@ -24,7 +24,7 @@ local utils = require("vectorcode.utils")
--- - `error`: error messages, if any.
--- - `code`: exit code (or error code) for the process.
--- - `signal`: _for cmd runner only_, the shell signal sent to the process.
---@field run fun(args: string[], timeout_ms: integer?, bufnr: integer):(result:table, error:table, code:integer, signal: integer?)
---@field run fun(args: string[], timeout_ms: integer?, bufnr: integer):(result:table|nil, error:table|nil, code:integer, signal: integer?)
--- Checks if a job associated with the given handle is currently running.
--- Returns true if the job is running, false otherwise.
---@field is_job_running fun(job_handle: integer):boolean
Expand Down
26 changes: 26 additions & 0 deletions lua/vectorcode/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,30 @@ function M.is_directory(f)
return stats and (stats.type == "directory") or false
end

---@param t table|string|nil
---@return string
M.flatten_table_to_string = function(t)
if t == nil then
return ""
end
if type(t) == "string" then
return t
end

-- Handle empty tables or tables with empty strings
local flattened = vim
.iter(t)
:flatten(math.huge)
:filter(function(item)
return type(item) == "string" and vim.trim(item) ~= ""
end)
:totable()

if #flattened == 0 then
return "Unknown error occurred"
end

return table.concat(flattened, "\n")
end

return M
Loading