mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
feat(panel): dual-mount topbar capsule, live multi-session HUD, and customizable topbar metrics (v1.4.3)
This commit is contained in:
@@ -3,22 +3,14 @@
|
||||
*
|
||||
* 负责:
|
||||
* 1. 向本地 Language Server 发起带 CSRF 鉴权与超时保护的 Connect RPC 调用 (`callLsRpc`);
|
||||
* 2. 拉取并缓存 `RetrieveUserQuotaSummary` 官方实时配额摘要(常规轮询使用 10 秒短期缓存,
|
||||
* 并在缓存过期或强制刷新时穿透 `language_server.exe` 内部 3 分钟陈旧缓存);
|
||||
* 3. 维护全局 `StreamDiagnostics` 实例以采样流式事件时序。
|
||||
* 2. 拉取并缓存 `RetrieveUserQuotaSummary` 官方实时配额摘要(常规轮询使用 10 秒短期缓存与失败退避冷却,
|
||||
* 并在缓存过期或强制刷新时穿透 `language_server.exe` 内部 3 分钟陈旧缓存)。
|
||||
*/
|
||||
|
||||
import { StreamDiagnostics } from '../stream-diagnostics.mjs';
|
||||
import { CSRF_TOKEN, DATA_DIR, LS_ADDR, quotaSummaryCache } from './config.mjs';
|
||||
import { CSRF_TOKEN, LS_BASE_URL, quotaSummaryCache } from './config.mjs';
|
||||
|
||||
/**
|
||||
* 全局流式传输诊断采样器实例。
|
||||
*/
|
||||
export const diagnostics = new StreamDiagnostics({
|
||||
dataDir: DATA_DIR,
|
||||
lsAddress: LS_ADDR,
|
||||
csrfToken: CSRF_TOKEN,
|
||||
});
|
||||
const QUOTA_CACHE_TTL_MS = 10_000;
|
||||
const QUOTA_RETRY_COOLDOWN_MS = 5_000;
|
||||
|
||||
/**
|
||||
* 调用本地 `LanguageServerService` 的指定 RPC 方法。
|
||||
@@ -29,13 +21,16 @@ export const diagnostics = new StreamDiagnostics({
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
export async function callLsRpc(method, body = {}, timeoutMs = 5000) {
|
||||
if (!LS_BASE_URL) {
|
||||
throw new Error('Language Server address (ANTIGRAVITY_LS_ADDRESS) is not configured');
|
||||
}
|
||||
const res = await fetch(
|
||||
`http://${LS_ADDR}/exa.language_server_pb.LanguageServerService/${method}`,
|
||||
`${LS_BASE_URL}/exa.language_server_pb.LanguageServerService/${method}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-codeium-csrf-token': CSRF_TOKEN,
|
||||
'x-codeium-csrf-token': CSRF_TOKEN || '',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
@@ -53,38 +48,63 @@ export async function callLsRpc(method, body = {}, timeoutMs = 5000) {
|
||||
*
|
||||
* 缓存策略:
|
||||
* - 常规轮询在 10 秒内直接复用内存缓存,避免高频打满上游接口;
|
||||
* - 当超过 10 秒或传入 `forceRefresh = true`(如手动刷新或检测到账号切换)时,
|
||||
* - 常规轮询发生网络失败或空载荷时进入 5 秒冷却退避,避免每 2 秒重复超时阻塞;
|
||||
* - 当超过 TTL 或传入 `forceRefresh = true`(如手动刷新或检测到账号切换)时,
|
||||
* 向 LS 传递 `{ forceRefresh: true }`,绕过 `language_server.exe` 自身的 3 分钟缓存。
|
||||
*
|
||||
* @param {boolean} [forceRefresh=false] 是否强制穿透本地 10 秒缓存
|
||||
* @returns {Promise<{data: object|null, fetchedAtMs: number, isStale: boolean, refreshFailed: boolean, error: string|null}>}
|
||||
* @param {boolean} [forceRefresh=false] 是否强制穿透本地缓存与失败冷却
|
||||
* @returns {Promise<{data: object|null, fetchedAtMs: number, isStale: boolean, refreshFailed: boolean, error: string|null, fromCache: boolean}>}
|
||||
*/
|
||||
export async function getOfficialQuotaSummary(forceRefresh = false) {
|
||||
const now = Date.now();
|
||||
if (!forceRefresh && quotaSummaryCache.data && now - quotaSummaryCache.ts < 10000) {
|
||||
if (!forceRefresh && quotaSummaryCache.data && now - quotaSummaryCache.ts < QUOTA_CACHE_TTL_MS) {
|
||||
return {
|
||||
data: quotaSummaryCache.data,
|
||||
fetchedAtMs: quotaSummaryCache.ts,
|
||||
isStale: false,
|
||||
refreshFailed: false,
|
||||
error: null,
|
||||
fromCache: true,
|
||||
};
|
||||
}
|
||||
if (
|
||||
!forceRefresh &&
|
||||
quotaSummaryCache.lastAttemptTs > 0 &&
|
||||
now - quotaSummaryCache.lastAttemptTs < QUOTA_RETRY_COOLDOWN_MS &&
|
||||
quotaSummaryCache.error
|
||||
) {
|
||||
return {
|
||||
data: quotaSummaryCache.data,
|
||||
fetchedAtMs: quotaSummaryCache.ts || 0,
|
||||
isStale: Boolean(quotaSummaryCache.data),
|
||||
refreshFailed: true,
|
||||
error: quotaSummaryCache.error,
|
||||
fromCache: true,
|
||||
};
|
||||
}
|
||||
|
||||
quotaSummaryCache.lastAttemptTs = now;
|
||||
try {
|
||||
const res = await callLsRpc('RetrieveUserQuotaSummary', { forceRefresh: true }, 6000);
|
||||
const payload = res?.response || res;
|
||||
if (Array.isArray(payload?.groups) || Array.isArray(payload?.buckets)) {
|
||||
const payload = res?.response !== undefined ? res.response : res;
|
||||
if (payload && typeof payload === 'object' && !Array.isArray(payload)) {
|
||||
const normalized =
|
||||
Array.isArray(payload.groups) || Array.isArray(payload.buckets)
|
||||
? payload
|
||||
: { ...payload, groups: [] };
|
||||
quotaSummaryCache.ts = now;
|
||||
quotaSummaryCache.data = payload;
|
||||
quotaSummaryCache.data = normalized;
|
||||
quotaSummaryCache.error = null;
|
||||
return {
|
||||
data: payload,
|
||||
data: normalized,
|
||||
fetchedAtMs: now,
|
||||
isStale: false,
|
||||
refreshFailed: false,
|
||||
error: null,
|
||||
fromCache: false,
|
||||
};
|
||||
}
|
||||
quotaSummaryCache.error = 'Quota summary empty';
|
||||
} catch (err) {
|
||||
console.error(`RetrieveUserQuotaSummary failed: ${err.message}`);
|
||||
quotaSummaryCache.error = err.message;
|
||||
@@ -95,5 +115,6 @@ export async function getOfficialQuotaSummary(forceRefresh = false) {
|
||||
isStale: Boolean(quotaSummaryCache.data),
|
||||
refreshFailed: true,
|
||||
error: quotaSummaryCache.error || 'Quota RPC unavailable',
|
||||
fromCache: false,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user