2024-08-17 15:14:30 +08:00
---NOTE: user will be merged with defaults and
---we add a default var_accessor for this table to config values.
2024-08-28 14:43:14 -04:00
2024-09-03 05:12:07 -04:00
local Utils = require ( " avante.utils " )
2024-08-28 14:43:14 -04:00
2024-08-17 15:14:30 +08:00
---@class avante.CoreConfig: avante.Config
2024-08-15 17:45:46 +08:00
local M = { }
2024-08-17 15:14:30 +08:00
---@class avante.Config
M.defaults = {
2024-08-19 05:40:57 -04:00
debug = false ,
2024-09-02 12:22:48 -04:00
---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | [string]
2024-08-28 20:51:20 +08:00
provider = " claude " , -- Only recommend using Claude
2024-08-30 19:07:52 -04:00
---@alias Tokenizer "tiktoken" | "hf"
-- Used for counting tokens and encoding text.
-- By default, we will use tiktoken.
-- For most providers that we support we will determine this automatically.
-- If you wish to use a given implementation, then you can override it here.
tokenizer = " tiktoken " ,
2024-09-03 04:09:13 -04:00
---@alias AvanteSystemPrompt string
-- Default system prompt. Users can override this with their own prompt
-- You can use `require('avante.config').override({system_prompt = "MY_SYSTEM_PROMPT"}) conditionally
-- in your own autocmds to do it per directory, or that fit your needs.
system_prompt = [ [
You are an excellent programming expert .
] ] ,
2024-08-19 08:35:36 -04:00
---@type AvanteSupportedProvider
2024-08-15 17:45:46 +08:00
openai = {
2024-08-25 00:32:16 +08:00
endpoint = " https://api.openai.com/v1 " ,
2024-08-15 17:45:46 +08:00
model = " gpt-4o " ,
2024-08-24 17:52:38 -04:00
timeout = 30000 , -- Timeout in milliseconds
2024-08-15 17:45:46 +08:00
temperature = 0 ,
max_tokens = 4096 ,
2024-08-19 08:35:36 -04:00
[ " local " ] = false ,
2024-08-15 17:45:46 +08:00
} ,
2024-08-29 23:36:39 -04:00
---@type AvanteSupportedProvider
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 = 4096 ,
} ,
2024-08-19 08:35:36 -04:00
---@type AvanteAzureProvider
2024-08-15 17:45:46 +08:00
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 " ,
2024-08-24 17:52:38 -04:00
timeout = 30000 , -- Timeout in milliseconds
2024-08-15 17:45:46 +08:00
temperature = 0 ,
max_tokens = 4096 ,
2024-08-19 08:35:36 -04:00
[ " local " ] = false ,
2024-08-15 17:45:46 +08:00
} ,
2024-08-19 08:35:36 -04:00
---@type AvanteSupportedProvider
2024-08-15 17:45:46 +08:00
claude = {
endpoint = " https://api.anthropic.com " ,
model = " claude-3-5-sonnet-20240620 " ,
2024-08-24 17:52:38 -04:00
timeout = 30000 , -- Timeout in milliseconds
2024-08-15 17:45:46 +08:00
temperature = 0 ,
2024-08-30 18:53:49 +08:00
max_tokens = 8000 ,
2024-08-23 09:36:40 -04:00
[ " local " ] = false ,
2024-08-15 17:45:46 +08:00
} ,
2024-08-24 17:52:38 -04:00
---@type AvanteSupportedProvider
2024-08-21 17:52:25 +01:00
gemini = {
2024-08-22 01:48:40 -04:00
endpoint = " https://generativelanguage.googleapis.com/v1beta/models " ,
2024-08-27 18:05:47 -04:00
model = " gemini-1.5-flash-latest " ,
2024-08-24 17:52:38 -04:00
timeout = 30000 , -- Timeout in milliseconds
2024-08-23 09:36:40 -04:00
temperature = 0 ,
max_tokens = 4096 ,
[ " local " ] = false ,
} ,
2024-08-25 14:26:42 +08:00
---@type AvanteSupportedProvider
2024-08-23 09:36:40 -04:00
cohere = {
2024-08-24 17:52:38 -04:00
endpoint = " https://api.cohere.com/v1 " ,
2024-08-30 13:39:36 -04:00
model = " command-r-plus-08-2024 " ,
2024-08-24 17:52:38 -04:00
timeout = 30000 , -- Timeout in milliseconds
2024-08-23 09:36:40 -04:00
temperature = 0 ,
2024-08-30 13:39:36 -04:00
max_tokens = 4096 ,
2024-08-22 01:48:40 -04:00
[ " local " ] = false ,
2024-08-21 17:52:25 +01:00
} ,
2024-08-19 05:40:57 -04:00
---To add support for custom provider, follow the format below
---See https://github.com/yetone/avante.nvim/README.md#custom-providers for more details
2024-08-19 08:35:36 -04:00
---@type {[string]: AvanteProvider}
2024-08-18 22:20:29 -04:00
vendors = { } ,
2024-08-19 05:40:57 -04:00
---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.
2024-08-29 01:09:26 -04:00
---2. auto_set_keymaps : Whether to automatically set the keymap for the current line. Default to true.
--- Note that avante will safely set these keymap. See https://github.com/yetone/avante.nvim/wiki#keymaps-and-api-i-guess for more details.
---3. auto_set_highlight_group : Whether to automatically set the highlight group for the current line. Default to true.
---4. support_paste_from_clipboard : Whether to support pasting image from clipboard. This will be determined automatically based whether img-clip is available or not.
2024-08-17 16:31:24 -04:00
behaviour = {
2024-09-03 14:03:59 +08:00
auto_suggestions = false , -- Experimental stage
2024-08-24 20:15:45 -04:00
auto_set_highlight_group = true ,
2024-08-29 01:09:26 -04:00
auto_set_keymaps = true ,
2024-08-19 05:40:57 -04:00
auto_apply_diff_after_generation = false ,
2024-08-27 06:57:29 -04:00
support_paste_from_clipboard = false ,
2024-08-17 16:31:24 -04:00
} ,
2024-08-25 03:12:53 -04:00
history = {
2024-09-03 05:12:07 -04:00
storage_path = vim.fn . stdpath ( " state " ) .. " /avante " ,
2024-08-28 11:52:12 -04:00
paste = {
extension = " png " ,
filename = " pasted-%Y-%m-%d-%H-%M-%S " ,
} ,
2024-08-25 03:12:53 -04:00
} ,
2024-08-15 17:45:46 +08:00
highlights = {
2024-08-17 14:14:02 -04:00
---@type AvanteConflictHighlights
2024-08-15 17:45:46 +08:00
diff = {
2024-08-19 05:40:57 -04:00
current = " DiffText " ,
incoming = " DiffAdd " ,
2024-08-15 17:45:46 +08:00
} ,
} ,
mappings = {
2024-08-28 23:56:00 -04:00
---@class AvanteConflictMappings
2024-08-15 17:45:46 +08:00
diff = {
ours = " co " ,
theirs = " ct " ,
2024-08-29 20:32:00 +08:00
all_theirs = " ca " ,
2024-08-15 17:45:46 +08:00
both = " cb " ,
2024-08-28 23:25:45 -07:00
cursor = " cc " ,
2024-08-15 17:45:46 +08:00
next = " ]x " ,
prev = " [x " ,
} ,
2024-09-03 14:03:59 +08:00
suggestion = {
accept = " <M-l> " ,
next = " <M-]> " ,
prev = " <M-[> " ,
dismiss = " <C-]> " ,
} ,
2024-08-17 10:39:59 -04:00
jump = {
next = " ]] " ,
prev = " [[ " ,
} ,
2024-08-25 00:51:59 +08:00
submit = {
normal = " <CR> " ,
insert = " <C-s> " ,
} ,
2024-08-28 23:56:00 -04:00
-- NOTE: The following will be safely set by avante.nvim
ask = " <leader>aa " ,
edit = " <leader>ae " ,
refresh = " <leader>ar " ,
2024-08-25 00:16:25 -04:00
toggle = {
2024-09-02 12:22:48 -04:00
default = " <leader>at " ,
2024-08-25 00:16:25 -04:00
debug = " <leader>ad " ,
hint = " <leader>ah " ,
2024-09-06 01:09:02 -04:00
suggestion = " <leader>as " ,
2024-08-25 00:16:25 -04:00
} ,
2024-08-15 17:45:46 +08:00
} ,
2024-08-17 15:14:30 +08:00
windows = {
2024-09-01 19:33:04 -04:00
---@alias AvantePosition "right" | "left" | "top" | "bottom"
2024-09-01 15:52:16 +08:00
position = " right " ,
2024-08-22 01:48:40 -04:00
wrap = true , -- similar to vim.o.wrap
2024-08-31 23:35:35 +08:00
width = 30 , -- default % based on available width in vertical layout
height = 30 , -- default % based on available height in horizontal layout
2024-08-21 23:31:11 +08:00
sidebar_header = {
2024-08-21 11:12:10 -04:00
align = " center " , -- left, center, right for title
2024-08-21 23:31:11 +08:00
rounded = true ,
2024-08-21 11:12:10 -04:00
} ,
2024-08-27 03:44:05 -04:00
input = {
prefix = " > " ,
} ,
2024-08-27 13:13:38 -04:00
edit = {
border = " rounded " ,
} ,
2024-08-17 15:14:30 +08:00
} ,
2024-09-02 12:22:48 -04:00
--- @class AvanteConflictConfig
2024-08-17 14:14:02 -04:00
diff = {
autojump = true ,
} ,
2024-08-19 12:40:33 +10:00
--- @class AvanteHintsConfig
hints = {
2024-08-19 05:11:38 -04:00
enabled = true ,
2024-08-19 12:40:33 +10:00
} ,
2024-08-15 17:45:46 +08:00
}
2024-08-17 15:14:30 +08:00
---@type avante.Config
M.options = { }
2024-09-02 12:22:48 -04:00
---@class avante.ConflictConfig: AvanteConflictConfig
2024-08-17 14:14:02 -04:00
---@field mappings AvanteConflictMappings
---@field highlights AvanteConflictHighlights
M.diff = { }
2024-09-02 12:22:48 -04:00
---@type Provider[]
M.providers = { }
2024-08-17 15:14:30 +08:00
---@param opts? avante.Config
function M . setup ( opts )
2024-09-03 05:12:07 -04:00
vim.validate ( { opts = { opts , " table " , true } } )
2024-09-01 19:33:04 -04:00
2024-08-28 14:43:14 -04:00
M.options = vim.tbl_deep_extend (
" force " ,
M.defaults ,
opts or { } ,
---@type avante.Config
{
behaviour = {
2024-09-06 01:09:02 -04:00
support_paste_from_clipboard = M.support_paste_image ( ) ,
2024-08-28 14:43:14 -04:00
} ,
}
)
2024-09-02 12:22:48 -04:00
M.providers = vim
. iter ( M.defaults )
2024-09-03 04:19:54 -04:00
: filter ( function ( _ , value ) return type ( value ) == " table " and value.endpoint ~= nil end )
2024-09-02 12:22:48 -04:00
: fold ( { } , function ( acc , k )
acc = vim.list_extend ( { } , acc )
acc = vim.list_extend ( acc , { k } )
return acc
end )
2024-08-17 14:14:02 -04:00
2024-09-03 05:12:07 -04:00
vim.validate ( { provider = { M.options . provider , " string " , false } } )
2024-09-01 19:33:04 -04:00
2024-08-17 14:14:02 -04:00
M.diff = vim.tbl_deep_extend (
" force " ,
{ } ,
M.options . diff ,
{ mappings = M.options . mappings.diff , highlights = M.options . highlights.diff }
)
2024-08-22 01:48:40 -04:00
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
2024-09-03 05:12:07 -04:00
vim.validate ( { vendors = { M.options . vendors , " table " , true } } )
2024-09-02 12:22:48 -04:00
M.providers = vim.list_extend ( M.providers , vim.tbl_keys ( M.options . vendors ) )
2024-08-22 01:48:40 -04:00
end
2024-08-17 15:14:30 +08:00
end
2024-08-18 22:20:29 -04:00
---@param opts? avante.Config
function M . override ( opts )
2024-09-03 05:12:07 -04:00
vim.validate ( { opts = { opts , " table " , true } } )
2024-09-01 19:33:04 -04:00
M.options = vim.tbl_deep_extend ( " force " , M.options , opts or { } )
2024-08-22 01:48:40 -04:00
M.diff = vim.tbl_deep_extend (
" force " ,
{ } ,
M.options . diff ,
{ mappings = M.options . mappings.diff , highlights = M.options . highlights.diff }
)
2024-08-29 17:08:39 -04:00
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
2024-09-03 04:19:54 -04:00
if not vim.tbl_contains ( M.providers , k ) then M.providers = vim.list_extend ( M.providers , { k } ) end
2024-08-29 17:08:39 -04:00
end
2024-09-03 05:12:07 -04:00
vim.validate ( { vendors = { M.options . vendors , " table " , true } } )
2024-08-29 17:08:39 -04:00
end
2024-08-18 22:20:29 -04:00
end
2024-08-17 15:14:30 +08:00
M = setmetatable ( M , {
__index = function ( _ , k )
2024-09-03 04:19:54 -04:00
if M.options [ k ] then return M.options [ k ] end
2024-08-17 15:14:30 +08:00
end ,
} )
2024-09-06 01:09:02 -04:00
M.support_paste_image = function ( ) return Utils.has ( " img-clip.nvim " ) or Utils.has ( " img-clip " ) end
2024-08-29 17:08:39 -04:00
2024-09-03 04:19:54 -04:00
M.get_window_width = function ( ) return math.ceil ( vim.o . columns * ( M.windows . width / 100 ) ) end
2024-08-15 17:45:46 +08:00
2024-08-22 01:48:40 -04:00
---@param provider Provider
---@return boolean
2024-09-03 04:19:54 -04:00
M.has_provider = function ( provider ) return M.options [ provider ] ~= nil or M.vendors [ provider ] ~= nil end
2024-08-22 01:48:40 -04:00
---get supported providers
---@param provider Provider
2024-08-24 17:52:38 -04:00
---@return AvanteProviderFunctor
2024-08-22 01:48:40 -04:00
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
2024-08-24 17:52:38 -04:00
M.BASE_PROVIDER_KEYS = {
" endpoint " ,
" model " ,
" deployment " ,
" api_version " ,
" proxy " ,
" allow_insecure " ,
" api_key_name " ,
" timeout " ,
2024-08-26 01:13:12 -04:00
-- internal
" local " ,
" _shellenv " ,
2024-08-31 13:39:50 -04:00
" tokenizer_id " ,
2024-09-03 04:09:13 -04:00
" use_xml_format " ,
2024-08-24 17:52:38 -04:00
}
2024-08-22 01:48:40 -04:00
2024-08-15 17:45:46 +08:00
return M