Skip to content

Commit c20450a

Browse files
Andy Yesteipete
authored andcommitted
fix(github-copilot): mint tokens with vscode chat identity
1 parent 6d89bf6 commit c20450a

5 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Docs: https://docs.openclaw.ai
4848
- OpenAI/Codex: point gateway missing-key recovery and wizard docs at the canonical `openai/gpt-5.5` plus Codex OAuth route, and fix trajectory export errors so they suggest the valid `openclaw sessions` command.
4949
- Google/Gemini: normalize retired `google/gemini-3-pro-preview` primary, fallback, and model-map refs during config load and unrelated config writes so saved config keeps targeting Gemini 3.1 Pro Preview.
5050
- Google/Gemini: normalize retired Gemini 3 Pro Preview ids inside emitted Google provider model config, so regenerated models.json rows test `google/gemini-3.1-pro-preview`.
51+
- GitHub Copilot: mint short-lived Copilot API tokens with the same `vscode-chat` integration identity used by runtime requests, and refresh legacy cached tokens missing that identity so image-capable Copilot models no longer inherit the `copilot-language-server` scope. Fixes #79946.
5152
- Plugins/doctor: drop stale managed npm install records when `openclaw doctor --fix` removes npm packages that shadow bundled plugins, so the rebuilt registry no longer resurrects the removed package metadata.
5253
- Discord/voice: reuse or suppress late realtime consult tool calls without stealing newer speaker context or speaking forced fallback answers twice.
5354
- Discord/voice: synthesize realtime playback timestamps from emitted Discord PCM so OpenAI realtime barge-in truncation no longer sees `audioEndMs=0` and skips legitimate interruptions.

‎extensions/github-copilot/models.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ describe("github-copilot token", () => {
359359
token: "cached;proxy-ep=proxy.example.com;",
360360
expiresAt: now + 60 * 60 * 1000,
361361
updatedAt: now,
362+
integrationId: "vscode-chat",
362363
});
363364

364365
const fetchImpl = vi.fn();

‎src/agents/copilot-dynamic-headers.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Context } from "@mariozechner/pi-ai";
2-
import { buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
2+
import { COPILOT_INTEGRATION_ID, buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
33

4-
export { buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
4+
export { COPILOT_INTEGRATION_ID, buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
55

66
function inferCopilotInitiator(messages: Context["messages"]): "agent" | "user" {
77
const last = messages[messages.length - 1];
@@ -43,7 +43,7 @@ export function buildCopilotDynamicHeaders(params: {
4343
}): Record<string, string> {
4444
return {
4545
...buildCopilotIdeHeaders(),
46-
"Copilot-Integration-Id": "vscode-chat",
46+
"Copilot-Integration-Id": COPILOT_INTEGRATION_ID,
4747
"Openai-Organization": "github-copilot",
4848
"x-initiator": inferCopilotInitiator(params.messages),
4949
...(params.hasImages ? { "Copilot-Vision-Request": "true" } : {}),

‎src/agents/github-copilot-token.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import { buildCopilotIdeHeaders } from "./copilot-dynamic-headers.js";
2+
import { COPILOT_INTEGRATION_ID, buildCopilotIdeHeaders } from "./copilot-dynamic-headers.js";
33
import {
44
deriveCopilotApiBaseUrlFromToken,
55
resolveCopilotApiToken,
@@ -47,7 +47,7 @@ describe("resolveCopilotApiToken", () => {
4747
expect(result.expiresAt).toBe(12_345_678_901_000);
4848
});
4949

50-
it("sends IDE headers when exchanging the GitHub token", async () => {
50+
it("sends IDE and integration headers when exchanging the GitHub token", async () => {
5151
const fetchImpl = vi.fn(async () => ({
5252
ok: true,
5353
json: async () => ({
@@ -71,7 +71,40 @@ describe("resolveCopilotApiToken", () => {
7171
expect(init.headers).toEqual({
7272
Accept: "application/json",
7373
Authorization: "Bearer github-token",
74+
"Copilot-Integration-Id": COPILOT_INTEGRATION_ID,
7475
...buildCopilotIdeHeaders({ includeApiVersion: true }),
7576
});
7677
});
78+
79+
it("refreshes legacy cached tokens without the vscode-chat integration identity", async () => {
80+
const fetchImpl = vi.fn(async () => ({
81+
ok: true,
82+
json: async () => ({
83+
token: "fresh-copilot-token",
84+
expires_at: Math.floor(Date.now() / 1000) + 3600,
85+
}),
86+
}));
87+
const saveJsonFileImpl = vi.fn();
88+
89+
const result = await resolveCopilotApiToken({
90+
githubToken: "github-token",
91+
cachePath: "/tmp/github-copilot-token-test.json",
92+
loadJsonFileImpl: () => ({
93+
token: "legacy-copilot-token",
94+
expiresAt: Date.now() + 60 * 60 * 1000,
95+
updatedAt: Date.now(),
96+
}),
97+
saveJsonFileImpl,
98+
fetchImpl: fetchImpl as unknown as typeof fetch,
99+
});
100+
101+
expect(result.token).toBe("fresh-copilot-token");
102+
expect(fetchImpl).toHaveBeenCalledTimes(1);
103+
expect(saveJsonFileImpl).toHaveBeenCalledWith("/tmp/github-copilot-token-test.json", {
104+
token: "fresh-copilot-token",
105+
expiresAt: expect.any(Number),
106+
updatedAt: expect.any(Number),
107+
integrationId: COPILOT_INTEGRATION_ID,
108+
});
109+
});
77110
});

‎src/plugin-sdk/provider-auth.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ export const COPILOT_EDITOR_VERSION = "vscode/1.96.2";
9898
export const COPILOT_USER_AGENT = "GitHubCopilotChat/0.26.7";
9999
export const COPILOT_EDITOR_PLUGIN_VERSION = "copilot-chat/0.35.0";
100100
export const COPILOT_GITHUB_API_VERSION = "2025-04-01";
101+
export const COPILOT_INTEGRATION_ID = "vscode-chat";
101102
export const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.githubcopilot.com";
102103

103104
export type CachedCopilotToken = {
104105
token: string;
105106
expiresAt: number;
106107
updatedAt: number;
108+
integrationId?: string;
107109
};
108110

109111
export function buildCopilotIdeHeaders(
@@ -124,7 +126,7 @@ function resolveCopilotTokenCachePath(env: NodeJS.ProcessEnv = process.env) {
124126
}
125127

126128
function isCopilotTokenUsable(cache: CachedCopilotToken, now = Date.now()): boolean {
127-
return cache.expiresAt - now > 5 * 60 * 1000;
129+
return cache.integrationId === COPILOT_INTEGRATION_ID && cache.expiresAt - now > 5 * 60 * 1000;
128130
}
129131

130132
function parseCopilotTokenResponse(value: unknown): {
@@ -232,6 +234,7 @@ export async function resolveCopilotApiToken(params: {
232234
headers: {
233235
Accept: "application/json",
234236
Authorization: `Bearer ${params.githubToken}`,
237+
"Copilot-Integration-Id": COPILOT_INTEGRATION_ID,
235238
...buildCopilotIdeHeaders({ includeApiVersion: true }),
236239
},
237240
});
@@ -245,6 +248,7 @@ export async function resolveCopilotApiToken(params: {
245248
token: json.token,
246249
expiresAt: json.expiresAt,
247250
updatedAt: Date.now(),
251+
integrationId: COPILOT_INTEGRATION_ID,
248252
};
249253
saveJsonFileFn(cachePath, payload);
250254

0 commit comments

Comments
 (0)