diff --git a/lua/plugins/copilot.lua b/lua/plugins/copilot.lua index 8b935e7..49f40bb 100644 --- a/lua/plugins/copilot.lua +++ b/lua/plugins/copilot.lua @@ -42,6 +42,43 @@ return { }, } + -- Copilot considers any response with a user name authenticated, even + -- when the server says `NotAuthorized`. That caused completion requests + -- to be sent with invalid credentials and produced `[Copilot.lua] table`. + -- Only allow suggestions after an explicit successful (`OK`) status. + local auth = require 'copilot.auth' + local client = require 'copilot.client' + local api = require 'copilot.api' + local auth_cache = { value = nil, checked_at = 0, checking = false } + + auth.is_authenticated = function(callback) + if not client.initialized then + return false + end + + local now = vim.uv.now() + if auth_cache.value ~= nil and now - auth_cache.checked_at < 30 * 1000 then + return auth_cache.value + end + + local lsp_client = client.get() + if not lsp_client or auth_cache.checking then + return false + end + + auth_cache.checking = true + api.check_status(lsp_client, {}, function(err, status) + auth_cache.checking = false + auth_cache.checked_at = vim.uv.now() + auth_cache.value = not err and status and status.status == 'OK' and status.user ~= nil + if callback then + callback(err) + end + end) + + return false + end + -- Hide Copilot suggestions when the completion menu is open -- local cmp_ok, cmp = pcall(require, 'cmp') -- if cmp_ok then