@anthonyhan/token-pilot 0.4.0 → 0.4.1

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 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);
@@ -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()]}`,
@@ -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() {
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonyhan/token-pilot",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "你所有 AI 工具的本地账单 + 预算控制器 — 只读本地日志,零入侵,不出本机",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1397,11 +1397,28 @@ function renderTok(){
1397
1397
  }
1398
1398
 
1399
1399
  /* ---------- footer ---------- */
1400
+ /* 版本号:app_version(正在跑的二进制)优先,退到数据里的 appVersion;
1401
+ 两者都有却不一致时如实并列——这能一眼看出「二进制升了、数据还是旧的」。 */
1402
+ var APP_VER=null;
1403
+ function verText(){
1404
+ var app=APP_VER, data=(D&&D.appVersion)||null;
1405
+ if(app&&data&&app!==data)return "v"+app+" · 数据 v"+data;
1406
+ if(app||data)return "v"+(app||data);
1407
+ return "";
1408
+ }
1409
+ if(inTauri()&&window.__TAURI__.core){
1410
+ try{
1411
+ window.__TAURI__.core.invoke('app_version').then(function(v){
1412
+ if(v){APP_VER=String(v);if(D)renderFooter()}
1413
+ }).catch(function(){});
1414
+ }catch(e){}
1415
+ }
1400
1416
  function renderFooter(){
1401
1417
  var on=0;
1402
1418
  (D.adapters||[]).forEach(function(t){(t.chips||[]).forEach(function(c){if(c.status==="on")on++})});
1403
1419
  var t=D.generatedAt?new Date(D.generatedAt):new Date();
1404
- document.querySelector("footer .notes").textContent="数据更新于 "+p2(t.getHours())+":"+p2(t.getMinutes())+":"+p2(t.getSeconds())+" · "+on+" 个数据源接入 · 价格为公开目录价估算 · 数据不出本机";
1420
+ var v=verText();
1421
+ document.querySelector("footer .notes").textContent="数据更新于 "+p2(t.getHours())+":"+p2(t.getMinutes())+":"+p2(t.getSeconds())+" · "+on+" 个数据源接入 · 价格为公开目录价估算 · 数据不出本机"+(v?" · "+v:"");
1405
1422
  }
1406
1423
 
1407
1424
  /* ---------- 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}
@@ -226,6 +230,7 @@ body.closed .reopen:hover{color:var(--fg)}
226
230
  <header class="p-head" data-od-id="panel-head">
227
231
  <span class="p-logo"><svg viewBox="0 0 24 24"><path d="M13 2L4 14h6l-1 8 9-12h-6l1-8z"/></svg></span>
228
232
  <h1>Token Pilot</h1>
233
+ <span class="ver-badge" title="当前版本"></span>
229
234
  <span class="st">本地 · 零采集</span>
230
235
  <span class="sp"></span>
231
236
  <span class="p-date num">10月20日 周二</span>
@@ -343,6 +348,29 @@ body.closed .reopen:hover{color:var(--fg)}
343
348
  var T=window.__TAURI__,tauriInvoke=null;
344
349
  try{if(T){tauriInvoke=(T.core&&T.core.invoke)||T.invoke||null}}catch(e){tauriInvoke=null}
345
350
 
351
+ /* ===== 版本号 =====
352
+ 来源优先级(高 → 低),因为两边的更新节奏不同:
353
+ 1. app_version(Tauri 命令):正在跑的二进制版本,权威
354
+ 2. __TP_DATA__.appVersion(数据文件):数据是哪个版本的 CLI 生成的
355
+ 3. 都没有就不显示(老数据 + 浏览器打开)
356
+ 两个都有且不一致时如实显示「app vX · 数据 vY」——这正是自动更新后
357
+ 「二进制升了、后台 sync 还没跑」或反过来时的真实现状,藏起来反而误导。 */
358
+ var APP_VER=null;
359
+ function setVer(){
360
+ var el=document.querySelector(".p-head .ver-badge");
361
+ if(!el)return;
362
+ var app=APP_VER;
363
+ var data=D&&D.appVersion||null;
364
+ var txt="";
365
+ if(app&&data&&app!==data)txt="v"+app+" · 数据 v"+data;
366
+ else if(app||data)txt="v"+(app||data);
367
+ el.textContent=txt;
368
+ el.title=(app&&data&&app!==data)?"当前程序 v"+app+",数据由 v"+data+" 生成":"当前版本";
369
+ }
370
+ if(tauriInvoke){
371
+ try{tauriInvoke("app_version").then(function(v){if(v){APP_VER=String(v);setVer()}},function(){})}catch(e){}
372
+ }
373
+
346
374
  /* ===== 主题切换(localStorage 'tp-theme') ===== */
347
375
  var root=document.documentElement,btn=$("#themeBtn");
348
376
  var panelEl=document.querySelector(".panel");
@@ -633,6 +661,7 @@ body.closed .reopen:hover{color:var(--fg)}
633
661
  renderTokTrend();
634
662
  renderLive();
635
663
  renderSavings();
664
+ setVer();
636
665
  }
637
666
  function applyData(d){
638
667
  if(!d||typeof d!=="object")return;