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