forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquota.ts
More file actions
273 lines (236 loc) · 7.7 KB
/
Copy pathquota.ts
File metadata and controls
273 lines (236 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {
ANTIGRAVITY_ENDPOINT_PROD,
ANTIGRAVITY_HEADERS,
ANTIGRAVITY_PROVIDER_ID,
} from "../constants";
import { accessTokenExpired, formatRefreshParts, parseRefreshParts } from "./auth";
import { ensureProjectContext } from "./project";
import { refreshAccessToken } from "./token";
import { getModelFamily } from "./transform/model-resolver";
import type { PluginClient, OAuthAuthDetails } from "./types";
import type { AccountMetadataV3 } from "./storage";
const FETCH_TIMEOUT_MS = 10000;
export type QuotaGroup = "claude" | "gemini-pro" | "gemini-flash";
export interface QuotaGroupSummary {
remainingFraction?: number;
resetTime?: string;
modelCount: number;
}
export interface QuotaSummary {
groups: Partial<Record<QuotaGroup, QuotaGroupSummary>>;
modelCount: number;
error?: string;
}
export type AccountQuotaStatus = "ok" | "disabled" | "error";
export interface AccountQuotaResult {
index: number;
email?: string;
status: AccountQuotaStatus;
error?: string;
disabled?: boolean;
quota?: QuotaSummary;
updatedAccount?: AccountMetadataV3;
}
interface FetchAvailableModelsResponse {
models?: Record<string, FetchAvailableModelEntry>;
}
interface FetchAvailableModelEntry {
quotaInfo?: {
remainingFraction?: number;
resetTime?: string;
};
displayName?: string;
modelName?: string;
}
function buildAuthFromAccount(account: AccountMetadataV3): OAuthAuthDetails {
return {
type: "oauth",
refresh: formatRefreshParts({
refreshToken: account.refreshToken,
projectId: account.projectId,
managedProjectId: account.managedProjectId,
}),
access: undefined,
expires: undefined,
};
}
function normalizeRemainingFraction(value: unknown): number | undefined {
if (typeof value !== "number" || !Number.isFinite(value)) {
return undefined;
}
if (value < 0) return 0;
if (value > 1) return 1;
return value;
}
function parseResetTime(resetTime?: string): number | null {
if (!resetTime) return null;
const timestamp = Date.parse(resetTime);
if (!Number.isFinite(timestamp)) {
return null;
}
return timestamp;
}
function classifyQuotaGroup(modelName: string, displayName?: string): QuotaGroup | null {
const combined = `${modelName} ${displayName ?? ""}`.toLowerCase();
if (combined.includes("claude")) {
return "claude";
}
const isGemini3 = combined.includes("gemini-3") || combined.includes("gemini 3");
if (!isGemini3) {
return null;
}
const family = getModelFamily(modelName);
return family === "gemini-flash" ? "gemini-flash" : "gemini-pro";
}
function aggregateQuota(models?: Record<string, FetchAvailableModelEntry>): QuotaSummary {
const groups: Partial<Record<QuotaGroup, QuotaGroupSummary>> = {};
if (!models) {
return { groups, modelCount: 0 };
}
let totalCount = 0;
for (const [modelName, entry] of Object.entries(models)) {
const group = classifyQuotaGroup(modelName, entry.displayName ?? entry.modelName);
if (!group) {
continue;
}
const quotaInfo = entry.quotaInfo;
const remainingFraction = quotaInfo
? normalizeRemainingFraction(quotaInfo.remainingFraction)
: undefined;
const resetTime = quotaInfo?.resetTime;
const resetTimestamp = parseResetTime(resetTime);
totalCount += 1;
const existing = groups[group];
const nextCount = (existing?.modelCount ?? 0) + 1;
const nextRemaining =
remainingFraction === undefined
? existing?.remainingFraction
: existing?.remainingFraction === undefined
? remainingFraction
: Math.min(existing.remainingFraction, remainingFraction);
let nextResetTime = existing?.resetTime;
if (resetTimestamp !== null) {
if (!existing?.resetTime) {
nextResetTime = resetTime;
} else {
const existingTimestamp = parseResetTime(existing.resetTime);
if (existingTimestamp === null || resetTimestamp < existingTimestamp) {
nextResetTime = resetTime;
}
}
}
groups[group] = {
remainingFraction: nextRemaining,
resetTime: nextResetTime,
modelCount: nextCount,
};
}
return { groups, modelCount: totalCount };
}
async function fetchWithTimeout(url: string, options: RequestInit, timeoutMs = FETCH_TIMEOUT_MS): Promise<Response> {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...options, signal: controller.signal });
} finally {
clearTimeout(timeout);
}
}
async function fetchAvailableModels(
accessToken: string,
projectId: string,
): Promise<FetchAvailableModelsResponse> {
const endpoint = ANTIGRAVITY_ENDPOINT_PROD;
const quotaUserAgent = ANTIGRAVITY_HEADERS["User-Agent"] || "antigravity/windows/amd64";
const errors: string[] = [];
const body = projectId ? { project: projectId } : {};
const response = await fetchWithTimeout(`${endpoint}/v1internal:fetchAvailableModels`, {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
"User-Agent": quotaUserAgent,
},
body: JSON.stringify(body),
});
if (response.ok) {
return (await response.json()) as FetchAvailableModelsResponse;
}
const message = await response.text().catch(() => "");
const snippet = message.trim().slice(0, 200);
errors.push(
`fetchAvailableModels ${response.status} at ${endpoint}${snippet ? `: ${snippet}` : ""}`,
);
throw new Error(errors.join("; ") || "fetchAvailableModels failed");
}
function applyAccountUpdates(account: AccountMetadataV3, auth: OAuthAuthDetails): AccountMetadataV3 | undefined {
const parts = parseRefreshParts(auth.refresh);
if (!parts.refreshToken) {
return undefined;
}
const updated: AccountMetadataV3 = {
...account,
refreshToken: parts.refreshToken,
projectId: parts.projectId ?? account.projectId,
managedProjectId: parts.managedProjectId ?? account.managedProjectId,
};
const changed =
updated.refreshToken !== account.refreshToken ||
updated.projectId !== account.projectId ||
updated.managedProjectId !== account.managedProjectId;
return changed ? updated : undefined;
}
export async function checkAccountsQuota(
accounts: AccountMetadataV3[],
client: PluginClient,
providerId = ANTIGRAVITY_PROVIDER_ID,
): Promise<AccountQuotaResult[]> {
const results: AccountQuotaResult[] = [];
for (const [index, account] of accounts.entries()) {
const disabled = account.enabled === false;
let auth = buildAuthFromAccount(account);
try {
if (accessTokenExpired(auth)) {
const refreshed = await refreshAccessToken(auth, client, providerId);
if (!refreshed) {
throw new Error("Token refresh failed");
}
auth = refreshed;
}
const projectContext = await ensureProjectContext(auth);
auth = projectContext.auth;
const updatedAccount = applyAccountUpdates(account, auth);
let quotaResult: QuotaSummary;
try {
const response = await fetchAvailableModels(
auth.access ?? "",
projectContext.effectiveProjectId,
);
quotaResult = aggregateQuota(response.models);
} catch (error) {
quotaResult = {
groups: {},
modelCount: 0,
error: error instanceof Error ? error.message : String(error),
};
}
results.push({
index,
email: account.email,
status: "ok",
disabled,
quota: quotaResult,
updatedAccount,
});
} catch (error) {
results.push({
index,
email: account.email,
status: "error",
disabled,
error: error instanceof Error ? error.message : String(error),
});
}
}
return results;
}