fix(github): proactively refresh missing/expired Copilot token on models discovery

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
minhnhat166
2026-06-08 10:19:10 +07:00
committed by decolua
co-authored by Cursor
parent 24a4f0863f
commit c572c68717
+16 -10
View File
@@ -220,6 +220,9 @@ export async function updateProviderCredentials(connectionId, newCredentials) {
*/ */
export async function checkAndRefreshToken(provider, credentials) { export async function checkAndRefreshToken(provider, credentials) {
let creds = { ...credentials }; let creds = { ...credentials };
if (!creds.connectionId && creds.id) {
creds.connectionId = creds.id;
}
// ── 1. Regular access-token expiry ──────────────────────────────────────── // ── 1. Regular access-token expiry ────────────────────────────────────────
if (_shouldRefreshCredentials(provider, creds)) { if (_shouldRefreshCredentials(provider, creds)) {
@@ -261,23 +264,26 @@ export async function checkAndRefreshToken(provider, credentials) {
} }
// ── 2. GitHub Copilot token expiry ──────────────────────────────────────── // ── 2. GitHub Copilot token expiry ────────────────────────────────────────
if (provider === "github" && creds.providerSpecificData?.copilotTokenExpiresAt) { if (provider === "github") {
const copilotExpiresAt = creds.providerSpecificData.copilotTokenExpiresAt * 1000; const copilotToken = creds.providerSpecificData?.copilotToken;
const copilotExpiresAt = creds.providerSpecificData?.copilotTokenExpiresAt
? creds.providerSpecificData.copilotTokenExpiresAt * 1000
: 0;
const now = Date.now(); const now = Date.now();
const remaining = copilotExpiresAt - now; const remaining = copilotExpiresAt - now;
if (remaining < TOKEN_EXPIRY_BUFFER_MS) { if (!copilotToken || remaining < TOKEN_EXPIRY_BUFFER_MS) {
log.info("TOKEN_REFRESH", "Copilot token expiring soon, refreshing proactively", { log.info("TOKEN_REFRESH", "Copilot token expiring soon or missing, refreshing proactively", {
provider, provider,
expiresIn: Math.round(remaining / 1000), expiresIn: copilotToken ? Math.round(remaining / 1000) : "missing",
}); });
const copilotToken = await refreshCopilotToken(creds.accessToken); const copilotTokenResult = await refreshCopilotToken(creds.accessToken);
if (copilotToken) { if (copilotTokenResult) {
const updatedSpecific = { const updatedSpecific = {
...creds.providerSpecificData, ...creds.providerSpecificData,
copilotToken: copilotToken.token, copilotToken: copilotTokenResult.token,
copilotTokenExpiresAt: copilotToken.expiresAt, copilotTokenExpiresAt: copilotTokenResult.expiresAt,
}; };
await updateProviderCredentials(creds.connectionId, { await updateProviderCredentials(creds.connectionId, {
@@ -285,7 +291,7 @@ export async function checkAndRefreshToken(provider, credentials) {
}); });
creds.providerSpecificData = updatedSpecific; creds.providerSpecificData = updatedSpecific;
creds.copilotToken = copilotToken.token; creds.copilotToken = copilotTokenResult.token;
} }
} }
} }