refactor: remove redundant local field to facilitate provider configuration (#858)

This commit is contained in:
yetone 2024-11-17 02:55:40 +08:00 committed by GitHub
parent 4acdcb6e8b
commit ff85b9c1e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 39 additions and 40 deletions

View File

@ -24,7 +24,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 4096, max_tokens = 4096,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
copilot = { copilot = {
@ -44,7 +43,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 4096, max_tokens = 4096,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
claude = { claude = {
@ -53,7 +51,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 8000, max_tokens = 8000,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
gemini = { gemini = {
@ -62,7 +59,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 4096, max_tokens = 4096,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
vertex = { vertex = {
@ -71,7 +67,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 4096, max_tokens = 4096,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
cohere = { cohere = {
@ -80,7 +75,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 4096, max_tokens = 4096,
["local"] = false,
}, },
---To add support for custom provider, follow the format below ---To add support for custom provider, follow the format below
---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details ---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details
@ -93,7 +87,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 8000, max_tokens = 8000,
["local"] = false,
}, },
---@type AvanteSupportedProvider ---@type AvanteSupportedProvider
["claude-opus"] = { ["claude-opus"] = {
@ -102,7 +95,6 @@ M.defaults = {
timeout = 30000, -- Timeout in milliseconds timeout = 30000, -- Timeout in milliseconds
temperature = 0, temperature = 0,
max_tokens = 8000, max_tokens = 8000,
["local"] = false,
}, },
}, },
---Specify the behaviour of avante.nvim ---Specify the behaviour of avante.nvim

View File

@ -22,7 +22,7 @@ M.parse_curl_args = function(provider, code_opts)
local headers = { local headers = {
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
} }
if not P.env.is_local("azure") then headers["api-key"] = provider.parse_api_key() end if P.env.require_api_key(base) then headers["api-key"] = provider.parse_api_key() end
return { return {
url = Utils.url_join( url = Utils.url_join(

View File

@ -101,7 +101,8 @@ M.parse_curl_args = function(provider, prompt_opts)
["anthropic-version"] = "2023-06-01", ["anthropic-version"] = "2023-06-01",
["anthropic-beta"] = "prompt-caching-2024-07-31", ["anthropic-beta"] = "prompt-caching-2024-07-31",
} }
if not P.env.is_local("claude") then headers["x-api-key"] = provider.parse_api_key() end
if P.env.require_api_key(base) then headers["x-api-key"] = provider.parse_api_key() end
local messages = M.parse_messages(prompt_opts) local messages = M.parse_messages(prompt_opts)

View File

@ -82,7 +82,7 @@ M.parse_curl_args = function(provider, code_opts)
.. "." .. "."
.. vim.version().patch, .. vim.version().patch,
} }
if not P.env.is_local("cohere") then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end if P.env.require_api_key(base) then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
return { return {
url = Utils.url_join(base.endpoint, "/chat"), url = Utils.url_join(base.endpoint, "/chat"),

View File

@ -118,7 +118,7 @@ end
---@field github_token CopilotToken? ---@field github_token CopilotToken?
M.state = nil M.state = nil
M.api_key_name = P.AVANTE_INTERNAL_KEY M.api_key_name = ""
M.tokenizer_id = "gpt-4o" M.tokenizer_id = "gpt-4o"
M.role_map = { M.role_map = {
user = "user", user = "user",

View File

@ -108,32 +108,32 @@ E.parse_envvar = function(Opts)
local cmd = type(api_key_name) == "table" and api_key_name or api_key_name:match("^cmd:(.*)") local cmd = type(api_key_name) == "table" and api_key_name or api_key_name:match("^cmd:(.*)")
local key = nil local value = nil
if cmd ~= nil then if cmd ~= nil then
-- NOTE: in case api_key_name is cmd, and users still set envvar -- NOTE: in case api_key_name is cmd, and users still set envvar
-- We will try to get envvar first -- We will try to get envvar first
if Opts._shellenv ~= nil and Opts._shellenv ~= M.AVANTE_INTERNAL_KEY then if Opts._shellenv ~= nil and Opts._shellenv ~= "" then
key = os.getenv(Opts._shellenv) value = os.getenv(Opts._shellenv)
if key ~= nil then if value ~= nil then
---@diagnostic disable: no-unknown E.cache[cache_key] = value
E.cache[Opts._shellenv] = key
E.cache[cache_key] = key
vim.g.avante_login = true vim.g.avante_login = true
return key return value
end end
end end
if type(cmd) == "string" then cmd = vim.split(cmd, " ", { trimempty = true }) end if type(cmd) == "string" then cmd = vim.split(cmd, " ", { trimempty = true }) end
Utils.debug("running command:", cmd)
local exit_codes = { 0 } local exit_codes = { 0 }
local ok, job_or_err = pcall(vim.system, cmd, { text = true }, function(result) local ok, job_or_err = pcall(vim.system, cmd, { text = true }, function(result)
Utils.debug("command result:", result)
local code = result.code local code = result.code
local stderr = result.stderr or "" local stderr = result.stderr or ""
local stdout = result.stdout and vim.split(result.stdout, "\n") or {} local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
if vim.tbl_contains(exit_codes, code) then if vim.tbl_contains(exit_codes, code) then
key = stdout[1] value = stdout[1]
E.cache[cache_key] = key E.cache[cache_key] = value
vim.g.avante_login = true vim.g.avante_login = true
else else
Utils.error("Failed to get API key: (error code" .. code .. ")\n" .. stderr, { once = true, title = "Avante" }) Utils.error("Failed to get API key: (error code" .. code .. ")\n" .. stderr, { once = true, title = "Avante" })
@ -145,15 +145,15 @@ E.parse_envvar = function(Opts)
return return
end end
else else
key = os.getenv(api_key_name) value = os.getenv(api_key_name)
end end
if key ~= nil then if value ~= nil then
E.cache[cache_key] = key E.cache[cache_key] = value
vim.g.avante_login = true vim.g.avante_login = true
end end
return key return value
end end
--- initialize the environment variable for current neovim session. --- initialize the environment variable for current neovim session.
@ -161,17 +161,17 @@ end
---@param opts {refresh: boolean, provider: AvanteProviderFunctor} ---@param opts {refresh: boolean, provider: AvanteProviderFunctor}
---@private ---@private
E.setup = function(opts) E.setup = function(opts)
if opts.provider["local"] then local var = opts.provider.api_key_name
if var == nil or var == "" then
vim.g.avante_login = true vim.g.avante_login = true
return return
end end
local var = opts.provider.api_key_name
opts.provider.setup() opts.provider.setup()
-- check if var is a all caps string -- check if var is a all caps string
if var == M.AVANTE_INTERNAL_KEY or type(var) == "table" or var:match("^cmd:(.*)") then return end if type(var) == "table" or var:match("^cmd:(.*)") then return end
local refresh = opts.refresh or false local refresh = opts.refresh or false
@ -248,16 +248,21 @@ end
E.REQUEST_LOGIN_PATTERN = "AvanteRequestLogin" E.REQUEST_LOGIN_PATTERN = "AvanteRequestLogin"
---@param provider Provider ---@param provider AvanteDefaultBaseProvider
E.is_local = function(provider) E.require_api_key = function(provider)
local cur = M.get_config(provider) if provider["local"] ~= nil then
return cur["local"] ~= nil and cur["local"] or false if provider["local"] then
vim.deprecate('"local" = true', "api_key_name = ''", "0.1.0", "avante.nvim")
else
vim.deprecate('"local" = false', "api_key_name", "0.1.0", "avante.nvim")
end
return not provider["local"]
end
return provider.api_key_name ~= nil and provider.api_key_name ~= ""
end end
M.env = E M.env = E
M.AVANTE_INTERNAL_KEY = "__avante_env_internal"
M = setmetatable(M, { M = setmetatable(M, {
---@param t avante.Providers ---@param t avante.Providers
---@param k Provider ---@param k Provider
@ -272,7 +277,6 @@ M = setmetatable(M, {
local BaseOpts = M.get_config(Opts.__inherited_from) local BaseOpts = M.get_config(Opts.__inherited_from)
local ok, module = pcall(require, "avante.providers." .. Opts.__inherited_from) local ok, module = pcall(require, "avante.providers." .. Opts.__inherited_from)
if not ok then error("Failed to load provider: " .. Opts.__inherited_from) end if not ok then error("Failed to load provider: " .. Opts.__inherited_from) end
Opts._shellenv = module.api_key_name ~= M.AVANTE_INTERNAL_KEY and module.api_key_name or nil
t[k] = vim.tbl_deep_extend("keep", Opts, BaseOpts, module) t[k] = vim.tbl_deep_extend("keep", Opts, BaseOpts, module)
else else
t[k] = Opts t[k] = Opts
@ -280,7 +284,6 @@ M = setmetatable(M, {
else else
local ok, module = pcall(require, "avante.providers." .. k) local ok, module = pcall(require, "avante.providers." .. k)
if not ok then error("Failed to load provider: " .. k) end if not ok then error("Failed to load provider: " .. k) end
Opts._shellenv = module.api_key_name ~= M.AVANTE_INTERNAL_KEY and module.api_key_name or nil
t[k] = vim.tbl_deep_extend("keep", Opts, module) t[k] = vim.tbl_deep_extend("keep", Opts, module)
end end
@ -294,8 +297,9 @@ M = setmetatable(M, {
if t[k].has == nil then t[k].has = function() return E.parse_envvar(t[k]) ~= nil end end if t[k].has == nil then t[k].has = function() return E.parse_envvar(t[k]) ~= nil end end
if t[k].setup == nil then if t[k].setup == nil then
local base = M.parse_config(t[k])
t[k].setup = function() t[k].setup = function()
if not E.is_local(k) then t[k].parse_api_key() end if E.require_api_key(base) then t[k].parse_api_key() end
require("avante.tokenizers").setup(t[k].tokenizer_id) require("avante.tokenizers").setup(t[k].tokenizer_id)
end end
end end

View File

@ -125,7 +125,8 @@ M.parse_curl_args = function(provider, code_opts)
local headers = { local headers = {
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
} }
if not P.env.is_local("openai") then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
if P.env.require_api_key(base) then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
-- NOTE: When using "o1" set the supported parameters only -- NOTE: When using "o1" set the supported parameters only
local stream = true local stream = true

View File

@ -16,6 +16,7 @@ M.parse_response = Gemini.parse_response
local function execute_command(command) local function execute_command(command)
local handle = io.popen(command) local handle = io.popen(command)
if not handle then error("Failed to execute command: " .. command) end
local result = handle:read("*a") local result = handle:read("*a")
handle:close() handle:close()
return result:match("^%s*(.-)%s*$") return result:match("^%s*(.-)%s*$")