fix: Gemini/Antigravity quota sources and update labels (#379)
* refactor: unify Google quota window logic and labels * chore: add OAuth client id/secret and notes in quota providers
This commit is contained in:
@@ -47,6 +47,28 @@ const normalizeAuthEntry = (entry) => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const asObject = (value) => (value && typeof value === 'object' ? value : null);
|
||||
|
||||
const asNonEmptyString = (value) => {
|
||||
if (typeof value !== 'string') return null;
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
};
|
||||
|
||||
const parseGoogleRefreshToken = (rawRefreshToken) => {
|
||||
const refreshToken = asNonEmptyString(rawRefreshToken);
|
||||
if (!refreshToken) {
|
||||
return { refreshToken: null, projectId: null, managedProjectId: null };
|
||||
}
|
||||
|
||||
const [rawToken = '', rawProject = '', rawManagedProject = ''] = refreshToken.split('|');
|
||||
return {
|
||||
refreshToken: asNonEmptyString(rawToken),
|
||||
projectId: asNonEmptyString(rawProject),
|
||||
managedProjectId: asNonEmptyString(rawManagedProject)
|
||||
};
|
||||
};
|
||||
|
||||
const toNumber = (value) => {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return value;
|
||||
@@ -159,8 +181,7 @@ export const listConfiguredQuotaProviders = () => {
|
||||
configured.add('codex');
|
||||
}
|
||||
|
||||
const googleAuth = normalizeAuthEntry(getAuthEntry(auth, ['google', 'antigravity']));
|
||||
if (googleAuth?.access || googleAuth?.token || googleAuth?.refresh) {
|
||||
if (resolveGeminiCliAuth(auth) || resolveAntigravityAuth()) {
|
||||
configured.add('google');
|
||||
}
|
||||
|
||||
@@ -185,14 +206,6 @@ export const listConfiguredQuotaProviders = () => {
|
||||
configured.add('github-copilot-addon');
|
||||
}
|
||||
|
||||
for (const filePath of ANTIGRAVITY_ACCOUNTS_PATHS) {
|
||||
const data = readJsonFile(filePath);
|
||||
if (Array.isArray(data?.accounts) && data.accounts.length > 0) {
|
||||
configured.add('google');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(configured);
|
||||
};
|
||||
|
||||
@@ -268,16 +281,29 @@ export const fetchOpenaiQuota = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const GOOGLE_CLIENT_ID =
|
||||
// OAuth Secret value used to init client
|
||||
// Note: It's ok to save this in git because this is an installed application
|
||||
// as described here: https://developers.google.com/identity/protocols/oauth2#installed
|
||||
// "The process results in a client ID and, in some cases, a client secret,
|
||||
// which you embed in the source code of your application. (In this context,
|
||||
// the client secret is obviously not treated as a secret.)"
|
||||
// ref: https://github.com/opgginc/opencode-bar
|
||||
|
||||
const ANTIGRAVITY_GOOGLE_CLIENT_ID =
|
||||
'1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com';
|
||||
const GOOGLE_CLIENT_SECRET = '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';
|
||||
const ANTIGRAVITY_GOOGLE_CLIENT_SECRET = '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';
|
||||
const GEMINI_GOOGLE_CLIENT_ID =
|
||||
'681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com';
|
||||
const GEMINI_GOOGLE_CLIENT_SECRET = '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';
|
||||
const DEFAULT_PROJECT_ID = 'rising-fact-p41fc';
|
||||
const GOOGLE_WINDOW_SECONDS = 5 * 60 * 60;
|
||||
const GOOGLE_FIVE_HOUR_WINDOW_SECONDS = 5 * 60 * 60;
|
||||
const GOOGLE_DAILY_WINDOW_SECONDS = 24 * 60 * 60;
|
||||
const GOOGLE_PRIMARY_ENDPOINT = 'https://cloudcode-pa.googleapis.com';
|
||||
|
||||
const GOOGLE_ENDPOINTS = [
|
||||
'https://daily-cloudcode-pa.sandbox.googleapis.com',
|
||||
'https://autopush-cloudcode-pa.sandbox.googleapis.com',
|
||||
'https://cloudcode-pa.googleapis.com'
|
||||
GOOGLE_PRIMARY_ENDPOINT
|
||||
];
|
||||
|
||||
const GOOGLE_HEADERS = {
|
||||
@@ -287,26 +313,52 @@ const GOOGLE_HEADERS = {
|
||||
'{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}'
|
||||
};
|
||||
|
||||
const resolveGoogleAuth = () => {
|
||||
const auth = readAuthFile();
|
||||
const entry = normalizeAuthEntry(getAuthEntry(auth, ['google', 'antigravity']));
|
||||
if (entry) {
|
||||
const accessToken = entry.access ?? entry.token;
|
||||
let refreshToken = entry.refresh;
|
||||
let projectId = undefined;
|
||||
if (refreshToken && refreshToken.includes('|')) {
|
||||
const parts = refreshToken.split('|');
|
||||
refreshToken = parts[0];
|
||||
projectId = parts[1];
|
||||
}
|
||||
return {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
expires: entry.expires,
|
||||
projectId
|
||||
};
|
||||
const resolveGoogleWindow = (sourceId, resetAt) => {
|
||||
if (sourceId === 'gemini') {
|
||||
return { label: 'daily', seconds: GOOGLE_DAILY_WINDOW_SECONDS };
|
||||
}
|
||||
|
||||
if (sourceId === 'antigravity') {
|
||||
const remainingSeconds = typeof resetAt === 'number'
|
||||
? Math.max(0, Math.round((resetAt - Date.now()) / 1000))
|
||||
: null;
|
||||
|
||||
if (remainingSeconds !== null && remainingSeconds > 10 * 60 * 60) {
|
||||
return { label: 'daily', seconds: GOOGLE_DAILY_WINDOW_SECONDS };
|
||||
}
|
||||
|
||||
return { label: '5h', seconds: GOOGLE_FIVE_HOUR_WINDOW_SECONDS };
|
||||
}
|
||||
|
||||
return { label: 'daily', seconds: GOOGLE_DAILY_WINDOW_SECONDS };
|
||||
};
|
||||
|
||||
const resolveGeminiCliAuth = (auth) => {
|
||||
const entry = normalizeAuthEntry(getAuthEntry(auth, ['google', 'google.oauth']));
|
||||
const entryObject = asObject(entry);
|
||||
if (!entryObject) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const oauthObject = asObject(entryObject.oauth) ?? entryObject;
|
||||
const accessToken = asNonEmptyString(oauthObject.access) ?? asNonEmptyString(oauthObject.token);
|
||||
const refreshParts = parseGoogleRefreshToken(oauthObject.refresh);
|
||||
|
||||
if (!accessToken && !refreshParts.refreshToken) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
sourceId: 'gemini',
|
||||
sourceLabel: 'Gemini',
|
||||
accessToken,
|
||||
refreshToken: refreshParts.refreshToken,
|
||||
projectId: refreshParts.projectId ?? refreshParts.managedProjectId,
|
||||
expires: toTimestamp(oauthObject.expires)
|
||||
};
|
||||
};
|
||||
|
||||
const resolveAntigravityAuth = () => {
|
||||
for (const filePath of ANTIGRAVITY_ACCOUNTS_PATHS) {
|
||||
const data = readJsonFile(filePath);
|
||||
const accounts = data?.accounts;
|
||||
@@ -314,9 +366,15 @@ const resolveGoogleAuth = () => {
|
||||
const index = typeof data.activeIndex === 'number' ? data.activeIndex : 0;
|
||||
const account = accounts[index] ?? accounts[0];
|
||||
if (account?.refreshToken) {
|
||||
const refreshParts = parseGoogleRefreshToken(account.refreshToken);
|
||||
return {
|
||||
refreshToken: account.refreshToken,
|
||||
projectId: account.projectId ?? account.managedProjectId,
|
||||
sourceId: 'antigravity',
|
||||
sourceLabel: 'Antigravity',
|
||||
refreshToken: refreshParts.refreshToken,
|
||||
projectId: asNonEmptyString(account.projectId)
|
||||
?? asNonEmptyString(account.managedProjectId)
|
||||
?? refreshParts.projectId
|
||||
?? refreshParts.managedProjectId,
|
||||
email: account.email
|
||||
};
|
||||
}
|
||||
@@ -326,13 +384,44 @@ const resolveGoogleAuth = () => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const refreshGoogleAccessToken = async (refreshToken) => {
|
||||
const resolveGoogleAuthSources = () => {
|
||||
const auth = readAuthFile();
|
||||
const sources = [];
|
||||
|
||||
const geminiAuth = resolveGeminiCliAuth(auth);
|
||||
if (geminiAuth) {
|
||||
sources.push(geminiAuth);
|
||||
}
|
||||
|
||||
const antigravityAuth = resolveAntigravityAuth();
|
||||
if (antigravityAuth) {
|
||||
sources.push(antigravityAuth);
|
||||
}
|
||||
|
||||
return sources;
|
||||
};
|
||||
|
||||
const resolveGoogleOAuthClient = (sourceId) => {
|
||||
if (sourceId === 'gemini') {
|
||||
return {
|
||||
clientId: GEMINI_GOOGLE_CLIENT_ID,
|
||||
clientSecret: GEMINI_GOOGLE_CLIENT_SECRET
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
clientId: ANTIGRAVITY_GOOGLE_CLIENT_ID,
|
||||
clientSecret: ANTIGRAVITY_GOOGLE_CLIENT_SECRET
|
||||
};
|
||||
};
|
||||
|
||||
const refreshGoogleAccessToken = async (refreshToken, clientId, clientSecret) => {
|
||||
const response = await fetch('https://oauth2.googleapis.com/token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams({
|
||||
client_id: GOOGLE_CLIENT_ID,
|
||||
client_secret: GOOGLE_CLIENT_SECRET,
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
refresh_token: refreshToken,
|
||||
grant_type: 'refresh_token'
|
||||
})
|
||||
@@ -346,6 +435,30 @@ const refreshGoogleAccessToken = async (refreshToken) => {
|
||||
return typeof data?.access_token === 'string' ? data.access_token : null;
|
||||
};
|
||||
|
||||
const fetchGoogleQuotaBuckets = async (accessToken, projectId) => {
|
||||
const body = projectId ? { project: projectId } : {};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${GOOGLE_PRIMARY_ENDPOINT}/v1internal:retrieveUserQuota`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(15000)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const fetchGoogleModels = async (accessToken, projectId) => {
|
||||
const body = projectId ? { project: projectId } : {};
|
||||
|
||||
@@ -374,8 +487,8 @@ const fetchGoogleModels = async (accessToken, projectId) => {
|
||||
};
|
||||
|
||||
export const fetchGoogleQuota = async () => {
|
||||
const auth = resolveGoogleAuth();
|
||||
if (!auth) {
|
||||
const authSources = resolveGoogleAuthSources();
|
||||
if (!authSources.length) {
|
||||
return buildResult({
|
||||
providerId: 'google',
|
||||
providerName: 'Google',
|
||||
@@ -385,62 +498,107 @@ export const fetchGoogleQuota = async () => {
|
||||
});
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
let accessToken = auth.accessToken;
|
||||
if (!accessToken || (typeof auth.expires === 'number' && auth.expires <= now)) {
|
||||
if (!auth.refreshToken) {
|
||||
return buildResult({
|
||||
providerId: 'google',
|
||||
providerName: 'Google',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: 'Missing refresh token'
|
||||
});
|
||||
}
|
||||
accessToken = await refreshGoogleAccessToken(auth.refreshToken);
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
return buildResult({
|
||||
providerId: 'google',
|
||||
providerName: 'Google',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: 'Failed to refresh OAuth token'
|
||||
});
|
||||
}
|
||||
|
||||
const projectId = auth.projectId ?? DEFAULT_PROJECT_ID;
|
||||
const payload = await fetchGoogleModels(accessToken, projectId);
|
||||
if (!payload) {
|
||||
return buildResult({
|
||||
providerId: 'google',
|
||||
providerName: 'Google',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: 'Failed to fetch models'
|
||||
});
|
||||
}
|
||||
|
||||
const models = {};
|
||||
for (const [modelName, modelData] of Object.entries(payload.models ?? {})) {
|
||||
const remainingFraction = modelData?.quotaInfo?.remainingFraction;
|
||||
const remainingPercent = typeof remainingFraction === 'number'
|
||||
? Math.round(remainingFraction * 100)
|
||||
: null;
|
||||
const usedPercent = remainingPercent !== null ? Math.max(0, 100 - remainingPercent) : null;
|
||||
const resetAt = modelData?.quotaInfo?.resetTime
|
||||
? new Date(modelData.quotaInfo.resetTime).getTime()
|
||||
: null;
|
||||
models[modelName] = {
|
||||
windows: {
|
||||
'5h': toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds: GOOGLE_WINDOW_SECONDS,
|
||||
resetAt
|
||||
})
|
||||
const sourceErrors = [];
|
||||
|
||||
for (const source of authSources) {
|
||||
const now = Date.now();
|
||||
let accessToken = source.accessToken;
|
||||
|
||||
if (!accessToken || (typeof source.expires === 'number' && source.expires <= now)) {
|
||||
if (!source.refreshToken) {
|
||||
sourceErrors.push(`${source.sourceLabel}: Missing refresh token`);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
const { clientId, clientSecret } = resolveGoogleOAuthClient(source.sourceId);
|
||||
accessToken = await refreshGoogleAccessToken(source.refreshToken, clientId, clientSecret);
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
sourceErrors.push(`${source.sourceLabel}: Failed to refresh OAuth token`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const projectId = source.projectId ?? DEFAULT_PROJECT_ID;
|
||||
let mergedAnyModel = false;
|
||||
|
||||
if (source.sourceId === 'gemini') {
|
||||
const quotaPayload = await fetchGoogleQuotaBuckets(accessToken, projectId);
|
||||
const buckets = Array.isArray(quotaPayload?.buckets) ? quotaPayload.buckets : [];
|
||||
|
||||
for (const bucket of buckets) {
|
||||
const modelId = asNonEmptyString(bucket?.modelId);
|
||||
if (!modelId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const scopedName = modelId.startsWith(`${source.sourceId}/`)
|
||||
? modelId
|
||||
: `${source.sourceId}/${modelId}`;
|
||||
|
||||
const remainingFraction = toNumber(bucket?.remainingFraction);
|
||||
const remainingPercent = remainingFraction !== null
|
||||
? Math.round(remainingFraction * 100)
|
||||
: null;
|
||||
const usedPercent = remainingPercent !== null ? Math.max(0, 100 - remainingPercent) : null;
|
||||
const resetAt = toTimestamp(bucket?.resetTime);
|
||||
const window = resolveGoogleWindow(source.sourceId, resetAt);
|
||||
|
||||
models[scopedName] = {
|
||||
windows: {
|
||||
[window.label]: toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds: window.seconds,
|
||||
resetAt
|
||||
})
|
||||
}
|
||||
};
|
||||
mergedAnyModel = true;
|
||||
}
|
||||
}
|
||||
|
||||
const payload = await fetchGoogleModels(accessToken, projectId);
|
||||
if (payload) {
|
||||
for (const [modelName, modelData] of Object.entries(payload.models ?? {})) {
|
||||
const scopedName = modelName.startsWith(`${source.sourceId}/`)
|
||||
? modelName
|
||||
: `${source.sourceId}/${modelName}`;
|
||||
|
||||
const remainingFraction = modelData?.quotaInfo?.remainingFraction;
|
||||
const remainingPercent = typeof remainingFraction === 'number'
|
||||
? Math.round(remainingFraction * 100)
|
||||
: null;
|
||||
const usedPercent = remainingPercent !== null ? Math.max(0, 100 - remainingPercent) : null;
|
||||
const resetAt = modelData?.quotaInfo?.resetTime
|
||||
? new Date(modelData.quotaInfo.resetTime).getTime()
|
||||
: null;
|
||||
const window = resolveGoogleWindow(source.sourceId, resetAt);
|
||||
models[scopedName] = {
|
||||
windows: {
|
||||
[window.label]: toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds: window.seconds,
|
||||
resetAt
|
||||
})
|
||||
}
|
||||
};
|
||||
mergedAnyModel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mergedAnyModel) {
|
||||
sourceErrors.push(`${source.sourceLabel}: Failed to fetch models`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Object.keys(models).length) {
|
||||
return buildResult({
|
||||
providerId: 'google',
|
||||
providerName: 'Google',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: sourceErrors[0] ?? 'Failed to fetch models'
|
||||
});
|
||||
}
|
||||
|
||||
return buildResult({
|
||||
|
||||
Reference in New Issue
Block a user