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:
57
lua/avante/model_selector.lua
Normal file
57
lua/avante/model_selector.lua
Normal 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
|
||||
Reference in New Issue
Block a user