mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
feat(panel): add single-line top models to client sources and fast scope=conversation session switching
This commit is contained in:
@@ -4,7 +4,12 @@
|
||||
*/
|
||||
|
||||
import { createReactiveStore } from '../core/reactive.js';
|
||||
import { callSidecarApi, getWindowBridgeConfig, isWindowOverlayMode } from '../core/httpClient.js';
|
||||
import {
|
||||
abortSidecarApiKey,
|
||||
callSidecarApi,
|
||||
getWindowBridgeConfig,
|
||||
isWindowOverlayMode,
|
||||
} from '../core/httpClient.js';
|
||||
import {
|
||||
formatAgentBadgeText,
|
||||
formatAgentLabel,
|
||||
@@ -139,9 +144,11 @@ export const telemetryStore = createReactiveStore({
|
||||
|
||||
let pollTimer = null;
|
||||
let feedbackTimer = null;
|
||||
let deferredConvSyncTimer = null;
|
||||
let activeBundleVersion = 0;
|
||||
let latestRequestSeq = 0;
|
||||
let latestConsumptionReqSeq = 0;
|
||||
let latestConversationSwitchSeq = 0;
|
||||
let activeForceCount = 0;
|
||||
let consecutivePollFailures = 0;
|
||||
let inFlightTelemetryPoll = null;
|
||||
@@ -149,8 +156,10 @@ let inFlightPresetsPrefetch = null;
|
||||
let lastPrefetchedUsageRevision = 0;
|
||||
let consumptionCacheScopeSig = '';
|
||||
const MAX_CONSUMPTION_RANGE_CACHE = 12;
|
||||
const MAX_CONVERSATION_VIEW_CACHE = 12;
|
||||
const CONSUMPTION_PRESET_KEYS = ['cycle', 'today', '24h', '7d', '14d', '30d', 'all'];
|
||||
const consumptionRangeCache = new Map();
|
||||
const conversationViewCache = new Map();
|
||||
let lastReceivedSteps = null;
|
||||
let loopActive = false;
|
||||
let loopIntervalMs = 2000;
|
||||
@@ -182,6 +191,14 @@ export function clearConsumptionRangeCache() {
|
||||
inFlightPresetsPrefetch = null;
|
||||
}
|
||||
|
||||
export function clearConversationViewCache() {
|
||||
conversationViewCache.clear();
|
||||
if (deferredConvSyncTimer) {
|
||||
clearTimeout(deferredConvSyncTimer);
|
||||
deferredConvSyncTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function buildConsumptionScopeSig(payload) {
|
||||
if (!payload || typeof payload !== 'object') return '';
|
||||
const acct = payload.account || {};
|
||||
@@ -195,10 +212,112 @@ function syncConsumptionCacheScope(payload) {
|
||||
if (!nextSig) return;
|
||||
if (consumptionCacheScopeSig && consumptionCacheScopeSig !== nextSig) {
|
||||
clearConsumptionRangeCache();
|
||||
clearConversationViewCache();
|
||||
}
|
||||
consumptionCacheScopeSig = nextSig;
|
||||
}
|
||||
|
||||
function recordConversationViewToCache(payload) {
|
||||
const active = payload?.activeConversation;
|
||||
const cidLower = String(active?.cascadeId || '').trim().toLowerCase();
|
||||
if (!cidLower || !Array.isArray(active?.recentSteps)) return;
|
||||
const entry = {
|
||||
cascadeId: active.cascadeId,
|
||||
activeConversation: active,
|
||||
activePool: payload.quotas?.activePool || null,
|
||||
weekly: payload.quotas?.weekly || null,
|
||||
sessionConsumption: payload.consumption?.session || null,
|
||||
sessionPresets:
|
||||
payload.consumption?.sessionPresets ||
|
||||
(payload.consumption?.presets
|
||||
? Object.fromEntries(
|
||||
Object.entries(payload.consumption.presets).map(([k, v]) => [k, v?.session || null])
|
||||
)
|
||||
: null),
|
||||
stepRevision: String(payload.stepRevision || ''),
|
||||
cachedAtMs: Date.now(),
|
||||
};
|
||||
conversationViewCache.delete(cidLower);
|
||||
conversationViewCache.set(cidLower, entry);
|
||||
while (conversationViewCache.size > MAX_CONVERSATION_VIEW_CACHE) {
|
||||
conversationViewCache.delete(conversationViewCache.keys().next().value);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCachedConversationRootId(telemetry, effectiveConvId) {
|
||||
const target = String(effectiveConvId || '').trim();
|
||||
const convs = Array.isArray(telemetry?.conversations) ? telemetry.conversations : [];
|
||||
if (!target) {
|
||||
return String(convs[0]?.cascadeId || '').trim().toLowerCase();
|
||||
}
|
||||
const lower = target.toLowerCase();
|
||||
if (conversationViewCache.has(lower)) return lower;
|
||||
const matchedRoot = convs.find(
|
||||
(c) =>
|
||||
String(c?.cascadeId || '').toLowerCase() === lower ||
|
||||
(Array.isArray(c?.memberCascadeIds) &&
|
||||
c.memberCascadeIds.some((m) => String(m || '').toLowerCase() === lower))
|
||||
);
|
||||
return String(matchedRoot?.cascadeId || lower).trim().toLowerCase();
|
||||
}
|
||||
|
||||
function applyConversationSliceToTelemetry(baseTelemetry, slice) {
|
||||
if (!baseTelemetry || !slice?.activeConversation) return baseTelemetry;
|
||||
const activeCidLower = String(slice.activeConversation.cascadeId || '').toLowerCase();
|
||||
const prevQuotas = baseTelemetry.quotas || {};
|
||||
const nextQuotas =
|
||||
slice.activePool || slice.weekly
|
||||
? {
|
||||
...prevQuotas,
|
||||
...(slice.activePool ? { activePool: slice.activePool } : {}),
|
||||
...(slice.weekly ? { weekly: slice.weekly } : {}),
|
||||
}
|
||||
: prevQuotas;
|
||||
|
||||
const prevCons = baseTelemetry.consumption || {};
|
||||
let nextPresets = prevCons.presets;
|
||||
if (slice.sessionPresets && prevCons.presets && typeof prevCons.presets === 'object') {
|
||||
nextPresets = { ...prevCons.presets };
|
||||
for (const [k, sBucket] of Object.entries(slice.sessionPresets)) {
|
||||
if (sBucket && nextPresets[k]) {
|
||||
nextPresets[k] = { ...nextPresets[k], session: sBucket };
|
||||
}
|
||||
}
|
||||
}
|
||||
const nextCons =
|
||||
slice.sessionConsumption || nextPresets !== prevCons.presets
|
||||
? {
|
||||
...prevCons,
|
||||
...(slice.sessionConsumption ? { session: slice.sessionConsumption } : {}),
|
||||
...(nextPresets !== prevCons.presets ? { presets: nextPresets } : {}),
|
||||
}
|
||||
: prevCons;
|
||||
|
||||
const prevRunning = Array.isArray(baseTelemetry.runningConversations)
|
||||
? baseTelemetry.runningConversations
|
||||
: null;
|
||||
let nextRunning = prevRunning;
|
||||
if (prevRunning && prevRunning.length > 0 && activeCidLower) {
|
||||
let anyChanged = false;
|
||||
const mapped = prevRunning.map((item) => {
|
||||
const shouldSelect = String(item?.cascadeId || '').toLowerCase() === activeCidLower;
|
||||
if (Boolean(item?.isCurrentSelected) === shouldSelect) return item;
|
||||
anyChanged = true;
|
||||
return { ...item, isCurrentSelected: shouldSelect };
|
||||
});
|
||||
if (anyChanged) nextRunning = mapped;
|
||||
}
|
||||
|
||||
return {
|
||||
...baseTelemetry,
|
||||
...(slice.stepRevision ? { stepRevision: slice.stepRevision } : {}),
|
||||
activeConversation: slice.activeConversation,
|
||||
quotas: nextQuotas,
|
||||
consumption: nextCons,
|
||||
...(nextRunning ? { runningConversations: nextRunning } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function putConsumptionRangeCacheEntry(cacheKey, entry) {
|
||||
if (!cacheKey || !entry) return;
|
||||
consumptionRangeCache.delete(cacheKey);
|
||||
@@ -1012,6 +1131,7 @@ async function requestTelemetry(params, force) {
|
||||
|
||||
let resolvedPayload = reconcileIncomingTelemetrySettings(payload, reqSettingsRev);
|
||||
syncConsumptionCacheScope(resolvedPayload);
|
||||
recordConversationViewToCache(resolvedPayload);
|
||||
if (resolvedPayload?.consumption) {
|
||||
recordConsumptionSliceToCache(resolvedPayload.consumption, resolvedPayload.modelBreakdown, {
|
||||
customStartMs: reqStartMs,
|
||||
@@ -1198,6 +1318,162 @@ async function fetchConsumptionRangeSlice({
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速切换当前查看的会话:
|
||||
* - 命中前端 `conversationViewCache` 时,0ms 原子切换当前会话快照,并静默走轻量 `scope=conversation` 校准;
|
||||
* - 未命中缓存时直接走零 LS RPC、零全盘扫描的 `scope=conversation` 通道;
|
||||
* - 若后端提示该会话需要补读最新运行态或懒迁移字段(`needsBackgroundSync`),展示后再在后台静默补齐。
|
||||
*/
|
||||
export async function selectConversation(
|
||||
nextConvId,
|
||||
{ hostRouteConversationId = undefined } = {}
|
||||
) {
|
||||
const switchSeq = ++latestConversationSwitchSeq;
|
||||
latestRequestSeq += 1;
|
||||
inFlightTelemetryPoll = null;
|
||||
abortSidecarApiKey('telemetry-poll');
|
||||
if (deferredConvSyncTimer) {
|
||||
clearTimeout(deferredConvSyncTimer);
|
||||
deferredConvSyncTimer = null;
|
||||
}
|
||||
|
||||
const targetSelectedId = String(nextConvId || '').trim();
|
||||
const prevSt = telemetryStore.getState();
|
||||
const nextHostRouteId =
|
||||
hostRouteConversationId !== undefined
|
||||
? String(hostRouteConversationId || '').trim()
|
||||
: prevSt.hostRouteConversationId;
|
||||
const effectiveConvId = targetSelectedId || nextHostRouteId || '';
|
||||
const statePatch = {
|
||||
selectedConversationId: targetSelectedId,
|
||||
...(hostRouteConversationId !== undefined ? { hostRouteConversationId: nextHostRouteId } : {}),
|
||||
};
|
||||
|
||||
const prevTelemetry = prevSt.telemetry;
|
||||
const lookupRootId = resolveCachedConversationRootId(prevTelemetry, effectiveConvId);
|
||||
const cachedEntry = lookupRootId ? conversationViewCache.get(lookupRootId) : null;
|
||||
const cachedSteps =
|
||||
cachedEntry && cachedEntry.stepRevision
|
||||
? readDisplaySteps({
|
||||
stepRevision: cachedEntry.stepRevision,
|
||||
activeConversation: cachedEntry.activeConversation,
|
||||
})
|
||||
: null;
|
||||
|
||||
if (cachedEntry && prevTelemetry) {
|
||||
const optimisticTelemetry = applyConversationSliceToTelemetry(prevTelemetry, cachedEntry);
|
||||
if (cachedSteps) {
|
||||
lastReceivedSteps = cachedSteps;
|
||||
}
|
||||
telemetryStore.setState({
|
||||
...statePatch,
|
||||
telemetry: optimisticTelemetry,
|
||||
});
|
||||
} else {
|
||||
telemetryStore.setState(statePatch);
|
||||
}
|
||||
|
||||
const current = telemetryStore.getState();
|
||||
const params = new URLSearchParams();
|
||||
if (effectiveConvId) params.set('conversationId', effectiveConvId);
|
||||
if (current.selectedTimeRange) params.set('range', current.selectedTimeRange);
|
||||
if (current.selectedTimeRange === 'custom') {
|
||||
if (current.customStartMs > 0) params.set('startMs', String(current.customStartMs));
|
||||
if (current.customEndMs > 0) params.set('endMs', String(current.customEndMs));
|
||||
}
|
||||
params.set('stepDetails', 'display');
|
||||
params.set('stepRevision', cachedSteps?.revision || '');
|
||||
params.set('scope', 'conversation');
|
||||
|
||||
const reqSettingsRev = settingsRevision;
|
||||
try {
|
||||
const payload = await callSidecarApi(`/api/telemetry?${params}`, {
|
||||
method: 'GET',
|
||||
abortKey: 'conversation-switch',
|
||||
});
|
||||
if (!payload || typeof payload !== 'object') return null;
|
||||
if (switchSeq !== latestConversationSwitchSeq) return payload;
|
||||
|
||||
const latestSt = telemetryStore.getState();
|
||||
const latestEffectiveId =
|
||||
String(latestSt.selectedConversationId || latestSt.hostRouteConversationId || '').trim();
|
||||
if (latestEffectiveId !== effectiveConvId) return payload;
|
||||
|
||||
const active = payload.activeConversation;
|
||||
if (payload.stepsUnchanged) {
|
||||
const stepsSource =
|
||||
(cachedSteps && payload.stepRevision === cachedSteps.revision ? cachedSteps : null) ||
|
||||
(lastReceivedSteps && payload.stepRevision === lastReceivedSteps.revision
|
||||
? lastReceivedSteps
|
||||
: null);
|
||||
if (!active || !stepsSource) {
|
||||
return fetchTelemetry({ force: false });
|
||||
}
|
||||
active.recentSteps = stepsSource.recentSteps;
|
||||
active.agents.forEach((agent, index) => {
|
||||
const steps = stepsSource.agentSteps[index];
|
||||
if (steps) agent.recentSteps = steps;
|
||||
});
|
||||
}
|
||||
|
||||
if (payload.scope === 'conversation' && active) {
|
||||
lastReceivedSteps = readDisplaySteps(payload) || lastReceivedSteps;
|
||||
recordConversationViewToCache(payload);
|
||||
const baseTelemetry = latestSt.telemetry;
|
||||
if (baseTelemetry) {
|
||||
const mergedTelemetry = applyConversationSliceToTelemetry(baseTelemetry, {
|
||||
activeConversation: active,
|
||||
activePool: payload.quotas?.activePool || null,
|
||||
weekly: payload.quotas?.weekly || null,
|
||||
sessionConsumption: payload.consumption?.session || null,
|
||||
sessionPresets: payload.consumption?.sessionPresets || null,
|
||||
stepRevision: payload.stepRevision || '',
|
||||
});
|
||||
if (!isEquivalentTelemetry(baseTelemetry, mergedTelemetry)) {
|
||||
telemetryStore.setState({
|
||||
telemetry: mergedTelemetry,
|
||||
minuteTick: Math.floor(Date.now() / 30000),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return fetchTelemetry({ force: false });
|
||||
}
|
||||
if (payload.needsBackgroundSync && loopActive && typeof setTimeout === 'function') {
|
||||
deferredConvSyncTimer = setTimeout(() => {
|
||||
deferredConvSyncTimer = null;
|
||||
if (switchSeq === latestConversationSwitchSeq && loopActive) {
|
||||
void fetchTelemetry({ force: false });
|
||||
}
|
||||
}, 35);
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
// 冷启动或全新未入缓存会话回退至完整载荷
|
||||
const resolvedPayload = reconcileIncomingTelemetrySettings(payload, reqSettingsRev);
|
||||
syncConsumptionCacheScope(resolvedPayload);
|
||||
lastReceivedSteps = readDisplaySteps(resolvedPayload) || lastReceivedSteps;
|
||||
recordConversationViewToCache(resolvedPayload);
|
||||
if (resolvedPayload?.consumption) {
|
||||
recordConsumptionSliceToCache(resolvedPayload.consumption, resolvedPayload.modelBreakdown, {
|
||||
customStartMs: Number(current.customStartMs || 0),
|
||||
customEndMs: Number(current.customEndMs || 0),
|
||||
});
|
||||
}
|
||||
if (!isEquivalentTelemetry(latestSt.telemetry, resolvedPayload)) {
|
||||
telemetryStore.setState({
|
||||
telemetry: resolvedPayload,
|
||||
minuteTick: Math.floor(Date.now() / 30000),
|
||||
syncState: latestSt.syncState === 'synced' ? 'synced' : 'idle',
|
||||
});
|
||||
}
|
||||
return resolvedPayload;
|
||||
} catch (err) {
|
||||
if (err?.name === 'AbortError' || switchSeq !== latestConversationSwitchSeq) return null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换「用量统计」时间范围:
|
||||
* - 命中前端 rangeCache 时,在单次 setState 内 0ms 原子更新概览/趋势/模型/来源(若 usageRevision 一致则 0 次 HTTP 请求);
|
||||
@@ -1482,8 +1758,13 @@ export async function saveSettingsPatch(patch) {
|
||||
}
|
||||
|
||||
const needsTelemetryRecompute = patchKeys.some((k) => TELEMETRY_RECOMPUTE_SETTING_KEYS.has(k));
|
||||
if (Object.prototype.hasOwnProperty.call(safePatch, 'weeklyLimitMode')) {
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(safePatch, 'weeklyLimitMode') ||
|
||||
Object.prototype.hasOwnProperty.call(safePatch, 'weeklyTokenBudget') ||
|
||||
Object.prototype.hasOwnProperty.call(safePatch, 'weeklyCostBudget')
|
||||
) {
|
||||
clearConsumptionRangeCache();
|
||||
clearConversationViewCache();
|
||||
}
|
||||
if (needsTelemetryRecompute) {
|
||||
inFlightTelemetryPoll = null;
|
||||
@@ -1549,6 +1830,7 @@ export async function resetAllSettings() {
|
||||
}
|
||||
inFlightTelemetryPoll = null;
|
||||
clearConsumptionRangeCache();
|
||||
clearConversationViewCache();
|
||||
void fetchTelemetry({ force: false });
|
||||
return res;
|
||||
}
|
||||
@@ -1556,6 +1838,7 @@ export async function resetAllSettings() {
|
||||
export async function rebuildTrajectoryCache() {
|
||||
const res = await callSidecarApi('/api/cache/rebuild', { method: 'POST', body: {} });
|
||||
clearConsumptionRangeCache();
|
||||
clearConversationViewCache();
|
||||
await fetchTelemetry({ force: true });
|
||||
return res;
|
||||
}
|
||||
@@ -1563,6 +1846,7 @@ export async function rebuildTrajectoryCache() {
|
||||
export async function triggerCloudSync() {
|
||||
const res = await callSidecarApi('/api/cloud-sync', { method: 'POST', body: {} });
|
||||
clearConsumptionRangeCache();
|
||||
clearConversationViewCache();
|
||||
await fetchTelemetry({ force: true });
|
||||
return res;
|
||||
}
|
||||
@@ -1636,6 +1920,10 @@ export function stopAsyncTelemetryLoop() {
|
||||
clearTimeout(feedbackTimer);
|
||||
feedbackTimer = null;
|
||||
}
|
||||
if (deferredConvSyncTimer) {
|
||||
clearTimeout(deferredConvSyncTimer);
|
||||
deferredConvSyncTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
|
||||
Reference in New Issue
Block a user