mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
Split the 13 export async function refresh<Provider>Token() implementations
out of open-sse/services/tokenRefresh.ts (2249 lines, frozen) into their own
co-located leaf modules under open-sse/services/tokenRefresh/providers/, with
shared OAuth-error classification (extractOAuthErrorCode/readRefreshErrorBody)
and the form-body builder moved to tokenRefresh/shared.ts. tokenRefresh.ts now
keeps only the cross-provider orchestrator (refreshAccessToken dispatcher,
getAccessToken dedup/mutex/CAS-guard layers, refreshWithRetry circuit
breaker) and re-exports every previously-public symbol so no importer needed
to change (open-sse/index.ts, executors, src/sse/services/tokenRefresh.ts,
tests). File shrinks 2249 -> 989 lines; every new leaf is well under the
800-line cap.
Pure move, zero behavior change — verified via typecheck:core, check:cycles,
eslint (0 new any), file-size/complexity/cognitive-complexity ratchets (all
under baseline), and the full token-refresh test surface (token-refresh-*,
oauth-providers-*, executor-{kiro,github,gitlab,codex,antigravity,default-base},
token-health-check*, kiro-external-idp, codebuddy-cn-provider,
windsurf-devin-executors, grok-cli-*, agy-*, xai/zed-oauth-provider,
deepseek-web-autorefresh, ghe-copilot). Updated the 8 structural (text-based)
assertions in oauth-providers-error-handling.test.ts that read function
bodies straight from tokenRefresh.ts to read from the new per-provider files
instead — same assertions, new location.
The provider-module split was originally proposed by KooshaPari in PR #7338
against a base too old to merge cleanly; redone here from scratch against the
current release/v3.8.49 tip, credit preserved via co-authorship.
Co-authored-by: KooshaPari <KooshaPari@users.noreply.github.com>
72 lines
2.3 KiB
TypeScript
72 lines
2.3 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 { runWithProxyContext } from "../../../utils/proxyFetch.ts";
|
|
import type { RefreshLogger } from "../shared.ts";
|
|
|
|
/**
|
|
* CodeBuddy CN (Tencent) token refresh — POST /v2/plugin/auth/token/refresh with
|
|
* the refresh token carried in the X-Refresh-Token header (not a form body),
|
|
* matching the official CodeBuddy CLI. Response: { code: 0, data: <token> }.
|
|
*/
|
|
export async function refreshCodebuddyCnToken(
|
|
refreshToken: string,
|
|
log: RefreshLogger,
|
|
proxyConfig: unknown = null
|
|
) {
|
|
if (!refreshToken) return null;
|
|
const { CODEBUDDY_CN_CONFIG } = await import("@/lib/oauth/constants/oauth");
|
|
const oauth = CODEBUDDY_CN_CONFIG;
|
|
try {
|
|
const response = await runWithProxyContext(proxyConfig, () =>
|
|
fetch(oauth.refreshUrl, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Accept: "application/json",
|
|
"User-Agent": oauth.userAgent,
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
"X-Domain": "copilot.tencent.com",
|
|
"X-Refresh-Token": refreshToken,
|
|
"X-Auth-Refresh-Source": "plugin",
|
|
"X-Product": "SaaS",
|
|
},
|
|
body: "{}",
|
|
})
|
|
);
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
log?.error?.("TOKEN_REFRESH", "Failed to refresh CodeBuddy CN token", {
|
|
status: response.status,
|
|
error: errorText,
|
|
});
|
|
return null;
|
|
}
|
|
|
|
const data = await response.json();
|
|
if (data?.code !== 0 || !data?.data?.accessToken) {
|
|
log?.error?.("TOKEN_REFRESH", "CodeBuddy CN token refresh returned no token", {
|
|
code: data?.code,
|
|
msg: data?.msg,
|
|
});
|
|
return null;
|
|
}
|
|
|
|
log?.info?.("TOKEN_REFRESH", "Successfully refreshed CodeBuddy CN token", {
|
|
hasNewAccessToken: !!data.data.accessToken,
|
|
hasNewRefreshToken: !!data.data.refreshToken,
|
|
expiresIn: data.data.expiresIn,
|
|
});
|
|
|
|
return {
|
|
accessToken: data.data.accessToken,
|
|
refreshToken: data.data.refreshToken || refreshToken,
|
|
expiresIn: data.data.expiresIn,
|
|
};
|
|
} catch (error) {
|
|
log?.error?.("TOKEN_REFRESH", `Network error refreshing CodeBuddy CN token: ${error?.message}`);
|
|
return null;
|
|
}
|
|
}
|