feat(model): add model selection (#961)

* feat(model): add model selection with keybinding

* lint

* rename model_select to model_selector
This commit is contained in:
Michael Gendy
2025-02-18 20:01:21 +02:00
committed by GitHub
parent 85370a5a1b
commit 9a191abce5
4 changed files with 67 additions and 0 deletions

View File

@@ -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")

View File

@@ -318,6 +318,7 @@ M._defaults = {
files = {
add_current = "<leader>ac", -- Add current buffer to selected files
},
select_model = "<leader>a?", -- Select model command
},
windows = {
---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart"

View File

@@ -75,6 +75,7 @@ H.keymaps = function()
vim.keymap.set({ "n", "v" }, "<Plug>(AvanteConflictCursor)", function() Diff.choose("cursor") end)
vim.keymap.set("n", "<Plug>(AvanteConflictNextConflict)", function() Diff.find_next("ours") end)
vim.keymap.set("n", "<Plug>(AvanteConflictPrevConflict)", function() Diff.find_prev("ours") end)
vim.keymap.set("n", "<Plug>(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

View File

@@ -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