11local constants = require (' CopilotChat.constants' )
22local notify = require (' CopilotChat.notify' )
33local utils = require (' CopilotChat.utils' )
4+ local config = require (' CopilotChat.config' )
45local plenary_utils = require (' plenary.async.util' )
56local log = require (' plenary.log' )
67
78local EDITOR_VERSION = ' Neovim/' .. vim .version ().major .. ' .' .. vim .version ().minor .. ' .' .. vim .version ().patch
89
9- --- @class CopilotChat
10- --- @field config CopilotChat.config.Config
11- --- @field chat CopilotChat.ui.chat.Chat
12- local MC = setmetatable ({}, {
13- __index = function (t , key )
14- if key == ' config' then
15- return require (' CopilotChat.config' )
16- end
17- return rawget (t , key )
18- end ,
19- })
20-
2110local token_cache = nil
2211local unsaved_token_cache = {}
2312local function load_tokens ()
6352--- @return string
6453local function github_device_flow (tag , client_id , scope )
6554 local function request_device_code ()
66- local res = utils .curl_post (' https://' .. MC . config .github_instance_url .. ' /login/device/code' , {
55+ local res = utils .curl_post (' https://' .. config .github_instance_url .. ' /login/device/code' , {
6756 body = {
6857 client_id = client_id ,
6958 scope = scope ,
@@ -79,7 +68,7 @@ local function github_device_flow(tag, client_id, scope)
7968 while true do
8069 plenary_utils .sleep (interval * 1000 )
8170
82- local res = utils .curl_post (' https://' .. MC . config .github_instance_url .. ' /login/oauth/access_token' , {
71+ local res = utils .curl_post (' https://' .. config .github_instance_url .. ' /login/oauth/access_token' , {
8372 body = {
8473 client_id = client_id ,
8574 device_code = device_code ,
@@ -159,7 +148,7 @@ local function get_github_copilot_token(tag)
159148 local parsed_data = utils .json_decode (file_data )
160149 if parsed_data then
161150 for key , value in pairs (parsed_data ) do
162- if string.find (key , MC . config .github_instance_url ) and value and value .oauth_token then
151+ if string.find (key , config .github_instance_url ) and value and value .oauth_token then
163152 return set_token (tag , value .oauth_token , false )
164153 end
165154 end
@@ -186,7 +175,7 @@ local function get_github_models_token(tag)
186175
187176 -- loading token from gh cli if available
188177 if vim .fn .executable (' gh' ) == 0 then
189- local result = utils .system ({ ' gh' , ' auth' , ' token' , ' -h' , MC . config .github_instance_url })
178+ local result = utils .system ({ ' gh' , ' auth' , ' token' , ' -h' , config .github_instance_url })
190179 if result and result .code == 0 and result .stdout then
191180 local gh_token = vim .trim (result .stdout )
192181 if gh_token ~= ' ' and not gh_token :find (' no oauth token' ) then
@@ -227,12 +216,12 @@ M.copilot = {
227216 endpoints_api = ' ' ,
228217
229218 get_headers = function ()
230- local url = ' https://' .. MC . config .github_instance_api_url .. ' /copilot_internal/v2/token'
219+ local url = ' https://' .. config .github_instance_api_url .. ' /copilot_internal/v2/token'
231220 log .debug (' get headers - get ' .. url )
232221 local response , err = utils .curl_get (url , {
233222 json_response = true ,
234223 headers = {
235- [' Authorization' ] = ' Token ' .. get_github_copilot_token (MC . config .github_instance_api_url ),
224+ [' Authorization' ] = ' Token ' .. get_github_copilot_token (config .github_instance_api_url ),
236225 },
237226 })
238227
@@ -250,7 +239,7 @@ M.copilot = {
250239 )
251240 error (
252241 ' get_headers authenticated, but missing key "endpoints.api" in server response. Check log for details: '
253- .. MC . config .log_path
242+ .. config .log_path
254243 )
255244 end
256245
@@ -264,10 +253,10 @@ M.copilot = {
264253 end ,
265254
266255 get_info = function (headers )
267- local response , err = utils .curl_get (' https://' .. MC . config .github_instance_url .. ' /copilot_internal/user' , {
256+ local response , err = utils .curl_get (' https://' .. config .github_instance_url .. ' /copilot_internal/user' , {
268257 json_response = true ,
269258 headers = {
270- [' Authorization' ] = ' Token ' .. get_github_copilot_token (MC . config .github_instance_url ),
259+ [' Authorization' ] = ' Token ' .. get_github_copilot_token (config .github_instance_url ),
271260 },
272261 })
273262
0 commit comments