feat: header 增加当前会话总 token 消耗统计

- 新增 totalTokens 元素(帮助按钮右侧,🪙 图标 + 数字)
- chat-area.ts 新增 updateTotalTokens() 函数
- 四个触发点:新消息完成、新建会话、加载历史、应用初始化
- 数字自动格式化(toLocaleString)
This commit is contained in:
thzxx
2026-04-07 02:00:27 +08:00
parent a80baf363c
commit 79e4cac19b
6 changed files with 35 additions and 3 deletions
+12
View File
@@ -384,6 +384,18 @@ export function enableAutoScroll(): void {
if (scrollBtnEl) scrollBtnEl.style.display = 'none';
}
/** 统计当前会话总 token 消耗并更新 header 显示 */
export function updateTotalTokens(): void {
const session = state.get<ChatSession | null>(KEYS.CURRENT_SESSION);
const el = document.querySelector('#totalTokensValue');
if (!el) return;
if (!session) { el.textContent = '0'; return; }
const total = session.messages
.filter(m => m.role === 'assistant' && m.eval_count)
.reduce((sum, m) => sum + (m.eval_count || 0), 0);
el.textContent = total > 0 ? total.toLocaleString() : '0';
}
// ── 导出功能 ──
async function nativeSaveFile(defaultName: string, content: string): Promise<void> {