Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lua/codecompanion/adapters/http/copilot/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ end
---@return table
local function handlers(adapter)
local model_opts = resolve_model_opts(adapter)
local current_url = adapter.url or "https://api.githubcopilot.com/chat/completions"
local base_url = current_url:gsub("/chat/completions$", ""):gsub("/responses$", "")
Comment on lines +37 to +38
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I miss something here or is this basically enough if we expect base url in adapter.url

Suggested change
local current_url = adapter.url or "https://api.githubcopilot.com/chat/completions"
local base_url = current_url:gsub("/chat/completions$", ""):gsub("/responses$", "")
local base_url = adapter.url or "https://api.githubcopilot.com"


if model_opts.endpoint == "responses" then
adapter.url = "https://api.githubcopilot.com/responses"
adapter.url = base_url .. "/responses"

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

Expand Down Expand Up @@ -74,7 +77,7 @@ local function handlers(adapter)
return responses.handlers
end

adapter.url = "https://api.githubcopilot.com/chat/completions"
adapter.url = base_url .. "/chat/completions"
return require("codecompanion.adapters.http.openai").handlers
end

Expand Down
11 changes: 10 additions & 1 deletion lua/codecompanion/adapters/http/copilot/stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ local function get_statistics()

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

local host = vim.env.GH_HOST or "github.com"
local endpoint
if host == "github.com" then
endpoint = "https://api.github.com/copilot_internal/v2/token"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
endpoint = "https://api.github.com/copilot_internal/v2/token"
endpoint = "https://api.github.com/copilot_internal/user"

this seems to be a copy paste error, the path was "/copilot_internal/user" not "*/token" it's for stats not to request a token.

else
-- GitHub Enterprise usually puts the API under /api/v3
endpoint = string.format("https://%s/api/v3/copilot_internal/v2/token", host)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
endpoint = string.format("https://%s/api/v3/copilot_internal/v2/token", host)
endpoint = string.format("https://%s/api/v3/copilot_internal/user", host)

end

local ok, response = pcall(function()
return Curl.get("https://api.github.com/copilot_internal/user", {
return Curl.get(endpoint, {
sync = true,
headers = {
Authorization = "Bearer " .. oauth_token,
Expand Down
13 changes: 11 additions & 2 deletions lua/codecompanion/adapters/http/copilot/token.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ local function get_oauth_token()

userdata = vim.json.decode(userdata)
for key, value in pairs(userdata) do
if string.find(key, "github.com") then
if string.find(key, vim.env.GH_HOST or "github.com") then
return value.oauth_token
end
end
Expand Down Expand Up @@ -136,8 +136,17 @@ local function get_copilot_token()
_token_fetch_in_progress = true
log:trace("Authorizing GitHub Copilot token")

local host = vim.env.GH_HOST or "github.com"
local endpoint
if host == "github.com" then
endpoint = "https://api.github.com/copilot_internal/v2/token"
else
-- GitHub Enterprise usually puts the API under /api/v3
endpoint = string.format("https://%s/api/v3/copilot_internal/v2/token", host)
end

local ok, request = pcall(function()
return Curl.get("https://api.github.com/copilot_internal/v2/token", {
return Curl.get(endpoint, {
headers = {
Authorization = "Bearer " .. (M._oauth_token or ""),
Accept = "application/json",
Expand Down