@anthonyhan/token-pilot 0.4.2 → 0.4.4

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.
@@ -7,6 +7,43 @@ import { savingsAdvice } from './savings.js';
7
7
  import { computeCompression } from './compression.js';
8
8
  import { buildAdapterTiers, HOST_TIERS } from './registry.js';
9
9
  import { APP_VERSION } from './version.js';
10
+ /**
11
+ * 目录里挑一批「大概率会用到」的模型,给全新用户做默认单价行。
12
+ *
13
+ * 为什么需要:设置面板的模型单价原本只列「近 30 天日志里出现过的模型」,
14
+ * 全新用户没有任何日志,列表就是空的——想改价都不知道该填哪些模型名。
15
+ *
16
+ * 挑选依据:先给各家的旗舰与常用款,覆盖主流宿主默认会用的模型;
17
+ * 不追求全量(44 个全列会淹没面板),也绝不覆盖用户已在用/已覆写的条目。
18
+ */
19
+ const PREFERRED_MODEL_ORDER = [
20
+ 'claude-opus-5-5',
21
+ 'claude-sonnet-5-5',
22
+ 'claude-haiku-5-5',
23
+ 'gpt-5',
24
+ 'gemini-3-pro',
25
+ 'gemini-3-flash',
26
+ 'deepseek-v4-pro',
27
+ 'deepseek-v4.1-flash',
28
+ 'glm-5.3',
29
+ 'glm-5',
30
+ 'kimi-k2',
31
+ 'qwen3-coder',
32
+ ];
33
+ function commonModelNames(pricing) {
34
+ const have = new Set(Object.keys(pricing.models));
35
+ const out = [];
36
+ for (const n of PREFERRED_MODEL_ORDER) {
37
+ if (have.has(n))
38
+ out.push(n);
39
+ }
40
+ // 偏好的都没命中(定价表改名了):退回按表内顺序取前若干个,
41
+ // 保证任何版本的定价表都不会让这块又变空。
42
+ if (out.length === 0) {
43
+ return Object.keys(pricing.models).slice(0, 8);
44
+ }
45
+ return out;
46
+ }
10
47
  const AGENT_NAMES = {
11
48
  'claude-code': 'Claude Code',
12
49
  'kimi-code': 'Kimi Code',
@@ -810,6 +847,18 @@ async function buildSettings(events, cfg, pricing, fx, d30) {
810
847
  if (!names.includes(k))
811
848
  names.push(k);
812
849
  }
850
+ // 全新用户没有任何日志 → names 为空 → 设置面板「本地模型单价」整块空白,
851
+ // 想手工填价都不知道该填哪些模型名。这里补一批目录里的常用模型做默认行,
852
+ // 让用户一打开就能看到价格、直接改,而不是面对空列表。
853
+ // 只补到还缺的位置,不覆盖已从日志里识别出的真实在用模型。
854
+ if (names.length < 12) {
855
+ for (const name of commonModelNames(pricing)) {
856
+ if (names.length >= 12)
857
+ break;
858
+ if (!names.includes(name))
859
+ names.push(name);
860
+ }
861
+ }
813
862
  const models = names.slice(0, 12).map((name) => {
814
863
  const p = priceFor(name, pricing);
815
864
  const cr = p.cacheRead ?? p.input * 0.1;
@@ -0,0 +1,44 @@
1
+ // 把额度查询的底层错误翻译成用户能看懂、能动手的话。
2
+ //
3
+ // 背景:额度查询走 `python3 -c` 发 HTTP 请求,底层异常直接冒到界面上,
4
+ // 用户看到的是
5
+ // <urlopen error [Errno 8] nodename nor servname provided, or not known>
6
+ // 这句话对用户零信息——既没说是什么问题,也没说能怎么办。
7
+ //
8
+ // 这里按错误特征分类。不认识的错误原样返回,不硬套:编造的提示比没有更糟。
9
+ /** 网络/DNS 类:解析不到域名、连不上、超时 */
10
+ const NETWORK_PATTERNS = [
11
+ 'nodename nor servname', // macOS DNS 失败
12
+ 'name or service not known', // Linux DNS 失败
13
+ 'temporary failure in name resolution',
14
+ 'getaddrinfo',
15
+ 'urlopen error',
16
+ 'connection refused',
17
+ 'connection reset',
18
+ 'timed out',
19
+ 'timeout',
20
+ 'network is unreachable',
21
+ 'no route to host',
22
+ 'ssl',
23
+ 'certificate',
24
+ ];
25
+ export function explainQuotaError(raw) {
26
+ const low = raw.toLowerCase();
27
+ if (NETWORK_PATTERNS.some((p) => low.includes(p))) {
28
+ // DNS 失败单独说,因为自建 DNS / 公司网络环境下最常见
29
+ if (low.includes('nodename') || low.includes('name or service') || low.includes('getaddrinfo')) {
30
+ return '域名解析失败,连不上该平台的服务器(DNS 或网络被拦截)。开启系统代理后重试';
31
+ }
32
+ return '网络请求失败,连不上该平台的服务器。检查网络或开启系统代理后重试';
33
+ }
34
+ if (/401|403|unauthorized|forbidden|api key/i.test(raw)) {
35
+ return 'API Key 无效或已过期';
36
+ }
37
+ if (/429|rate limit/i.test(raw)) {
38
+ return '请求过于频繁被限流,稍后重试';
39
+ }
40
+ if (/5\d\d/.test(raw)) {
41
+ return '平台服务端异常,稍后重试';
42
+ }
43
+ return raw;
44
+ }
@@ -14,6 +14,7 @@
14
14
  import { execFile } from 'node:child_process';
15
15
  import { promisify } from 'node:util';
16
16
  import { resolveKey } from '../credentials.js';
17
+ import { explainQuotaError } from '../errors.js';
17
18
  const execFileP = promisify(execFile);
18
19
  async function httpJson(url, key, authHeader = 'bearer') {
19
20
  const script = `
@@ -33,12 +34,13 @@ except Exception as e:
33
34
  try {
34
35
  const { stdout } = await execFileP('python3', ['-c', script, url, key, authHeader], { timeout: 20_000 });
35
36
  const d = JSON.parse(stdout);
37
+ // 底层异常(Python urllib 的原始报错)翻译成人话,别把 traceback 甩给用户
36
38
  if (d.__error)
37
- return { ok: false, error: d.__error };
39
+ return { ok: false, error: explainQuotaError(d.__error) };
38
40
  return { ok: true, data: d };
39
41
  }
40
42
  catch (e) {
41
- return { ok: false, error: String(e).slice(0, 150) };
43
+ return { ok: false, error: explainQuotaError(String(e).slice(0, 150)) };
42
44
  }
43
45
  }
44
46
  /** DeepSeek:现金余额 */
@@ -20,6 +20,7 @@ import os from 'node:os';
20
20
  import path from 'node:path';
21
21
  import { promisify } from 'node:util';
22
22
  import { fromConfig, fromCcSwitch } from '../credentials.js';
23
+ import { explainQuotaError } from '../errors.js';
23
24
  const execFileP = promisify(execFile);
24
25
  const USAGE_URL = 'https://opencode.ai/zen/go/v1/usage';
25
26
  /** 窗口 id → 中文名 + 时长(分钟) */
@@ -102,7 +103,7 @@ export async function fetchOpencodeQuota() {
102
103
  ? '凭证无效或已过期,请在 OpenCode 里重新登录'
103
104
  : /403/.test(res.__error)
104
105
  ? '接口拒绝访问(403)——若刚登录过请重试'
105
- : res.__error;
106
+ : explainQuotaError(res.__error);
106
107
  return { ...base, error: hint };
107
108
  }
108
109
  if (!res.usage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonyhan/token-pilot",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "你所有 AI 工具的本地账单 + 预算控制器 — 只读本地日志,零入侵,不出本机",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1402,14 +1402,15 @@ function renderTok(){
1402
1402
  }
1403
1403
 
1404
1404
  /* ---------- footer ---------- */
1405
- /* 版本号:app_version(正在跑的二进制)优先,退到数据里的 appVersion;
1406
- 两者都有却不一致时如实并列——这能一眼看出「二进制升了、数据还是旧的」。 */
1405
+ /* 版本号:只显示正在运行的二进制版本(app_version 命令)。
1406
+ 数据里的 appVersion 是生成数据的 CLI 版本,两条更新路径节奏不同
1407
+ (npm 装 CLI 常比 app 自动更新快),并列显示会让人误以为出错了。
1408
+ 拿不到 app 版本时(浏览器直接打开看板)才退回数据版本。 */
1407
1409
  var APP_VER=null;
1408
1410
  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 "";
1411
+ if(APP_VER)return "v"+APP_VER;
1412
+ var data=(D&&D.appVersion)||null;
1413
+ return data?"v"+data:"";
1413
1414
  }
1414
1415
  if(inTauri()&&window.__TAURI__.core){
1415
1416
  try{
package/web/panel.html CHANGED
@@ -240,6 +240,19 @@ body.closed .reopen:hover{color:var(--fg)}
240
240
  .open-full:focus-visible{outline:2px solid var(--info);outline-offset:2px}
241
241
  .p-foot .note{color:var(--muted);font-size:10.5px;text-align:right}
242
242
 
243
+ /* 右下角缩放手柄:无边框窗口没有系统边框,用三条斜线示意可拖。
244
+ 放在最上层(z-index)并留出热区,否则会被 footer 的透明区域挡住点不到。 */
245
+ .panel{position:relative}
246
+ .resize-grip{position:absolute;right:2px;bottom:2px;width:16px;height:16px;z-index:5;cursor:nwse-resize;
247
+ display:grid;place-items:center;border-radius:0 0 17px 0;transition:background .15s var(--ease)}
248
+ .resize-grip::after{content:"";width:8px;height:8px;
249
+ background:
250
+ linear-gradient(135deg,transparent 42%,var(--muted) 42%,var(--muted) 52%,transparent 52%),
251
+ linear-gradient(135deg,transparent 66%,var(--muted) 66%,var(--muted) 76%,transparent 76%);
252
+ opacity:.55;transition:opacity .15s var(--ease)}
253
+ .resize-grip:hover{background:color-mix(in oklab,var(--info) 12%,transparent)}
254
+ .resize-grip:hover::after{opacity:1}
255
+
243
256
  @media (max-width:460px){
244
257
  .stage{padding:14px 8px 24px}
245
258
  .kpis{grid-template-columns:repeat(2,1fr)}
@@ -368,6 +381,10 @@ body.closed .reopen:hover{color:var(--fg)}
368
381
  <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>
369
382
  <span class="note">本地运行 · 数据不出本机</span>
370
383
  </footer>
384
+ <!-- 缩放手柄:无边框窗口没有系统边框,只能自己做一个。
385
+ 只放右下角一个角柄:菜单栏弹窗常见需求是「稍微放大看更多」,
386
+ 四条边都放反而容易误触。 -->
387
+ <div class="resize-grip" id="resizeGrip" title="拖动调整大小" aria-label="调整面板大小"></div>
371
388
  </aside>
372
389
  </div>
373
390
 
@@ -381,23 +398,26 @@ body.closed .reopen:hover{color:var(--fg)}
381
398
  try{if(T){tauriInvoke=(T.core&&T.core.invoke)||T.invoke||null}}catch(e){tauriInvoke=null}
382
399
 
383
400
  /* ===== 版本号 =====
384
- 来源优先级(高 → 低),因为两边的更新节奏不同:
401
+ 只显示「当前运行的程序」版本,来源优先级:
385
402
  1. app_version(Tauri 命令):正在跑的二进制版本,权威
386
- 2. __TP_DATA__.appVersion(数据文件):数据是哪个版本的 CLI 生成的
387
- 3. 都没有就不显示(老数据 + 浏览器打开)
388
- 两个都有且不一致时如实显示「app vX · 数据 vY」——这正是自动更新后
389
- 「二进制升了、后台 sync 还没跑」或反过来时的真实现状,藏起来反而误导。 */
403
+ 2. __TP_DATA__.appVersion(数据文件):仅在拿不到 1 时兜底
404
+ (浏览器直接打开面板,没有 Tauri 环境)
405
+ 3. 都没有就不显示
406
+ 刻意不把两者并列。数据文件由 CLI 生成,npm 装 CLI 往往比 app
407
+ 自动更新快,两者不一致是常态;并列显示「v0.4.1 · 数据 v0.4.2」
408
+ 只会让人以为出了问题。 */
390
409
  var APP_VER=null;
391
410
  function setVer(){
392
411
  var el=document.querySelector(".p-head .ver-badge");
393
412
  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);
413
+ // 只显示**当前运行的程序**版本。数据文件里的 appVersion 是生成数据的
414
+ // CLI 的版本,两者节奏不同(npm 装 CLI 比 app 自动更新快是常态),
415
+ // 并列成「v0.4.1 · 数据 v0.4.2」会让人以为哪里坏了——那不是错误,
416
+ // 也不是用户需要关心的信息。只有拿不到 app 版本(浏览器打开面板)时,
417
+ // 才退回用数据版本,避免完全不显示。
418
+ var txt=APP_VER?"v"+APP_VER:(D&&D.appVersion?"v"+D.appVersion:"");
399
419
  el.textContent=txt;
400
- el.title=(app&&data&&app!==data)?"当前程序 v"+app+",数据由 v"+data+" 生成":"当前版本";
420
+ el.title="当前版本";
401
421
  }
402
422
  if(tauriInvoke){
403
423
  try{tauriInvoke("app_version").then(function(v){if(v){APP_VER=String(v);setVer()}},function(){})}catch(e){}
@@ -438,6 +458,30 @@ body.closed .reopen:hover{color:var(--fg)}
438
458
  openFull.setAttribute("href","http://127.0.0.1:17895/");
439
459
  }
440
460
 
461
+ /* ===== 面板缩放 =====
462
+ 无边框窗口(decorations:false)没有系统边框可拖,这里自己实现:
463
+ 按住右下角手柄时调 Rust 的 start_resize_dragging,交给系统做真正的
464
+ 拖拽缩放(比在 JS 里算 setSize 平滑,也没有抖动)。
465
+ 拖完(鼠标松开)把尺寸存进 config.json,下次启动恢复。
466
+ 位置不存:每次打开仍然贴托盘图标,这是菜单栏弹窗该有的行为。 */
467
+ (function initResize(){
468
+ var grip=document.getElementById("resizeGrip");
469
+ if(!grip)return;
470
+ if(!tauriInvoke){grip.style.display="none";return} // 浏览器里预览面板时没有意义
471
+ var dragging=false;
472
+ grip.addEventListener("mousedown",function(e){
473
+ e.preventDefault();
474
+ dragging=true;
475
+ try{tauriInvoke("start_panel_resize",{direction:"se"}).then(function(){},function(){})}catch(x){}
476
+ });
477
+ // 松手就落盘:拖拽期间系统在改尺寸,频繁写盘没意义
478
+ window.addEventListener("mouseup",function(){
479
+ if(!dragging)return;
480
+ dragging=false;
481
+ try{tauriInvoke("save_panel_size_cmd",{width:0,height:0}).then(function(){},function(){})}catch(x){}
482
+ });
483
+ })();
484
+
441
485
  /* ===== 数据到达前的静态 spark 占位 ===== */
442
486
  document.querySelectorAll(".spark[data-spark]").forEach(function(el){
443
487
  var vals=el.dataset.spark.split(",").map(Number);