fix: models list (#2111)
This commit is contained in:
@@ -6,12 +6,32 @@ local Selector = require("avante.ui.selector")
|
|||||||
---@class avante.ModelSelector
|
---@class avante.ModelSelector
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
M.models_list_invoked = {}
|
||||||
|
M.models_list_returned = {}
|
||||||
|
|
||||||
|
local models_list_cached_result = {}
|
||||||
|
|
||||||
---@param provider_name string
|
---@param provider_name string
|
||||||
---@param cfg table
|
---@param cfg table
|
||||||
---@return table
|
---@return table
|
||||||
local function create_model_entries(provider_name, cfg)
|
local function create_model_entries(provider_name, cfg)
|
||||||
if cfg.models_list then
|
if cfg.models_list then
|
||||||
local models_list = type(cfg.models_list) == "function" and cfg:models_list() or cfg.models_list
|
local models_list
|
||||||
|
if type(cfg.models_list) == "function" then
|
||||||
|
if M.models_list_invoked[cfg.models_list] then return {} end
|
||||||
|
M.models_list_invoked[cfg.models_list] = true
|
||||||
|
local cached_result = models_list_cached_result[cfg.models_list]
|
||||||
|
if cached_result then
|
||||||
|
models_list = cached_result
|
||||||
|
else
|
||||||
|
models_list = cfg.models_list()
|
||||||
|
models_list_cached_result[cfg.models_list] = models_list
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if M.models_list_returned[cfg.models_list] then return {} end
|
||||||
|
M.models_list_returned[cfg.models_list] = true
|
||||||
|
models_list = cfg.models_list
|
||||||
|
end
|
||||||
if not models_list then return {} end
|
if not models_list then return {} end
|
||||||
-- If models_list is defined, use it to create entries
|
-- If models_list is defined, use it to create entries
|
||||||
local models = vim
|
local models = vim
|
||||||
@@ -41,6 +61,8 @@ local function create_model_entries(provider_name, cfg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.open()
|
function M.open()
|
||||||
|
M.models_list_invoked = {}
|
||||||
|
M.models_list_returned = {}
|
||||||
local models = {}
|
local models = {}
|
||||||
|
|
||||||
-- Collect models from main providers and vendors
|
-- Collect models from main providers and vendors
|
||||||
|
|||||||
@@ -215,6 +215,10 @@ setmetatable(M, { __index = OpenAI })
|
|||||||
|
|
||||||
function M:models_list()
|
function M:models_list()
|
||||||
if M._model_list_cache then return M._model_list_cache end
|
if M._model_list_cache then return M._model_list_cache end
|
||||||
|
if not M._is_setup then M.setup() end
|
||||||
|
-- refresh token synchronously, only if it has expired
|
||||||
|
-- (this should rarely happen, as we refresh the token in the background)
|
||||||
|
H.refresh_token(false, false)
|
||||||
local curl_opts = {
|
local curl_opts = {
|
||||||
headers = {
|
headers = {
|
||||||
["Content-Type"] = "application/json",
|
["Content-Type"] = "application/json",
|
||||||
@@ -341,6 +345,8 @@ function M.setup_file_watcher()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M._is_setup = false
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
local copilot_token_file = Path:new(copilot_path)
|
local copilot_token_file = Path:new(copilot_path)
|
||||||
|
|
||||||
@@ -369,6 +375,7 @@ function M.setup()
|
|||||||
|
|
||||||
require("avante.tokenizers").setup(M.tokenizer_id)
|
require("avante.tokenizers").setup(M.tokenizer_id)
|
||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
|
M._is_setup = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.cleanup()
|
function M.cleanup()
|
||||||
|
|||||||
Reference in New Issue
Block a user