fix(github-copilot): send IDE auth headers on runtime requests (#60755)

* Fix Copilot IDE auth headers

* fix(github-copilot): align tests and changelog

* fix(changelog): scope copilot replacement entry

---------

Co-authored-by: VACInc <3279061+VACInc@users.noreply.github.com>
This commit is contained in:
Vincent Koc
2026-04-04 17:22:19 +09:00
committed by GitHub
parent 38ed8c355a
commit cdccbf2c1c
5 changed files with 57 additions and 3 deletions

View File

@@ -1,5 +1,9 @@
import type { Context } from "@mariozechner/pi-ai";
export const COPILOT_EDITOR_VERSION = "vscode/1.96.2";
export const COPILOT_USER_AGENT = "GitHubCopilotChat/0.26.7";
export const COPILOT_GITHUB_API_VERSION = "2025-04-01";
function inferCopilotInitiator(messages: Context["messages"]): "agent" | "user" {
const last = messages[messages.length - 1];
return last && last.role !== "user" ? "agent" : "user";
@@ -17,11 +21,24 @@ export function hasCopilotVisionInput(messages: Context["messages"]): boolean {
});
}
export function buildCopilotIdeHeaders(
params: {
includeApiVersion?: boolean;
} = {},
): Record<string, string> {
return {
"Editor-Version": COPILOT_EDITOR_VERSION,
"User-Agent": COPILOT_USER_AGENT,
...(params.includeApiVersion ? { "X-Github-Api-Version": COPILOT_GITHUB_API_VERSION } : {}),
};
}
export function buildCopilotDynamicHeaders(params: {
messages: Context["messages"];
hasImages: boolean;
}): Record<string, string> {
return {
...buildCopilotIdeHeaders(),
"X-Initiator": inferCopilotInitiator(params.messages),
"Openai-Intent": "conversation-edits",
...(params.hasImages ? { "Copilot-Vision-Request": "true" } : {}),

View File

@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { buildCopilotIdeHeaders } from "./copilot-dynamic-headers.js";
import {
deriveCopilotApiBaseUrlFromToken,
resolveCopilotApiToken,
@@ -45,4 +46,34 @@ describe("resolveCopilotApiToken", () => {
expect(result.expiresAt).toBe(12_345_678_901_000);
});
it("sends IDE headers when exchanging the GitHub token", async () => {
const fetchImpl = vi.fn(async () => ({
ok: true,
json: async () => ({
token: "copilot-token",
expires_at: Math.floor(Date.now() / 1000) + 3600,
}),
}));
await resolveCopilotApiToken({
githubToken: "github-token",
cachePath: "/tmp/github-copilot-token-test.json",
loadJsonFileImpl: () => undefined,
saveJsonFileImpl: () => undefined,
fetchImpl: fetchImpl as unknown as typeof fetch,
});
expect(fetchImpl).toHaveBeenCalledWith(
"https://api.github.com/copilot_internal/v2/token",
expect.objectContaining({
method: "GET",
headers: expect.objectContaining({
Accept: "application/json",
Authorization: "Bearer github-token",
...buildCopilotIdeHeaders({ includeApiVersion: true }),
}),
}),
);
});
});

View File

@@ -1,6 +1,7 @@
import path from "node:path";
import { resolveStateDir } from "../config/paths.js";
import { loadJsonFile, saveJsonFile } from "../infra/json-file.js";
import { buildCopilotIdeHeaders } from "./copilot-dynamic-headers.js";
import { resolveProviderEndpoint } from "./provider-attribution.js";
const COPILOT_TOKEN_URL = "https://api.github.com/copilot_internal/v2/token";
@@ -135,6 +136,7 @@ export async function resolveCopilotApiToken(params: {
headers: {
Accept: "application/json",
Authorization: `Bearer ${params.githubToken}`,
...buildCopilotIdeHeaders({ includeApiVersion: true }),
},
});