mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-26 00:52:18 +03:00
Retargetado para release/v3.8.51 (release/v3.8.50 está congelada — freeze issue #11439). Validado em lote combinado (batch-0824h2, junto de #11435/#11436/#11437) contra o tip de release/v3.8.51: typecheck:core limpo, gates estáticos OK, 127/127 testes focados passando. Investigação sólida com repro real via container isolado, três causas independentes identificadas e corrigidas com testes de regressão dedicados para cada uma. Obrigado pela contribuição!
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
// @ts-nocheck
|
|
// Extracted from open-sse/services/tokenRefresh.ts — see ../shared.ts for
|
|
// provenance notes (ported idea from KooshaPari's PR #7338, redone on tip).
|
|
import { getGitHubCopilotRefreshHeaders } from "../../../config/providerHeaderProfiles.ts";
|
|
import { runWithProxyContext } from "../../../utils/proxyFetch.ts";
|
|
|
|
/**
|
|
* Refresh GitHub Copilot token using a GitHub access token.
|
|
*
|
|
* `baseUrl` defaults to github.com's Copilot API but can be overridden to a
|
|
* GitHub Enterprise host's `<gheUrl>/api/v3` so the same helper serves both
|
|
* the `github` and `ghe-copilot` providers (GHE has its own per-enterprise
|
|
* Copilot token endpoint; api.github.com never issues a token scoped to a
|
|
* GHE account).
|
|
*/
|
|
export async function refreshCopilotToken(
|
|
githubAccessToken,
|
|
log,
|
|
proxyConfig: unknown = null,
|
|
baseUrl: string = "https://api.github.com"
|
|
) {
|
|
try {
|
|
const tokenUrl = `${baseUrl.replace(/\/+$/, "")}/copilot_internal/v2/token`;
|
|
const response = await runWithProxyContext(proxyConfig, () =>
|
|
fetch(tokenUrl, {
|
|
headers: getGitHubCopilotRefreshHeaders(`token ${githubAccessToken}`),
|
|
})
|
|
);
|
|
|
|
if (!response.ok) {
|
|
log?.error?.("TOKEN_REFRESH", "Failed to refresh Copilot token", {
|
|
status: response.status,
|
|
});
|
|
return { status: response.status };
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
log?.info?.("TOKEN_REFRESH", "Successfully refreshed Copilot token", {
|
|
hasToken: !!data.token,
|
|
expiresAt: data.expires_at,
|
|
});
|
|
|
|
return {
|
|
token: data.token,
|
|
expiresAt: data.expires_at,
|
|
};
|
|
} catch (error) {
|
|
log?.error?.("TOKEN_REFRESH", "Error refreshing Copilot token", {
|
|
errorType: error?.name || "Error",
|
|
});
|
|
return { status: null };
|
|
}
|
|
}
|