@anthonyhan/token-pilot 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.js +7 -1
- package/dist/core/aggregate.js +2 -0
- package/dist/core/collector.js +14 -3
- package/dist/core/doctor.js +50 -1
- package/dist/core/version.js +25 -0
- package/package.json +1 -1
- package/web/dashboard.html +27 -5
- package/web/panel.html +161 -0
package/dist/cli/index.js
CHANGED
|
@@ -262,6 +262,12 @@ async function cmdCompressReport() {
|
|
|
262
262
|
}
|
|
263
263
|
async function main() {
|
|
264
264
|
const cmd = process.argv[2] ?? 'today';
|
|
265
|
+
// 报版本:用户想确认装的是哪一版(也方便 issue 里贴环境)
|
|
266
|
+
if (cmd === '--version' || cmd === '-v' || cmd === 'version') {
|
|
267
|
+
const { APP_VERSION } = await import('../core/version.js');
|
|
268
|
+
console.log(APP_VERSION);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
265
271
|
if (cmd === 'serve') {
|
|
266
272
|
const port = Number(process.argv[3] ?? 17895);
|
|
267
273
|
await serve(port);
|
|
@@ -294,7 +300,7 @@ async function main() {
|
|
|
294
300
|
if (cmd === 'today' || cmd === 'month' || cmd === 'report' || cmd === 'all') {
|
|
295
301
|
return cmdReport(cmd === 'all' ? 'all' : cmd);
|
|
296
302
|
}
|
|
297
|
-
console.log('用法:token-pilot [today|month|report|sync|dash|serve|prices|quota|doctor]');
|
|
303
|
+
console.log('用法:token-pilot [today|month|report|sync|dash|serve|prices|quota|doctor|version]');
|
|
298
304
|
}
|
|
299
305
|
main().catch((e) => {
|
|
300
306
|
console.error('错误:', e.message);
|
package/dist/core/aggregate.js
CHANGED
|
@@ -6,6 +6,7 @@ import { detectRetryLoops } from './retry-detector.js';
|
|
|
6
6
|
import { savingsAdvice } from './savings.js';
|
|
7
7
|
import { computeCompression } from './compression.js';
|
|
8
8
|
import { buildAdapterTiers, HOST_TIERS } from './registry.js';
|
|
9
|
+
import { APP_VERSION } from './version.js';
|
|
9
10
|
const AGENT_NAMES = {
|
|
10
11
|
'claude-code': 'Claude Code',
|
|
11
12
|
'kimi-code': 'Kimi Code',
|
|
@@ -515,6 +516,7 @@ export async function buildDashboardData(rawEvents, cfg, pricing, contentEvents
|
|
|
515
516
|
quota = null;
|
|
516
517
|
}
|
|
517
518
|
return {
|
|
519
|
+
appVersion: APP_VERSION,
|
|
518
520
|
generatedAt: nowMs,
|
|
519
521
|
usdToCny: fx,
|
|
520
522
|
dateLabel: `${now.getMonth() + 1}月${now.getDate()}日 周${WD[now.getDay()]}`,
|
package/dist/core/collector.js
CHANGED
|
@@ -38,6 +38,12 @@ const LOCK_PATH = path.join(CONFIG_DIR, 'sync.lock');
|
|
|
38
38
|
const WINDOW_MS = 40 * 86400_000;
|
|
39
39
|
const LOCK_TTL_MS = 120_000;
|
|
40
40
|
// ---------- 单飞锁 ----------
|
|
41
|
+
/// 抢锁。返回 true = 抢到了(调用方负责 release)。
|
|
42
|
+
///
|
|
43
|
+
/// 写锁失败(目录只读、沙箱 EPERM、磁盘满)不能让整次 sync 崩掉:
|
|
44
|
+
/// 锁只是防多进程叠加的优化,拿不到就退化成本次不加锁跑——总比看板
|
|
45
|
+
/// 直接返回 {} 什么都看不到强。这里吞掉写错误,只在拿不到时返回 true,
|
|
46
|
+
/// 由调用方的 finally 里的 release 自行忽略"文件不存在"。
|
|
41
47
|
export async function acquireSyncLock() {
|
|
42
48
|
try {
|
|
43
49
|
const raw = await fs.readFile(LOCK_PATH, 'utf8');
|
|
@@ -54,10 +60,15 @@ export async function acquireSyncLock() {
|
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
62
|
catch {
|
|
57
|
-
//
|
|
63
|
+
// 无锁文件(或读不到)
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
await fs.mkdir(CONFIG_DIR, { recursive: true });
|
|
67
|
+
await fs.writeFile(LOCK_PATH, JSON.stringify({ pid: process.pid, ts: Date.now() }), 'utf8');
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// 写不了锁文件:不加锁也要继续,不能因为锁而拿不到数据
|
|
58
71
|
}
|
|
59
|
-
await fs.mkdir(CONFIG_DIR, { recursive: true });
|
|
60
|
-
await fs.writeFile(LOCK_PATH, JSON.stringify({ pid: process.pid, ts: Date.now() }), 'utf8');
|
|
61
72
|
return true;
|
|
62
73
|
}
|
|
63
74
|
export async function releaseSyncLock() {
|
package/dist/core/doctor.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// 只会觉得「这个月花得少」。
|
|
7
7
|
//
|
|
8
8
|
// 所以 doctor 的核心职责是:把「你以为在读但实际没读到」的地方摊开来说清楚。
|
|
9
|
-
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
9
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
10
10
|
import { CONFIG_DIR, CONFIG_PATH, DATA_PATH, loadConfig } from './config.js';
|
|
11
11
|
import { loadPricing } from './pricing.js';
|
|
12
12
|
import { ALL_ADAPTERS } from './collector.js';
|
|
@@ -27,6 +27,48 @@ function dataAgeMinutes() {
|
|
|
27
27
|
return null;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* 检测「brew cask 与 app 自带更新器抢同一个 app」。
|
|
32
|
+
*
|
|
33
|
+
* 判定依据(任一成立即可报警,不要求 brew 命令可用):
|
|
34
|
+
* 1. Homebrew 的 Caskroom 里存在 token-pilot 记录 —— 说明这个 app 曾被 brew 装着;
|
|
35
|
+
* 2. 存在 brew 的软链 Caskroom/<ver>/token-pilot.app 指向 /Applications 下的同名 app。
|
|
36
|
+
*
|
|
37
|
+
* 冲突后果(真实踩过):Tauri updater 原地覆盖 /Applications 里的 app,
|
|
38
|
+
* brew 记录仍停在旧版本号,且 brew 会把这个软链当坏安装修,两个更新器互踩,
|
|
39
|
+
* 用户只看到「检查更新失败」「brew upgrade 不起作用」。
|
|
40
|
+
*
|
|
41
|
+
* 只读检测,不执行 brew 命令(GUI 启动的进程 PATH 里常常没有 brew)。
|
|
42
|
+
*/
|
|
43
|
+
export function detectUpdaterConflict() {
|
|
44
|
+
const caskroom = [
|
|
45
|
+
'/opt/homebrew/Caskroom/token-pilot',
|
|
46
|
+
'/usr/local/Caskroom/token-pilot',
|
|
47
|
+
].find((p) => existsSync(p));
|
|
48
|
+
if (!caskroom)
|
|
49
|
+
return null;
|
|
50
|
+
// 读出 Caskroom 目录里记着的版本(目录名就是 brew 认为的版本)
|
|
51
|
+
let versions = [];
|
|
52
|
+
try {
|
|
53
|
+
versions = readdirSync(caskroom).filter((n) => /^\d+\.\d+/.test(n));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
/* 读不到就当没记录 */
|
|
57
|
+
}
|
|
58
|
+
const appLink = versions
|
|
59
|
+
.map((v) => path.join(caskroom, v, 'token-pilot.app'))
|
|
60
|
+
.find((p) => existsSync(p));
|
|
61
|
+
const verText = versions.length ? `brew 记录版本 ${versions.join(', ')}` : 'brew 有安装记录';
|
|
62
|
+
return {
|
|
63
|
+
level: 'warn',
|
|
64
|
+
title: '更新通道冲突(brew 与自动更新抢同一个 app)',
|
|
65
|
+
detail: `${caskroom} 存在,说明这个 app 曾被 Homebrew 安装(${verText})。` +
|
|
66
|
+
'app 自带的自动更新会原地覆盖 /Applications 里的程序,而 brew 仍按自己的记录管理,' +
|
|
67
|
+
'两边互踩会导致「检查更新失败」或 brew upgrade 反而装回旧版。',
|
|
68
|
+
fix: '二选一:① 只用自动更新 —— `brew uninstall --cask token-pilot`(app 保留在 /Applications);' +
|
|
69
|
+
'② 只用 brew —— 别点「安装更新」,每次 `brew upgrade --cask token-pilot`',
|
|
70
|
+
};
|
|
71
|
+
}
|
|
30
72
|
export async function runDoctor() {
|
|
31
73
|
const items = [];
|
|
32
74
|
const push = (i) => items.push(i);
|
|
@@ -42,6 +84,13 @@ export async function runDoctor() {
|
|
|
42
84
|
fix: '跑一次 `token-pilot sync` 就会建好',
|
|
43
85
|
});
|
|
44
86
|
}
|
|
87
|
+
// ── 1b. 更新通道冲突:brew cask 与 app 自带更新器抢同一个 app ──
|
|
88
|
+
// 真实踩过:brew 装的 app 被 Tauri updater 原地覆盖成新版本,brew 仍记着
|
|
89
|
+
// 旧版本号;于是 `brew upgrade` 会倒退、而 updater 又被 brew 的软链结构
|
|
90
|
+
// 绊住——两边都更新不动,用户只看到「检查更新失败」。
|
|
91
|
+
const conflict = detectUpdaterConflict();
|
|
92
|
+
if (conflict)
|
|
93
|
+
push(conflict);
|
|
45
94
|
// ── 2. 采集宿主:逐个检测,重点说清「装了但读不到」 ──
|
|
46
95
|
const detected = [];
|
|
47
96
|
const missing = [];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// 版本号 —— 单一真源是 package.json。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要这个模块:以前「app 的版本」只存在于二进制里,用户在看板和面板上
|
|
4
|
+
// 看不到自己装的是哪一版,报问题时说不清版本,判断「自动更新有没有生效」
|
|
5
|
+
// 也只能靠猜。现在版本号随数据一起下发给前端(DashboardData.appVersion)。
|
|
6
|
+
//
|
|
7
|
+
// 为什么不 `import pkg from '../../package.json'`:那样 tsc 会把源码相对路径
|
|
8
|
+
// 原样保留,编译后 dist/core/version.js 去读 dist 外的 package.json,只能靠
|
|
9
|
+
// 目录布局碰运气(rootDir=src 时必失效)。这里按「从 dist/core/ 回到包根」
|
|
10
|
+
// 解析,不管 cwd 在哪都对。
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { dirname, resolve } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
function readPackageVersion() {
|
|
15
|
+
try {
|
|
16
|
+
const here = dirname(fileURLToPath(import.meta.url)); // .../dist/core
|
|
17
|
+
const pkgPath = resolve(here, '..', '..', 'package.json'); // -> 包根
|
|
18
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
19
|
+
return pkg.version ?? '0.0.0';
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return '0.0.0'; // 版本读不到不该让看板挂掉
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const APP_VERSION = readPackageVersion();
|
package/package.json
CHANGED
package/web/dashboard.html
CHANGED
|
@@ -613,12 +613,12 @@ footer .notes{max-width:560px}
|
|
|
613
613
|
<div id="cmpBody"><div class="cmp-empty">本月暂无明显可压缩的上下文浪费。</div></div>
|
|
614
614
|
</section>
|
|
615
615
|
|
|
616
|
-
<section class="sec acc-violet full-only" data-od-id="quota" style="margin-top:16px">
|
|
616
|
+
<section class="sec acc-violet full-only" data-od-id="quota" id="quotaSection" style="margin-top:16px;display:none">
|
|
617
617
|
<div class="sec-head">
|
|
618
618
|
<h2 class="sec-title"><span class="icon-chip ic-violet"><svg viewBox="0 0 24 24"><path d="M3 12a9 9 0 0 1 9-9"/><path d="M21 12a9 9 0 0 1-9 9"/><path d="M12 7v5l3 3"/></svg></span>订阅额度与余额</h2>
|
|
619
619
|
<span class="sec-note" id="quotaNote">还能用多少 · 与账单互补</span>
|
|
620
620
|
</div>
|
|
621
|
-
<div id="quotaBody"
|
|
621
|
+
<div id="quotaBody"></div>
|
|
622
622
|
</section>
|
|
623
623
|
|
|
624
624
|
<section class="sec acc-blue full-only" data-od-id="adapters" style="margin-top:16px">
|
|
@@ -1280,12 +1280,17 @@ function qPace(w){
|
|
|
1280
1280
|
var QSRC={api:'官方接口',cli:'官方 CLI',computed:'本地计算',manual:'手动登记'};
|
|
1281
1281
|
function renderQuota(){
|
|
1282
1282
|
var box=document.getElementById('quotaBody');
|
|
1283
|
-
|
|
1283
|
+
var sec=document.getElementById('quotaSection');
|
|
1284
|
+
if(!box||!sec)return;
|
|
1284
1285
|
var q=D.quota;
|
|
1286
|
+
// 没有任何可监控平台就整块隐藏——空壳标题比不显示更碍事,
|
|
1287
|
+
// 也没有信息量。面板用同一规则(见 web/panel.html 的 renderQuota)。
|
|
1285
1288
|
if(!q||!q.providers||!q.providers.length){
|
|
1286
|
-
|
|
1289
|
+
sec.style.display='none';
|
|
1290
|
+
box.innerHTML='';
|
|
1287
1291
|
return;
|
|
1288
1292
|
}
|
|
1293
|
+
sec.style.display='';
|
|
1289
1294
|
// 三段:订阅窗口 / Token Plan / 现金余额
|
|
1290
1295
|
var subs=[],plans=[],bals=[];
|
|
1291
1296
|
q.providers.forEach(function(p){
|
|
@@ -1397,11 +1402,28 @@ function renderTok(){
|
|
|
1397
1402
|
}
|
|
1398
1403
|
|
|
1399
1404
|
/* ---------- footer ---------- */
|
|
1405
|
+
/* 版本号:app_version(正在跑的二进制)优先,退到数据里的 appVersion;
|
|
1406
|
+
两者都有却不一致时如实并列——这能一眼看出「二进制升了、数据还是旧的」。 */
|
|
1407
|
+
var APP_VER=null;
|
|
1408
|
+
function verText(){
|
|
1409
|
+
var app=APP_VER, data=(D&&D.appVersion)||null;
|
|
1410
|
+
if(app&&data&&app!==data)return "v"+app+" · 数据 v"+data;
|
|
1411
|
+
if(app||data)return "v"+(app||data);
|
|
1412
|
+
return "";
|
|
1413
|
+
}
|
|
1414
|
+
if(inTauri()&&window.__TAURI__.core){
|
|
1415
|
+
try{
|
|
1416
|
+
window.__TAURI__.core.invoke('app_version').then(function(v){
|
|
1417
|
+
if(v){APP_VER=String(v);if(D)renderFooter()}
|
|
1418
|
+
}).catch(function(){});
|
|
1419
|
+
}catch(e){}
|
|
1420
|
+
}
|
|
1400
1421
|
function renderFooter(){
|
|
1401
1422
|
var on=0;
|
|
1402
1423
|
(D.adapters||[]).forEach(function(t){(t.chips||[]).forEach(function(c){if(c.status==="on")on++})});
|
|
1403
1424
|
var t=D.generatedAt?new Date(D.generatedAt):new Date();
|
|
1404
|
-
|
|
1425
|
+
var v=verText();
|
|
1426
|
+
document.querySelector("footer .notes").textContent="数据更新于 "+p2(t.getHours())+":"+p2(t.getMinutes())+":"+p2(t.getSeconds())+" · "+on+" 个数据源接入 · 价格为公开目录价估算 · 数据不出本机"+(v?" · "+v:"");
|
|
1405
1427
|
}
|
|
1406
1428
|
|
|
1407
1429
|
/* ---------- tooltips ---------- */
|
package/web/panel.html
CHANGED
|
@@ -70,6 +70,10 @@ button{font:inherit;cursor:pointer;border:0;background:none;color:inherit}
|
|
|
70
70
|
.p-logo{width:24px;height:24px;border-radius:8px;background:linear-gradient(120deg,var(--accent),var(--accent-2) 60%,var(--accent-3));display:grid;place-items:center;flex:none}
|
|
71
71
|
.p-logo svg{width:13px;height:13px;fill:oklch(1 0 0)}
|
|
72
72
|
.p-head h1{font-size:13.5px;font-weight:800;margin:0}
|
|
73
|
+
/* 版本号:紧跟品牌名的小号等宽数字。不抢标题的视觉重量,但要一眼看得到、
|
|
74
|
+
选中可复制(报 issue / 判断自动更新是否生效时都要贴)。 */
|
|
75
|
+
.p-head .ver-badge{font-size:10.5px;font-weight:600;color:var(--muted);font-variant-numeric:tabular-nums;letter-spacing:.02em;user-select:text;padding:1px 0}
|
|
76
|
+
.p-head .ver-badge:empty{display:none}
|
|
73
77
|
.p-head .st{font-size:10.5px;font-weight:700;color:var(--pos-text);background:color-mix(in oklab,var(--pos) 12%,var(--tint));border-radius:999px;padding:1px 8px}
|
|
74
78
|
.p-head .sp{flex:1}
|
|
75
79
|
.p-date{color:var(--muted);font-size:11.5px}
|
|
@@ -206,6 +210,30 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
206
210
|
.save-amt.warn{color:var(--warn);background:color-mix(in oklab,var(--warn) 12%,var(--tint))}
|
|
207
211
|
.save-amt.info{color:var(--info);background:color-mix(in oklab,var(--info) 13%,var(--tint))}
|
|
208
212
|
|
|
213
|
+
/* 订阅额度与余额:面板宽度有限,每个平台一行,窗口用细进度条 + 百分比。
|
|
214
|
+
与看板保持同一份数据契约(DashboardData.quota),只换更紧凑的表达。 */
|
|
215
|
+
.quota{margin:0 14px 4px}
|
|
216
|
+
.q-prov{padding:7px 0;border-bottom:1px solid var(--gridln)}
|
|
217
|
+
.q-prov:last-child{border-bottom:0}
|
|
218
|
+
.q-head{display:flex;align-items:baseline;gap:6px}
|
|
219
|
+
.q-name{font-weight:700;font-size:12px;flex:1;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
220
|
+
.q-src{color:var(--muted);font-size:9.5px;font-weight:600;flex:none}
|
|
221
|
+
.q-win{display:flex;align-items:center;gap:7px;margin-top:4px}
|
|
222
|
+
.q-lab{color:var(--muted);font-size:10.5px;flex:none;min-width:52px;max-width:96px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
223
|
+
.q-bar{flex:1;height:5px;border-radius:3px;background:var(--track);overflow:hidden;min-width:36px}
|
|
224
|
+
.q-bar i{display:block;height:100%;border-radius:3px;background:var(--pos)}
|
|
225
|
+
.q-bar i.mid{background:var(--accent)}
|
|
226
|
+
.q-bar i.hot{background:var(--warn)}
|
|
227
|
+
.q-pct{font-size:10.5px;font-weight:700;min-width:34px;text-align:right;flex:none}
|
|
228
|
+
.q-pct.hot{color:var(--warn)}
|
|
229
|
+
.q-abs{color:var(--muted);font-size:9.5px;flex:none}
|
|
230
|
+
.q-bal{display:flex;align-items:baseline;gap:6px;margin-top:4px}
|
|
231
|
+
.q-bal .amt{font-size:14px;font-weight:800}
|
|
232
|
+
.q-bal .amt.low{color:var(--warn)}
|
|
233
|
+
.q-sub{color:var(--muted);font-size:10px;margin-top:2px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
234
|
+
.q-err{color:var(--muted);font-size:10.5px;margin-top:3px}
|
|
235
|
+
.q-empty{color:var(--muted);font-size:11px;padding:6px 0}
|
|
236
|
+
|
|
209
237
|
.p-foot{display:flex;align-items:center;justify-content:space-between;gap:10px;margin-top:8px;padding:10px 14px 12px;border-top:1px solid var(--border)}
|
|
210
238
|
.open-full{display:inline-flex;align-items:center;gap:5px;font-size:12px;font-weight:700;color:var(--info);border:1px solid color-mix(in oklab,var(--info) 30%,var(--tint));border-radius:9px;padding:6px 12px;transition:background .15s var(--ease),border-color .25s var(--ease),color .25s var(--ease)}
|
|
211
239
|
.open-full:hover{background:color-mix(in oklab,var(--info) 10%,var(--tint))}
|
|
@@ -226,6 +254,7 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
226
254
|
<header class="p-head" data-od-id="panel-head">
|
|
227
255
|
<span class="p-logo"><svg viewBox="0 0 24 24"><path d="M13 2L4 14h6l-1 8 9-12h-6l1-8z"/></svg></span>
|
|
228
256
|
<h1>Token Pilot</h1>
|
|
257
|
+
<span class="ver-badge" title="当前版本"></span>
|
|
229
258
|
<span class="st">本地 · 零采集</span>
|
|
230
259
|
<span class="sp"></span>
|
|
231
260
|
<span class="p-date num">10月20日 周二</span>
|
|
@@ -304,6 +333,12 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
304
333
|
<div class="live-more num">另有 1 个空闲会话 · 8 分钟前停更</div>
|
|
305
334
|
</div>
|
|
306
335
|
|
|
336
|
+
<div class="sec-t" data-od-id="quota-title" id="quotaTitle" style="display:none">
|
|
337
|
+
<h2><span class="ic" style="--cc:var(--accent-3)"><svg viewBox="0 0 24 24"><path d="M3 12h4l3-7 4 14 3-7h4"/></svg></span>订阅额度与余额</h2>
|
|
338
|
+
<span class="more">还剩多少</span>
|
|
339
|
+
</div>
|
|
340
|
+
<div class="quota num" data-od-id="quota" id="quotaBox" style="display:none"></div>
|
|
341
|
+
|
|
307
342
|
<div class="sec-t" data-od-id="savings-title">
|
|
308
343
|
<h2><span class="ic" style="--cc:var(--info)"><svg viewBox="0 0 24 24"><path d="M9 18h6M10 21h4M12 3a6 6 0 0 1 4 10c-.8.7-1 1.5-1 2.5h-6c0-1-.2-1.8-1-2.5a6 6 0 0 1 4-10z"/></svg></span>省钱建议</h2>
|
|
309
344
|
<span class="more">事后分析 · 不拦截</span>
|
|
@@ -326,6 +361,8 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
326
361
|
</div>
|
|
327
362
|
</div>
|
|
328
363
|
|
|
364
|
+
|
|
365
|
+
|
|
329
366
|
</div>
|
|
330
367
|
<footer class="p-foot" data-od-id="panel-foot">
|
|
331
368
|
<a class="open-full" href="http://127.0.0.1:17895/">打开完整看板<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M7 17L17 7M9 7h8v8"/></svg></a>
|
|
@@ -343,6 +380,29 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
343
380
|
var T=window.__TAURI__,tauriInvoke=null;
|
|
344
381
|
try{if(T){tauriInvoke=(T.core&&T.core.invoke)||T.invoke||null}}catch(e){tauriInvoke=null}
|
|
345
382
|
|
|
383
|
+
/* ===== 版本号 =====
|
|
384
|
+
来源优先级(高 → 低),因为两边的更新节奏不同:
|
|
385
|
+
1. app_version(Tauri 命令):正在跑的二进制版本,权威
|
|
386
|
+
2. __TP_DATA__.appVersion(数据文件):数据是哪个版本的 CLI 生成的
|
|
387
|
+
3. 都没有就不显示(老数据 + 浏览器打开)
|
|
388
|
+
两个都有且不一致时如实显示「app vX · 数据 vY」——这正是自动更新后
|
|
389
|
+
「二进制升了、后台 sync 还没跑」或反过来时的真实现状,藏起来反而误导。 */
|
|
390
|
+
var APP_VER=null;
|
|
391
|
+
function setVer(){
|
|
392
|
+
var el=document.querySelector(".p-head .ver-badge");
|
|
393
|
+
if(!el)return;
|
|
394
|
+
var app=APP_VER;
|
|
395
|
+
var data=D&&D.appVersion||null;
|
|
396
|
+
var txt="";
|
|
397
|
+
if(app&&data&&app!==data)txt="v"+app+" · 数据 v"+data;
|
|
398
|
+
else if(app||data)txt="v"+(app||data);
|
|
399
|
+
el.textContent=txt;
|
|
400
|
+
el.title=(app&&data&&app!==data)?"当前程序 v"+app+",数据由 v"+data+" 生成":"当前版本";
|
|
401
|
+
}
|
|
402
|
+
if(tauriInvoke){
|
|
403
|
+
try{tauriInvoke("app_version").then(function(v){if(v){APP_VER=String(v);setVer()}},function(){})}catch(e){}
|
|
404
|
+
}
|
|
405
|
+
|
|
346
406
|
/* ===== 主题切换(localStorage 'tp-theme') ===== */
|
|
347
407
|
var root=document.documentElement,btn=$("#themeBtn");
|
|
348
408
|
var panelEl=document.querySelector(".panel");
|
|
@@ -620,6 +680,105 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
620
680
|
}).join("");
|
|
621
681
|
}
|
|
622
682
|
|
|
683
|
+
/* ===== 订阅额度与余额 =====
|
|
684
|
+
面板宽度只有 480,塞不下看板那种「每个窗口一行」的完整表。
|
|
685
|
+
这里每个平台压成一行:显示最紧张的那个窗口,鼠标悬停看全部。
|
|
686
|
+
数据契约与看板完全一致(D.quota.providers),分类口径也一致:
|
|
687
|
+
windows → 订阅额度(会重置)
|
|
688
|
+
tokenPlan → 预付 token 包
|
|
689
|
+
balance → 现金余额
|
|
690
|
+
三者经济含义不同,不能合并算。 */
|
|
691
|
+
var QSRC={api:"官方接口",cli:"官方 CLI",computed:"本地计算",manual:"手动登记"};
|
|
692
|
+
function qResets(iso){
|
|
693
|
+
if(!iso)return "";
|
|
694
|
+
var t=Date.parse(iso);if(isNaN(t))return "";
|
|
695
|
+
var ms=t-Date.now();
|
|
696
|
+
if(ms<=0)return "即将重置";
|
|
697
|
+
var m=Math.floor(ms/60000);
|
|
698
|
+
if(m<60)return m+" 分钟后";
|
|
699
|
+
var h=Math.floor(m/60);
|
|
700
|
+
if(h<24)return h+" 小时后";
|
|
701
|
+
return Math.floor(h/24)+" 天后";
|
|
702
|
+
}
|
|
703
|
+
function qTightest(windows){
|
|
704
|
+
var best=null;
|
|
705
|
+
(windows||[]).forEach(function(w){
|
|
706
|
+
if(!best||(+w.percent||0)>(+best.percent||0))best=w;
|
|
707
|
+
});
|
|
708
|
+
return best;
|
|
709
|
+
}
|
|
710
|
+
function qAll(windows){
|
|
711
|
+
return (windows||[]).map(function(w){
|
|
712
|
+
return w.label+" "+(Math.round((+w.percent||0)*10)/10)+"%"+
|
|
713
|
+
(w.used!=null&&w.total!=null?" ("+w.used+"/"+w.total+")":"")+
|
|
714
|
+
(w.resetsAt?" · "+qResets(w.resetsAt)+"刷新":"");
|
|
715
|
+
}).join("\n");
|
|
716
|
+
}
|
|
717
|
+
function renderQuota(){
|
|
718
|
+
var title=document.getElementById("quotaTitle");
|
|
719
|
+
var box=document.getElementById("quotaBox");
|
|
720
|
+
if(!box||!title)return;
|
|
721
|
+
var q=D&&D.quota;
|
|
722
|
+
var provs=(q&&q.providers)||[];
|
|
723
|
+
if(!provs.length){
|
|
724
|
+
// 与看板同一规则:没有任何可监控平台时整块隐藏。
|
|
725
|
+
// 空标题 +「暂未检测到」只会占位置,用户也做不了什么。
|
|
726
|
+
title.style.display="none";
|
|
727
|
+
box.style.display="none";
|
|
728
|
+
box.innerHTML="";
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
var rows=provs.map(function(p){
|
|
732
|
+
var src='<span class="q-src">'+esc(QSRC[p.source]||p.source||"")+'</span>';
|
|
733
|
+
var head='<div class="q-head"><span class="q-name">'+esc(p.displayName)+'</span>'+src+'</div>';
|
|
734
|
+
|
|
735
|
+
if(!p.ok&&!(p.balance)){
|
|
736
|
+
return '<div class="q-prov">'+head+
|
|
737
|
+
'<div class="q-err">'+esc(p.error||"获取失败")+'</div></div>';
|
|
738
|
+
}
|
|
739
|
+
// 现金余额:直接给数额,低于 20 标红(和看板一致的口径)
|
|
740
|
+
if(p.balance){
|
|
741
|
+
var amt=+p.balance.amount;
|
|
742
|
+
var low=amt<20;
|
|
743
|
+
return '<div class="q-prov">'+head+'<div class="q-bal">'+
|
|
744
|
+
'<span class="amt'+(low?" low":"")+'">'+
|
|
745
|
+
(p.balance.currency==="CNY"?"¥":"$")+amt.toFixed(2)+'</span>'+
|
|
746
|
+
(low?'<span class="q-err" style="margin:0">余额偏低</span>':"")+
|
|
747
|
+
'</div></div>';
|
|
748
|
+
}
|
|
749
|
+
var wins=p.windows||[];
|
|
750
|
+
if(wins.length){
|
|
751
|
+
var t=qTightest(wins);
|
|
752
|
+
var pct=Math.max(0,Math.min(100,+t.percent||0));
|
|
753
|
+
var cls=pct>=90?"hot":(pct>=70?"mid":"");
|
|
754
|
+
var abs=(t.used!=null&&t.total!=null)?'<span class="q-abs">'+t.used+"/"+t.total+'</span>':"";
|
|
755
|
+
var more=wins.length>1?'<span class="q-abs">共 '+wins.length+' 个窗口</span>':"";
|
|
756
|
+
return '<div class="q-prov" title="'+esc(qAll(wins))+'">'+head+
|
|
757
|
+
'<div class="q-win">'+
|
|
758
|
+
'<span class="q-lab" title="'+esc(t.label)+'">最紧张</span>'+
|
|
759
|
+
'<div class="q-bar"><i class="'+cls+'" style="width:'+pct+'%"></i></div>'+
|
|
760
|
+
'<span class="q-pct '+cls+'">'+(Math.round(pct*10)/10)+'%</span>'+
|
|
761
|
+
'</div>'+
|
|
762
|
+
'<div class="q-sub">'+esc(t.label)+
|
|
763
|
+
(abs?" · "+abs:"")+
|
|
764
|
+
(t.resetsAt?" · "+esc(qResets(t.resetsAt))+"后刷新":"")+
|
|
765
|
+
(more?" · "+more:"")+
|
|
766
|
+
'</div>'+
|
|
767
|
+
'</div>';
|
|
768
|
+
}
|
|
769
|
+
var tp=p.tokenPlan;
|
|
770
|
+
if(tp){
|
|
771
|
+
return '<div class="q-prov">'+head+'<div class="q-bal">'+
|
|
772
|
+
'<span class="amt">'+(tp.remainingTokens!=null?fmtTokS(tp.remainingTokens):"—")+'</span>'+
|
|
773
|
+
'<span class="q-abs">剩余 tokens</span></div></div>';
|
|
774
|
+
}
|
|
775
|
+
return '<div class="q-prov">'+head+'<div class="q-err">暂无额度数据</div></div>';
|
|
776
|
+
}).join("");
|
|
777
|
+
title.style.display="";
|
|
778
|
+
box.style.display="";
|
|
779
|
+
box.innerHTML=rows;
|
|
780
|
+
}
|
|
781
|
+
|
|
623
782
|
/* ===== 数据装载:__TP_DATA__ → __TAURI__ → /api/data ===== */
|
|
624
783
|
function renderAll(){
|
|
625
784
|
if(!D)return;
|
|
@@ -633,6 +792,8 @@ body.closed .reopen:hover{color:var(--fg)}
|
|
|
633
792
|
renderTokTrend();
|
|
634
793
|
renderLive();
|
|
635
794
|
renderSavings();
|
|
795
|
+
renderQuota();
|
|
796
|
+
setVer();
|
|
636
797
|
}
|
|
637
798
|
function applyData(d){
|
|
638
799
|
if(!d||typeof d!=="object")return;
|