Skip to content

Commit 1eef643

Browse files
committed
refactor: simplify HTTP headers
Remove unnecessary HTTP headers and standardize header casing across the codebase. This simplifies the header management by: - Removing unused machine ID and session ID tracking - Using consistent header casing (capitalized) - Removing redundant version headers - Simplifying authorization header format These changes make the code more maintainable while preserving core functionality. Source: https://github.com/zed-industries/zed/blob/ad43bbbf5eda59eba65309735472e0be58b4f7dd/crates/copilot/src/copilot_chat.rs#L324
1 parent cdb9c5f commit 1eef643

2 files changed

Lines changed: 15 additions & 31 deletions

File tree

lua/CopilotChat/client.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ local Client = class(function(self, providers)
214214
self.current_job = nil
215215
self.expires_at = nil
216216
self.headers = nil
217-
self.machineid = utils.machine_id()
218217
end)
219218

220219
--- Authenticate with GitHub and get the required headers
@@ -229,8 +228,7 @@ function Client:authenticate(provider_name)
229228
notify.publish(notify.STATUS, 'Authenticating to provider ' .. provider_name)
230229

231230
local token, expires_at = provider.get_token()
232-
local sessionid = utils.uuid() .. tostring(math.floor(os.time() * 1000))
233-
headers = provider.get_headers(token, sessionid, self.machineid)
231+
headers = provider.get_headers(token)
234232
self.provider_cache[provider_name].headers = headers
235233
self.provider_cache[provider_name].expires_at = expires_at
236234
end

lua/CopilotChat/config/providers.lua

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local utils = require('CopilotChat.utils')
1717
---@class CopilotChat.Provider
1818
---@field disabled nil|boolean
1919
---@field embeddings nil|string
20-
---@field get_headers fun(token:string, sessionid:string, machineid:string):table<string, string>
20+
---@field get_headers fun(token:string):table<string, string>
2121
---@field get_token fun():string,number?
2222
---@field get_agents nil|fun(headers:table):table<CopilotChat.Provider.agent>
2323
---@field get_models nil|fun(headers:table):table<CopilotChat.Provider.model>
@@ -31,16 +31,6 @@ local EDITOR_VERSION = 'Neovim/'
3131
.. '.'
3232
.. vim.version().patch
3333

34-
local VERSION_HEADERS = {
35-
['editor-version'] = EDITOR_VERSION,
36-
['editor-plugin-version'] = 'CopilotChat.nvim/2.0.0',
37-
['user-agent'] = 'CopilotChat.nvim/2.0.0',
38-
['sec-fetch-site'] = 'none',
39-
['sec-fetch-mode'] = 'no-cors',
40-
['sec-fetch-dest'] = 'empty',
41-
['priority'] = 'u=4, i',
42-
}
43-
4434
--- Get the github oauth cached token
4535
---@return string|nil
4636
local cached_github_token = nil
@@ -92,25 +82,21 @@ local M = {}
9282
M.copilot = {
9383
embeddings = 'copilot_embeddings',
9484

95-
get_headers = function(token, sessionid, machineid)
96-
return vim.tbl_extend('force', {
97-
['authorization'] = 'Bearer ' .. token,
98-
['x-request-id'] = utils.uuid(),
99-
['vscode-sessionid'] = sessionid,
100-
['vscode-machineid'] = machineid,
101-
['copilot-integration-id'] = 'vscode-chat',
102-
['openai-organization'] = 'github-copilot',
103-
['openai-intent'] = 'conversation-panel',
104-
['content-type'] = 'application/json',
105-
}, VERSION_HEADERS)
85+
get_headers = function(token)
86+
return {
87+
['Authorization'] = 'Bearer ' .. token,
88+
['Editor-Version'] = EDITOR_VERSION,
89+
['Copilot-Integration-Id'] = 'vscode-chat',
90+
['Content-Type'] = 'application/json',
91+
}
10692
end,
10793

10894
get_token = function()
10995
local response, err = utils.curl_get('https://api.github.com/copilot_internal/v2/token', {
110-
headers = vim.tbl_extend('force', {
111-
['authorization'] = 'token ' .. get_github_token(),
112-
['accept'] = 'application/json',
113-
}, VERSION_HEADERS),
96+
headers = {
97+
['Authorization'] = 'Token ' .. get_github_token(),
98+
['Accept'] = 'application/json',
99+
}
114100
})
115101

116102
if err then
@@ -237,8 +223,8 @@ M.github_models = {
237223

238224
get_headers = function(token)
239225
return {
240-
['authorization'] = 'bearer ' .. token,
241-
['content-type'] = 'application/json',
226+
['Authorization'] = 'Bearer ' .. token,
227+
['Content-Type'] = 'application/json',
242228
['x-ms-useragent'] = EDITOR_VERSION,
243229
['x-ms-user-agent'] = EDITOR_VERSION,
244230
}

0 commit comments

Comments
 (0)