opencodekit 0.18.15 → 0.18.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,25 +17,25 @@ const CLIENT_ID = "Ov23li8tweQw6odWQebz";
|
|
|
17
17
|
|
|
18
18
|
// Logger function that will be set by the plugin
|
|
19
19
|
let log: (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
level: "debug" | "info" | "warn" | "error",
|
|
21
|
+
message: string,
|
|
22
|
+
extra?: Record<string, any>,
|
|
23
23
|
) => void = () => {};
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Set the logger function from the plugin context
|
|
27
27
|
*/
|
|
28
28
|
function setLogger(client: any) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
log = (level, message, extra) => {
|
|
30
|
+
client.app
|
|
31
|
+
.log({
|
|
32
|
+
service: "copilot-auth",
|
|
33
|
+
level,
|
|
34
|
+
message,
|
|
35
|
+
extra,
|
|
36
|
+
})
|
|
37
|
+
.catch(() => {}); // Fire and forget, don't block on logging
|
|
38
|
+
};
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// Add a small safety buffer when polling to avoid hitting the server
|
|
@@ -43,64 +43,81 @@ function setLogger(client: any) {
|
|
|
43
43
|
const OAUTH_POLLING_SAFETY_MARGIN_MS = 3000; // 3 seconds
|
|
44
44
|
|
|
45
45
|
const HEADERS = {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
"User-Agent": "GitHubCopilotChat/0.35.0",
|
|
47
|
+
"Editor-Version": "vscode/1.107.0",
|
|
48
|
+
"Editor-Plugin-Version": "copilot-chat/0.35.0",
|
|
49
|
+
"Copilot-Integration-Id": "vscode-chat",
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
const RESPONSES_API_ALTERNATE_INPUT_TYPES = [
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
53
|
+
"file_search_call",
|
|
54
|
+
"computer_call",
|
|
55
|
+
"computer_call_output",
|
|
56
|
+
"web_search_call",
|
|
57
|
+
"function_call",
|
|
58
|
+
"function_call_output",
|
|
59
|
+
"image_generation_call",
|
|
60
|
+
"code_interpreter_call",
|
|
61
|
+
"local_shell_call",
|
|
62
|
+
"local_shell_call_output",
|
|
63
|
+
"mcp_list_tools",
|
|
64
|
+
"mcp_approval_request",
|
|
65
|
+
"mcp_approval_response",
|
|
66
|
+
"mcp_call",
|
|
67
|
+
"reasoning",
|
|
68
68
|
];
|
|
69
69
|
|
|
70
70
|
function normalizeDomain(url: string): string {
|
|
71
|
-
|
|
71
|
+
return url.replace(/^https?:\/\//, "").replace(/\/$/, "");
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
function getUrls(domain: string) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
return {
|
|
76
|
+
DEVICE_CODE_URL: `https://${domain}/login/device/code`,
|
|
77
|
+
ACCESS_TOKEN_URL: `https://${domain}/login/oauth/access_token`,
|
|
78
|
+
};
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
82
82
|
|
|
83
83
|
// Rate limit handling configuration
|
|
84
84
|
const RATE_LIMIT_CONFIG = {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
maxRetries: 3,
|
|
86
|
+
baseDelayMs: 2000, // Start with 2 seconds
|
|
87
|
+
maxDelayMs: 60000, // Cap at 60 seconds
|
|
88
|
+
defaultCooldownMs: 60000, // Default cooldown when Retry-After header is missing
|
|
89
|
+
maxFallbacks: 4, // Max model fallback switches per request
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
// Per-model rate limit state (in-memory, resets on restart)
|
|
93
93
|
interface RateLimitEntry {
|
|
94
|
-
|
|
94
|
+
rateLimitedUntil: number; // Unix timestamp (ms)
|
|
95
95
|
}
|
|
96
96
|
const rateLimitState = new Map<string, RateLimitEntry>();
|
|
97
97
|
|
|
98
98
|
// Model fallback chains: same-family alternatives when a model is rate-limited
|
|
99
99
|
const MODEL_FALLBACK_CHAINS: Record<string, string[]> = {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
// Claude family
|
|
101
|
+
"claude-opus-4.6": [
|
|
102
|
+
"claude-opus-4.5",
|
|
103
|
+
"claude-sonnet-4.6",
|
|
104
|
+
"claude-sonnet-4.5",
|
|
105
|
+
],
|
|
106
|
+
"claude-opus-4.5": [
|
|
107
|
+
"claude-opus-4.6",
|
|
108
|
+
"claude-sonnet-4.5",
|
|
109
|
+
"claude-sonnet-4.6",
|
|
110
|
+
],
|
|
111
|
+
"claude-sonnet-4.6": [
|
|
112
|
+
"claude-sonnet-4.5",
|
|
113
|
+
"claude-opus-4.6",
|
|
114
|
+
"claude-opus-4.5",
|
|
115
|
+
],
|
|
116
|
+
"claude-sonnet-4.5": [
|
|
117
|
+
"claude-sonnet-4.6",
|
|
118
|
+
"claude-opus-4.5",
|
|
119
|
+
"claude-opus-4.6",
|
|
120
|
+
],
|
|
104
121
|
};
|
|
105
122
|
|
|
106
123
|
/**
|
|
@@ -108,35 +125,35 @@ const MODEL_FALLBACK_CHAINS: Record<string, string[]> = {
|
|
|
108
125
|
* Returns cooldown in milliseconds, or null if header is missing/unparseable.
|
|
109
126
|
*/
|
|
110
127
|
function parseRetryAfter(response: Response): number | null {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
128
|
+
const header = response.headers.get("retry-after");
|
|
129
|
+
if (!header) return null;
|
|
130
|
+
// Try as seconds first (most common)
|
|
131
|
+
const seconds = parseInt(header, 10);
|
|
132
|
+
if (!isNaN(seconds) && seconds > 0) return seconds * 1000;
|
|
133
|
+
// Try as HTTP date
|
|
134
|
+
const date = Date.parse(header);
|
|
135
|
+
if (!isNaN(date)) return Math.max(0, date - Date.now());
|
|
136
|
+
return null;
|
|
120
137
|
}
|
|
121
138
|
|
|
122
139
|
function isModelRateLimited(model: string): boolean {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
140
|
+
const entry = rateLimitState.get(model);
|
|
141
|
+
if (!entry) return false;
|
|
142
|
+
if (Date.now() >= entry.rateLimitedUntil) {
|
|
143
|
+
rateLimitState.delete(model);
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
130
147
|
}
|
|
131
148
|
|
|
132
149
|
function markModelRateLimited(model: string, cooldownMs: number): void {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
150
|
+
rateLimitState.set(model, {
|
|
151
|
+
rateLimitedUntil: Date.now() + cooldownMs,
|
|
152
|
+
});
|
|
153
|
+
log(
|
|
154
|
+
"info",
|
|
155
|
+
`Marked ${model} as rate-limited for ${Math.round(cooldownMs / 1000)}s`,
|
|
156
|
+
);
|
|
140
157
|
}
|
|
141
158
|
|
|
142
159
|
/**
|
|
@@ -144,29 +161,29 @@ function markModelRateLimited(model: string, cooldownMs: number): void {
|
|
|
144
161
|
* Skips models that are themselves rate-limited.
|
|
145
162
|
*/
|
|
146
163
|
function getNextFallbackModel(model: string): string | null {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
164
|
+
const chain = MODEL_FALLBACK_CHAINS[model];
|
|
165
|
+
if (!chain) return null;
|
|
166
|
+
for (const fallback of chain) {
|
|
167
|
+
if (!isModelRateLimited(fallback)) return fallback;
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
153
170
|
}
|
|
154
171
|
|
|
155
172
|
/**
|
|
156
173
|
* Swap the model field in a fetch RequestInit body.
|
|
157
174
|
*/
|
|
158
175
|
function swapModelInBody(
|
|
159
|
-
|
|
160
|
-
|
|
176
|
+
init: RequestInit | undefined,
|
|
177
|
+
newModel: string,
|
|
161
178
|
): RequestInit | undefined {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
179
|
+
if (!init?.body || typeof init.body !== "string") return init;
|
|
180
|
+
try {
|
|
181
|
+
const body = JSON.parse(init.body);
|
|
182
|
+
body.model = newModel;
|
|
183
|
+
return { ...init, body: JSON.stringify(body) };
|
|
184
|
+
} catch {
|
|
185
|
+
return init;
|
|
186
|
+
}
|
|
170
187
|
}
|
|
171
188
|
|
|
172
189
|
// Maximum length for item IDs in the OpenAI Responses API
|
|
@@ -178,17 +195,17 @@ const MAX_RESPONSE_API_ID_LENGTH = 64;
|
|
|
178
195
|
* See: https://github.com/vercel/ai/issues/5171
|
|
179
196
|
*/
|
|
180
197
|
function sanitizeResponseId(id: string): string {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
198
|
+
if (!id || id.length <= MAX_RESPONSE_API_ID_LENGTH) return id;
|
|
199
|
+
// Use a simple hash: take first 8 chars + hash of full string for uniqueness
|
|
200
|
+
// Format: "h_" + first 8 chars + "_" + base36 hash (up to ~50 chars total)
|
|
201
|
+
let hash = 0;
|
|
202
|
+
for (let i = 0; i < id.length; i++) {
|
|
203
|
+
hash = ((hash << 5) - hash + id.charCodeAt(i)) | 0;
|
|
204
|
+
}
|
|
205
|
+
const hashStr = Math.abs(hash).toString(36);
|
|
206
|
+
const prefix = id.slice(0, 8);
|
|
207
|
+
// Ensure total length <= 64: "h_" (2) + prefix (8) + "_" (1) + hash
|
|
208
|
+
return `h_${prefix}_${hashStr}`.slice(0, MAX_RESPONSE_API_ID_LENGTH);
|
|
192
209
|
}
|
|
193
210
|
|
|
194
211
|
/**
|
|
@@ -196,632 +213,631 @@ function sanitizeResponseId(id: string): string {
|
|
|
196
213
|
* Recursively checks `id` and `call_id` fields on each input item.
|
|
197
214
|
*/
|
|
198
215
|
function sanitizeResponseInputIds(input: any[]): any[] {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
+
return input.map((item: any) => {
|
|
217
|
+
if (!item || typeof item !== "object") return item;
|
|
218
|
+
const sanitized = { ...item };
|
|
219
|
+
if (
|
|
220
|
+
typeof sanitized.id === "string" &&
|
|
221
|
+
sanitized.id.length > MAX_RESPONSE_API_ID_LENGTH
|
|
222
|
+
) {
|
|
223
|
+
sanitized.id = sanitizeResponseId(sanitized.id);
|
|
224
|
+
}
|
|
225
|
+
if (
|
|
226
|
+
typeof sanitized.call_id === "string" &&
|
|
227
|
+
sanitized.call_id.length > MAX_RESPONSE_API_ID_LENGTH
|
|
228
|
+
) {
|
|
229
|
+
sanitized.call_id = sanitizeResponseId(sanitized.call_id);
|
|
230
|
+
}
|
|
231
|
+
return sanitized;
|
|
232
|
+
});
|
|
216
233
|
}
|
|
217
234
|
|
|
218
235
|
/**
|
|
219
236
|
* Retries: 2s, 4s, 8s (with jitter)
|
|
220
237
|
*/
|
|
221
238
|
function calculateRetryDelay(attempt: number): number {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
239
|
+
const exponentialDelay = RATE_LIMIT_CONFIG.baseDelayMs * 2 ** attempt;
|
|
240
|
+
const jitter = Math.random() * 1000; // Add 0-1s random jitter
|
|
241
|
+
const delay = Math.min(
|
|
242
|
+
exponentialDelay + jitter,
|
|
243
|
+
RATE_LIMIT_CONFIG.maxDelayMs,
|
|
244
|
+
);
|
|
245
|
+
return Math.round(delay);
|
|
229
246
|
}
|
|
230
247
|
|
|
231
248
|
export const CopilotAuthPlugin: Plugin = async ({ client: sdk }) => {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
};
|
|
249
|
+
// Initialize logger with the SDK client
|
|
250
|
+
setLogger(sdk);
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
auth: {
|
|
254
|
+
provider: "github-copilot",
|
|
255
|
+
loader: async (getAuth, provider) => {
|
|
256
|
+
const info = await getAuth();
|
|
257
|
+
if (!info || info.type !== "oauth") return {};
|
|
258
|
+
|
|
259
|
+
// Enterprise URL support for baseURL
|
|
260
|
+
const enterpriseUrl = (info as any).enterpriseUrl;
|
|
261
|
+
const baseURL = enterpriseUrl
|
|
262
|
+
? `https://copilot-api.${normalizeDomain(enterpriseUrl)}`
|
|
263
|
+
: undefined;
|
|
264
|
+
|
|
265
|
+
if (provider && provider.models) {
|
|
266
|
+
for (const [_modelId, model] of Object.entries(provider.models)) {
|
|
267
|
+
model.cost = {
|
|
268
|
+
input: 0,
|
|
269
|
+
output: 0,
|
|
270
|
+
cache: {
|
|
271
|
+
read: 0,
|
|
272
|
+
write: 0,
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// All models use the standard github-copilot SDK
|
|
277
|
+
// Reasoning support for Claude models is handled via:
|
|
278
|
+
// 1. The fetch wrapper adds thinking_budget to request body
|
|
279
|
+
// 2. The fetch wrapper strips invalid thinking blocks from messages
|
|
280
|
+
model.api.npm = "@ai-sdk/github-copilot";
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
baseURL,
|
|
286
|
+
apiKey: "",
|
|
287
|
+
async fetch(input, init) {
|
|
288
|
+
const info = await getAuth();
|
|
289
|
+
if (info.type !== "oauth") return fetch(input, init);
|
|
290
|
+
|
|
291
|
+
let isAgentCall = false;
|
|
292
|
+
let isVisionRequest = false;
|
|
293
|
+
let modifiedBody: any;
|
|
294
|
+
let isClaudeModel = false;
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
const body =
|
|
298
|
+
typeof init?.body === "string"
|
|
299
|
+
? JSON.parse(init.body)
|
|
300
|
+
: init?.body;
|
|
301
|
+
|
|
302
|
+
const url = input.toString();
|
|
303
|
+
|
|
304
|
+
// Check if this is a Claude model request
|
|
305
|
+
const modelId = body?.model || "";
|
|
306
|
+
isClaudeModel = modelId.toLowerCase().includes("claude");
|
|
307
|
+
|
|
308
|
+
// Completions API
|
|
309
|
+
if (body?.messages && url.includes("completions")) {
|
|
310
|
+
// Keep local logic: detect if any message is assistant/tool
|
|
311
|
+
isAgentCall = body.messages.some((msg: any) =>
|
|
312
|
+
["tool", "assistant"].includes(msg.role),
|
|
313
|
+
);
|
|
314
|
+
isVisionRequest = body.messages.some(
|
|
315
|
+
(msg: any) =>
|
|
316
|
+
Array.isArray(msg.content) &&
|
|
317
|
+
msg.content.some((part: any) => part.type === "image_url"),
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
// For Claude models, add thinking_budget to enable reasoning
|
|
321
|
+
// The Copilot API accepts this parameter and returns reasoning_text/reasoning_opaque
|
|
322
|
+
if (isClaudeModel) {
|
|
323
|
+
// Use configured thinking_budget from model options, or default to 10000
|
|
324
|
+
const thinkingBudget = body.thinking_budget || 10000;
|
|
325
|
+
|
|
326
|
+
// Fix for "Invalid signature in thinking block" error:
|
|
327
|
+
// The Copilot API uses reasoning_text/reasoning_opaque format for thinking
|
|
328
|
+
// When these are passed back without proper signature, it causes errors
|
|
329
|
+
// Solution: Ensure reasoning_opaque is present when reasoning_text exists,
|
|
330
|
+
// or remove reasoning content entirely if signature is invalid/missing
|
|
331
|
+
const cleanedMessages = body.messages.map(
|
|
332
|
+
(msg: any, idx: number) => {
|
|
333
|
+
if (msg.role !== "assistant") return msg;
|
|
334
|
+
|
|
335
|
+
// Log message structure for debugging
|
|
336
|
+
log("debug", `Processing assistant message ${idx}`, {
|
|
337
|
+
has_reasoning_text: !!msg.reasoning_text,
|
|
338
|
+
has_reasoning_opaque: !!msg.reasoning_opaque,
|
|
339
|
+
content_type: typeof msg.content,
|
|
340
|
+
content_is_array: Array.isArray(msg.content),
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// If message has reasoning_text but no/invalid reasoning_opaque, remove reasoning
|
|
344
|
+
if (msg.reasoning_text && !msg.reasoning_opaque) {
|
|
345
|
+
log(
|
|
346
|
+
"warn",
|
|
347
|
+
`Removing reasoning_text without reasoning_opaque from message ${idx}`,
|
|
348
|
+
);
|
|
349
|
+
const { reasoning_text: _unused, ...cleanedMsg } = msg;
|
|
350
|
+
return cleanedMsg;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// If content is an array, check for thinking blocks
|
|
354
|
+
if (Array.isArray(msg.content)) {
|
|
355
|
+
const hasThinkingBlock = msg.content.some(
|
|
356
|
+
(part: any) => part.type === "thinking",
|
|
357
|
+
);
|
|
358
|
+
if (hasThinkingBlock) {
|
|
359
|
+
log(
|
|
360
|
+
"debug",
|
|
361
|
+
`Message ${idx} has thinking blocks in content array`,
|
|
362
|
+
);
|
|
363
|
+
// Filter out thinking blocks without signatures
|
|
364
|
+
const cleanedContent = msg.content.filter(
|
|
365
|
+
(part: any) => {
|
|
366
|
+
if (part.type === "thinking") {
|
|
367
|
+
if (!part.signature) {
|
|
368
|
+
log(
|
|
369
|
+
"warn",
|
|
370
|
+
`Removing thinking block without signature`,
|
|
371
|
+
);
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return true;
|
|
376
|
+
},
|
|
377
|
+
);
|
|
378
|
+
return {
|
|
379
|
+
...msg,
|
|
380
|
+
content:
|
|
381
|
+
cleanedContent.length > 0 ? cleanedContent : null,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return msg;
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
modifiedBody = {
|
|
391
|
+
...body,
|
|
392
|
+
messages: cleanedMessages,
|
|
393
|
+
thinking_budget: thinkingBudget,
|
|
394
|
+
};
|
|
395
|
+
log("info", `Adding thinking_budget for Claude model`, {
|
|
396
|
+
model: modelId,
|
|
397
|
+
thinking_budget: thinkingBudget,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// For GPT models (o1, gpt-5, etc.), add reasoning parameter
|
|
402
|
+
const isGptModel =
|
|
403
|
+
modelId.toLowerCase().includes("gpt") ||
|
|
404
|
+
modelId.toLowerCase().includes("o1") ||
|
|
405
|
+
modelId.toLowerCase().includes("o3") ||
|
|
406
|
+
modelId.toLowerCase().includes("o4");
|
|
407
|
+
|
|
408
|
+
if (isGptModel && !isClaudeModel) {
|
|
409
|
+
// Get reasoning effort from body options or default to "medium"
|
|
410
|
+
const reasoningEffort =
|
|
411
|
+
body.reasoning?.effort ||
|
|
412
|
+
body.reasoningEffort ||
|
|
413
|
+
body.reasoning_effort ||
|
|
414
|
+
"medium";
|
|
415
|
+
|
|
416
|
+
modifiedBody = {
|
|
417
|
+
...(modifiedBody || body),
|
|
418
|
+
reasoning: {
|
|
419
|
+
effort: reasoningEffort,
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// Also pass through other reasoning options if present
|
|
424
|
+
if (body.reasoningSummary || body.reasoning?.summary) {
|
|
425
|
+
modifiedBody.reasoning.summary =
|
|
426
|
+
body.reasoningSummary || body.reasoning?.summary;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
log("info", `Adding reasoning for GPT model`, {
|
|
430
|
+
model: modelId,
|
|
431
|
+
reasoning_effort: reasoningEffort,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Responses API
|
|
437
|
+
if (body?.input) {
|
|
438
|
+
// Sanitize long IDs from Copilot backend (can be 400+ chars)
|
|
439
|
+
// OpenAI Responses API enforces a 64-char max on item IDs
|
|
440
|
+
const sanitizedInput = sanitizeResponseInputIds(body.input);
|
|
441
|
+
const inputWasSanitized =
|
|
442
|
+
sanitizedInput !== body.input &&
|
|
443
|
+
JSON.stringify(sanitizedInput) !== JSON.stringify(body.input);
|
|
444
|
+
|
|
445
|
+
if (inputWasSanitized) {
|
|
446
|
+
log("info", "Sanitized long IDs in Responses API input", {
|
|
447
|
+
original_count: body.input.filter(
|
|
448
|
+
(item: any) =>
|
|
449
|
+
(typeof item?.id === "string" &&
|
|
450
|
+
item.id.length > MAX_RESPONSE_API_ID_LENGTH) ||
|
|
451
|
+
(typeof item?.call_id === "string" &&
|
|
452
|
+
item.call_id.length > MAX_RESPONSE_API_ID_LENGTH),
|
|
453
|
+
).length,
|
|
454
|
+
});
|
|
455
|
+
modifiedBody = {
|
|
456
|
+
...(modifiedBody || body),
|
|
457
|
+
input: sanitizedInput,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
isAgentCall = (sanitizedInput || body.input).some(
|
|
462
|
+
(item: any) =>
|
|
463
|
+
item?.role === "assistant" ||
|
|
464
|
+
(item?.type &&
|
|
465
|
+
RESPONSES_API_ALTERNATE_INPUT_TYPES.includes(item.type)),
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
isVisionRequest = body.input.some(
|
|
469
|
+
(item: any) =>
|
|
470
|
+
Array.isArray(item?.content) &&
|
|
471
|
+
item.content.some(
|
|
472
|
+
(part: any) => part.type === "input_image",
|
|
473
|
+
),
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Messages API (Anthropic style)
|
|
478
|
+
if (body?.messages && !url.includes("completions")) {
|
|
479
|
+
isAgentCall = body.messages.some((msg: any) =>
|
|
480
|
+
["tool", "assistant"].includes(msg.role),
|
|
481
|
+
);
|
|
482
|
+
isVisionRequest = body.messages.some(
|
|
483
|
+
(item: any) =>
|
|
484
|
+
Array.isArray(item?.content) &&
|
|
485
|
+
item.content.some(
|
|
486
|
+
(part: any) =>
|
|
487
|
+
part?.type === "image" ||
|
|
488
|
+
(part?.type === "tool_result" &&
|
|
489
|
+
Array.isArray(part?.content) &&
|
|
490
|
+
part.content.some(
|
|
491
|
+
(nested: any) => nested?.type === "image",
|
|
492
|
+
)),
|
|
493
|
+
),
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
} catch {}
|
|
497
|
+
|
|
498
|
+
const headers: Record<string, string> = {
|
|
499
|
+
"x-initiator": isAgentCall ? "agent" : "user",
|
|
500
|
+
...(init?.headers as Record<string, string>),
|
|
501
|
+
...HEADERS,
|
|
502
|
+
Authorization: `Bearer ${info.refresh}`,
|
|
503
|
+
"Openai-Intent": "conversation-edits",
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
if (isVisionRequest) {
|
|
507
|
+
headers["Copilot-Vision-Request"] = "true";
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Official only deletes lowercase "authorization"
|
|
511
|
+
delete headers["x-api-key"];
|
|
512
|
+
delete headers["authorization"];
|
|
513
|
+
|
|
514
|
+
// Prepare the final init object with potentially modified body
|
|
515
|
+
const finalInit = {
|
|
516
|
+
...init,
|
|
517
|
+
headers,
|
|
518
|
+
...(modifiedBody ? { body: JSON.stringify(modifiedBody) } : {}),
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
// Extract model from request body for rate limit tracking
|
|
522
|
+
let currentModel = "";
|
|
523
|
+
try {
|
|
524
|
+
const bodyObj =
|
|
525
|
+
typeof finalInit.body === "string"
|
|
526
|
+
? JSON.parse(finalInit.body)
|
|
527
|
+
: finalInit.body;
|
|
528
|
+
currentModel = bodyObj?.model || "";
|
|
529
|
+
} catch {}
|
|
530
|
+
|
|
531
|
+
// Pre-flight: if current model is already known rate-limited, switch to fallback
|
|
532
|
+
let activeFinalInit: RequestInit = finalInit;
|
|
533
|
+
if (currentModel && isModelRateLimited(currentModel)) {
|
|
534
|
+
const fallback = getNextFallbackModel(currentModel);
|
|
535
|
+
if (fallback) {
|
|
536
|
+
log(
|
|
537
|
+
"info",
|
|
538
|
+
`Model ${currentModel} is rate-limited, pre-switching to ${fallback}`,
|
|
539
|
+
);
|
|
540
|
+
activeFinalInit =
|
|
541
|
+
swapModelInBody(finalInit, fallback) || finalInit;
|
|
542
|
+
currentModel = fallback;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Retry logic with model fallback and exponential backoff for rate limiting
|
|
547
|
+
let lastError: Error | undefined;
|
|
548
|
+
let fallbacksUsed = 0;
|
|
549
|
+
let attempt = 0;
|
|
550
|
+
|
|
551
|
+
while (attempt <= RATE_LIMIT_CONFIG.maxRetries) {
|
|
552
|
+
try {
|
|
553
|
+
const response = await fetch(input, activeFinalInit);
|
|
554
|
+
|
|
555
|
+
if (response.status === 429) {
|
|
556
|
+
// Parse Retry-After header for server-suggested cooldown
|
|
557
|
+
const retryAfterMs = parseRetryAfter(response);
|
|
558
|
+
const cooldownMs =
|
|
559
|
+
retryAfterMs ?? RATE_LIMIT_CONFIG.defaultCooldownMs;
|
|
560
|
+
|
|
561
|
+
// Mark this model as rate-limited
|
|
562
|
+
if (currentModel) {
|
|
563
|
+
markModelRateLimited(currentModel, cooldownMs);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Try fallback model (doesn't count against retry budget)
|
|
567
|
+
if (
|
|
568
|
+
currentModel &&
|
|
569
|
+
fallbacksUsed < RATE_LIMIT_CONFIG.maxFallbacks
|
|
570
|
+
) {
|
|
571
|
+
const fallback = getNextFallbackModel(currentModel);
|
|
572
|
+
if (fallback) {
|
|
573
|
+
log(
|
|
574
|
+
"warn",
|
|
575
|
+
`Rate limited on ${currentModel}, switching to ${fallback}`,
|
|
576
|
+
{
|
|
577
|
+
retry_after_ms: retryAfterMs,
|
|
578
|
+
cooldown_ms: cooldownMs,
|
|
579
|
+
fallbacks_used: fallbacksUsed + 1,
|
|
580
|
+
},
|
|
581
|
+
);
|
|
582
|
+
activeFinalInit =
|
|
583
|
+
swapModelInBody(activeFinalInit, fallback) ||
|
|
584
|
+
activeFinalInit;
|
|
585
|
+
currentModel = fallback;
|
|
586
|
+
fallbacksUsed++;
|
|
587
|
+
continue; // Retry immediately with new model, no delay
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// No fallback available — use exponential backoff on same model
|
|
592
|
+
if (attempt < RATE_LIMIT_CONFIG.maxRetries) {
|
|
593
|
+
const delay =
|
|
594
|
+
retryAfterMs != null
|
|
595
|
+
? Math.min(retryAfterMs, RATE_LIMIT_CONFIG.maxDelayMs)
|
|
596
|
+
: calculateRetryDelay(attempt);
|
|
597
|
+
log(
|
|
598
|
+
"warn",
|
|
599
|
+
`Rate limited (429), no fallback available, waiting ${delay}ms`,
|
|
600
|
+
{
|
|
601
|
+
delay_ms: delay,
|
|
602
|
+
attempt: attempt + 1,
|
|
603
|
+
max_retries: RATE_LIMIT_CONFIG.maxRetries,
|
|
604
|
+
fallbacks_exhausted: true,
|
|
605
|
+
},
|
|
606
|
+
);
|
|
607
|
+
await sleep(delay);
|
|
608
|
+
attempt++;
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// Exhausted retries and fallbacks
|
|
613
|
+
throw new Error(
|
|
614
|
+
`[Copilot] Rate limited. Tried ${fallbacksUsed} fallback model(s) and ${attempt} retries. Model: ${currentModel}`,
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Response transformation is handled by the custom SDK at
|
|
619
|
+
// .opencode/plugin/sdk/copilot/
|
|
620
|
+
return response;
|
|
621
|
+
} catch (error) {
|
|
622
|
+
lastError = error as Error;
|
|
623
|
+
|
|
624
|
+
// Network errors might be transient, retry
|
|
625
|
+
if (attempt < RATE_LIMIT_CONFIG.maxRetries) {
|
|
626
|
+
const delay = calculateRetryDelay(attempt);
|
|
627
|
+
log("warn", `Request failed, retrying`, {
|
|
628
|
+
delay_ms: delay,
|
|
629
|
+
attempt: attempt + 1,
|
|
630
|
+
max_retries: RATE_LIMIT_CONFIG.maxRetries,
|
|
631
|
+
error: lastError.message,
|
|
632
|
+
});
|
|
633
|
+
await sleep(delay);
|
|
634
|
+
attempt++;
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
throw error;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// Exhausted all retries
|
|
642
|
+
if (lastError) {
|
|
643
|
+
throw new Error(
|
|
644
|
+
`[Copilot] Max retries (${RATE_LIMIT_CONFIG.maxRetries}) exceeded. Last error: ${lastError.message}`,
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
throw new Error(
|
|
648
|
+
`[Copilot] Max retries (${RATE_LIMIT_CONFIG.maxRetries}) exceeded`,
|
|
649
|
+
);
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
},
|
|
653
|
+
methods: [
|
|
654
|
+
{
|
|
655
|
+
type: "oauth",
|
|
656
|
+
label: "Login with GitHub Copilot",
|
|
657
|
+
prompts: [
|
|
658
|
+
{
|
|
659
|
+
type: "select",
|
|
660
|
+
key: "deploymentType",
|
|
661
|
+
message: "Select GitHub deployment type",
|
|
662
|
+
options: [
|
|
663
|
+
{
|
|
664
|
+
label: "GitHub.com",
|
|
665
|
+
value: "github.com",
|
|
666
|
+
hint: "Public",
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
label: "GitHub Enterprise",
|
|
670
|
+
value: "enterprise",
|
|
671
|
+
hint: "Data residency or self-hosted",
|
|
672
|
+
},
|
|
673
|
+
],
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
type: "text",
|
|
677
|
+
key: "enterpriseUrl",
|
|
678
|
+
message: "Enter your GitHub Enterprise URL or domain",
|
|
679
|
+
placeholder: "company.ghe.com or https://company.ghe.com",
|
|
680
|
+
condition: (inputs: any) =>
|
|
681
|
+
inputs.deploymentType === "enterprise",
|
|
682
|
+
validate: (value: string) => {
|
|
683
|
+
if (!value) return "URL or domain is required";
|
|
684
|
+
try {
|
|
685
|
+
const url = value.includes("://")
|
|
686
|
+
? new URL(value)
|
|
687
|
+
: new URL(`https://${value}`);
|
|
688
|
+
if (!url.hostname)
|
|
689
|
+
return "Please enter a valid URL or domain";
|
|
690
|
+
return undefined;
|
|
691
|
+
} catch {
|
|
692
|
+
return "Please enter a valid URL (e.g., company.ghe.com or https://company.ghe.com)";
|
|
693
|
+
}
|
|
694
|
+
},
|
|
695
|
+
},
|
|
696
|
+
],
|
|
697
|
+
async authorize(inputs: any = {}) {
|
|
698
|
+
const deploymentType = inputs.deploymentType || "github.com";
|
|
699
|
+
|
|
700
|
+
let domain = "github.com";
|
|
701
|
+
let actualProvider = "github-copilot";
|
|
702
|
+
|
|
703
|
+
if (deploymentType === "enterprise") {
|
|
704
|
+
const enterpriseUrl = inputs.enterpriseUrl;
|
|
705
|
+
domain = normalizeDomain(enterpriseUrl);
|
|
706
|
+
actualProvider = "github-copilot-enterprise";
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
const urls = getUrls(domain);
|
|
710
|
+
|
|
711
|
+
const deviceResponse = await fetch(urls.DEVICE_CODE_URL, {
|
|
712
|
+
method: "POST",
|
|
713
|
+
headers: {
|
|
714
|
+
Accept: "application/json",
|
|
715
|
+
"Content-Type": "application/json",
|
|
716
|
+
"User-Agent": "GitHubCopilotChat/0.35.0",
|
|
717
|
+
},
|
|
718
|
+
body: JSON.stringify({
|
|
719
|
+
client_id: CLIENT_ID,
|
|
720
|
+
scope: "read:user",
|
|
721
|
+
}),
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
if (!deviceResponse.ok) {
|
|
725
|
+
throw new Error("Failed to initiate device authorization");
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
const deviceData = await deviceResponse.json();
|
|
729
|
+
|
|
730
|
+
return {
|
|
731
|
+
url: deviceData.verification_uri,
|
|
732
|
+
instructions: `Enter code: ${deviceData.user_code}`,
|
|
733
|
+
method: "auto",
|
|
734
|
+
callback: async () => {
|
|
735
|
+
while (true) {
|
|
736
|
+
const response = await fetch(urls.ACCESS_TOKEN_URL, {
|
|
737
|
+
method: "POST",
|
|
738
|
+
headers: {
|
|
739
|
+
Accept: "application/json",
|
|
740
|
+
"Content-Type": "application/json",
|
|
741
|
+
"User-Agent": "GitHubCopilotChat/0.35.0",
|
|
742
|
+
},
|
|
743
|
+
body: JSON.stringify({
|
|
744
|
+
client_id: CLIENT_ID,
|
|
745
|
+
device_code: deviceData.device_code,
|
|
746
|
+
grant_type:
|
|
747
|
+
"urn:ietf:params:oauth:grant-type:device_code",
|
|
748
|
+
}),
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
if (!response.ok) return { type: "failed" };
|
|
752
|
+
|
|
753
|
+
const data = await response.json();
|
|
754
|
+
|
|
755
|
+
if (data.access_token) {
|
|
756
|
+
const result: {
|
|
757
|
+
type: "success";
|
|
758
|
+
refresh: string;
|
|
759
|
+
access: string;
|
|
760
|
+
expires: number;
|
|
761
|
+
provider?: string;
|
|
762
|
+
enterpriseUrl?: string;
|
|
763
|
+
} = {
|
|
764
|
+
type: "success",
|
|
765
|
+
refresh: data.access_token,
|
|
766
|
+
access: data.access_token,
|
|
767
|
+
expires: 0,
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
if (actualProvider === "github-copilot-enterprise") {
|
|
771
|
+
result.provider = "github-copilot-enterprise";
|
|
772
|
+
result.enterpriseUrl = domain;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
return result;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
if (data.error === "authorization_pending") {
|
|
779
|
+
await sleep(
|
|
780
|
+
deviceData.interval * 1000 +
|
|
781
|
+
OAUTH_POLLING_SAFETY_MARGIN_MS,
|
|
782
|
+
);
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
if (data.error === "slow_down") {
|
|
787
|
+
// Based on the RFC spec, we must add 5 seconds to our current polling interval.
|
|
788
|
+
let newInterval = (deviceData.interval + 5) * 1000;
|
|
789
|
+
|
|
790
|
+
if (
|
|
791
|
+
data.interval &&
|
|
792
|
+
typeof data.interval === "number" &&
|
|
793
|
+
data.interval > 0
|
|
794
|
+
) {
|
|
795
|
+
newInterval = data.interval * 1000;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
await sleep(newInterval + OAUTH_POLLING_SAFETY_MARGIN_MS);
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
if (data.error) return { type: "failed" };
|
|
803
|
+
|
|
804
|
+
await sleep(
|
|
805
|
+
deviceData.interval * 1000 + OAUTH_POLLING_SAFETY_MARGIN_MS,
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
},
|
|
809
|
+
};
|
|
810
|
+
},
|
|
811
|
+
},
|
|
812
|
+
],
|
|
813
|
+
},
|
|
814
|
+
// Hook to add custom headers for Claude reasoning support
|
|
815
|
+
"chat.headers": async (input: any, output: any) => {
|
|
816
|
+
// Only apply to GitHub Copilot provider
|
|
817
|
+
if (!input.model?.providerID?.includes("github-copilot")) return;
|
|
818
|
+
|
|
819
|
+
// Add Anthropic beta header for interleaved thinking (extended reasoning)
|
|
820
|
+
// This is required for Claude models to return thinking blocks
|
|
821
|
+
if (input.model?.api?.npm === "@ai-sdk/anthropic") {
|
|
822
|
+
output.headers["anthropic-beta"] = "interleaved-thinking-2025-05-14";
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// Mark subagent sessions as agent-initiated (matching standard Copilot tools)
|
|
826
|
+
try {
|
|
827
|
+
const session = await sdk.session
|
|
828
|
+
.get({
|
|
829
|
+
path: {
|
|
830
|
+
id: input.sessionID,
|
|
831
|
+
},
|
|
832
|
+
throwOnError: true,
|
|
833
|
+
})
|
|
834
|
+
.catch(() => undefined);
|
|
835
|
+
if (session?.data?.parentID) {
|
|
836
|
+
output.headers["x-initiator"] = "agent";
|
|
837
|
+
}
|
|
838
|
+
} catch {
|
|
839
|
+
// Ignore errors from session lookup
|
|
840
|
+
}
|
|
841
|
+
},
|
|
842
|
+
};
|
|
827
843
|
};
|