support allow_insecure and proxy ops Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
44
lua/avante/providers/azure.lua
Normal file
44
lua/avante/providers/azure.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "AZURE_OPENAI_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = O.parse_message
|
||||
M.parse_response = O.parse_response
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
}
|
||||
if not P.env.is_local("azure") then
|
||||
headers["api-key"] = os.getenv(M.API_KEY)
|
||||
end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" })
|
||||
.. "/openai/deployments/"
|
||||
.. base.deployment
|
||||
.. "/chat/completions?api-version="
|
||||
.. base.api_version,
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = headers,
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
messages = M.parse_message(code_opts),
|
||||
stream = true,
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
112
lua/avante/providers/claude.lua
Normal file
112
lua/avante/providers/claude.lua
Normal file
@@ -0,0 +1,112 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local Tiktoken = require("avante.tiktoken")
|
||||
local P = require("avante.providers")
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "ANTHROPIC_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = function(opts)
|
||||
local code_prompt_obj = {
|
||||
type = "text",
|
||||
text = string.format("<code>```%s\n%s```</code>", opts.code_lang, opts.code_content),
|
||||
}
|
||||
|
||||
if Tiktoken.count(code_prompt_obj.text) > 1024 then
|
||||
code_prompt_obj.cache_control = { type = "ephemeral" }
|
||||
end
|
||||
|
||||
if opts.selected_code_content then
|
||||
code_prompt_obj.text = string.format("<code_context>```%s\n%s```</code_context>", opts.code_lang, opts.code_content)
|
||||
end
|
||||
|
||||
local message_content = {
|
||||
code_prompt_obj,
|
||||
}
|
||||
|
||||
if opts.selected_code_content then
|
||||
local selected_code_obj = {
|
||||
type = "text",
|
||||
text = string.format("<code>```%s\n%s```</code>", opts.code_lang, opts.selected_code_content),
|
||||
}
|
||||
|
||||
if Tiktoken.count(selected_code_obj.text) > 1024 then
|
||||
selected_code_obj.cache_control = { type = "ephemeral" }
|
||||
end
|
||||
|
||||
table.insert(message_content, selected_code_obj)
|
||||
end
|
||||
|
||||
table.insert(message_content, {
|
||||
type = "text",
|
||||
text = string.format("<question>%s</question>", opts.question),
|
||||
})
|
||||
|
||||
local user_prompt = opts.base_prompt
|
||||
|
||||
local user_prompt_obj = {
|
||||
type = "text",
|
||||
text = user_prompt,
|
||||
}
|
||||
|
||||
if Tiktoken.count(user_prompt_obj.text) > 1024 then
|
||||
user_prompt_obj.cache_control = { type = "ephemeral" }
|
||||
end
|
||||
|
||||
table.insert(message_content, user_prompt_obj)
|
||||
|
||||
return {
|
||||
{
|
||||
role = "user",
|
||||
content = message_content,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
M.parse_response = function(data_stream, event_state, opts)
|
||||
if event_state == "content_block_delta" then
|
||||
local ok, json = pcall(vim.json.decode, data_stream)
|
||||
if not ok then
|
||||
return
|
||||
end
|
||||
opts.on_chunk(json.delta.text)
|
||||
elseif event_state == "message_stop" then
|
||||
opts.on_complete(nil)
|
||||
return
|
||||
elseif event_state == "error" then
|
||||
opts.on_complete(vim.json.decode(data_stream))
|
||||
end
|
||||
end
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
["anthropic-version"] = "2023-06-01",
|
||||
["anthropic-beta"] = "prompt-caching-2024-07-31",
|
||||
}
|
||||
if not P.env.is_local("claude") then
|
||||
headers["x-api-key"] = os.getenv(M.API_KEY)
|
||||
end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/v1/messages",
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = headers,
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
model = base.model,
|
||||
messages = M.parse_message(code_opts),
|
||||
stream = true,
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
229
lua/avante/providers/copilot.lua
Normal file
229
lua/avante/providers/copilot.lua
Normal file
@@ -0,0 +1,229 @@
|
||||
local curl = require("plenary.curl")
|
||||
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
---@class CopilotToken
|
||||
---@field annotations_enabled boolean
|
||||
---@field chat_enabled boolean
|
||||
---@field chat_jetbrains_enabled boolean
|
||||
---@field code_quote_enabled boolean
|
||||
---@field codesearch boolean
|
||||
---@field copilotignore_enabled boolean
|
||||
---@field endpoints {api: string, ["origin-tracker"]: string, proxy: string, telemetry: string}
|
||||
---@field expires_at integer
|
||||
---@field individual boolean
|
||||
---@field nes_enabled boolean
|
||||
---@field prompt_8k boolean
|
||||
---@field public_suggestions string
|
||||
---@field refresh_in integer
|
||||
---@field sku string
|
||||
---@field snippy_load_test_enabled boolean
|
||||
---@field telemetry string
|
||||
---@field token string
|
||||
---@field tracking_id string
|
||||
---@field vsc_electron_fetcher boolean
|
||||
---@field xcode boolean
|
||||
---@field xcode_chat boolean
|
||||
---
|
||||
---@private
|
||||
---@class AvanteCopilot: table<string, any>
|
||||
---@field token? CopilotToken
|
||||
---@field github_token? string
|
||||
---@field sessionid? string
|
||||
---@field machineid? string
|
||||
M.copilot = nil
|
||||
|
||||
local H = {}
|
||||
|
||||
local version_headers = {
|
||||
["editor-version"] = "Neovim/" .. vim.version().major .. "." .. vim.version().minor .. "." .. vim.version().patch,
|
||||
["editor-plugin-version"] = "avante.nvim/0.0.0",
|
||||
["user-agent"] = "avante.nvim/0.0.0",
|
||||
}
|
||||
|
||||
---@return string
|
||||
H.uuid = function()
|
||||
local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
||||
return (
|
||||
string.gsub(template, "[xy]", function(c)
|
||||
local v = (c == "x") and math.random(0, 0xf) or math.random(8, 0xb)
|
||||
return string.format("%x", v)
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
---@return string
|
||||
H.machine_id = function()
|
||||
local length = 65
|
||||
local hex_chars = "0123456789abcdef"
|
||||
local hex = ""
|
||||
for _ = 1, length do
|
||||
hex = hex .. hex_chars:sub(math.random(1, #hex_chars), math.random(1, #hex_chars))
|
||||
end
|
||||
return hex
|
||||
end
|
||||
|
||||
---@return string | nil
|
||||
H.find_config_path = function()
|
||||
local config = vim.fn.expand("$XDG_CONFIG_HOME")
|
||||
if config and vim.fn.isdirectory(config) > 0 then
|
||||
return config
|
||||
elseif vim.fn.has("win32") > 0 then
|
||||
config = vim.fn.expand("~/AppData/Local")
|
||||
if vim.fn.isdirectory(config) > 0 then
|
||||
return config
|
||||
end
|
||||
else
|
||||
config = vim.fn.expand("~/.config")
|
||||
if vim.fn.isdirectory(config) > 0 then
|
||||
return config
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
H.cached_token = function()
|
||||
-- loading token from the environment only in GitHub Codespaces
|
||||
local token = os.getenv("GITHUB_TOKEN")
|
||||
local codespaces = os.getenv("CODESPACES")
|
||||
if token and codespaces then
|
||||
return token
|
||||
end
|
||||
|
||||
-- loading token from the file
|
||||
local config_path = H.find_config_path()
|
||||
if not config_path then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- token can be sometimes in apps.json sometimes in hosts.json
|
||||
local file_paths = {
|
||||
config_path .. "/github-copilot/hosts.json",
|
||||
config_path .. "/github-copilot/apps.json",
|
||||
}
|
||||
|
||||
for _, file_path in ipairs(file_paths) do
|
||||
if vim.fn.filereadable(file_path) == 1 then
|
||||
local userdata = vim.fn.json_decode(vim.fn.readfile(file_path))
|
||||
for key, value in pairs(userdata) do
|
||||
if string.find(key, "github.com") then
|
||||
return value.oauth_token
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param token string
|
||||
---@param sessionid string
|
||||
---@param machineid string
|
||||
---@return table<string, string>
|
||||
H.generate_headers = function(token, sessionid, machineid)
|
||||
local headers = {
|
||||
["authorization"] = "Bearer " .. token,
|
||||
["x-request-id"] = H.uuid(),
|
||||
["vscode-sessionid"] = sessionid,
|
||||
["vscode-machineid"] = machineid,
|
||||
["copilot-integration-id"] = "vscode-chat",
|
||||
["openai-organization"] = "github-copilot",
|
||||
["openai-intent"] = "conversation-panel",
|
||||
["content-type"] = "application/json",
|
||||
}
|
||||
for key, value in pairs(version_headers) do
|
||||
headers[key] = value
|
||||
end
|
||||
return headers
|
||||
end
|
||||
|
||||
M.API_KEY = P.AVANTE_INTERNAL_KEY
|
||||
|
||||
M.has = function()
|
||||
if Utils.has("copilot.lua") or Utils.has("copilot.vim") or H.find_config_path() then
|
||||
return true
|
||||
end
|
||||
Utils.warn("copilot is not setup correctly. Please use copilot.lua or copilot.vim for authentication.")
|
||||
return false
|
||||
end
|
||||
|
||||
M.parse_message = O.parse_message
|
||||
M.parse_response = O.parse_response
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local github_token = H.cached_token()
|
||||
|
||||
if not github_token then
|
||||
error(
|
||||
"No GitHub token found, please use `:Copilot auth` to setup with `copilot.lua` or `:Copilot setup` with `copilot.vim`"
|
||||
)
|
||||
end
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local on_done = function()
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat/completions",
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = H.generate_headers(M.copilot.token.token, M.copilot.sessionid, M.copilot.machineid),
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
mode = base.model,
|
||||
n = 1,
|
||||
top_p = 1,
|
||||
stream = true,
|
||||
messages = M.parse_message(code_opts),
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
local result = nil
|
||||
|
||||
if not M.copilot.token or (M.copilot.token.expires_at and M.copilot.token.expires_at <= math.floor(os.time())) then
|
||||
local sessionid = H.uuid() .. tostring(math.floor(os.time() * 1000))
|
||||
|
||||
local url = "https://api.github.com/copilot_internal/v2/token"
|
||||
local headers = {
|
||||
["Authorization"] = "token " .. github_token,
|
||||
["Accept"] = "application/json",
|
||||
}
|
||||
for key, value in pairs(version_headers) do
|
||||
headers[key] = value
|
||||
end
|
||||
|
||||
local response = curl.get(url, {
|
||||
timeout = Config.copilot.timeout,
|
||||
headers = headers,
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
on_error = function(err)
|
||||
error("Failed to get response: " .. vim.inspect(err))
|
||||
end,
|
||||
})
|
||||
|
||||
M.copilot.sessionid = sessionid
|
||||
M.copilot.token = vim.json.decode(response.body)
|
||||
result = on_done()
|
||||
else
|
||||
result = on_done()
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
M.setup = function()
|
||||
if not M.copilot then
|
||||
M.copilot = {
|
||||
sessionid = nil,
|
||||
token = nil,
|
||||
github_token = H.cached_token(),
|
||||
machineid = H.machine_id(),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
41
lua/avante/providers/deepseek.lua
Normal file
41
lua/avante/providers/deepseek.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "DEEPSEEK_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = O.parse_message
|
||||
M.parse_response = O.parse_response
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
}
|
||||
if not P.env.is_local("deepseek") then
|
||||
headers["Authorization"] = "Bearer " .. os.getenv(M.API_KEY)
|
||||
end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat/completions",
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = headers,
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
model = base.model,
|
||||
messages = M.parse_message(code_opts),
|
||||
stream = true,
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
79
lua/avante/providers/gemini.lua
Normal file
79
lua/avante/providers/gemini.lua
Normal file
@@ -0,0 +1,79 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "GROQ_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = function(opts)
|
||||
local code_prompt_obj = {
|
||||
text = string.format("<code>```%s\n%s```</code>", opts.code_lang, opts.code_content),
|
||||
}
|
||||
|
||||
if opts.selected_code_content then
|
||||
code_prompt_obj.text = string.format("<code_context>```%s\n%s```</code_context>", opts.code_lang, opts.code_content)
|
||||
end
|
||||
|
||||
-- parts ready
|
||||
local message_content = {
|
||||
code_prompt_obj,
|
||||
}
|
||||
|
||||
if opts.selected_code_content then
|
||||
local selected_code_obj = {
|
||||
text = string.format("<code>```%s\n%s```</code>", opts.code_lang, opts.selected_code_content),
|
||||
}
|
||||
|
||||
table.insert(message_content, selected_code_obj)
|
||||
end
|
||||
|
||||
-- insert a part into parts
|
||||
table.insert(message_content, {
|
||||
text = string.format("<question>%s</question>", opts.question),
|
||||
})
|
||||
|
||||
return {
|
||||
systemInstruction = {
|
||||
role = "user",
|
||||
parts = {
|
||||
{
|
||||
text = opts.system_prompt .. "\n" .. opts.base_prompt,
|
||||
},
|
||||
},
|
||||
},
|
||||
contents = {
|
||||
{
|
||||
role = "user",
|
||||
parts = message_content,
|
||||
},
|
||||
},
|
||||
}
|
||||
end
|
||||
M.parse_response = function(data_stream, _, opts)
|
||||
local json = vim.json.decode(data_stream)
|
||||
opts.on_chunk(json.candidates[1].content.parts[1].text)
|
||||
end
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" })
|
||||
.. "/"
|
||||
.. base.model
|
||||
.. ":streamGenerateContent?alt=sse&key="
|
||||
.. os.getenv(M.API_KEY),
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = { ["Content-Type"] = "application/json" },
|
||||
body = vim.tbl_deep_extend("force", {}, M.parse_message(code_opts), body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
41
lua/avante/providers/groq.lua
Normal file
41
lua/avante/providers/groq.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "GROQ_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = O.parse_message
|
||||
M.parse_response = O.parse_response
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
}
|
||||
if not P.env.is_local("groq") then
|
||||
headers["Authorization"] = "Bearer " .. os.getenv(M.API_KEY)
|
||||
end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/openai/v1/chat/completions",
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = headers,
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
model = base.model,
|
||||
messages = M.parse_message(code_opts),
|
||||
stream = true,
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
282
lua/avante/providers/init.lua
Normal file
282
lua/avante/providers/init.lua
Normal file
@@ -0,0 +1,282 @@
|
||||
local api = vim.api
|
||||
|
||||
local Config = require("avante.config")
|
||||
local Utils = require("avante.utils")
|
||||
local Dressing = require("avante.ui.dressing")
|
||||
|
||||
---@class AvanteHandlerOptions: table<[string], string>
|
||||
---@field on_chunk AvanteChunkParser
|
||||
---@field on_complete AvanteCompleteParser
|
||||
---
|
||||
---@class AvantePromptOptions: table<[string], string>
|
||||
---@field base_prompt AvanteBasePrompt
|
||||
---@field system_prompt AvanteSystemPrompt
|
||||
---@field question string
|
||||
---@field code_lang string
|
||||
---@field code_content string
|
||||
---@field selected_code_content? string
|
||||
---
|
||||
---@class AvanteBaseMessage
|
||||
---@field role "user" | "system"
|
||||
---@field content string
|
||||
---
|
||||
---@class AvanteClaudeMessage: AvanteBaseMessage
|
||||
---@field role "user"
|
||||
---@field content {type: "text", text: string, cache_control?: {type: "ephemeral"}}[]
|
||||
---
|
||||
---@class AvanteGeminiMessage
|
||||
---@field role "user"
|
||||
---@field parts { text: string }[]
|
||||
---
|
||||
---@alias AvanteChatMessage AvanteClaudeMessage | OpenAIMessage | AvanteGeminiMessage
|
||||
---
|
||||
---@alias AvanteMessageParser fun(opts: AvantePromptOptions): AvanteChatMessage[]
|
||||
---
|
||||
---@class AvanteCurlOutput: {url: string, proxy: string, insecure: boolean, body: table<string, any> | string, headers: table<string, string>}
|
||||
---@alias AvanteCurlArgsParser fun(opts: AvanteProvider, code_opts: AvantePromptOptions): AvanteCurlOutput
|
||||
---
|
||||
---@class ResponseParser
|
||||
---@field on_chunk fun(chunk: string): any
|
||||
---@field on_complete fun(err: string|nil): any
|
||||
---@alias AvanteResponseParser fun(data_stream: string, event_state: string, opts: ResponseParser): nil
|
||||
---
|
||||
---@class AvanteDefaultBaseProvider: table<string, any>
|
||||
---@field endpoint? string
|
||||
---@field model? string
|
||||
---@field local? boolean
|
||||
---@field proxy? string
|
||||
---@field allow_insecure? boolean
|
||||
---
|
||||
---@class AvanteSupportedProvider: AvanteDefaultBaseProvider
|
||||
---@field temperature? number
|
||||
---@field max_tokens? number
|
||||
---
|
||||
---@class AvanteAzureProvider: AvanteDefaultBaseProvider
|
||||
---@field deployment string
|
||||
---@field api_version string
|
||||
---@field temperature number
|
||||
---@field max_tokens number
|
||||
---
|
||||
---@class AvanteCopilotProvider: AvanteSupportedProvider
|
||||
---@field timeout number
|
||||
---
|
||||
---@class AvanteGeminiProvider: AvanteDefaultBaseProvider
|
||||
---@field model string
|
||||
---
|
||||
---@class AvanteProvider: AvanteDefaultBaseProvider
|
||||
---@field api_key_name string
|
||||
---@field parse_response_data AvanteResponseParser
|
||||
---@field parse_curl_args AvanteCurlArgsParser
|
||||
---@field parse_stream_data? AvanteStreamParser
|
||||
---
|
||||
---@alias AvanteStreamParser fun(line: string, handler_opts: AvanteHandlerOptions): nil
|
||||
---@alias AvanteChunkParser fun(chunk: string): any
|
||||
---@alias AvanteCompleteParser fun(err: string|nil): nil
|
||||
---@alias AvanteLLMConfigHandler fun(opts: AvanteSupportedProvider): AvanteDefaultBaseProvider, table<string, any>
|
||||
---
|
||||
---@class AvanteProviderFunctor
|
||||
---@field parse_message AvanteMessageParser
|
||||
---@field parse_response AvanteResponseParser
|
||||
---@field parse_curl_args AvanteCurlArgsParser
|
||||
---@field setup? fun(): nil
|
||||
---@field has fun(): boolean
|
||||
---@field API_KEY string
|
||||
---@field parse_stream_data? AvanteStreamParser
|
||||
---
|
||||
---@class avante.Providers
|
||||
---@field openai AvanteProviderFunctor
|
||||
---@field copilot AvanteProviderFunctor
|
||||
---@field claude AvanteProviderFunctor
|
||||
---@field azure AvanteProviderFunctor
|
||||
---@field deepseek AvanteProviderFunctor
|
||||
---@field gemini AvanteProviderFunctor
|
||||
---@field groq AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
setmetatable(M, {
|
||||
---@param t avante.Providers
|
||||
---@param k Provider
|
||||
__index = function(t, k)
|
||||
if Config.vendors[k] ~= nil then
|
||||
---@type AvanteProvider
|
||||
local v = Config.vendors[k]
|
||||
|
||||
-- Patch from vendors similar to supported providers.
|
||||
t[k] = setmetatable({}, { __index = v })
|
||||
t[k].API_KEY = v.api_key_name
|
||||
-- Hack for aliasing and makes it sane for us.
|
||||
t[k].parse_response = v.parse_response_data
|
||||
t[k].has = function()
|
||||
return os.getenv(v.api_key_name) and true or false
|
||||
end
|
||||
|
||||
return t[k]
|
||||
end
|
||||
|
||||
---@type AvanteProviderFunctor
|
||||
t[k] = require("avante.providers." .. k)
|
||||
return t[k]
|
||||
end,
|
||||
})
|
||||
|
||||
---@class EnvironmentHandler
|
||||
local E = {}
|
||||
|
||||
---@private
|
||||
E._once = false
|
||||
|
||||
--- intialize the environment variable for current neovim session.
|
||||
--- This will only run once and spawn a UI for users to input the envvar.
|
||||
---@param opts {refresh: boolean, provider: AvanteProviderFunctor}
|
||||
---@private
|
||||
E.setup = function(opts)
|
||||
local var = opts.provider.API_KEY
|
||||
|
||||
if var == M.AVANTE_INTERNAL_KEY then
|
||||
return
|
||||
end
|
||||
|
||||
local refresh = opts.refresh or false
|
||||
|
||||
---@param value string
|
||||
---@return nil
|
||||
local function on_confirm(value)
|
||||
if value then
|
||||
vim.fn.setenv(var, value)
|
||||
else
|
||||
if not opts.provider.has() then
|
||||
Utils.warn("Failed to set " .. var .. ". Avante won't work as expected", { once = true, title = "Avante" })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if refresh then
|
||||
vim.defer_fn(function()
|
||||
Dressing.initialize_input_buffer({ opts = { prompt = "Enter " .. var .. ": " }, on_confirm = on_confirm })
|
||||
end, 200)
|
||||
elseif not E._once then
|
||||
E._once = true
|
||||
api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "WinEnter" }, {
|
||||
pattern = "*",
|
||||
once = true,
|
||||
callback = function()
|
||||
vim.defer_fn(function()
|
||||
-- only mount if given buffer is not of buftype ministarter, dashboard, alpha, qf
|
||||
local exclude_buftypes = { "qf", "nofile" }
|
||||
local exclude_filetypes = {
|
||||
"NvimTree",
|
||||
"Outline",
|
||||
"help",
|
||||
"dashboard",
|
||||
"alpha",
|
||||
"qf",
|
||||
"ministarter",
|
||||
"TelescopePrompt",
|
||||
"gitcommit",
|
||||
"gitrebase",
|
||||
"DressingInput",
|
||||
}
|
||||
if
|
||||
not vim.tbl_contains(exclude_buftypes, vim.bo.buftype)
|
||||
and not vim.tbl_contains(exclude_filetypes, vim.bo.filetype)
|
||||
and not opts.provider.has()
|
||||
then
|
||||
Dressing.initialize_input_buffer({
|
||||
opts = { prompt = "Enter " .. var .. ": " },
|
||||
on_confirm = on_confirm,
|
||||
})
|
||||
end
|
||||
end, 200)
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
---@param provider Provider
|
||||
E.is_local = function(provider)
|
||||
local cur = M.get(provider)
|
||||
return cur["local"] ~= nil and cur["local"] or false
|
||||
end
|
||||
|
||||
M.env = E
|
||||
|
||||
M.AVANTE_INTERNAL_KEY = "__avante_env_internal"
|
||||
|
||||
M.setup = function()
|
||||
---@type AvanteProviderFunctor
|
||||
local provider = M[Config.provider]
|
||||
E.setup({ provider = provider })
|
||||
|
||||
if provider.setup ~= nil then
|
||||
provider.setup()
|
||||
end
|
||||
|
||||
M.commands()
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param provider Provider
|
||||
function M.refresh(provider)
|
||||
---@type AvanteProviderFunctor
|
||||
local p = M[Config.provider]
|
||||
if not p.has() then
|
||||
E.setup({ provider = p, refresh = true })
|
||||
else
|
||||
Utils.info("Switch to provider: " .. provider, { once = true, title = "Avante" })
|
||||
end
|
||||
require("avante.config").override({ provider = provider })
|
||||
end
|
||||
|
||||
local default_providers = { "openai", "claude", "azure", "deepseek", "groq", "gemini", "copilot" }
|
||||
|
||||
---@private
|
||||
M.commands = function()
|
||||
api.nvim_create_user_command("AvanteSwitchProvider", function(args)
|
||||
local cmd = vim.trim(args.args or "")
|
||||
M.refresh(cmd)
|
||||
end, {
|
||||
nargs = 1,
|
||||
desc = "avante: switch provider",
|
||||
complete = function(_, line)
|
||||
if line:match("^%s*AvanteSwitchProvider %w") then
|
||||
return {}
|
||||
end
|
||||
local prefix = line:match("^%s*AvanteSwitchProvider (%w*)") or ""
|
||||
-- join two tables
|
||||
local Keys = vim.list_extend(default_providers, vim.tbl_keys(Config.vendors or {}))
|
||||
return vim.tbl_filter(function(key)
|
||||
return key:find(prefix) == 1
|
||||
end, Keys)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
---@param opts AvanteProvider | AvanteSupportedProvider
|
||||
---@return AvanteDefaultBaseProvider, table<string, any>
|
||||
M.parse_config = function(opts)
|
||||
---@type AvanteDefaultBaseProvider
|
||||
local s1 = {}
|
||||
---@type table<string, any>
|
||||
local s2 = {}
|
||||
|
||||
for key, value in pairs(opts) do
|
||||
if vim.tbl_contains(Config.BASE_PROVIDER_KEYS, key) then
|
||||
s1[key] = value
|
||||
else
|
||||
s2[key] = value
|
||||
end
|
||||
end
|
||||
|
||||
return s1, vim.tbl_filter(function(it)
|
||||
return type(it) ~= "function"
|
||||
end, s2)
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param provider Provider
|
||||
M.get = function(provider)
|
||||
local cur = Config.get_provider(provider or Config.provider)
|
||||
return type(cur) == "function" and cur() or cur
|
||||
end
|
||||
|
||||
return M
|
||||
110
lua/avante/providers/openai.lua
Normal file
110
lua/avante/providers/openai.lua
Normal file
@@ -0,0 +1,110 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local P = require("avante.providers")
|
||||
|
||||
---@class OpenAIChatResponse
|
||||
---@field id string
|
||||
---@field object "chat.completion" | "chat.completion.chunk"
|
||||
---@field created integer
|
||||
---@field model string
|
||||
---@field system_fingerprint string
|
||||
---@field choices? OpenAIResponseChoice[]
|
||||
---@field usage {prompt_tokens: integer, completion_tokens: integer, total_tokens: integer}
|
||||
---
|
||||
---@class OpenAIResponseChoice
|
||||
---@field index integer
|
||||
---@field delta OpenAIMessage
|
||||
---@field logprobs? integer
|
||||
---@field finish_reason? "stop" | "length"
|
||||
---
|
||||
---@class OpenAIMessage
|
||||
---@field role? "user" | "system" | "assistant"
|
||||
---@field content string
|
||||
---
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
M.API_KEY = "OPENAI_API_KEY"
|
||||
|
||||
M.has = function()
|
||||
return os.getenv(M.API_KEY) and true or false
|
||||
end
|
||||
|
||||
M.parse_message = function(opts)
|
||||
local user_prompt = opts.base_prompt
|
||||
.. "\n\nCODE:\n"
|
||||
.. "```"
|
||||
.. opts.code_lang
|
||||
.. "\n"
|
||||
.. opts.code_content
|
||||
.. "\n```"
|
||||
.. "\n\nQUESTION:\n"
|
||||
.. opts.question
|
||||
|
||||
if opts.selected_code_content ~= nil then
|
||||
user_prompt = opts.base_prompt
|
||||
.. "\n\nCODE CONTEXT:\n"
|
||||
.. "```"
|
||||
.. opts.code_lang
|
||||
.. "\n"
|
||||
.. opts.code_content
|
||||
.. "\n```"
|
||||
.. "\n\nCODE:\n"
|
||||
.. "```"
|
||||
.. opts.code_lang
|
||||
.. "\n"
|
||||
.. opts.selected_code_content
|
||||
.. "\n```"
|
||||
.. "\n\nQUESTION:\n"
|
||||
.. opts.question
|
||||
end
|
||||
|
||||
return {
|
||||
{ role = "system", content = opts.system_prompt },
|
||||
{ role = "user", content = user_prompt },
|
||||
}
|
||||
end
|
||||
|
||||
M.parse_response = function(data_stream, _, opts)
|
||||
if data_stream:match('"%[DONE%]":') then
|
||||
opts.on_complete(nil)
|
||||
return
|
||||
end
|
||||
if data_stream:match('"delta":') then
|
||||
---@type OpenAIChatResponse
|
||||
local json = vim.json.decode(data_stream)
|
||||
if json.choices and json.choices[1] then
|
||||
local choice = json.choices[1]
|
||||
if choice.finish_reason == "stop" then
|
||||
opts.on_complete(nil)
|
||||
elseif choice.delta.content then
|
||||
opts.on_chunk(choice.delta.content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
M.parse_curl_args = function(provider, code_opts)
|
||||
local base, body_opts = P.parse_config(provider)
|
||||
|
||||
local headers = {
|
||||
["Content-Type"] = "application/json",
|
||||
}
|
||||
if not P.env.is_local("openai") then
|
||||
headers["Authorization"] = "Bearer " .. os.getenv(M.API_KEY)
|
||||
end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/v1/chat/completions",
|
||||
proxy = base.proxy,
|
||||
insecure = base.allow_insecure,
|
||||
headers = headers,
|
||||
body = vim.tbl_deep_extend("force", {
|
||||
model = base.model,
|
||||
messages = M.parse_message(code_opts),
|
||||
stream = true,
|
||||
}, body_opts),
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user