fix(copilot): refresh token before sending the request (#791)
Co-authored-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
parent
9fbe0256f2
commit
c516883b99
@ -91,7 +91,7 @@ H.refresh_token = function()
|
|||||||
not M.state.github_token
|
not M.state.github_token
|
||||||
or (M.state.github_token.expires_at and M.state.github_token.expires_at < math.floor(os.time()))
|
or (M.state.github_token.expires_at and M.state.github_token.expires_at < math.floor(os.time()))
|
||||||
then
|
then
|
||||||
curl.get(H.chat_auth_url, {
|
local response = curl.get(H.chat_auth_url, {
|
||||||
headers = {
|
headers = {
|
||||||
["Authorization"] = "token " .. M.state.oauth_token,
|
["Authorization"] = "token " .. M.state.oauth_token,
|
||||||
["Accept"] = "application/json",
|
["Accept"] = "application/json",
|
||||||
@ -99,14 +99,16 @@ H.refresh_token = function()
|
|||||||
timeout = Config.copilot.timeout,
|
timeout = Config.copilot.timeout,
|
||||||
proxy = Config.copilot.proxy,
|
proxy = Config.copilot.proxy,
|
||||||
insecure = Config.copilot.allow_insecure,
|
insecure = Config.copilot.allow_insecure,
|
||||||
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 response.status == 200 then
|
||||||
|
M.state.github_token = vim.json.decode(response.body)
|
||||||
local file = Path:new(copilot_path)
|
local file = Path:new(copilot_path)
|
||||||
file:write(vim.json.encode(M.state.github_token), "w")
|
file:write(vim.json.encode(M.state.github_token), "w")
|
||||||
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,
|
else
|
||||||
})
|
error("Failed to get success response: " .. vim.inspect(response))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -168,7 +170,8 @@ M.setup = function()
|
|||||||
oauth_token = H.get_oauth_token(),
|
oauth_token = H.get_oauth_token(),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
H.refresh_token()
|
|
||||||
|
vim.schedule(function() H.refresh_token() end)
|
||||||
|
|
||||||
require("avante.tokenizers").setup(M.tokenizer_id)
|
require("avante.tokenizers").setup(M.tokenizer_id)
|
||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user