mirror of
https://gitea.com/PublicAffairs/openai-github-copilot.git
synced 2025-07-22 12:00:37 +02:00
Use dedicated endpoint
individual/business/enterprise
This commit is contained in:
@@ -25,25 +25,32 @@ export default {
|
||||
});
|
||||
}
|
||||
const tokenCache = env.KV;
|
||||
let token;
|
||||
let token, endpoint;
|
||||
try {
|
||||
token = await tokenCache.get(authKey);
|
||||
if (token) {
|
||||
endpoint = await tokenCache.get(authKey + "-ep");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
if (!token) {
|
||||
let expires_at, errResponse;
|
||||
({ token, expires_at, errResponse } = await getToken(authKey));
|
||||
let expires_at, endpoints, errResponse;
|
||||
({ token, expires_at, endpoints, errResponse } = await getToken(authKey));
|
||||
if (errResponse) { return errResponse; }
|
||||
endpoint = endpoints?.api;
|
||||
if (expires_at) {
|
||||
try {
|
||||
await tokenCache.put(authKey, token, { expiration: expires_at });
|
||||
if (endpoint) {
|
||||
await tokenCache.put(authKey + "-ep", endpoint, { expiration: expires_at });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return makeRequest(request, url.pathname, token);
|
||||
return makeRequest(request, url.pathname, token, endpoint || "https://api.githubcopilot.com");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -99,7 +106,7 @@ const getToken = async (authKey) => {
|
||||
return data;
|
||||
};
|
||||
|
||||
const makeHeaders = async (token) => {
|
||||
const makeHeaders = async (token, endpoint) => {
|
||||
const createSha256Hash = async (input) => {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(input);
|
||||
@@ -108,7 +115,7 @@ const makeHeaders = async (token) => {
|
||||
};
|
||||
return {
|
||||
"Authorization": `Bearer ${token}`,
|
||||
"Host": "api.githubcopilot.com",
|
||||
"Host": endpoint.replace(/^https?:\/\//, ""),
|
||||
"X-Request-Id": crypto.randomUUID(),
|
||||
"X-Github-Api-Version": apiVersion,
|
||||
"Vscode-Sessionid": crypto.randomUUID() + Date.now().toString(),
|
||||
@@ -125,13 +132,13 @@ const makeHeaders = async (token) => {
|
||||
};
|
||||
};
|
||||
|
||||
const makeRequest = async (request, path, token) => {
|
||||
const makeRequest = async (request, path, token, endpoint) => {
|
||||
if (path.startsWith("/v1/")) {
|
||||
path = path.substring(3);
|
||||
}
|
||||
const response = await fetch(`https://api.githubcopilot.com${path}`, {
|
||||
const response = await fetch(`${endpoint}${path}`, {
|
||||
method: request.method,
|
||||
headers: await makeHeaders(token),
|
||||
headers: await makeHeaders(token, endpoint),
|
||||
body: request.body,
|
||||
});
|
||||
const headers = new Headers(response.headers);
|
||||
|
Reference in New Issue
Block a user