chore: run stylua [generated] (#460)
* chore: add stylua Signed-off-by: Aaron Pham <contact@aarnphm.xyz> * chore: running stylua Signed-off-by: Aaron Pham <contact@aarnphm.xyz> --------- Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
---@field temperature number
|
||||
---@field max_tokens number
|
||||
|
||||
local Utils = require("avante.utils")
|
||||
local P = require("avante.providers")
|
||||
local Utils = require "avante.utils"
|
||||
local P = require "avante.providers"
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
@@ -23,9 +23,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 not P.env.is_local "azure" then headers["api-key"] = provider.parse_api_key() end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Clipboard = require("avante.clipboard")
|
||||
local P = require("avante.providers")
|
||||
local Utils = require "avante.utils"
|
||||
local Clipboard = require "avante.clipboard"
|
||||
local P = require "avante.providers"
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
@@ -29,9 +29,7 @@ M.parse_message = function(opts)
|
||||
type = "text",
|
||||
text = opts.user_prompt,
|
||||
}
|
||||
if Utils.tokens.calculate_tokens(opts.user_prompt) then
|
||||
user_prompt_obj.cache_control = { type = "ephemeral" }
|
||||
end
|
||||
if Utils.tokens.calculate_tokens(opts.user_prompt) then user_prompt_obj.cache_control = { type = "ephemeral" } end
|
||||
|
||||
table.insert(message_content, user_prompt_obj)
|
||||
|
||||
@@ -46,9 +44,7 @@ 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
|
||||
if not ok then return end
|
||||
opts.on_chunk(json.delta.text)
|
||||
elseif event_state == "message_stop" then
|
||||
opts.on_complete(nil)
|
||||
@@ -69,9 +65,7 @@ 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 not P.env.is_local "claude" then headers["x-api-key"] = provider.parse_api_key() end
|
||||
|
||||
local messages = M.parse_message(prompt_opts)
|
||||
|
||||
@@ -110,7 +104,7 @@ M.on_error = function(result)
|
||||
|
||||
if error_type == "insufficient_quota" then
|
||||
error_msg = "You don't have any credits or have exceeded your quota. Please check your plan and billing details."
|
||||
elseif error_type == "invalid_request_error" and error_msg:match("temperature") then
|
||||
elseif error_type == "invalid_request_error" and error_msg:match "temperature" then
|
||||
error_msg = "Invalid temperature value. Please ensure it's between 0 and 1."
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
local Utils = require("avante.utils")
|
||||
local P = require("avante.providers")
|
||||
local Utils = require "avante.utils"
|
||||
local P = require "avante.providers"
|
||||
|
||||
---@alias CohereFinishReason "COMPLETE" | "LENGTH" | "ERROR"
|
||||
---
|
||||
@@ -52,9 +52,7 @@ M.parse_stream_data = function(data, opts)
|
||||
return
|
||||
end
|
||||
---@cast json CohereTextGenerationResponse
|
||||
if json.event_type == "text-generation" then
|
||||
opts.on_chunk(json.text)
|
||||
end
|
||||
if json.event_type == "text-generation" then opts.on_chunk(json.text) end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -71,9 +69,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 not P.env.is_local "cohere" then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat",
|
||||
|
||||
@@ -25,12 +25,12 @@
|
||||
---@field xcode boolean
|
||||
---@field xcode_chat boolean
|
||||
|
||||
local curl = require("plenary.curl")
|
||||
local curl = require "plenary.curl"
|
||||
|
||||
local Config = require("avante.config")
|
||||
local Path = require("plenary.path")
|
||||
local Utils = require("avante.utils")
|
||||
local P = require("avante.providers")
|
||||
local Config = require "avante.config"
|
||||
local Path = require "plenary.path"
|
||||
local Utils = require "avante.utils"
|
||||
local P = require "avante.providers"
|
||||
local O = require("avante.providers").openai
|
||||
|
||||
local H = {}
|
||||
@@ -41,29 +41,25 @@ local H = {}
|
||||
---
|
||||
---@return string
|
||||
H.get_oauth_token = function()
|
||||
local xdg_config = vim.fn.expand("$XDG_CONFIG_HOME")
|
||||
local xdg_config = vim.fn.expand "$XDG_CONFIG_HOME"
|
||||
local os_name = Utils.get_os_name()
|
||||
---@type string
|
||||
local config_dir
|
||||
|
||||
if vim.tbl_contains({ "linux", "darwin" }, os_name) then
|
||||
config_dir = (xdg_config and vim.fn.isdirectory(xdg_config) > 0) and xdg_config or vim.fn.expand("~/.config")
|
||||
config_dir = (xdg_config and vim.fn.isdirectory(xdg_config) > 0) and xdg_config or vim.fn.expand "~/.config"
|
||||
else
|
||||
config_dir = vim.fn.expand("~/AppData/Local")
|
||||
config_dir = vim.fn.expand "~/AppData/Local"
|
||||
end
|
||||
|
||||
--- hosts.json (copilot.lua), apps.json (copilot.vim)
|
||||
---@type Path[]
|
||||
local paths = vim.iter({ "hosts.json", "apps.json" }):fold({}, function(acc, path)
|
||||
local yason = Path:new(config_dir):joinpath("github-copilot", path)
|
||||
if yason:exists() then
|
||||
table.insert(acc, yason)
|
||||
end
|
||||
if yason:exists() then table.insert(acc, yason) end
|
||||
return acc
|
||||
end)
|
||||
if #paths == 0 then
|
||||
error("You must setup copilot with either copilot.lua or copilot.vim", 2)
|
||||
end
|
||||
if #paths == 0 then error("You must setup copilot with either copilot.lua or copilot.vim", 2) end
|
||||
|
||||
local yason = paths[1]
|
||||
return vim
|
||||
@@ -71,9 +67,7 @@ H.get_oauth_token = function()
|
||||
---@type table<string, OAuthToken>
|
||||
vim.json.decode(yason:read())
|
||||
)
|
||||
:filter(function(k, _)
|
||||
return k:match("github.com")
|
||||
end)
|
||||
:filter(function(k, _) return k:match "github.com" end)
|
||||
---@param acc {oauth_token: string}
|
||||
:fold({}, function(acc, _, v)
|
||||
acc.oauth_token = v.oauth_token
|
||||
@@ -83,17 +77,13 @@ H.get_oauth_token = function()
|
||||
end
|
||||
|
||||
H.chat_auth_url = "https://api.github.com/copilot_internal/v2/token"
|
||||
H.chat_completion_url = function(base_url)
|
||||
return Utils.trim(base_url, { prefix = "/" }) .. "/chat/completions"
|
||||
end
|
||||
H.chat_completion_url = function(base_url) return Utils.trim(base_url, { prefix = "/" }) .. "/chat/completions" end
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
H.refresh_token = function()
|
||||
if not M.state then
|
||||
error("internal initialization error")
|
||||
end
|
||||
if not M.state then error "internal initialization error" end
|
||||
|
||||
if
|
||||
not M.state.github_token
|
||||
@@ -107,14 +97,10 @@ H.refresh_token = function()
|
||||
timeout = Config.copilot.timeout,
|
||||
proxy = Config.copilot.proxy,
|
||||
insecure = Config.copilot.allow_insecure,
|
||||
on_error = function(err)
|
||||
error("Failed to get response: " .. vim.inspect(err))
|
||||
end,
|
||||
on_error = function(err) error("Failed to get response: " .. vim.inspect(err)) end,
|
||||
callback = function(output)
|
||||
M.state.github_token = vim.json.decode(output.body)
|
||||
if not vim.g.avante_login then
|
||||
vim.g.avante_login = true
|
||||
end
|
||||
if not vim.g.avante_login then vim.g.avante_login = true end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local Utils = require("avante.utils")
|
||||
local P = require("avante.providers")
|
||||
local Clipboard = require("avante.clipboard")
|
||||
local Utils = require "avante.utils"
|
||||
local P = require "avante.providers"
|
||||
local Clipboard = require "avante.clipboard"
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
@@ -47,9 +47,7 @@ end
|
||||
|
||||
M.parse_response = function(data_stream, _, opts)
|
||||
local ok, json = pcall(vim.json.decode, data_stream)
|
||||
if not ok then
|
||||
opts.on_complete(json)
|
||||
end
|
||||
if not ok then opts.on_complete(json) end
|
||||
if json.candidates then
|
||||
if #json.candidates > 0 then
|
||||
opts.on_chunk(json.candidates[1].content.parts[1].text)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
local api = vim.api
|
||||
|
||||
local Config = require("avante.config")
|
||||
local Utils = require("avante.utils")
|
||||
local Dressing = require("avante.ui.dressing")
|
||||
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
|
||||
@@ -98,15 +98,11 @@ E.cache = {}
|
||||
---@return string | nil
|
||||
E.parse_envvar = function(Opts)
|
||||
local api_key_name = Opts.api_key_name
|
||||
if api_key_name == nil then
|
||||
error("Requires api_key_name")
|
||||
end
|
||||
if api_key_name == nil then error "Requires api_key_name" end
|
||||
|
||||
if E.cache[api_key_name] ~= nil then
|
||||
return E.cache[api_key_name]
|
||||
end
|
||||
if E.cache[api_key_name] ~= nil then return E.cache[api_key_name] end
|
||||
|
||||
local cmd = api_key_name:match("^cmd:(.*)")
|
||||
local cmd = api_key_name:match "^cmd:(.*)"
|
||||
|
||||
local key = nil
|
||||
|
||||
@@ -177,9 +173,7 @@ E.setup = function(opts)
|
||||
opts.provider.setup()
|
||||
|
||||
-- check if var is a all caps string
|
||||
if var == M.AVANTE_INTERNAL_KEY or var:match("^cmd:(.*)") then
|
||||
return
|
||||
end
|
||||
if var == M.AVANTE_INTERNAL_KEY or var:match "^cmd:(.*)" then return end
|
||||
|
||||
local refresh = opts.refresh or false
|
||||
|
||||
@@ -214,10 +208,10 @@ E.setup = function(opts)
|
||||
"noice",
|
||||
}
|
||||
if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.has() then
|
||||
Dressing.initialize_input_buffer({
|
||||
Dressing.initialize_input_buffer {
|
||||
opts = { prompt = "Enter " .. var .. ": " },
|
||||
on_confirm = on_confirm,
|
||||
})
|
||||
}
|
||||
end
|
||||
end, 200)
|
||||
end
|
||||
@@ -260,37 +254,23 @@ M = setmetatable(M, {
|
||||
t[k] = Opts
|
||||
else
|
||||
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)
|
||||
end
|
||||
|
||||
t[k].parse_api_key = function()
|
||||
return E.parse_envvar(t[k])
|
||||
end
|
||||
t[k].parse_api_key = function() return E.parse_envvar(t[k]) end
|
||||
|
||||
-- default to gpt-4o as tokenizer
|
||||
if t[k].tokenizer_id == nil then
|
||||
t[k].tokenizer_id = "gpt-4o"
|
||||
end
|
||||
if t[k].tokenizer_id == nil then t[k].tokenizer_id = "gpt-4o" end
|
||||
|
||||
if t[k].use_xml_format == nil then
|
||||
t[k].use_xml_format = false
|
||||
end
|
||||
if t[k].use_xml_format == nil then t[k].use_xml_format = false end
|
||||
|
||||
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
|
||||
t[k].setup = function()
|
||||
if not E.is_local(k) then
|
||||
t[k].parse_api_key()
|
||||
end
|
||||
if not E.is_local(k) then t[k].parse_api_key() end
|
||||
require("avante.tokenizers").setup(t[k].tokenizer_id)
|
||||
end
|
||||
end
|
||||
@@ -304,16 +284,16 @@ M.setup = function()
|
||||
|
||||
---@type AvanteProviderFunctor
|
||||
local provider = M[Config.provider]
|
||||
E.setup({ provider = provider })
|
||||
E.setup { provider = provider }
|
||||
end
|
||||
|
||||
---@param provider Provider
|
||||
function M.refresh(provider)
|
||||
require("avante.config").override({ provider = provider })
|
||||
require("avante.config").override { provider = provider }
|
||||
|
||||
---@type AvanteProviderFunctor
|
||||
local p = M[Config.provider]
|
||||
E.setup({ provider = p, refresh = true })
|
||||
E.setup { provider = p, refresh = true }
|
||||
Utils.info("Switch to provider: " .. provider, { once = true, title = "Avante" })
|
||||
end
|
||||
|
||||
@@ -334,15 +314,10 @@ M.parse_config = function(opts)
|
||||
end
|
||||
|
||||
return s1,
|
||||
vim
|
||||
.iter(s2)
|
||||
:filter(function(_, v)
|
||||
return type(v) ~= "function"
|
||||
end)
|
||||
:fold({}, function(acc, k, v)
|
||||
acc[k] = v
|
||||
return acc
|
||||
end)
|
||||
vim.iter(s2):filter(function(_, v) return type(v) ~= "function" end):fold({}, function(acc, k, v)
|
||||
acc[k] = v
|
||||
return acc
|
||||
end)
|
||||
end
|
||||
|
||||
---@private
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
local Clipboard = require("avante.clipboard")
|
||||
local P = require("avante.providers")
|
||||
local Utils = require "avante.utils"
|
||||
local Config = require "avante.config"
|
||||
local Clipboard = require "avante.clipboard"
|
||||
local P = require "avante.providers"
|
||||
|
||||
---@class OpenAIChatResponse
|
||||
---@field id string
|
||||
@@ -29,9 +29,7 @@ M.api_key_name = "OPENAI_API_KEY"
|
||||
M.tokenizer_id = "gpt-4o"
|
||||
|
||||
---@param opts AvantePromptOptions
|
||||
M.get_user_message = function(opts)
|
||||
return opts.user_prompt
|
||||
end
|
||||
M.get_user_message = function(opts) return opts.user_prompt end
|
||||
|
||||
M.parse_message = function(opts)
|
||||
---@type string | OpenAIMessage[]
|
||||
@@ -58,11 +56,11 @@ M.parse_message = function(opts)
|
||||
end
|
||||
|
||||
M.parse_response = function(data_stream, _, opts)
|
||||
if data_stream:match('"%[DONE%]":') then
|
||||
if data_stream:match '"%[DONE%]":' then
|
||||
opts.on_complete(nil)
|
||||
return
|
||||
end
|
||||
if data_stream:match('"delta":') then
|
||||
if data_stream:match '"delta":' then
|
||||
---@type OpenAIChatResponse
|
||||
local json = vim.json.decode(data_stream)
|
||||
if json.choices and json.choices[1] then
|
||||
@@ -70,9 +68,7 @@ M.parse_response = function(data_stream, _, opts)
|
||||
if choice.finish_reason == "stop" then
|
||||
opts.on_complete(nil)
|
||||
elseif choice.delta.content then
|
||||
if choice.delta.content ~= vim.NIL then
|
||||
opts.on_chunk(choice.delta.content)
|
||||
end
|
||||
if choice.delta.content ~= vim.NIL then opts.on_chunk(choice.delta.content) end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -84,9 +80,7 @@ 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 not P.env.is_local "openai" then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
|
||||
|
||||
return {
|
||||
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat/completions",
|
||||
|
||||
Reference in New Issue
Block a user