-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgoogle.ts
More file actions
377 lines (318 loc) · 9.99 KB
/
google.ts
File metadata and controls
377 lines (318 loc) · 9.99 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Google Cloud 额度查询模块
*
* [输入]: ~/.config/opencode/antigravity-accounts.json 中的账号信息
* [输出]: 格式化的额度使用情况(按重置时间自动分组)
* [定位]: 被 mystatus.ts 调用,处理 Google Cloud 账号
* [同步]: mystatus.ts, types.ts, utils.ts, i18n.ts
*/
import { existsSync } from "fs";
import { readFile } from "fs/promises";
import { homedir } from "os";
import { join } from "path";
import { t, currentLang } from "./i18n";
import {
type QueryResult,
type AntigravityAccount,
type AntigravityAccountsFile,
HIGH_USAGE_THRESHOLD,
} from "./types";
import { createProgressBar, fetchWithTimeout, safeMax } from "./utils";
// ============================================================================
// 类型定义
// ============================================================================
interface GoogleQuotaResponse {
models: Record<
string,
{
quotaInfo?: {
remainingFraction?: number;
resetTime?: string;
};
}
>;
}
/** 单个模型的额度信息 */
interface ModelQuota {
displayName: string;
remainPercent: number;
resetTimeDisplay: string;
}
/** 账号额度信息 */
interface AccountQuotaInfo {
email: string;
models: ModelQuota[];
maxUsage: number;
}
/** 模型配置 */
interface ModelConfig {
key: string;
altKey?: string;
display: string;
}
// ============================================================================
// 常量
// ============================================================================
const GOOGLE_QUOTA_API_URL =
"https://cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels";
const USER_AGENT = "antigravity/1.11.9 windows/amd64";
// 需要显示的 4 个模型配置
const MODELS_TO_DISPLAY: ModelConfig[] = [
{ key: "gemini-3-pro-high", altKey: "gemini-3-pro-low", display: "G3 Pro" },
{ key: "gemini-3-pro-image", display: "G3 Image" },
{ key: "gemini-3-flash", display: "G3 Flash" },
{
key: "claude-opus-4-5-thinking",
altKey: "claude-opus-4-5",
display: "Claude",
},
];
// 获取 Antigravity 账号文件路径
function getAntigravityAccountsPath(): string {
const home = homedir();
const configDir =
process.platform === "win32"
? process.env.APPDATA || join(home, "AppData", "Roaming")
: join(home, ".config");
return join(configDir, "opencode", "antigravity-accounts.json");
}
const GOOGLE_TOKEN_REFRESH_URL = "https://oauth2.googleapis.com/token";
// OAuth credentials from opencode-antigravity-auth project
// Source: https://github.com/NoeFabris/opencode-antigravity-auth
const GOOGLE_CLIENT_ID =
"1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com";
const GOOGLE_CLIENT_SECRET = "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";
// ============================================================================
// 工具函数
// ============================================================================
/**
* 格式化重置时间为简短显示(如 "4h 59m")
*/
function formatResetTimeShort(isoTime: string): string {
if (!isoTime) return "-";
try {
const resetDate = new Date(isoTime);
const now = new Date();
const diffMs = resetDate.getTime() - now.getTime();
if (diffMs <= 0) return currentLang === "zh" ? "已重置" : "reset";
const diffMinutes = Math.floor(diffMs / 60000);
const days = Math.floor(diffMinutes / 1440);
const hours = Math.floor((diffMinutes % 1440) / 60);
const minutes = diffMinutes % 60;
if (days > 0) {
return `${days}d ${hours}h`;
}
return `${hours}h ${minutes}m`;
} catch {
return "-";
}
}
/**
* 从 API 响应中提取 4 个模型的额度信息
*/
function extractModelQuotas(data: GoogleQuotaResponse): ModelQuota[] {
const quotas: ModelQuota[] = [];
for (const modelConfig of MODELS_TO_DISPLAY) {
let modelInfo = data.models[modelConfig.key];
// 如果主 key 没有数据,尝试 altKey
if (!modelInfo && modelConfig.altKey) {
modelInfo = data.models[modelConfig.altKey];
}
// 只要模型存在就显示(即使没有 quotaInfo 或额度为 0)
if (modelInfo) {
const remainingFraction = modelInfo.quotaInfo?.remainingFraction ?? 0;
quotas.push({
displayName: modelConfig.display,
remainPercent: Math.round(remainingFraction * 100),
resetTimeDisplay: formatResetTimeShort(
modelInfo.quotaInfo?.resetTime || "",
),
});
}
}
return quotas;
}
/**
* 刷新 Google access token
*/
async function refreshAccessToken(
refreshToken: string,
): Promise<{ access_token: string; expires_in: number }> {
const params = new URLSearchParams({
client_id: GOOGLE_CLIENT_ID,
client_secret: GOOGLE_CLIENT_SECRET,
refresh_token: refreshToken,
grant_type: "refresh_token",
});
const response = await fetch(GOOGLE_TOKEN_REFRESH_URL, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: params,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(t.googleApiError(response.status, errorText));
}
return response.json();
}
// ============================================================================
// API 调用
// ============================================================================
/**
* 获取 Google Cloud 使用情况
*/
async function fetchGoogleUsage(
accessToken: string,
projectId: string,
): Promise<GoogleQuotaResponse> {
const response = await fetchWithTimeout(GOOGLE_QUOTA_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
"User-Agent": USER_AGENT,
},
body: JSON.stringify({ project: projectId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(t.googleApiError(response.status, errorText));
}
return response.json() as Promise<GoogleQuotaResponse>;
}
/**
* 查询单个账号的额度
*/
async function fetchAccountQuota(
account: AntigravityAccount,
): Promise<{
success: boolean;
models?: ModelQuota[];
maxUsage?: number;
error?: string;
}> {
try {
// 刷新 access token
const { access_token } = await refreshAccessToken(account.refreshToken);
// 使用 projectId 或 managedProjectId
const projectId = account.projectId || account.managedProjectId;
if (!projectId) {
return { success: false, error: t.googleNoProjectId };
}
// 查询额度
const data = await fetchGoogleUsage(access_token, projectId);
// 提取 4 个模型的额度
const models = extractModelQuotas(data);
if (models.length === 0) {
return { success: true, models: undefined, maxUsage: 0 };
}
// 计算最大使用率
const maxUsage = safeMax(models.map((m) => 100 - m.remainPercent));
return { success: true, models, maxUsage };
} catch (err) {
return {
success: false,
error: err instanceof Error ? err.message : String(err),
};
}
}
// ============================================================================
// 格式化输出
// ============================================================================
/**
* 格式化单个账号的额度(4 个模型分别显示)
*/
function formatAccountQuota(quotaInfo: AccountQuotaInfo): string {
const lines: string[] = [];
// 标题行
lines.push(`### ${quotaInfo.email}`);
if (quotaInfo.models.length === 0) {
lines.push("");
lines.push(t.noQuotaData);
return lines.join("\n");
}
lines.push("");
// 每个模型一行:模型名 | 重置时间 | 百分比
for (const model of quotaInfo.models) {
const progressBar = createProgressBar(model.remainPercent, 20);
lines.push(
`${model.displayName.padEnd(10)} ${model.resetTimeDisplay.padEnd(10)} ${progressBar} ${model.remainPercent}%`,
);
}
// 警告
if (quotaInfo.maxUsage >= HIGH_USAGE_THRESHOLD) {
lines.push("");
lines.push(t.limitReached);
}
return lines.join("\n");
}
// ============================================================================
// 导出接口
// ============================================================================
/**
* 查询所有 Antigravity 账号的额度
* @returns 查询结果
*/
export async function queryGoogleUsage(): Promise<QueryResult | null> {
const accountsPath = getAntigravityAccountsPath();
if (!existsSync(accountsPath)) {
return null;
}
try {
// 读取账号文件
const content = await readFile(accountsPath, "utf-8");
const file = JSON.parse(content) as AntigravityAccountsFile;
if (!file.accounts || file.accounts.length === 0) {
return {
success: true,
output: t.noQuotaData,
};
}
// 过滤掉没有邮箱的账号
const validAccounts = file.accounts.filter((account) => account.email);
if (validAccounts.length === 0) {
return {
success: true,
output: t.noQuotaData,
};
}
// 并行查询所有账号
const results = await Promise.all(
validAccounts.map((account: AntigravityAccount) =>
fetchAccountQuota(account).then(
(result) => ({ account, result }) as const,
),
),
);
// 收集输出
const outputs: string[] = [];
for (const { account, result } of results) {
if (!result.success) {
outputs.push(`${account.email || t.unknown}: ${result.error}`);
} else if (result.models && result.models.length > 0) {
const quotaInfo: AccountQuotaInfo = {
email: account.email || t.unknown,
models: result.models,
maxUsage: result.maxUsage || 0,
};
outputs.push(formatAccountQuota(quotaInfo));
}
}
// 如果没有符合条件的账号
if (outputs.length === 0) {
return {
success: true,
output: t.noQuotaData,
};
}
return {
success: true,
output: outputs.join("\n\n"),
};
} catch (err) {
return {
success: false,
error: err instanceof Error ? err.message : String(err),
};
}
}