惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
U
Unit 42
MyScale Blog
MyScale Blog
J
Java Code Geeks
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
量子位
月光博客
月光博客
G
Google Developers Blog
V
V2EX
博客园 - 聂微东
宝玉的分享
宝玉的分享
IT之家
IT之家
Vercel News
Vercel News

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(oauth): bound GitHub Copilot requests · openclaw/open...
vincentkoc · 2026-05-28 · via Recent Commits to openclaw:main

@@ -22,6 +22,7 @@ const COPILOT_HEADERS = {

2222

const INITIAL_POLL_INTERVAL_MULTIPLIER = 1.2;

2323

const SLOW_DOWN_POLL_INTERVAL_MULTIPLIER = 1.4;

2424

const COPILOT_ROUTER_ID_PREFIX = "accounts/";

25+

const COPILOT_REQUEST_TIMEOUT_MS = 30_000;

25262627

type DeviceCodeResponse = {

2728

device_code: string;

@@ -50,6 +51,10 @@ type CopilotModelListEntry = {

5051

type?: unknown;

5152

};

5253

};

54+

type CopilotRequestOptions = {

55+

signal?: AbortSignal;

56+

timeoutMs?: number;

57+

};

53585459

export function normalizeDomain(input: string): string | null {

5560

const trimmed = input.trim();

@@ -107,29 +112,88 @@ export function getGitHubCopilotBaseUrl(token?: string, enterpriseDomain?: strin

107112

return "https://api.individual.githubcopilot.com";

108113

}

109114110-

async function fetchJson(url: string, init: RequestInit): Promise<unknown> {

111-

const response = await fetch(url, init);

115+

function formatCopilotRequestError(

116+

operation: string,

117+

error: unknown,

118+

options: Required<Pick<CopilotRequestOptions, "timeoutMs">> & {

119+

signal?: AbortSignal;

120+

},

121+

): Error {

122+

if (options.signal?.aborted) {

123+

return new Error("Login cancelled");

124+

}

125+

if (error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError")) {

126+

return new Error(`GitHub Copilot ${operation} timed out after ${options.timeoutMs}ms`);

127+

}

128+

return error instanceof Error

129+

? error

130+

: new Error(`GitHub Copilot ${operation} failed: ${String(error)}`);

131+

}

132+133+

function buildCopilotRequestSignal(options: CopilotRequestOptions): AbortSignal {

134+

const timeoutSignal = AbortSignal.timeout(options.timeoutMs ?? COPILOT_REQUEST_TIMEOUT_MS);

135+

if (!options.signal) {

136+

return timeoutSignal;

137+

}

138+

return AbortSignal.any([options.signal, timeoutSignal]);

139+

}

140+141+

async function fetchResponse(

142+

url: string,

143+

init: RequestInit,

144+

operation: string,

145+

options: CopilotRequestOptions = {},

146+

): Promise<Response> {

147+

const timeoutMs = options.timeoutMs ?? COPILOT_REQUEST_TIMEOUT_MS;

148+

try {

149+

return await fetch(url, {

150+

...init,

151+

signal: buildCopilotRequestSignal({ ...options, timeoutMs }),

152+

});

153+

} catch (error) {

154+

throw formatCopilotRequestError(operation, error, {

155+

signal: options.signal,

156+

timeoutMs,

157+

});

158+

}

159+

}

160+161+

async function fetchJson(

162+

url: string,

163+

init: RequestInit,

164+

operation: string,

165+

options: CopilotRequestOptions = {},

166+

): Promise<unknown> {

167+

const response = await fetchResponse(url, init, operation, options);

112168

if (!response.ok) {

113169

const text = await response.text();

114170

throw new Error(`${response.status} ${response.statusText}: ${text}`);

115171

}

116172

return response.json();

117173

}

118174119-

async function startDeviceFlow(domain: string): Promise<DeviceCodeResponse> {

175+

async function startDeviceFlow(

176+

domain: string,

177+

options: CopilotRequestOptions = {},

178+

): Promise<DeviceCodeResponse> {

120179

const urls = getUrls(domain);

121-

const data = await fetchJson(urls.deviceCodeUrl, {

122-

method: "POST",

123-

headers: {

124-

Accept: "application/json",

125-

"Content-Type": "application/x-www-form-urlencoded",

126-

"User-Agent": "GitHubCopilotChat/0.35.0",

180+

const data = await fetchJson(

181+

urls.deviceCodeUrl,

182+

{

183+

method: "POST",

184+

headers: {

185+

Accept: "application/json",

186+

"Content-Type": "application/x-www-form-urlencoded",

187+

"User-Agent": "GitHubCopilotChat/0.35.0",

188+

},

189+

body: new URLSearchParams({

190+

client_id: CLIENT_ID,

191+

scope: "read:user",

192+

}),

127193

},

128-

body: new URLSearchParams({

129-

client_id: CLIENT_ID,

130-

scope: "read:user",

131-

}),

132-

});

194+

"device code request",

195+

options,

196+

);

133197134198

if (!data || typeof data !== "object") {

135199

throw new Error("Invalid device code response");

@@ -205,19 +269,24 @@ async function pollForGitHubAccessToken(

205269

const waitMs = Math.min(Math.ceil(intervalMs * intervalMultiplier), remainingMs);

206270

await abortableSleep(waitMs, signal);

207271208-

const raw = await fetchJson(urls.accessTokenUrl, {

209-

method: "POST",

210-

headers: {

211-

Accept: "application/json",

212-

"Content-Type": "application/x-www-form-urlencoded",

213-

"User-Agent": "GitHubCopilotChat/0.35.0",

272+

const raw = await fetchJson(

273+

urls.accessTokenUrl,

274+

{

275+

method: "POST",

276+

headers: {

277+

Accept: "application/json",

278+

"Content-Type": "application/x-www-form-urlencoded",

279+

"User-Agent": "GitHubCopilotChat/0.35.0",

280+

},

281+

body: new URLSearchParams({

282+

client_id: CLIENT_ID,

283+

device_code: deviceCode,

284+

grant_type: "urn:ietf:params:oauth:grant-type:device_code",

285+

}),

214286

},

215-

body: new URLSearchParams({

216-

client_id: CLIENT_ID,

217-

device_code: deviceCode,

218-

grant_type: "urn:ietf:params:oauth:grant-type:device_code",

219-

}),

220-

});

287+

"device token request",

288+

{ signal },

289+

);

221290222291

if (

223292

raw &&

@@ -267,17 +336,23 @@ async function pollForGitHubAccessToken(

267336

export async function refreshGitHubCopilotToken(

268337

refreshToken: string,

269338

enterpriseDomain?: string,

339+

options: CopilotRequestOptions = {},

270340

): Promise<OAuthCredentials> {

271341

const domain = enterpriseDomain || "github.com";

272342

const urls = getUrls(domain);

273343274-

const raw = await fetchJson(urls.copilotTokenUrl, {

275-

headers: {

276-

Accept: "application/json",

277-

Authorization: `Bearer ${refreshToken}`,

278-

...COPILOT_HEADERS,

344+

const raw = await fetchJson(

345+

urls.copilotTokenUrl,

346+

{

347+

headers: {

348+

Accept: "application/json",

349+

Authorization: `Bearer ${refreshToken}`,

350+

...COPILOT_HEADERS,

351+

},

279352

},

280-

});

353+

"token refresh request",

354+

options,

355+

);

281356282357

if (!raw || typeof raw !== "object") {

283358

throw new Error("Invalid Copilot token response");

@@ -306,22 +381,28 @@ async function enableGitHubCopilotModel(

306381

token: string,

307382

modelId: string,

308383

enterpriseDomain?: string,

384+

options: CopilotRequestOptions = {},

309385

): Promise<boolean> {

310386

const baseUrl = getGitHubCopilotBaseUrl(token, enterpriseDomain);

311387

const url = `${baseUrl}/models/${modelId}/policy`;

312388313389

try {

314-

const response = await fetch(url, {

315-

method: "POST",

316-

headers: {

317-

"Content-Type": "application/json",

318-

Authorization: `Bearer ${token}`,

319-

...COPILOT_HEADERS,

320-

"openai-intent": "chat-policy",

321-

"x-interaction-type": "chat-policy",

390+

const response = await fetchResponse(

391+

url,

392+

{

393+

method: "POST",

394+

headers: {

395+

"Content-Type": "application/json",

396+

Authorization: `Bearer ${token}`,

397+

...COPILOT_HEADERS,

398+

"openai-intent": "chat-policy",

399+

"x-interaction-type": "chat-policy",

400+

},

401+

body: JSON.stringify({ state: "enabled" }),

322402

},

323-

body: JSON.stringify({ state: "enabled" }),

324-

});

403+

"model policy request",

404+

options,

405+

);

325406

return response.ok;

326407

} catch {

327408

return false;

@@ -331,21 +412,23 @@ async function enableGitHubCopilotModel(

331412

async function listGitHubCopilotModelIds(

332413

token: string,

333414

enterpriseDomain?: string,

415+

options: CopilotRequestOptions = {},

334416

): Promise<string[]> {

335417

const baseUrl = getGitHubCopilotBaseUrl(token, enterpriseDomain);

336418

const url = `${baseUrl}/models`;

337419

try {

338-

const response = await fetch(url, {

339-

headers: {

340-

Accept: "application/json",

341-

Authorization: `Bearer ${token}`,

342-

...COPILOT_HEADERS,

420+

const raw = await fetchJson(

421+

url,

422+

{

423+

headers: {

424+

Accept: "application/json",

425+

Authorization: `Bearer ${token}`,

426+

...COPILOT_HEADERS,

427+

},

343428

},

344-

});

345-

if (!response.ok) {

346-

return [];

347-

}

348-

const raw = await response.json();

429+

"model list request",

430+

options,

431+

);

349432

const data = raw && typeof raw === "object" ? (raw as { data?: unknown }).data : undefined;

350433

if (!Array.isArray(data)) {

351434

return [];

@@ -425,7 +508,7 @@ export async function loginGitHubCopilot(options: {

425508

}

426509

const domain = enterpriseDomain || "github.com";

427510428-

const device = await startDeviceFlow(domain);

511+

const device = await startDeviceFlow(domain, { signal: options.signal });

429512

options.onAuth(device.verification_uri, `Enter code: ${device.user_code}`);

430513431514

const githubAccessToken = await pollForGitHubAccessToken(

@@ -479,5 +562,7 @@ export const githubCopilotOAuthProvider: OAuthProviderInterface = {

479562

};

480563481564

export const testing = {

565+

enableGitHubCopilotModel,

482566

listGitHubCopilotModelIds,

567+

startDeviceFlow,

483568

};