feat(release): v1.4.0 cloud pricing sync, one-click plugin update, and UI copy refresh

This commit is contained in:
yuhuan2498281558
2026-11-03 14:28:56 +09:00
parent cdcbb129f8
commit 551eeb35c3
38 changed files with 8278 additions and 4064 deletions
@@ -0,0 +1,171 @@
/**
* @fileoverview 纯 SVG 三次贝塞尔平滑曲线趋势图组件 (TrendAreaChart).
* 负责将按时间分桶的 Token 消耗序列渲染为带垂直渐变填充、虚线参考网格、
* 峰值描点与自适应防溢出的顶部峰值胶囊标签的面积图。
*/
import { h, patchAttr, patchStyleVar, patchText } from '../../core/domPatch.js';
import { formatTokensPrecise } from '../../core/formatters.js';
import { SVG_NS } from '../../icons/SvgIcon.js';
/**
* 将二维采样点序列转换为水平张力平滑的三次贝塞尔曲线 SVG 路径 (`M ... C ...`)。
* 控制点沿 X 轴取相邻点间距的 `0.45` 张力系数,Y 轴与各自端点齐平,
* 保证曲线在极值点处平缓过渡且不会产生上下过冲 (Overshoot)。
*
* @param {Array<{ x: number, y: number }>} points SVG 坐标系下的采样点数组
* @returns {string} SVG `<path>` 的 `d` 属性字符串
*/
function buildSmoothSplinePath(points) {
if (!points.length) return '';
if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
let d = `M ${points[0].x.toFixed(1)} ${points[0].y.toFixed(1)}`;
for (let i = 0; i < points.length - 1; i++) {
const p0 = points[i];
const p1 = points[i + 1];
const cpx1 = p0.x + (p1.x - p0.x) * 0.45;
const cpx2 = p1.x - (p1.x - p0.x) * 0.45;
d += ` C ${cpx1.toFixed(1)} ${p0.y.toFixed(1)}, ${cpx2.toFixed(1)} ${p1.y.toFixed(1)}, ${p1.x.toFixed(1)} ${p1.y.toFixed(1)}`;
}
return d;
}
/**
* 创建 SVG 历史趋势平滑面积图组件。
*
* SVG 视口坐标系 (`viewBox="0 0 460 116"`) 布局约定:
* - 水平绘图区:`leftX = 12` 至 `rightX = 448`(总跨度 `spanX = 436`),左右预留边距容纳端点圆点描边;
* - 垂直绘图区:顶部峰值线 `topY = 26` 至底部零基线 `bottomY = 98`(总跨度 `spanY = 72`),
* 顶部预留 26px 空间给 HTML 峰值胶囊 (`.trend-peak-pill`)。
*
* @returns {{
* el: HTMLElement,
* update: (series?: Array<{ label: string, tokens: number }>) => void
* }} 图表容器节点与序列更新方法
*/
export function createTrendAreaChart() {
const svg = document.createElementNS(SVG_NS, 'svg');
svg.setAttribute('viewBox', '0 0 460 116');
svg.setAttribute('class', 'trend-svg');
svg.setAttribute('aria-hidden', 'true');
// 垂直线性渐变:由顶部 36% 不透明度的主题色平滑衰减至底部全透明
const defs = document.createElementNS(SVG_NS, 'defs');
const grad = document.createElementNS(SVG_NS, 'linearGradient');
grad.setAttribute('id', 'trend-area-grad');
grad.setAttribute('x1', '0');
grad.setAttribute('y1', '0');
grad.setAttribute('x2', '0');
grad.setAttribute('y2', '1');
const stop1 = document.createElementNS(SVG_NS, 'stop');
stop1.setAttribute('offset', '0%');
stop1.setAttribute('stop-color', 'var(--ctx-cached)');
stop1.setAttribute('stop-opacity', '0.36');
const stop2 = document.createElementNS(SVG_NS, 'stop');
stop2.setAttribute('offset', '100%');
stop2.setAttribute('stop-color', 'var(--ctx-cached)');
stop2.setAttribute('stop-opacity', '0.0');
grad.appendChild(stop1);
grad.appendChild(stop2);
defs.appendChild(grad);
svg.appendChild(defs);
// 背景辅助参考网格线:3 条水平分位线 + 3 条垂直时间锚点分割线
const gridGroup = document.createElementNS(SVG_NS, 'g');
gridGroup.setAttribute('class', 'trend-grid');
for (const y of [26, 62, 98]) {
const line = document.createElementNS(SVG_NS, 'line');
line.setAttribute('x1', '10');
line.setAttribute('y1', String(y));
line.setAttribute('x2', '450');
line.setAttribute('y2', String(y));
gridGroup.appendChild(line);
}
for (const x of [118, 230, 342]) {
const line = document.createElementNS(SVG_NS, 'line');
line.setAttribute('x1', String(x));
line.setAttribute('y1', '16');
line.setAttribute('x2', String(x));
line.setAttribute('y2', '98');
gridGroup.appendChild(line);
}
svg.appendChild(gridGroup);
const areaPath = document.createElementNS(SVG_NS, 'path');
areaPath.setAttribute('fill', 'url(#trend-area-grad)');
svg.appendChild(areaPath);
const linePath = document.createElementNS(SVG_NS, 'path');
linePath.setAttribute('class', 'trend-line');
linePath.setAttribute('fill', 'none');
svg.appendChild(linePath);
const peakDot = document.createElementNS(SVG_NS, 'circle');
peakDot.setAttribute('r', '3');
peakDot.setAttribute('class', 'trend-peak-dot');
svg.appendChild(peakDot);
const peakPill = h('span', { className: 'trend-peak-pill text-mono', text: '0' });
const chartWrap = h('div', { className: 'trend-chart-wrap' }, [peakPill, svg]);
let lastLineD = '';
/**
* 根据最新时间分桶序列重绘平滑曲线、闭合渐变区域以及最高峰标记位置。
*/
function update(series = []) {
const slots = series.length >= 2 ? series : [
{ label: '00:00', tokens: 0 },
{ label: '06:00', tokens: 0 },
{ label: '12:00', tokens: 0 },
];
let maxTokens = 0;
let peakIdx = 0;
for (let i = 0; i < slots.length; i++) {
const t = Number(slots[i].tokens || 0);
if (t >= maxTokens) {
maxTokens = t;
peakIdx = i;
}
}
// SVG 绘图区边界与跨度参数(Y 轴向下递增,因此数值越大对应的 y 坐标越小)
const leftX = 12;
const rightX = 448;
const topY = 26;
const bottomY = 98;
const spanX = rightX - leftX;
const spanY = bottomY - topY;
const denom = Math.max(1, maxTokens);
const pts = slots.map((s, idx) => {
const x = leftX + (idx / Math.max(1, slots.length - 1)) * spanX;
const ratio = maxTokens > 0 ? Number(s.tokens || 0) / denom : 0;
const y = bottomY - ratio * spanY;
return { x, y };
});
// 仅在曲线路径发生变化时更新 DOM 属性,避免无意义的 SVG 重绘
const lineD = buildSmoothSplinePath(pts);
if (lineD !== lastLineD) {
lastLineD = lineD;
const areaD = `${lineD} L ${pts[pts.length - 1].x.toFixed(1)} ${bottomY} L ${pts[0].x.toFixed(1)} ${bottomY} Z`;
patchAttr(linePath, 'd', lineD);
patchAttr(areaPath, 'd', areaD);
}
// 定位峰值圆点,并将顶部峰值气泡水平位置钳制在 [8%, 92%] 区间内以防贴边裁切
const peakPt = pts[peakIdx] || { x: 230, y: bottomY };
patchAttr(peakDot, 'cx', peakPt.x.toFixed(1));
patchAttr(peakDot, 'cy', peakPt.y.toFixed(1));
patchText(peakPill, formatTokensPrecise(maxTokens));
const pctLeft = Math.max(8, Math.min(92, (peakPt.x / 460) * 100));
patchStyleVar(peakPill, '--peak-x', `${pctLeft.toFixed(1)}%`);
}
return { el: chartWrap, update };
}
@@ -7,7 +7,8 @@
import { defineComponent } from '../../core/component.js';
import { h, patchAttr, patchClasses, patchText } from '../../core/domPatch.js';
import { createSvgIcon, patchSvgIcon } from '../../icons/SvgIcon.js';
import { fetchTelemetry, telemetryStore } from '../../store/telemetryStore.js';
import { fetchTelemetry, telemetryStore, triggerPluginUpdate } from '../../store/telemetryStore.js';
import { uiStore } from '../../store/uiStore.js';
import { CustomSelect } from '../ui/CustomSelect.js';
export const TopSessionHeader = defineComponent({
@@ -16,14 +17,50 @@ export const TopSessionHeader = defineComponent({
const convSelect = CustomSelect({
modelValue: telemetryStore.getState().selectedConversationId,
icon: 'bolt',
placeholder: '自动跟随最新会话',
options: [{ value: '', label: '自动跟随最新会话' }],
placeholder: '跟随最新会话',
options: [{ value: '', label: '跟随最新会话' }],
onUpdateModelValue: (nextConvId) => {
telemetryStore.setState({ selectedConversationId: nextConvId });
fetchTelemetry({ force: false });
},
});
const updateBtnIcon = createSvgIcon('arrow-down', { size: 11 });
const updateBtnText = h('span', { text: '有新版本' });
let isHeaderUpdating = false;
const updateBtn = h(
'button',
{
type: 'button',
className: 'update-badge-btn',
hidden: true,
'aria-label': '发现新版本,点击升级',
onClick: async () => {
if (isHeaderUpdating) return;
isHeaderUpdating = true;
patchText(updateBtnText, '升级中…');
patchAttr(updateBtn, 'disabled', true);
try {
const res = await triggerPluginUpdate();
if (!res?.ok) {
uiStore.setState({ activeView: 'settings' });
}
} catch {
uiStore.setState({ activeView: 'settings' });
} finally {
isHeaderUpdating = false;
patchAttr(updateBtn, 'disabled', false);
const cs = telemetryStore.getState().telemetry?.cloudStatus;
patchText(
updateBtnText,
cs?.latestPluginVersion ? `升级 v${cs.latestPluginVersion}` : '有新版本'
);
}
},
},
[updateBtnIcon, updateBtnText]
);
const statusIcon = createSvgIcon('pulse', { size: 12 });
const statusText = h('span', { text: '实时 2s' });
const statusTag = h('span', { className: 'status-tag' }, [statusIcon, statusText]);
@@ -33,7 +70,7 @@ export const TopSessionHeader = defineComponent({
{
type: 'button',
className: 'icon-btn',
'aria-label': '立即刷新额度与测速数据',
'aria-label': '刷新数据',
onClick: () => fetchTelemetry({ force: true }),
},
[createSvgIcon('refresh', { size: 13 })]
@@ -41,7 +78,7 @@ export const TopSessionHeader = defineComponent({
const headerEl = h('header', { className: 'session-header' }, [
h('div', { className: 'session-left' }, [convSelect.el]),
h('div', { className: 'session-right' }, [statusTag, refreshBtn]),
h('div', { className: 'session-right' }, [updateBtn, statusTag, refreshBtn]),
]);
const unsubTelemetry = telemetryStore.subscribe(
@@ -51,10 +88,11 @@ export const TopSessionHeader = defineComponent({
conversations: st.telemetry?.conversations,
isRunning: st.telemetry?.activeConversation?.isRunning,
quotaMeta: st.telemetry?.quotas?.meta,
cloudStatus: st.telemetry?.cloudStatus,
}),
(slice) => {
const convs = slice.conversations || [];
const latestTitle = convs[0]?.title ? `自动跟随 (${convs[0].title})` : '自动跟随最新会话';
const latestTitle = convs[0]?.title ? `最新 (${convs[0].title})` : '跟随最新会话';
const options = [
{ value: '', label: latestTitle, meta: convs[0] ? `${convs[0].stepCount}步` : '' },
...convs.map((c) => ({
@@ -68,6 +106,17 @@ export const TopSessionHeader = defineComponent({
options,
});
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';
@@ -0,0 +1,47 @@
/**
* @fileoverview 通用 4 段式 Token 构成比例条组件 (FourSegmentBar).
* 用于在消费概览、热门模型明细及数据来源明细中直观展示:
* 直接输入 (Input)、缓存读取 (Cached)、内容生成 (Output) 与思考推理 (Thinking) 的 Token 占比。
*/
import { h, patchStyleVar } from '../../core/domPatch.js';
/**
* 创建 4 段式水平比例进度条实例。
* 通过动态更新各分段节点的 `--seg-w` CSS 变量实现零重排平滑过渡。
*
* @returns {{
* el: HTMLElement,
* update: (metrics: {
* inputTokens?: number,
* cacheReadTokens?: number,
* responseTokens?: number,
* thinkingTokens?: number
* }) => void
* }} 组件根元素与增量更新方法
*/
export function createFourSegmentBar() {
const segInput = h('div', { className: 'progress-seg seg-input' });
const segCached = h('div', { className: 'progress-seg seg-cached' });
const segOutput = h('div', { className: 'progress-seg seg-output' });
const segThinking = h('div', { className: 'progress-seg seg-thinking' });
const el = h('div', { className: 'progress-track cons-seg-bar' }, [
segInput,
segCached,
segOutput,
segThinking,
]);
/**
* 根据四类 Token 数量重新计算并写入各分段百分比宽度。
*/
function update({ inputTokens = 0, cacheReadTokens = 0, responseTokens = 0, thinkingTokens = 0 }) {
const total = Math.max(1, inputTokens + cacheReadTokens + responseTokens + thinkingTokens);
patchStyleVar(segInput, '--seg-w', `${((inputTokens / total) * 100).toFixed(2)}%`);
patchStyleVar(segCached, '--seg-w', `${((cacheReadTokens / total) * 100).toFixed(2)}%`);
patchStyleVar(segOutput, '--seg-w', `${((responseTokens / total) * 100).toFixed(2)}%`);
patchStyleVar(segThinking, '--seg-w', `${((thinkingTokens / total) * 100).toFixed(2)}%`);
}
return { el, update };
}