mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
feat(panel): subagent monitoring and 10-channel context window (v1.4.1)
This commit is contained in:
@@ -6,20 +6,36 @@
|
||||
|
||||
import { defineComponent } from '../../core/component.js';
|
||||
import { h, patchAttr, patchClasses, patchText } from '../../core/domPatch.js';
|
||||
import { formatUsd } from '../../core/formatters.js';
|
||||
import { createSvgIcon, patchSvgIcon } from '../../icons/SvgIcon.js';
|
||||
import { fetchTelemetry, telemetryStore, triggerPluginUpdate } from '../../store/telemetryStore.js';
|
||||
import {
|
||||
fetchTelemetry,
|
||||
formatAgentLabel,
|
||||
resolveScopedConversation,
|
||||
telemetryStore,
|
||||
triggerPluginUpdate,
|
||||
} from '../../store/telemetryStore.js';
|
||||
import { uiStore } from '../../store/uiStore.js';
|
||||
import { CustomSelect } from '../ui/CustomSelect.js';
|
||||
|
||||
function formatConvOptionMeta(c) {
|
||||
if (!c) return '';
|
||||
const subs = Number(c.subagentCount || 0);
|
||||
if (subs > 0) {
|
||||
return `主${c.stepCount || 0} · ${subs}子 (共${c.totalStepCount || c.stepCount || 0}步)`;
|
||||
}
|
||||
return `${c.stepCount || 0}步`;
|
||||
}
|
||||
|
||||
export const TopSessionHeader = defineComponent({
|
||||
name: 'TopSessionHeader',
|
||||
setup({ onCleanup }) {
|
||||
const convSelect = CustomSelect({
|
||||
modelValue: telemetryStore.getState().selectedConversationId,
|
||||
icon: 'bolt',
|
||||
placeholder: '跟随最新会话',
|
||||
options: [{ value: '', label: '跟随最新会话' }],
|
||||
onUpdateModelValue: (nextConvId) => {
|
||||
uiStore.setState({ selectedAgentScope: 'all', ctxInspectorAgentId: 'auto' });
|
||||
telemetryStore.setState({ selectedConversationId: nextConvId });
|
||||
fetchTelemetry({ force: false });
|
||||
},
|
||||
@@ -76,116 +92,237 @@ export const TopSessionHeader = defineComponent({
|
||||
[createSvgIcon('refresh', { size: 13 })]
|
||||
);
|
||||
|
||||
const headerEl = h('header', { className: 'session-header' }, [
|
||||
const headerRowEl = h('div', { className: 'session-header' }, [
|
||||
h('div', { className: 'session-left' }, [convSelect.el]),
|
||||
h('div', { className: 'session-right' }, [updateBtn, statusTag, refreshBtn]),
|
||||
]);
|
||||
|
||||
// --- 方案 A:顶栏二级主/子代理切换条 (.agent-scope-bar,无子代理时自动隐藏) ---
|
||||
const scopeAllBtnText = h('span', { text: '全部汇总' });
|
||||
const scopeAllBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'scope-pill-btn is-active',
|
||||
onClick: () =>
|
||||
uiStore.setState({ selectedAgentScope: 'all', ctxInspectorAgentId: 'auto' }),
|
||||
},
|
||||
[scopeAllBtnText]
|
||||
);
|
||||
|
||||
const scopeMainBtnText = h('span', { text: '主代理' });
|
||||
const scopeMainBtn = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: 'scope-pill-btn',
|
||||
onClick: () => uiStore.setState({ selectedAgentScope: 'main' }),
|
||||
},
|
||||
[scopeMainBtnText]
|
||||
);
|
||||
|
||||
const subagentSelect = CustomSelect({
|
||||
modelValue: '',
|
||||
icon: 'cpu',
|
||||
placeholder: '子代理 (0)',
|
||||
className: 'scope-subagent-select',
|
||||
options: [],
|
||||
onUpdateModelValue: (nextSubId) => {
|
||||
if (nextSubId) {
|
||||
uiStore.setState({ selectedAgentScope: nextSubId });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const scopeRightText = h('span', { className: 'agent-scope-right text-mono', text: '' });
|
||||
|
||||
const scopeBarEl = h(
|
||||
'div',
|
||||
{ className: 'agent-scope-bar', hidden: true },
|
||||
[
|
||||
h('div', { className: 'agent-scope-left' }, [
|
||||
scopeAllBtn,
|
||||
scopeMainBtn,
|
||||
subagentSelect.el,
|
||||
]),
|
||||
scopeRightText,
|
||||
]
|
||||
);
|
||||
|
||||
const headerEl = h('header', { className: 'top-session-stack' }, [headerRowEl, scopeBarEl]);
|
||||
|
||||
let lastActiveCascadeId = '';
|
||||
|
||||
function renderHeaderState() {
|
||||
const telSt = telemetryStore.getState();
|
||||
const uiSt = uiStore.getState();
|
||||
const telemetry = telSt.telemetry;
|
||||
const currentCascadeId = telemetry?.activeConversation?.cascadeId || '';
|
||||
if (currentCascadeId && lastActiveCascadeId && currentCascadeId !== lastActiveCascadeId) {
|
||||
lastActiveCascadeId = currentCascadeId;
|
||||
if (uiSt.selectedAgentScope !== 'all' || uiSt.ctxInspectorAgentId !== 'auto') {
|
||||
uiStore.setState({ selectedAgentScope: 'all', ctxInspectorAgentId: 'auto' });
|
||||
return;
|
||||
}
|
||||
} else if (currentCascadeId) {
|
||||
lastActiveCascadeId = currentCascadeId;
|
||||
}
|
||||
|
||||
const convs = telemetry?.conversations || [];
|
||||
const latestTitle = convs[0]?.title ? `最新 (${convs[0].title})` : '跟随最新会话';
|
||||
const options = [
|
||||
{ value: '', label: latestTitle, meta: convs[0] ? formatConvOptionMeta(convs[0]) : '' },
|
||||
...convs.map((c) => ({
|
||||
value: c.cascadeId,
|
||||
label: c.title || c.cascadeId.slice(0, 8),
|
||||
meta: formatConvOptionMeta(c),
|
||||
})),
|
||||
];
|
||||
convSelect.setProps({
|
||||
modelValue: telSt.selectedConversationId,
|
||||
options,
|
||||
});
|
||||
|
||||
const {
|
||||
hasSubagents,
|
||||
effectiveScope,
|
||||
subagents,
|
||||
mainAgent,
|
||||
aggregated,
|
||||
scopedConv,
|
||||
} = resolveScopedConversation(telemetry, uiSt.selectedAgentScope, uiSt.ctxInspectorAgentId);
|
||||
|
||||
patchAttr(scopeBarEl, 'hidden', !hasSubagents);
|
||||
if (hasSubagents) {
|
||||
patchText(scopeAllBtnText, `全部 (${aggregated?.stepCount ?? 0})`);
|
||||
patchClasses(scopeAllBtn, { 'is-active': effectiveScope === 'all' });
|
||||
|
||||
patchText(scopeMainBtnText, `主代理 (${mainAgent?.stepCount ?? 0})`);
|
||||
patchClasses(scopeMainBtn, { 'is-active': effectiveScope === 'main' });
|
||||
|
||||
const isSubSelected = effectiveScope !== 'all' && effectiveScope !== 'main';
|
||||
patchClasses(subagentSelect.el, { 'is-active-sub': isSubSelected });
|
||||
const subOptions = subagents.map((s) => ({
|
||||
value: s.agentId,
|
||||
label: formatAgentLabel(s),
|
||||
meta: `${s.stepCount || 0}步 · 窗${s.context?.usedPct ?? 0}%`,
|
||||
}));
|
||||
subagentSelect.setProps({
|
||||
modelValue: isSubSelected ? effectiveScope : '',
|
||||
placeholder: `子代理 (${subagents.length})`,
|
||||
options: subOptions,
|
||||
});
|
||||
|
||||
patchText(
|
||||
scopeRightText,
|
||||
formatUsd(scopedConv?.sessionCostUsd || 0, {
|
||||
unpricedCalls: scopedConv?.sessionUnpricedCalls,
|
||||
})
|
||||
);
|
||||
} else {
|
||||
subagentSelect.close();
|
||||
}
|
||||
|
||||
const cloudStatus = telemetry?.cloudStatus;
|
||||
const hasUpdate = Boolean(cloudStatus?.updateAvailable);
|
||||
patchAttr(updateBtn, 'hidden', !hasUpdate);
|
||||
if (hasUpdate && !isHeaderUpdating) {
|
||||
patchText(
|
||||
updateBtnText,
|
||||
cloudStatus?.latestPluginVersion
|
||||
? `升级 v${cloudStatus.latestPluginVersion}`
|
||||
: '有新版本'
|
||||
);
|
||||
}
|
||||
|
||||
const isSyncing = telSt.syncState === 'syncing';
|
||||
const isSynced = telSt.syncState === 'synced';
|
||||
const isError = telSt.syncState === 'error';
|
||||
const isRunning = Boolean(aggregated?.isRunning || telemetry?.activeConversation?.isRunning);
|
||||
const quotaMeta = telemetry?.quotas?.meta;
|
||||
const quotaStale = Boolean(quotaMeta?.refreshFailed || quotaMeta?.isStale);
|
||||
|
||||
patchClasses(refreshBtn, { 'is-spinning': isSyncing });
|
||||
patchAttr(refreshBtn, 'disabled', isSyncing);
|
||||
|
||||
if (isSyncing) {
|
||||
patchSvgIcon(statusIcon, 'refresh');
|
||||
patchText(statusText, '同步中');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': true,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (isSynced) {
|
||||
patchSvgIcon(statusIcon, 'check');
|
||||
patchText(statusText, '已刷新');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': true,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (isError) {
|
||||
patchSvgIcon(statusIcon, 'warning');
|
||||
patchText(statusText, '重试中');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': true,
|
||||
});
|
||||
} else if (isRunning) {
|
||||
const runningAgent = (telemetry?.activeConversation?.agents || []).find((a) => a.isRunning);
|
||||
const runSuffix =
|
||||
hasSubagents && runningAgent ? ` · ${runningAgent.badgeText || runningAgent.shortLabel}` : '';
|
||||
patchSvgIcon(statusIcon, 'bolt');
|
||||
patchText(statusText, `生成中${runSuffix}`);
|
||||
patchClasses(statusTag, {
|
||||
'state-running': true,
|
||||
'state-synced': false,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (quotaStale) {
|
||||
patchSvgIcon(statusIcon, 'warning');
|
||||
patchText(statusText, quotaMeta?.hasOfficialData ? '额度缓存' : '额度离线');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': true,
|
||||
});
|
||||
} else {
|
||||
patchSvgIcon(statusIcon, 'pulse');
|
||||
patchText(statusText, '实时 2s');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const unsubTelemetry = telemetryStore.subscribe(
|
||||
(st) => ({
|
||||
selectedConversationId: st.selectedConversationId,
|
||||
syncState: st.syncState,
|
||||
conversations: st.telemetry?.conversations,
|
||||
isRunning: st.telemetry?.activeConversation?.isRunning,
|
||||
quotaMeta: st.telemetry?.quotas?.meta,
|
||||
cloudStatus: st.telemetry?.cloudStatus,
|
||||
telemetry: st.telemetry,
|
||||
}),
|
||||
(slice) => {
|
||||
const convs = slice.conversations || [];
|
||||
const latestTitle = convs[0]?.title ? `最新 (${convs[0].title})` : '跟随最新会话';
|
||||
const options = [
|
||||
{ value: '', label: latestTitle, meta: convs[0] ? `${convs[0].stepCount}步` : '' },
|
||||
...convs.map((c) => ({
|
||||
value: c.cascadeId,
|
||||
label: c.title || c.cascadeId.slice(0, 8),
|
||||
meta: `${c.stepCount || 0}步`,
|
||||
})),
|
||||
];
|
||||
convSelect.setProps({
|
||||
modelValue: slice.selectedConversationId,
|
||||
options,
|
||||
});
|
||||
() => renderHeaderState()
|
||||
);
|
||||
|
||||
const hasUpdate = Boolean(slice.cloudStatus?.updateAvailable);
|
||||
patchAttr(updateBtn, 'hidden', !hasUpdate);
|
||||
if (hasUpdate && !isHeaderUpdating) {
|
||||
patchText(
|
||||
updateBtnText,
|
||||
slice.cloudStatus?.latestPluginVersion
|
||||
? `升级 v${slice.cloudStatus.latestPluginVersion}`
|
||||
: '有新版本'
|
||||
);
|
||||
}
|
||||
|
||||
const isSyncing = slice.syncState === 'syncing';
|
||||
const isSynced = slice.syncState === 'synced';
|
||||
const isError = slice.syncState === 'error';
|
||||
const isRunning = Boolean(slice.isRunning);
|
||||
const quotaStale = Boolean(slice.quotaMeta?.refreshFailed || slice.quotaMeta?.isStale);
|
||||
|
||||
patchClasses(refreshBtn, { 'is-spinning': isSyncing });
|
||||
patchAttr(refreshBtn, 'disabled', isSyncing);
|
||||
|
||||
if (isSyncing) {
|
||||
patchSvgIcon(statusIcon, 'refresh');
|
||||
patchText(statusText, '同步中');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': true,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (isSynced) {
|
||||
patchSvgIcon(statusIcon, 'check');
|
||||
patchText(statusText, '已刷新');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': true,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (isError) {
|
||||
patchSvgIcon(statusIcon, 'warning');
|
||||
patchText(statusText, '重试中');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': true,
|
||||
});
|
||||
} else if (isRunning) {
|
||||
patchSvgIcon(statusIcon, 'bolt');
|
||||
patchText(statusText, '生成中');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': true,
|
||||
'state-synced': false,
|
||||
'state-warn': false,
|
||||
});
|
||||
} else if (quotaStale) {
|
||||
patchSvgIcon(statusIcon, 'warning');
|
||||
patchText(statusText, slice.quotaMeta?.hasOfficialData ? '额度缓存' : '额度离线');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': true,
|
||||
});
|
||||
} else {
|
||||
patchSvgIcon(statusIcon, 'pulse');
|
||||
patchText(statusText, '实时 2s');
|
||||
patchClasses(statusTag, {
|
||||
'state-running': false,
|
||||
'state-synced': false,
|
||||
'state-warn': false,
|
||||
});
|
||||
}
|
||||
}
|
||||
const unsubUi = uiStore.subscribe(
|
||||
(st) => st.selectedAgentScope,
|
||||
() => renderHeaderState()
|
||||
);
|
||||
|
||||
onCleanup(() => {
|
||||
unsubTelemetry();
|
||||
unsubUi();
|
||||
convSelect.destroy();
|
||||
subagentSelect.destroy();
|
||||
});
|
||||
|
||||
return {
|
||||
el: headerEl,
|
||||
refs: { convSelect, statusTag, refreshBtn },
|
||||
refs: { convSelect, subagentSelect, statusTag, refreshBtn, scopeBarEl },
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user