refactor: remove redundant local field to facilitate provider configuration (#858)
This commit is contained in:
parent
4acdcb6e8b
commit
ff85b9c1e2
@ -24,7 +24,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 4096,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
copilot = {
|
||||
@ -44,7 +43,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 4096,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
claude = {
|
||||
@ -53,7 +51,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 8000,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
gemini = {
|
||||
@ -62,7 +59,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 4096,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
vertex = {
|
||||
@ -71,7 +67,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 4096,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
cohere = {
|
||||
@ -80,7 +75,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 4096,
|
||||
["local"] = false,
|
||||
},
|
||||
---To add support for custom provider, follow the format below
|
||||
---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details
|
||||
@ -93,7 +87,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 8000,
|
||||
["local"] = false,
|
||||
},
|
||||
---@type AvanteSupportedProvider
|
||||
["claude-opus"] = {
|
||||
@ -102,7 +95,6 @@ M.defaults = {
|
||||
timeout = 30000, -- Timeout in milliseconds
|
||||
temperature = 0,
|
||||
max_tokens = 8000,
|
||||
["local"] = false,
|
||||
},
|
||||
},
|
||||
---Specify the behaviour of avante.nvim
|
||||
|
@ -22,7 +22,7 @@ M.parse_curl_args = function(provider, code_opts)
|
||||
local headers = {
|
||||
["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 {
|
||||
url = Utils.url_join(
|
||||
|
@ -101,7 +101,8 @@ M.parse_curl_args = function(provider, prompt_opts)
|
||||
["anthropic-version"] = "2023-06-01",
|
||||
["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)
|
||||
|
||||
|
@ -82,7 +82,7 @@ M.parse_curl_args = function(provider, code_opts)
|
||||
.. "."
|
||||
.. 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 {
|
||||
url = Utils.url_join(base.endpoint, "/chat"),
|
||||
|
@ -118,7 +118,7 @@ end
|
||||
---@field github_token CopilotToken?
|
||||
M.state = nil
|
||||
|
||||
M.api_key_name = P.AVANTE_INTERNAL_KEY
|
||||
M.api_key_name = ""
|
||||
M.tokenizer_id = "gpt-4o"
|
||||
M.role_map = {
|
||||
user = "user",
|
||||
|
@ -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 key = nil
|
||||
local value = nil
|
||||
|
||||
if cmd ~= nil then
|
||||
-- NOTE: in case api_key_name is cmd, and users still set envvar
|
||||
-- We will try to get envvar first
|
||||
if Opts._shellenv ~= nil and Opts._shellenv ~= M.AVANTE_INTERNAL_KEY then
|
||||
key = os.getenv(Opts._shellenv)
|
||||
if key ~= nil then
|
||||
---@diagnostic disable: no-unknown
|
||||
E.cache[Opts._shellenv] = key
|
||||
E.cache[cache_key] = key
|
||||
if Opts._shellenv ~= nil and Opts._shellenv ~= "" then
|
||||
value = os.getenv(Opts._shellenv)
|
||||
if value ~= nil then
|
||||
E.cache[cache_key] = value
|
||||
vim.g.avante_login = true
|
||||
return key
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
if type(cmd) == "string" then cmd = vim.split(cmd, " ", { trimempty = true }) end
|
||||
|
||||
Utils.debug("running command:", cmd)
|
||||
local exit_codes = { 0 }
|
||||
local ok, job_or_err = pcall(vim.system, cmd, { text = true }, function(result)
|
||||
Utils.debug("command result:", result)
|
||||
local code = result.code
|
||||
local stderr = result.stderr or ""
|
||||
local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
|
||||
if vim.tbl_contains(exit_codes, code) then
|
||||
key = stdout[1]
|
||||
E.cache[cache_key] = key
|
||||
value = stdout[1]
|
||||
E.cache[cache_key] = value
|
||||
vim.g.avante_login = true
|
||||
else
|
||||
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
|
||||
end
|
||||
else
|
||||
key = os.getenv(api_key_name)
|
||||
value = os.getenv(api_key_name)
|
||||
end
|
||||
|
||||
if key ~= nil then
|
||||
E.cache[cache_key] = key
|
||||
if value ~= nil then
|
||||
E.cache[cache_key] = value
|
||||
vim.g.avante_login = true
|
||||
end
|
||||
|
||||
return key
|
||||
return value
|
||||
end
|
||||
|
||||
--- initialize the environment variable for current neovim session.
|
||||
@ -161,17 +161,17 @@ end
|
||||
---@param opts {refresh: boolean, provider: AvanteProviderFunctor}
|
||||
---@private
|
||||
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
|
||||
return
|
||||
end
|
||||
|
||||
local var = opts.provider.api_key_name
|
||||
|
||||
opts.provider.setup()
|
||||
|
||||
-- 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
|
||||
|
||||
@ -248,16 +248,21 @@ end
|
||||
|
||||
E.REQUEST_LOGIN_PATTERN = "AvanteRequestLogin"
|
||||
|
||||
---@param provider Provider
|
||||
E.is_local = function(provider)
|
||||
local cur = M.get_config(provider)
|
||||
return cur["local"] ~= nil and cur["local"] or false
|
||||
---@param provider AvanteDefaultBaseProvider
|
||||
E.require_api_key = function(provider)
|
||||
if provider["local"] ~= nil then
|
||||
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
|
||||
|
||||
M.env = E
|
||||
|
||||
M.AVANTE_INTERNAL_KEY = "__avante_env_internal"
|
||||
|
||||
M = setmetatable(M, {
|
||||
---@param t avante.Providers
|
||||
---@param k Provider
|
||||
@ -272,7 +277,6 @@ M = setmetatable(M, {
|
||||
local BaseOpts = M.get_config(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
|
||||
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)
|
||||
else
|
||||
t[k] = Opts
|
||||
@ -280,7 +284,6 @@ M = setmetatable(M, {
|
||||
else
|
||||
local ok, module = pcall(require, "avante.providers." .. k)
|
||||
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)
|
||||
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].setup == nil then
|
||||
local base = M.parse_config(t[k])
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
@ -125,7 +125,8 @@ M.parse_curl_args = function(provider, code_opts)
|
||||
local headers = {
|
||||
["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
|
||||
local stream = true
|
||||
|
@ -16,6 +16,7 @@ M.parse_response = Gemini.parse_response
|
||||
|
||||
local function execute_command(command)
|
||||
local handle = io.popen(command)
|
||||
if not handle then error("Failed to execute command: " .. command) end
|
||||
local result = handle:read("*a")
|
||||
handle:close()
|
||||
return result:match("^%s*(.-)%s*$")
|
||||
|
Loading…
x
Reference in New Issue
Block a user