diff --git a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua index d5a3c064..e5a940ff 100644 --- a/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/files_ls_tool.lua @@ -10,7 +10,7 @@ local default_opts = { } ---@param opts VectorCode.CodeCompanion.FilesLsToolOpts ----@return CodeCompanion.Agent.Tool +---@return CodeCompanion.Tools.Tool return function(opts) opts = vim.tbl_deep_extend("force", default_opts, opts or {}) local job_runner = @@ -18,14 +18,14 @@ return function(opts) opts.use_lsp ) local tool_name = "vectorcode_files_ls" - ---@type CodeCompanion.Agent.Tool|{} + ---@type CodeCompanion.Tools.Tool|{} return { name = tool_name, cmds = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param action {project_root: string} ---@return nil|{ status: string, data: string } - function(agent, action, _, cb) + function(tools, action, _, cb) local args = { "files", "ls", "--pipe" } if action ~= nil then action.project_root = action.project_root @@ -50,7 +50,7 @@ return function(opts) data = error, }) end - end, agent.chat.bufnr) + end, tools.chat.bufnr) end, }, schema = { @@ -70,9 +70,9 @@ return function(opts) }, }, output = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param stdout string[][] - success = function(_, agent, _, stdout) + success = function(_, tools, _, stdout) stdout = stdout[1] local user_message for i, col in ipairs(stdout) do @@ -82,8 +82,8 @@ return function(opts) else user_message = "" end - agent.chat:add_tool_output( - agent.tool, + tools.chat:add_tool_output( + tools.tool, string.format("%s", col), user_message ) diff --git a/lua/vectorcode/integrations/codecompanion/files_rm_tool.lua b/lua/vectorcode/integrations/codecompanion/files_rm_tool.lua index cf8d44de..b14aa175 100644 --- a/lua/vectorcode/integrations/codecompanion/files_rm_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/files_rm_tool.lua @@ -12,14 +12,14 @@ local default_opts = { ---@alias FilesRmArgs { paths: string[], project_root: string } ---@param opts VectorCode.CodeCompanion.FilesRmToolOpts ----@return CodeCompanion.Agent.Tool +---@return CodeCompanion.Tools return function(opts) opts = vim.tbl_deep_extend("force", default_opts, opts or {}) local tool_name = "vectorcode_files_rm" local job_runner = cc_common.initialise_runner(opts.use_lsp) - ---@type CodeCompanion.Agent.Tool|{} + ---@type CodeCompanion.Tools|{} return { name = tool_name, schema = { @@ -47,10 +47,10 @@ return function(opts) }, }, cmds = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param action VectoriseToolArgs ---@return nil|{ status: string, data: string } - function(agent, action, _, cb) + function(tools, action, _, cb) local args = { "files", "rm", "--pipe" } local project_root = vim.fs.abspath(vim.fs.normalize(action.project_root or "")) if project_root ~= "" then @@ -99,22 +99,22 @@ return function(opts) cb({ status = "error", data = { error = error, code = code } }) end end, - agent.chat.bufnr + tools.chat.bufnr ) end, }, output = { - ---@param self CodeCompanion.Agent.Tool + ---@param self CodeCompanion.Tools.Tool prompt = function(self, _) return string.format( "Remove %d files from VectorCode database?", #self.args.paths ) end, - ---@param self CodeCompanion.Agent.Tool - ---@param agent CodeCompanion.Agent - success = function(self, agent, _, _) - agent.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.") + ---@param self CodeCompanion.Tools.Tool + ---@param tools CodeCompanion.Tools + success = function(self, tools, _, _) + tools.chat:add_tool_output(self, "**VectorCode `files_rm` tool**: successful.") end, }, } diff --git a/lua/vectorcode/integrations/codecompanion/init.lua b/lua/vectorcode/integrations/codecompanion/init.lua index 736d02ef..5358c18b 100644 --- a/lua/vectorcode/integrations/codecompanion/init.lua +++ b/lua/vectorcode/integrations/codecompanion/init.lua @@ -1,50 +1,10 @@ ---@module "codecompanion" -local vc_config = require("vectorcode.config") -local check_cli_wrap = vc_config.check_cli_wrap - return { chat = { - ---@param component_cb (fun(result:VectorCode.QueryResult):string)? - make_slash_command = check_cli_wrap(function(component_cb) - vim.deprecate( - "vectorcode slash command", - "vectorcode tool/extension", - "0.8.0", - "VectorCode", - true - ) - return { - description = "Add relevant files from the codebase.", - ---@param chat CodeCompanion.Chat - callback = function(chat) - local codebase_prompt = "" - local vc_cache = vc_config.get_cacher_backend() - local bufnr = chat.context.bufnr - if not vc_cache.buf_is_registered(bufnr) then - return - end - codebase_prompt = - "The following are relevant files from the repository. Use them as extra context." - local query_result = vc_cache.make_prompt_component(bufnr, component_cb) - local id = tostring(query_result.count) .. " file(s) from codebase" - codebase_prompt = codebase_prompt .. query_result.content - chat:add_message( - { content = codebase_prompt, role = "user" }, - { visible = false, id = id } - ) - chat.references:add({ - source = "VectorCode", - name = "VectorCode", - id = id, - }) - end, - } - end), - ---@param subcommand sub_cmd ---@param opts VectorCode.CodeCompanion.ToolOpts - ---@return CodeCompanion.Agent.Tool + ---@return CodeCompanion.Tools.Tool make_tool = function(subcommand, opts) local has = require("codecompanion").has if has ~= nil and has("function-calling") then diff --git a/lua/vectorcode/integrations/codecompanion/ls_tool.lua b/lua/vectorcode/integrations/codecompanion/ls_tool.lua index 092aea50..32d77367 100644 --- a/lua/vectorcode/integrations/codecompanion/ls_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/ls_tool.lua @@ -25,7 +25,7 @@ local get_ls_tool_opts = function(opts) end ---@param opts VectorCode.CodeCompanion.LsToolOpts ----@return CodeCompanion.Agent.Tool +---@return CodeCompanion.Tools.Tool return function(opts) opts = get_ls_tool_opts(opts) local job_runner = @@ -33,13 +33,13 @@ return function(opts) opts.use_lsp ) local tool_name = "vectorcode_ls" - ---@type CodeCompanion.Agent.Tool|{} + ---@type CodeCompanion.Tools.Tool|{} return { name = tool_name, cmds = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@return nil|{ status: string, data: string } - function(agent, _, _, cb) + function(tools, _, _, cb) job_runner.run_async({ "ls", "--pipe" }, function(result, error) if vim.islist(result) and #result > 0 then cb({ status = "success", data = result }) @@ -52,7 +52,7 @@ return function(opts) data = error, }) end - end, agent.chat.bufnr) + end, tools.chat.bufnr) end, }, schema = { @@ -73,9 +73,9 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe }, }, output = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param stdout VectorCode.LsResult[][] - success = function(_, agent, _, stdout) + success = function(_, tools, _, stdout) stdout = stdout[1] local user_message for i, col in ipairs(stdout) do @@ -85,8 +85,8 @@ Where relevant, use paths from this tool as the `project_root` parameter in othe else user_message = "" end - agent.chat:add_tool_output( - agent.tool, + tools.chat:add_tool_output( + tools.tool, string.format("%s", col["project-root"]), user_message ) diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index 906353dc..296c32ec 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -164,7 +164,7 @@ local result_tracker = {} ---@param chat CodeCompanion.Chat ---@return VectorCode.QueryResult[] local filter_results = function(results, chat) - local existing_refs = chat.context_items or chat.refs or {} + local existing_refs = chat.context_items or {} existing_refs = vim .iter(existing_refs) @@ -316,7 +316,7 @@ When summarising the code, pay extra attention on information related to the que end ---@param opts VectorCode.CodeCompanion.QueryToolOpts? ----@return CodeCompanion.Agent.Tool +---@return CodeCompanion.Tools.Tool return check_cli_wrap(function(opts) opts = get_query_tool_opts(opts) assert( @@ -337,10 +337,10 @@ return check_cli_wrap(function(opts) return { name = tool_name, cmds = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param action QueryToolArgs ---@return nil|{ status: string, data: string } - function(agent, action, _, cb) + function(tools, action, _, cb) logger.info( "CodeCompanion query tool called with the following arguments:\n", action @@ -387,12 +387,14 @@ return check_cli_wrap(function(opts) end end - -- TODO: remove the `chat.refs` compatibility code when the upstream PR is released. - local context_items = agent.chat.context_items or agent.chat.refs - if opts.no_duplicate and context_items ~= nil and action.deduplicate then + if + opts.no_duplicate + and tools.chat.context_items ~= nil + and action.deduplicate + then -- exclude files that has been added to the context local existing_files = { "--exclude" } - for _, ref in pairs(context_items) do + for _, ref in pairs(tools.chat.context_items) do if ref.source == cc_common.tool_result_source then table.insert(existing_files, ref.id) elseif type(ref.path) == "string" then @@ -421,7 +423,7 @@ return check_cli_wrap(function(opts) if 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(agent.chat, result) --[[@as boolean]] + summary_opts.enabled = summary_opts.enabled(tools.chat, result) --[[@as boolean]] end if @@ -431,7 +433,7 @@ return check_cli_wrap(function(opts) then -- NOTE: deduplication in summary mode prevents the model from requesting -- the same content without summarysation. - result = filter_results(result, agent.chat) + result = filter_results(result, tools.chat) end local max_result = #result @@ -457,7 +459,7 @@ return check_cli_wrap(function(opts) data = error, }) end - end, agent.chat.bufnr) + end, tools.chat.bufnr) end, }, schema = { @@ -530,10 +532,10 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES }, }, output = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param cmd QueryToolArgs ---@param stderr table|string - error = function(self, agent, cmd, stderr) + error = function(self, tools, cmd, stderr) logger.error( ("CodeCompanion tool with command %s thrown with the following error: %s"):format( vim.inspect(cmd), @@ -543,7 +545,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES stderr = cc_common.flatten_table_to_string(stderr) if string.find(stderr, "InvalidCollectionException") then if cmd.project_root then - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, string.format( "`%s` hasn't been vectorised. Please use the `vectorcode_vectorise` tool or vectorise it from the CLI.", @@ -551,13 +553,13 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES ) ) else - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, "Failed to query from the requested project. Please verify the available projects via the `vectorcode_ls` tool or run it from the CLI." ) end else - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, string.format( "**VectorCode `query` Tool**: Failed with error:\n```\n%s\n```", @@ -566,17 +568,17 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES ) end end, - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param cmd QueryToolArgs ---@param stdout VectorCode.CodeCompanion.QueryToolResult[] - success = function(self, agent, cmd, stdout) + success = function(self, tools, cmd, stdout) stdout = stdout[1] logger.info( ("CodeCompanion tool with command %s finished."):format(vim.inspect(cmd)) ) if vim.tbl_isempty(stdout.raw_results) then logger.info("CodeCompanion query tool recieved empty result.") - return agent.chat:add_tool_output( + return tools.chat:add_tool_output( self, string.format( "`%s` tool returned empty result. Please retry without deduplication.", @@ -585,7 +587,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES "**VectorCode `query` Tool**: Retrieved 0 result. Retrying..." ) end - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, stdout.summary or table.concat(vim @@ -597,10 +599,9 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES string.format("**VectorCode Tool**: Retrieved %d %s(s)", stdout.count, mode) ) if not opts.chunk_mode then - local context = agent.chat.context or agent.chat.references for _, result in pairs(stdout.raw_results) do -- skip referencing because there will be multiple chunks with the same path (id). - context:add({ + tools.chat.context:add({ source = cc_common.tool_result_source, id = result.path, path = result.path, diff --git a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua index 5291b8b7..0846738f 100644 --- a/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/vectorise_tool.lua @@ -29,13 +29,13 @@ local get_vectorise_tool_opts = function(opts) end ---@param opts VectorCode.CodeCompanion.VectoriseToolOpts|{}|nil ----@return CodeCompanion.Agent.Tool +---@return CodeCompanion.Tools return function(opts) opts = get_vectorise_tool_opts(opts) local tool_name = "vectorcode_vectorise" local job_runner = cc_common.initialise_runner(opts.use_lsp) - ---@type CodeCompanion.Agent.Tool|{} + ---@type CodeCompanion.Tools|{} return { name = tool_name, schema = { @@ -66,10 +66,10 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv }, }, cmds = { - ---@param agent CodeCompanion.Agent + ---@param tools CodeCompanion.Tools ---@param action VectoriseToolArgs ---@return nil|{ status: string, data: string } - function(agent, action, _, cb) + function(tools, action, _, cb) local args = { "vectorise", "--pipe" } local project_root = vim.fs.abspath(vim.fs.normalize(action.project_root or "")) if project_root ~= "" then @@ -118,22 +118,22 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv cb({ status = "error", data = { error = error, code = code } }) end end, - agent.chat.bufnr + tools.chat.bufnr ) end, }, output = { - ---@param self CodeCompanion.Agent.Tool + ---@param self CodeCompanion.Tools.Tool prompt = function(self, _) return string.format("Vectorise %d files with VectorCode?", #self.args.paths) end, - ---@param self CodeCompanion.Agent.Tool - ---@param agent CodeCompanion.Agent + ---@param self CodeCompanion.Tools + ---@param tools CodeCompanion.Tools ---@param cmd VectoriseToolArgs ---@param stdout VectorCode.VectoriseResult[] - success = function(self, agent, cmd, stdout) + success = function(self, tools, cmd, stdout) stdout = stdout[1] - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, string.format( [[**VectorCode Vectorise Tool**: @@ -151,7 +151,7 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv ) ) if cmd.project_root and cmd.project_root then - agent.chat:add_tool_output( + tools.chat:add_tool_output( self, string.format("\nThe files were added to `%s`", cmd.project_root), ""