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