avante.nvim/lua/avante/config.lua
Aaron Pham 8d52229f16
refactor(llm): cleanup providers for future ops (closes #134) (#147)
support allow_insecure and proxy ops

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
2024-08-22 01:48:40 -04:00

215 lines
5.7 KiB
Lua

---NOTE: user will be merged with defaults and
---we add a default var_accessor for this table to config values.
---
---@class avante.CoreConfig: avante.Config
local M = {}
---@class avante.Config
M.defaults = {
debug = false,
---Currently, default supported providers include "claude", "openai", "azure", "deepseek", "groq", "gemini"
---For custom provider, see README.md
---@alias Provider "openai" | "claude" | "azure" | "deepseek" | "groq" | "copilot" | "gemini" | string
provider = "claude",
---@type AvanteSupportedProvider
openai = {
endpoint = "https://api.openai.com",
model = "gpt-4o",
temperature = 0,
max_tokens = 4096,
["local"] = false,
},
---@type AvanteCopilotProvider
copilot = {
endpoint = "https://api.githubcopilot.com",
model = "gpt-4o-2024-05-13",
proxy = nil, -- [protocol://]host[:port] Use this proxy
allow_insecure = false, -- Allow insecure server connections
timeout = 30000, -- Timeout in milliseconds
temperature = 0,
max_tokens = 8192,
},
---@type AvanteAzureProvider
azure = {
endpoint = "", -- example: "https://<your-resource-name>.openai.azure.com"
deployment = "", -- Azure deployment name (e.g., "gpt-4o", "my-gpt-4o-deployment")
api_version = "2024-06-01",
temperature = 0,
max_tokens = 4096,
["local"] = false,
},
---@type AvanteSupportedProvider
claude = {
endpoint = "https://api.anthropic.com",
model = "claude-3-5-sonnet-20240620",
["local"] = false,
temperature = 0,
max_tokens = 4096,
},
---@type AvanteSupportedProvider
deepseek = {
endpoint = "https://api.deepseek.com",
model = "deepseek-coder",
temperature = 0,
max_tokens = 4096,
["local"] = false,
},
---@type AvanteSupportedProvider
groq = {
endpoint = "https://api.groq.com",
model = "llama-3.1-70b-versatile",
temperature = 0,
max_tokens = 4096,
["local"] = false,
},
---@type AvanteGeminiProvider
gemini = {
endpoint = "https://generativelanguage.googleapis.com/v1beta/models",
model = "gemini-1.5-pro",
["local"] = false,
},
---To add support for custom provider, follow the format below
---See https://github.com/yetone/avante.nvim/README.md#custom-providers for more details
---@type {[string]: AvanteProvider}
vendors = {},
---Specify the behaviour of avante.nvim
---1. auto_apply_diff_after_generation: Whether to automatically apply diff after LLM response.
--- This would simulate similar behaviour to cursor. Default to false.
behaviour = {
auto_apply_diff_after_generation = false,
},
highlights = {
---@type AvanteConflictHighlights
diff = {
current = "DiffText",
incoming = "DiffAdd",
},
},
mappings = {
ask = "<leader>aa",
edit = "<leader>ae",
refresh = "<leader>ar",
--- @class AvanteConflictMappings
diff = {
ours = "co",
theirs = "ct",
none = "c0",
both = "cb",
next = "]x",
prev = "[x",
},
jump = {
next = "]]",
prev = "[[",
},
},
windows = {
wrap = true, -- similar to vim.o.wrap
width = 30, -- default % based on available width
sidebar_header = {
align = "center", -- left, center, right for title
rounded = true,
},
prompt = {
prefix = "> ", -- prefix for the prompt
},
},
--- @class AvanteConflictUserConfig
diff = {
debug = false,
autojump = true,
---@type string | fun(): any
list_opener = "copen",
},
--- @class AvanteHintsConfig
hints = {
enabled = true,
},
}
---@type avante.Config
M.options = {}
---@class avante.ConflictConfig: AvanteConflictUserConfig
---@field mappings AvanteConflictMappings
---@field highlights AvanteConflictHighlights
M.diff = {}
---@class AvanteHintsConfig
---@field enabled boolean
M.hints = {}
---@param opts? avante.Config
function M.setup(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
M.diff = vim.tbl_deep_extend(
"force",
{},
M.options.diff,
{ mappings = M.options.mappings.diff, highlights = M.options.highlights.diff }
)
M.hints = vim.tbl_deep_extend("force", {}, M.options.hints)
if next(M.options.vendors) ~= nil then
for k, v in pairs(M.options.vendors) do
M.options.vendors[k] = type(v) == "function" and v() or v
end
end
end
---@param opts? avante.Config
function M.override(opts)
opts = opts or {}
M.options = vim.tbl_deep_extend("force", M.options, opts)
M.diff = vim.tbl_deep_extend(
"force",
{},
M.options.diff,
{ mappings = M.options.mappings.diff, highlights = M.options.highlights.diff }
)
M.hints = vim.tbl_deep_extend("force", {}, M.options.hints)
end
M = setmetatable(M, {
__index = function(_, k)
if M.options[k] then
return M.options[k]
end
end,
})
function M.get_window_width()
return math.ceil(vim.o.columns * (M.windows.width / 100))
end
---@param provider Provider
---@return boolean
M.has_provider = function(provider)
return M.options[provider] ~= nil or M.vendors[provider] ~= nil
end
---get supported providers
---@param provider Provider
---@return AvanteProvider | fun(): AvanteProvider
M.get_provider = function(provider)
if M.options[provider] ~= nil then
return vim.deepcopy(M.options[provider], true)
elseif M.vendors[provider] ~= nil then
return vim.deepcopy(M.vendors[provider], true)
else
error("Failed to find provider: " .. provider, 2)
end
end
M.BASE_PROVIDER_KEYS = { "endpoint", "model", "local", "deployment", "api_version", "proxy", "allow_insecure" }
---@return {width: integer, height: integer}
function M.get_sidebar_layout_options()
local width = M.get_window_width()
local height = vim.o.lines
return { width = width, height = height }
end
return M