fix: updating provider config (#2183)

This commit is contained in:
yetone
2025-06-07 16:34:21 +08:00
committed by GitHub
parent 687da816f9
commit a2f31bd2e2
3 changed files with 42 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
local Utils = require("avante.utils") local Utils = require("avante.utils")
local P = require("avante.providers") local Providers = require("avante.providers")
---@class AvanteBedrockProviderFunctor ---@class AvanteBedrockProviderFunctor
local M = {} local M = {}
@@ -42,7 +42,7 @@ function M.setup()
end end
function M.load_model_handler() function M.load_model_handler()
local provider_conf, _ = P.parse_config(P["bedrock"]) local provider_conf, _ = Providers.parse_config(Providers["bedrock"])
local bedrock_model = provider_conf.model local bedrock_model = provider_conf.model
if provider_conf.model:match("anthropic") then bedrock_model = "claude" end if provider_conf.model:match("anthropic") then bedrock_model = "claude" end
@@ -99,7 +99,7 @@ end
---@param prompt_opts AvantePromptOptions ---@param prompt_opts AvantePromptOptions
---@return table ---@return table
function M:parse_curl_args(prompt_opts) function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self) local provider_conf, request_body = Providers.parse_config(self)
local access_key_id, secret_access_key, session_token, region local access_key_id, secret_access_key, session_token, region

View File

@@ -1,4 +1,4 @@
local api, fn = vim.api, vim.fn local api = vim.api
local Config = require("avante.config") local Config = require("avante.config")
local Utils = require("avante.utils") local Utils = require("avante.utils")
@@ -136,28 +136,34 @@ M = setmetatable(M, {
__index = function(t, k) __index = function(t, k)
if Config.providers[k] == nil then error("Failed to find provider: " .. k, 2) end if Config.providers[k] == nil then error("Failed to find provider: " .. k, 2) end
local provider_config = M.get_config(k) t[k] = setmetatable({}, {
__index = function(_, k_)
local provider_config = M.get_config(k)
if provider_config.__inherited_from ~= nil then if provider_config.__inherited_from ~= nil then
local base_provider_config = M.get_config(provider_config.__inherited_from) local base_provider_config = M.get_config(provider_config.__inherited_from)
local ok, module = pcall(require, "avante.providers." .. provider_config.__inherited_from) local ok, module = pcall(require, "avante.providers." .. provider_config.__inherited_from)
if not ok then error("Failed to load provider: " .. provider_config.__inherited_from, 2) end if not ok then error("Failed to load provider: " .. provider_config.__inherited_from, 2) end
provider_config = Utils.deep_extend_with_metatable("force", module, base_provider_config, provider_config) -- provider_config = Utils.deep_extend_with_metatable("force", module, base_provider_config, provider_config)
else provider_config = Utils.inherit({}, provider_config, base_provider_config, module)
local ok, module = pcall(require, "avante.providers." .. k) else
if ok then local ok, module = pcall(require, "avante.providers." .. k)
provider_config = Utils.deep_extend_with_metatable("force", module, provider_config) if ok then
elseif provider_config.parse_curl_args == nil then -- provider_config = Utils.deep_extend_with_metatable("force", module, provider_config)
error( provider_config = Utils.inherit({}, provider_config, module)
string.format( elseif provider_config.parse_curl_args == nil then
'The configuration of your provider "%s" is incorrect, missing the `__inherited_from` attribute or a custom `parse_curl_args` function. Please fix your provider configuration. For more details, see: https://github.com/yetone/avante.nvim/wiki/Custom-providers', error(
k string.format(
) 'The configuration of your provider "%s" is incorrect, missing the `__inherited_from` attribute or a custom `parse_curl_args` function. Please fix your provider configuration. For more details, see: https://github.com/yetone/avante.nvim/wiki/Custom-providers',
) k_
end )
end )
end
end
t[k] = provider_config return provider_config[k_]
end,
})
if t[k].parse_api_key == nil then t[k].parse_api_key = function() return E.parse_envvar(t[k]) end end if t[k].parse_api_key == nil then t[k].parse_api_key = function() return E.parse_envvar(t[k]) end end
@@ -221,17 +227,10 @@ end
---@return AvanteDefaultBaseProvider provider_opts ---@return AvanteDefaultBaseProvider provider_opts
---@return table<string, any> request_body ---@return table<string, any> request_body
function M.parse_config(opts) function M.parse_config(opts)
---@type AvanteDefaultBaseProvider
local provider_opts = {}
for key, value in pairs(opts) do
if key ~= "extra_request_body" then provider_opts[key] = value end
end
---@type table<string, any> ---@type table<string, any>
local request_body = opts.extra_request_body or {} local request_body = opts.extra_request_body or {}
return provider_opts, request_body return Utils.inherit({}, opts), request_body
end end
---@param provider_name avante.ProviderName ---@param provider_name avante.ProviderName

View File

@@ -1133,6 +1133,17 @@ function M.icon(string_with_icon, utf8_fallback)
end end
end end
function M.inherit(base, ...)
local children = { ... }
return setmetatable(base, {
__index = function(_, k)
for _, child in ipairs(children) do
if child[k] ~= nil then return child[k] end
end
end,
})
end
function M.deep_extend_with_metatable(behavior, ...) function M.deep_extend_with_metatable(behavior, ...)
local tables = { ... } local tables = { ... }
local base = tables[1] local base = tables[1]