-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathindex.mjs
More file actions
305 lines (275 loc) · 9.96 KB
/
index.mjs
File metadata and controls
305 lines (275 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
/**
* @type {import('@opencode-ai/plugin').Plugin}
*/
export async function CopilotAuthPlugin({ client }) {
const CLIENT_ID = "Iv1.b507a08c87ecfe98";
const HEADERS = {
"User-Agent": "GitHubCopilotChat/0.35.0",
"Editor-Version": "vscode/1.107.0",
"Editor-Plugin-Version": "copilot-chat/0.35.0",
"Copilot-Integration-Id": "vscode-chat",
};
const RESPONSES_API_ALTERNATE_INPUT_TYPES = [
"file_search_call",
"computer_call",
"computer_call_output",
"web_search_call",
"function_call",
"function_call_output",
"image_generation_call",
"code_interpreter_call",
"local_shell_call",
"local_shell_call_output",
"mcp_list_tools",
"mcp_approval_request",
"mcp_approval_response",
"mcp_call",
"reasoning",
];
function normalizeDomain(url) {
return url.replace(/^https?:\/\//, "").replace(/\/$/, "");
}
function getUrls(domain) {
return {
DEVICE_CODE_URL: `https://${domain}/login/device/code`,
ACCESS_TOKEN_URL: `https://${domain}/login/oauth/access_token`,
COPILOT_API_KEY_URL: `https://api.${domain}/copilot_internal/v2/token`,
};
}
return {
auth: {
provider: "github-copilot",
loader: async (getAuth, provider) => {
let info = await getAuth();
if (!info || info.type !== "oauth") return {};
if (provider && provider.models) {
for (const model of Object.values(provider.models)) {
model.cost = {
input: 0,
output: 0,
cache: {
read: 0,
write: 0,
},
};
}
}
// Set baseURL based on deployment type
const enterpriseUrl = info.enterpriseUrl;
const baseURL = enterpriseUrl
? `https://copilot-api.${normalizeDomain(enterpriseUrl)}`
: "https://api.githubcopilot.com";
return {
baseURL,
apiKey: "",
async fetch(input, init) {
const info = await getAuth();
if (info.type !== "oauth") return {};
if (!info.access || info.expires < Date.now()) {
const domain = info.enterpriseUrl
? normalizeDomain(info.enterpriseUrl)
: "github.com";
const urls = getUrls(domain);
const response = await fetch(urls.COPILOT_API_KEY_URL, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${info.refresh}`,
...HEADERS,
},
});
if (!response.ok) {
throw new Error(`Token refresh failed: ${response.status}`);
}
const tokenData = await response.json();
const saveProviderID = info.enterpriseUrl
? "github-copilot-enterprise"
: "github-copilot";
await client.auth.set({
path: {
id: saveProviderID,
},
body: {
type: "oauth",
refresh: info.refresh,
access: tokenData.token,
expires: tokenData.expires_at * 1000 - 5 * 60 * 1000,
...(info.enterpriseUrl && {
enterpriseUrl: info.enterpriseUrl,
}),
},
});
info.access = tokenData.token;
}
let isAgentCall = false;
let isVisionRequest = false;
try {
const body =
typeof init.body === "string"
? JSON.parse(init.body)
: init.body;
if (body?.messages) {
if (body.messages.length > 0) {
const lastMessage = body.messages[body.messages.length - 1];
isAgentCall = lastMessage.role && ["tool", "assistant"].includes(lastMessage.role);
}
isVisionRequest = body.messages.some(
(msg) =>
Array.isArray(msg.content) &&
msg.content.some((part) => part.type === "image_url"),
);
}
if (body?.input) {
const lastInput = body.input[body.input.length - 1];
const isAssistant = lastInput?.role === "assistant";
const hasAgentType = lastInput?.type
? RESPONSES_API_ALTERNATE_INPUT_TYPES.includes(lastInput.type)
: false;
isAgentCall = isAssistant || hasAgentType;
isVisionRequest =
Array.isArray(lastInput?.content) &&
lastInput.content.some((part) => part.type === "input_image");
}
} catch {}
const headers = {
...init.headers,
...HEADERS,
Authorization: `Bearer ${info.access}`,
"Openai-Intent": "conversation-edits",
"X-Initiator": isAgentCall ? "agent" : "user",
};
if (isVisionRequest) {
headers["Copilot-Vision-Request"] = "true";
}
delete headers["x-api-key"];
delete headers["authorization"];
return fetch(input, {
...init,
headers,
});
},
};
},
methods: [
{
type: "oauth",
label: "Login with GitHub Copilot",
prompts: [
{
type: "select",
key: "deploymentType",
message: "Select GitHub deployment type",
options: [
{
label: "GitHub.com",
value: "github.com",
hint: "Public",
},
{
label: "GitHub Enterprise",
value: "enterprise",
hint: "Data residency or self-hosted",
},
],
},
{
type: "text",
key: "enterpriseUrl",
message: "Enter your GitHub Enterprise URL or domain",
placeholder: "company.ghe.com or https://company.ghe.com",
condition: (inputs) => inputs.deploymentType === "enterprise",
validate: (value) => {
if (!value) return "URL or domain is required";
try {
const url = value.includes("://")
? new URL(value)
: new URL(`https://${value}`);
if (!url.hostname)
return "Please enter a valid URL or domain";
return undefined;
} catch {
return "Please enter a valid URL (e.g., company.ghe.com or https://company.ghe.com)";
}
},
},
],
async authorize(inputs = {}) {
const deploymentType = inputs.deploymentType || "github.com";
let domain = "github.com";
let actualProvider = "github-copilot";
if (deploymentType === "enterprise") {
const enterpriseUrl = inputs.enterpriseUrl;
domain = normalizeDomain(enterpriseUrl);
actualProvider = "github-copilot-enterprise";
}
const urls = getUrls(domain);
const deviceResponse = await fetch(urls.DEVICE_CODE_URL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "GitHubCopilotChat/0.35.0",
},
body: JSON.stringify({
client_id: CLIENT_ID,
scope: "read:user",
}),
});
if (!deviceResponse.ok) {
throw new Error("Failed to initiate device authorization");
}
const deviceData = await deviceResponse.json();
return {
url: deviceData.verification_uri,
instructions: `Enter code: ${deviceData.user_code}`,
method: "auto",
callback: async () => {
while (true) {
const response = await fetch(urls.ACCESS_TOKEN_URL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "GitHubCopilotChat/0.35.0",
},
body: JSON.stringify({
client_id: CLIENT_ID,
device_code: deviceData.device_code,
grant_type:
"urn:ietf:params:oauth:grant-type:device_code",
}),
});
if (!response.ok) return { type: "failed" };
const data = await response.json();
if (data.access_token) {
const result = {
type: "success",
refresh: data.access_token,
access: "",
expires: 0,
};
if (actualProvider === "github-copilot-enterprise") {
result.provider = "github-copilot-enterprise";
result.enterpriseUrl = domain;
}
return result;
}
if (data.error === "authorization_pending") {
await new Promise((resolve) =>
setTimeout(resolve, deviceData.interval * 1000),
);
continue;
}
if (data.error) return { type: "failed" };
await new Promise((resolve) =>
setTimeout(resolve, deviceData.interval * 1000),
);
continue;
}
},
};
},
},
],
},
};
}