From 1c4e0621995480c7c8644751908fd9ac9cd59e90 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Jun 2025 17:28:27 +0800 Subject: [PATCH] fix: models list (#2111) --- lua/avante/model_selector.lua | 24 +++++++++++++++++++++++- lua/avante/providers/copilot.lua | 7 +++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lua/avante/model_selector.lua b/lua/avante/model_selector.lua index c49f0a8..d6d4392 100644 --- a/lua/avante/model_selector.lua +++ b/lua/avante/model_selector.lua @@ -6,12 +6,32 @@ local Selector = require("avante.ui.selector") ---@class avante.ModelSelector local M = {} +M.models_list_invoked = {} +M.models_list_returned = {} + +local models_list_cached_result = {} + ---@param provider_name string ---@param cfg table ---@return table local function create_model_entries(provider_name, cfg) 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 models_list is defined, use it to create entries local models = vim @@ -41,6 +61,8 @@ local function create_model_entries(provider_name, cfg) end function M.open() + M.models_list_invoked = {} + M.models_list_returned = {} local models = {} -- Collect models from main providers and vendors diff --git a/lua/avante/providers/copilot.lua b/lua/avante/providers/copilot.lua index fa3ea2f..ad258b4 100644 --- a/lua/avante/providers/copilot.lua +++ b/lua/avante/providers/copilot.lua @@ -215,6 +215,10 @@ setmetatable(M, { __index = OpenAI }) function M:models_list() 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 = { headers = { ["Content-Type"] = "application/json", @@ -341,6 +345,8 @@ function M.setup_file_watcher() ) end +M._is_setup = false + function M.setup() local copilot_token_file = Path:new(copilot_path) @@ -369,6 +375,7 @@ function M.setup() require("avante.tokenizers").setup(M.tokenizer_id) vim.g.avante_login = true + M._is_setup = true end function M.cleanup()