@anthonyhan/token-pilot 0.4.1 → 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.
@@ -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 = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonyhan/token-pilot",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "你所有 AI 工具的本地账单 + 预算控制器 — 只读本地日志,零入侵,不出本机",
5
5
  "type": "module",
6
6
  "bin": {
@@ -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"><div class="cmp-empty">暂未检测到可监控的平台。</div></div>
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
- if(!box)return;
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
- box.innerHTML='<div class="cmp-empty">暂未检测到可监控的平台。装了 OpenCode / 火山 arkcli / 配置了 API Key 后会自动出现。</div>';
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){
package/web/panel.html CHANGED
@@ -210,6 +210,30 @@ body.closed .reopen:hover{color:var(--fg)}
210
210
  .save-amt.warn{color:var(--warn);background:color-mix(in oklab,var(--warn) 12%,var(--tint))}
211
211
  .save-amt.info{color:var(--info);background:color-mix(in oklab,var(--info) 13%,var(--tint))}
212
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
+
213
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)}
214
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)}
215
239
  .open-full:hover{background:color-mix(in oklab,var(--info) 10%,var(--tint))}
@@ -309,6 +333,12 @@ body.closed .reopen:hover{color:var(--fg)}
309
333
  <div class="live-more num">另有 1 个空闲会话 · 8 分钟前停更</div>
310
334
  </div>
311
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
+
312
342
  <div class="sec-t" data-od-id="savings-title">
313
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>
314
344
  <span class="more">事后分析 · 不拦截</span>
@@ -331,6 +361,8 @@ body.closed .reopen:hover{color:var(--fg)}
331
361
  </div>
332
362
  </div>
333
363
 
364
+
365
+
334
366
  </div>
335
367
  <footer class="p-foot" data-od-id="panel-foot">
336
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>
@@ -648,6 +680,105 @@ body.closed .reopen:hover{color:var(--fg)}
648
680
  }).join("");
649
681
  }
650
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
+
651
782
  /* ===== 数据装载:__TP_DATA__ → __TAURI__ → /api/data ===== */
652
783
  function renderAll(){
653
784
  if(!D)return;
@@ -661,6 +792,7 @@ body.closed .reopen:hover{color:var(--fg)}
661
792
  renderTokTrend();
662
793
  renderLive();
663
794
  renderSavings();
795
+ renderQuota();
664
796
  setVer();
665
797
  }
666
798
  function applyData(d){