diff --git a/lua/avante/api.lua b/lua/avante/api.lua index d5345b0..4f721c0 100644 --- a/lua/avante/api.lua +++ b/lua/avante/api.lua @@ -210,6 +210,8 @@ M.focus = function(opts) end end +M.select_model = function() require("avante.model_selector").open() end + return setmetatable(M, { __index = function(t, k) local module = require("avante") diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 23925b6..4846e54 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -318,6 +318,7 @@ M._defaults = { files = { add_current = "ac", -- Add current buffer to selected files }, + select_model = "a?", -- Select model command }, windows = { ---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart" diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 4c15bf4..c5e52e1 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -75,6 +75,7 @@ H.keymaps = function() vim.keymap.set({ "n", "v" }, "(AvanteConflictCursor)", function() Diff.choose("cursor") end) vim.keymap.set("n", "(AvanteConflictNextConflict)", function() Diff.find_next("ours") end) vim.keymap.set("n", "(AvanteConflictPrevConflict)", function() Diff.find_prev("ours") end) + vim.keymap.set("n", "(AvanteSelectModel)", function() require("avante.api").select_model() end) if Config.behaviour.auto_set_keymaps then Utils.safe_keymap_set( @@ -126,6 +127,12 @@ H.keymaps = function() noremap = true, silent = true, }) + Utils.safe_keymap_set( + "n", + Config.mappings.select_model, + function() require("avante.api").select_model() end, + { desc = "avante: select model" } + ) end if Config.behaviour.auto_suggestions then diff --git a/lua/avante/model_selector.lua b/lua/avante/model_selector.lua new file mode 100644 index 0000000..3127b90 --- /dev/null +++ b/lua/avante/model_selector.lua @@ -0,0 +1,57 @@ +local Utils = require("avante.utils") +local Config = require("avante.config") + +---@class avante.ModelSelector +local M = {} + +---@param provider string +---@param cfg table +---@return table? +local function create_model_entry(provider, cfg) + return cfg.model and { + name = provider .. "/" .. cfg.model, + provider = provider, + model = cfg.model, + } +end + +function M.open() + local models = {} + + -- Collect models from main providers and vendors + for _, provider in ipairs(Config.providers) do + local entry = create_model_entry(provider, Config.get_provider(provider)) + if entry then table.insert(models, entry) end + end + + for provider, cfg in pairs(Config.vendors or {}) do + if type(cfg) == "table" then + local entry = create_model_entry(provider, cfg) + if entry then table.insert(models, entry) end + end + end + + if #models == 0 then + Utils.warn("No models available in config") + return + end + + vim.ui.select(models, { + prompt = "Select Model:", + format_item = function(item) return item.name end, + }, function(choice) + if not choice then return end + + -- Switch provider if needed + if choice.provider ~= Config.provider then require("avante.providers").refresh(choice.provider) end + + -- Update config with new model + Config.override({ + [choice.provider] = vim.tbl_deep_extend("force", Config.get_provider(choice.provider), { model = choice.model }), + }) + + Utils.info("Switched to model: " .. choice.name) + end) +end + +return M