diff --git a/lua/vectorcode/init.lua b/lua/vectorcode/init.lua index f7e1e722..75e12ca8 100644 --- a/lua/vectorcode/init.lua +++ b/lua/vectorcode/init.lua @@ -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 @@ -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 @@ -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, }) diff --git a/lua/vectorcode/integrations/codecompanion/common.lua b/lua/vectorcode/integrations/codecompanion/common.lua index 1e5a7e7e..d3e9da2c 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -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 diff --git a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua index 98fd6e31..b318aa1d 100644 --- a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua @@ -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", @@ -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", diff --git a/lua/vectorcode/integrations/codecompanion/ls_tool.lua b/lua/vectorcode/integrations/codecompanion/ls_tool.lua index 788120fd..d90c8e79 100644 --- a/lua/vectorcode/integrations/codecompanion/ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/ls_tool.lua @@ -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 @@ -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", diff --git a/lua/vectorcode/integrations/codecompanion/prompts/init.lua b/lua/vectorcode/integrations/codecompanion/prompts/init.lua index b06b53f3..5b8d296f 100644 --- a/lua/vectorcode/integrations/codecompanion/prompts/init.lua +++ b/lua/vectorcode/integrations/codecompanion/prompts/init.lua @@ -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 = {} @@ -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, diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index 1ece23b7..1f361642 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -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 @@ -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]] @@ -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( diff --git a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua index cba59190..bbe1ffb9 100644 --- a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua @@ -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? } @@ -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) diff --git a/lua/vectorcode/jobrunner/init.lua b/lua/vectorcode/jobrunner/init.lua index b49754ce..db32cec1 100644 --- a/lua/vectorcode/jobrunner/init.lua +++ b/lua/vectorcode/jobrunner/init.lua @@ -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`). @@ -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 diff --git a/lua/vectorcode/utils.lua b/lua/vectorcode/utils.lua index 605d429b..c1ede61c 100644 --- a/lua/vectorcode/utils.lua +++ b/lua/vectorcode/utils.lua @@ -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