Skip to content

Commit

Permalink
fix(copilot): refresh token before sending the request (yetone#791)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
brewinski and aarnphm authored Nov 6, 2024
1 parent 9fbe025 commit c516883
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lua/avante/providers/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,24 @@ H.refresh_token = function()
not M.state.github_token
or (M.state.github_token.expires_at and M.state.github_token.expires_at < math.floor(os.time()))
then
curl.get(H.chat_auth_url, {
local response = curl.get(H.chat_auth_url, {
headers = {
["Authorization"] = "token " .. M.state.oauth_token,
["Accept"] = "application/json",
},
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,
callback = function(output)
M.state.github_token = vim.json.decode(output.body)
local file = Path:new(copilot_path)
file:write(vim.json.encode(M.state.github_token), "w")
if not vim.g.avante_login then vim.g.avante_login = true end
end,
})

if response.status == 200 then
M.state.github_token = vim.json.decode(response.body)
local file = Path:new(copilot_path)
file:write(vim.json.encode(M.state.github_token), "w")
if not vim.g.avante_login then vim.g.avante_login = true end
else
error("Failed to get success response: " .. vim.inspect(response))
end
end
end

Expand Down Expand Up @@ -168,7 +170,8 @@ M.setup = function()
oauth_token = H.get_oauth_token(),
}
end
H.refresh_token()

vim.schedule(function() H.refresh_token() end)

require("avante.tokenizers").setup(M.tokenizer_id)
vim.g.avante_login = true
Expand Down

0 comments on commit c516883

Please sign in to comment.