Skip to content

Commit cf561f3

Browse files
rmuirAmadeusK525
authored andcommitted
chore(copilot): use GH_HOST for Copilot endpoints and token lookup
Update Copilot HTTP requests and token lookup to respect the `GH_HOST` env var, allowing use with GitHub Enterprise instances. Some networks block the `github.com` endpoints, so they won't work at all. Provide a way for the user to hit different endpoint other than `github.com` (e.g. mycorp.ghe.com) without patching the source code. The `GH_HOST` environment variable is the same one used to override this for `gh` cli: https://cli.github.com/manual/gh_help_environment
1 parent 1ae6d55 commit cf561f3

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

lua/codecompanion/adapters/http/copilot/init.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ end
3333
---@return table
3434
local function handlers(adapter)
3535
local model_opts = resolve_model_opts(adapter)
36+
local current_url = adapter.url or "https://api.githubcopilot.com/chat/completions"
37+
local base_url = current_url:gsub("/chat/completions$", ""):gsub("/responses$", "")
38+
3639
if model_opts.endpoint == "responses" then
37-
adapter.url = "https://api.githubcopilot.com/responses"
40+
adapter.url = base_url .. "/responses"
3841

3942
local responses = require("codecompanion.adapters.http.openai_responses")
4043

@@ -73,7 +76,7 @@ local function handlers(adapter)
7376
return responses.handlers
7477
end
7578

76-
adapter.url = "https://api.githubcopilot.com/chat/completions"
79+
adapter.url = base_url .. "/chat/completions"
7780
return require("codecompanion.adapters.http.openai").handlers
7881
end
7982

lua/codecompanion/adapters/http/copilot/stats.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ local function get_statistics()
2727

2828
local oauth_token = token.fetch({ force = true }).oauth_token
2929

30+
local host = vim.env.GH_HOST or "github.com"
31+
local endpoint
32+
if host == "github.com" then
33+
endpoint = "https://api.github.com/copilot_internal/v2/token"
34+
else
35+
-- GitHub Enterprise usually puts the API under /api/v3
36+
endpoint = string.format("https://%s/api/v3/copilot_internal/v2/token", host)
37+
end
38+
3039
local ok, response = pcall(function()
31-
return Curl.get("https://api.github.com/copilot_internal/user", {
40+
return Curl.get(endpoint, {
3241
sync = true,
3342
headers = {
3443
Authorization = "Bearer " .. oauth_token,

lua/codecompanion/adapters/http/copilot/token.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ local function get_oauth_token()
104104

105105
userdata = vim.json.decode(userdata)
106106
for key, value in pairs(userdata) do
107-
if string.find(key, "github.com") then
107+
if string.find(key, vim.env.GH_HOST or "github.com") then
108108
return value.oauth_token
109109
end
110110
end
@@ -136,8 +136,17 @@ local function get_copilot_token()
136136
_token_fetch_in_progress = true
137137
log:trace("Authorizing GitHub Copilot token")
138138

139+
local host = vim.env.GH_HOST or "github.com"
140+
local endpoint
141+
if host == "github.com" then
142+
endpoint = "https://api.github.com/copilot_internal/v2/token"
143+
else
144+
-- GitHub Enterprise usually puts the API under /api/v3
145+
endpoint = string.format("https://%s/api/v3/copilot_internal/v2/token", host)
146+
end
147+
139148
local ok, request = pcall(function()
140-
return Curl.get("https://api.github.com/copilot_internal/v2/token", {
149+
return Curl.get(endpoint, {
141150
headers = {
142151
Authorization = "Bearer " .. (M._oauth_token or ""),
143152
Accept = "application/json",

0 commit comments

Comments
 (0)