From 0639d1e28878c03560af4cac0ca4a984b991c65c Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Tue, 23 Sep 2025 11:37:12 +0800 Subject: [PATCH 1/3] fix(nvim): Handle `nil` results --- lua/vectorcode/init.lua | 6 +++--- lua/vectorcode/integrations/codecompanion/common.lua | 5 ++++- lua/vectorcode/integrations/codecompanion/query_tool.lua | 7 ++++++- lua/vectorcode/jobrunner/init.lua | 4 ++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lua/vectorcode/init.lua b/lua/vectorcode/init.lua index f7e1e722..7dc70edc 100644 --- a/lua/vectorcode/init.lua +++ b/lua/vectorcode/init.lua @@ -60,7 +60,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 +191,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 = table.concat(vim.iter(result or {}):flatten(math.huge):totable()), + stderr = table.concat(vim.iter(error or {}):flatten(math.huge):totable()), code = code, signal = signal, }) diff --git a/lua/vectorcode/integrations/codecompanion/common.lua b/lua/vectorcode/integrations/codecompanion/common.lua index 1e5a7e7e..209449aa 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -10,9 +10,12 @@ 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 t == nil then + return "" + end if type(t) == "string" then return t end diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index 1ece23b7..d365e7b9 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -465,7 +465,12 @@ 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) - 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]] 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 From a803746304e56c8544ce53b13e85110558dbd60e Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Tue, 23 Sep 2025 12:24:13 +0800 Subject: [PATCH 2/3] refactor(nvim): Move `flatten_table_to_string` to `vectorcode.utils` --- lua/vectorcode/init.lua | 5 ++-- .../integrations/codecompanion/common.lua | 29 +++++-------------- .../codecompanion/files_ls_tool.lua | 3 +- .../integrations/codecompanion/ls_tool.lua | 3 +- .../codecompanion/prompts/init.lua | 2 +- .../integrations/codecompanion/query_tool.lua | 5 ++-- .../codecompanion/vectorise_tool.lua | 3 +- lua/vectorcode/utils.lua | 26 +++++++++++++++++ 8 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lua/vectorcode/init.lua b/lua/vectorcode/init.lua index 7dc70edc..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 @@ -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 or {}):flatten(math.huge):totable()), - stderr = table.concat(vim.iter(error or {}):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 209449aa..d3e9da2c 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -13,27 +13,14 @@ return { ---@param t table|string|nil ---@return string 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") + 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..dfc0c74b 100644 --- a/lua/vectorcode/integrations/codecompanion/ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/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 logger = vc_config.logger ---@type VectorCode.CodeCompanion.LsToolOpts @@ -45,7 +46,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..9dcd7c52 100644 --- a/lua/vectorcode/integrations/codecompanion/prompts/init.lua +++ b/lua/vectorcode/integrations/codecompanion/prompts/init.lua @@ -132,7 +132,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 d365e7b9..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,7 +464,7 @@ 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 result ~= nil @@ -603,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/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 From c4f5ed4a1da7fbc4a6517a542ebd4f80162970ff Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Tue, 23 Sep 2025 12:26:40 +0800 Subject: [PATCH 3/3] fix(nvim): remove unused `require` --- lua/vectorcode/integrations/codecompanion/ls_tool.lua | 1 - lua/vectorcode/integrations/codecompanion/prompts/init.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/lua/vectorcode/integrations/codecompanion/ls_tool.lua b/lua/vectorcode/integrations/codecompanion/ls_tool.lua index dfc0c74b..d90c8e79 100644 --- a/lua/vectorcode/integrations/codecompanion/ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/ls_tool.lua @@ -1,6 +1,5 @@ ---@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 diff --git a/lua/vectorcode/integrations/codecompanion/prompts/init.lua b/lua/vectorcode/integrations/codecompanion/prompts/init.lua index 9dcd7c52..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 = {}