Skip to content

Commit 01d38b2

Browse files
authored
feat(providers): prioritize gh clie auth if available for github models (#1240)
Also add healthchecks Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 02cf9e5 commit 01d38b2

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ CopilotChat.nvim is a Neovim plugin that brings GitHub Copilot Chat capabilities
4040
4141
## Optional Dependencies
4242

43-
- [copilot.vim](https://github.com/github/copilot.vim) - For `:Copilot setup` authorization, otherwise in-built method i used
44-
4543
- [tiktoken_core](https://github.com/gptlang/lua-tiktoken) - For accurate token counting
4644
- Arch Linux: Install [`luajit-tiktoken-bin`](https://aur.archlinux.org/packages/luajit-tiktoken-bin) or [`lua51-tiktoken-bin`](https://aur.archlinux.org/packages/lua51-tiktoken-bin) from AUR
4745
- Via luarocks: `sudo luarocks install --lua-version 5.1 tiktoken_core`

lua/CopilotChat/config/providers.lua

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ end
9999

100100
--- Get the github copilot oauth cached token (gu_ token)
101101
---@return string
102-
local function get_github_token(tag)
102+
local function get_github_copilot_token(tag)
103103
local function config_path()
104104
local config = vim.fs.normalize('$XDG_CONFIG_HOME')
105105
if config and vim.uv.fs_stat(config) then
@@ -157,6 +157,33 @@ local function get_github_token(tag)
157157
return github_device_flow(tag, 'Iv1.b507a08c87ecfe98', '')
158158
end
159159

160+
local function get_github_models_token(tag)
161+
local token = get_token(tag)
162+
if token then
163+
return token
164+
end
165+
166+
-- loading token from the environment only in GitHub Codespaces
167+
local codespaces = os.getenv('CODESPACES')
168+
token = os.getenv('GITHUB_TOKEN')
169+
if token and codespaces then
170+
return set_token(tag, token, false)
171+
end
172+
173+
-- loading token from gh cli if available
174+
if vim.fn.executable('gh') == 0 then
175+
local result = utils.system({ 'gh', 'auth', 'token', '-h', 'github.com' })
176+
if result and result.code == 0 and result.stdout then
177+
local gh_token = vim.trim(result.stdout)
178+
if gh_token ~= '' and not gh_token:find('no oauth token') then
179+
return set_token(tag, gh_token, false)
180+
end
181+
end
182+
end
183+
184+
return github_device_flow(tag, '178c6fc778ccc68e1d6a', 'read:user copilot')
185+
end
186+
160187
---@class CopilotChat.config.providers.Options
161188
---@field model CopilotChat.client.Model
162189
---@field temperature number?
@@ -188,7 +215,7 @@ M.copilot = {
188215
local response, err = utils.curl_get('https://api.github.com/copilot_internal/v2/token', {
189216
json_response = true,
190217
headers = {
191-
['Authorization'] = 'Token ' .. get_github_token('github_copilot'),
218+
['Authorization'] = 'Token ' .. get_github_copilot_token('github_copilot'),
192219
},
193220
})
194221

@@ -209,7 +236,7 @@ M.copilot = {
209236
local response, err = utils.curl_get('https://api.github.com/copilot_internal/user', {
210237
json_response = true,
211238
headers = {
212-
['Authorization'] = 'Token ' .. get_github_token('github_copilot'),
239+
['Authorization'] = 'Token ' .. get_github_copilot_token('github_copilot'),
213240
},
214241
})
215242

@@ -430,7 +457,7 @@ M.github_models = {
430457

431458
get_headers = function()
432459
return {
433-
['Authorization'] = 'Bearer ' .. github_device_flow('github_models', 'Ov23liqtJusaUH38tIoK', 'read:user copilot'),
460+
['Authorization'] = 'Bearer ' .. get_github_models_token('github_models'),
434461
}
435462
end,
436463

lua/CopilotChat/health.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ function M.check()
5555
error('setup: not called, required for plugin to work. See `:h CopilotChat-installation`.')
5656
end
5757

58-
start('CopilotChat.nvim [filesystem]')
59-
6058
local testfile = os.tmpname()
6159
local f = io.open(testfile, 'w')
6260
local writable = false
@@ -104,6 +102,13 @@ function M.check()
104102
ok('lynx: ' .. lynx_version)
105103
end
106104

105+
local gh_version = run_command('gh', '--version')
106+
if gh_version == false then
107+
warn('gh: missing, optional for improved GitHub authorization. See "https://cli.github.com/".')
108+
else
109+
ok('gh: ' .. gh_version)
110+
end
111+
107112
start('CopilotChat.nvim [dependencies]')
108113

109114
if lualib_installed('plenary') then
@@ -118,7 +123,7 @@ function M.check()
118123
ok('copilot: ' .. (has_copilot and 'copilot.lua' or 'copilot.vim'))
119124
else
120125
warn(
121-
'copilot: missing, optional for improved auth implementation. Install "github/copilot.vim" or "zbirenbaum/copilot.lua" plugins.'
126+
'copilot: missing, optional for improved Copilot authorization. Install "github/copilot.vim" or "zbirenbaum/copilot.lua" plugins.'
122127
)
123128
end
124129

0 commit comments

Comments
 (0)