Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.7 KB
Raw Blame
// @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 };
  }
}