@anthonyhan/token-pilot 0.2.3 → 0.3.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/README.md +38 -13
- package/bin/token-pilot.js +10 -7
- package/dist/adapters/claude-code.js +251 -0
- package/dist/adapters/codex.js +163 -0
- package/dist/adapters/dsh.js +163 -0
- package/dist/adapters/kimi-code.js +165 -0
- package/dist/adapters/opencode.js +126 -0
- package/dist/adapters/pi.js +119 -0
- package/dist/adapters/qoder.js +51 -0
- package/dist/adapters/types.js +7 -0
- package/dist/adapters/volc-ark.js +92 -0
- package/dist/adapters/workbuddy.js +144 -0
- package/dist/cli/index.js +286 -0
- package/dist/cli/serve.js +211 -0
- package/dist/core/aggregate.js +813 -0
- package/dist/core/alert.js +32 -0
- package/dist/core/autocompact.js +40 -0
- package/dist/core/breaker.js +49 -0
- package/dist/core/budget.js +23 -0
- package/dist/core/collector.js +213 -0
- package/dist/core/compression.js +129 -0
- package/dist/core/config.js +44 -0
- package/dist/core/contract.js +4 -0
- package/dist/core/metrics.js +53 -0
- package/dist/core/price-fetch.js +128 -0
- package/dist/core/pricing.js +156 -0
- package/dist/core/registry.js +144 -0
- package/dist/core/retry-detector.js +41 -0
- package/dist/core/rtk.js +58 -0
- package/dist/core/savings.js +43 -0
- package/dist/core/schema.js +3 -0
- package/dist/core/unknown-models.js +50 -0
- package/package.json +18 -5
- package/web/dashboard.html +98 -4
- package/.github/FUNDING.yml +0 -3
- package/.github/workflows/build-desktop.yml +0 -68
- package/.idea/go.imports.xml +0 -10
- package/.idea/modules.xml +0 -8
- package/.idea/sonarlint/vulnerabilitystore/default/index.pb +0 -0
- package/.idea/thriftCompilerAssist.xml +0 -6
- package/.idea/token-pilot.iml +0 -9
- package/.idea/vcs.xml +0 -6
- package/README-en.md +0 -127
- package/docs/assets/dashboard.png +0 -0
- package/docs/assets/icon.png +0 -0
- package/docs/assets/menubar-panel.png +0 -0
- package/docs/assets/sponsor-alipay.png +0 -0
- package/docs/assets/sponsor-wechat.png +0 -0
- package/llms.txt +0 -40
- package/menubar/package-lock.json +0 -248
- package/menubar/package.json +0 -16
- package/menubar/src/dashboard.html +0 -1266
- package/menubar/src/index.html +0 -674
- package/menubar/src-tauri/Cargo.lock +0 -4963
- package/menubar/src-tauri/Cargo.toml +0 -15
- package/menubar/src-tauri/build.rs +0 -3
- package/menubar/src-tauri/gen/schemas/acl-manifests.json +0 -1
- package/menubar/src-tauri/gen/schemas/capabilities.json +0 -1
- package/menubar/src-tauri/gen/schemas/desktop-schema.json +0 -2526
- package/menubar/src-tauri/gen/schemas/macOS-schema.json +0 -2526
- package/menubar/src-tauri/icons/128x128.png +0 -0
- package/menubar/src-tauri/icons/128x128@2x.png +0 -0
- package/menubar/src-tauri/icons/32x32.png +0 -0
- package/menubar/src-tauri/icons/64x64.png +0 -0
- package/menubar/src-tauri/icons/Square107x107Logo.png +0 -0
- package/menubar/src-tauri/icons/Square142x142Logo.png +0 -0
- package/menubar/src-tauri/icons/Square150x150Logo.png +0 -0
- package/menubar/src-tauri/icons/Square284x284Logo.png +0 -0
- package/menubar/src-tauri/icons/Square30x30Logo.png +0 -0
- package/menubar/src-tauri/icons/Square310x310Logo.png +0 -0
- package/menubar/src-tauri/icons/Square44x44Logo.png +0 -0
- package/menubar/src-tauri/icons/Square71x71Logo.png +0 -0
- package/menubar/src-tauri/icons/Square89x89Logo.png +0 -0
- package/menubar/src-tauri/icons/StoreLogo.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-anydpi-v26/ic_launcher.xml +0 -5
- package/menubar/src-tauri/icons/android/mipmap-hdpi/ic_launcher.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-hdpi/ic_launcher_foreground.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-mdpi/ic_launcher.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-mdpi/ic_launcher_foreground.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xhdpi/ic_launcher.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xhdpi/ic_launcher_foreground.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxhdpi/ic_launcher_foreground.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher_foreground.png +0 -0
- package/menubar/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/menubar/src-tauri/icons/android/values/ic_launcher_background.xml +0 -4
- package/menubar/src-tauri/icons/icon.icns +0 -0
- package/menubar/src-tauri/icons/icon.ico +0 -0
- package/menubar/src-tauri/icons/icon.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-20x20@1x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-20x20@2x-1.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-20x20@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-20x20@3x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-29x29@1x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-29x29@2x-1.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-29x29@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-29x29@3x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-40x40@1x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-40x40@2x-1.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-40x40@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-40x40@3x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-512@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-60x60@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-60x60@3x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-76x76@1x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-76x76@2x.png +0 -0
- package/menubar/src-tauri/icons/ios/AppIcon-83.5x83.5@2x.png +0 -0
- package/menubar/src-tauri/icons/tray-win.png +0 -0
- package/menubar/src-tauri/icons/tray.png +0 -0
- package/menubar/src-tauri/src/main.rs +0 -486
- package/menubar/src-tauri/tauri.conf.json +0 -56
- package/scripts/gen-icons.py +0 -153
- package/scripts/reconcile.py +0 -162
- package/scripts/verify-dashboard.py +0 -101
- package/src/adapters/claude-code.ts +0 -246
- package/src/adapters/codex.ts +0 -166
- package/src/adapters/dsh.ts +0 -156
- package/src/adapters/kimi-code.ts +0 -164
- package/src/adapters/opencode.ts +0 -120
- package/src/adapters/pi.ts +0 -99
- package/src/adapters/qoder.ts +0 -43
- package/src/adapters/types.ts +0 -39
- package/src/adapters/volc-ark.ts +0 -102
- package/src/adapters/workbuddy.ts +0 -135
- package/src/cli/index.ts +0 -294
- package/src/cli/serve.ts +0 -209
- package/src/core/aggregate.ts +0 -821
- package/src/core/alert.ts +0 -33
- package/src/core/autocompact.ts +0 -46
- package/src/core/budget.ts +0 -42
- package/src/core/collector.ts +0 -209
- package/src/core/compression.ts +0 -163
- package/src/core/config.ts +0 -71
- package/src/core/contract.ts +0 -181
- package/src/core/metrics.ts +0 -72
- package/src/core/price-fetch.ts +0 -112
- package/src/core/pricing.ts +0 -182
- package/src/core/registry.ts +0 -141
- package/src/core/retry-detector.ts +0 -64
- package/src/core/rtk.ts +0 -58
- package/src/core/savings.ts +0 -55
- package/src/core/schema.ts +0 -66
- package/src/core/unknown-models.ts +0 -64
- package/tests/adapters-dsh.test.ts +0 -35
- package/tests/adapters-opencode.test.ts +0 -39
- package/tests/adapters-pi.test.ts +0 -33
- package/tests/adapters-workbuddy.test.ts +0 -34
- package/tests/adapters.test.ts +0 -45
- package/tests/aggregate.test.ts +0 -153
- package/tests/compression.test.ts +0 -71
- package/tests/fixtures/claude-session.jsonl +0 -9
- package/tests/fixtures/codex-rollout.jsonl +0 -5
- package/tests/fixtures/kimi-wire.jsonl +0 -6
- package/tests/pricing-timebased.test.ts +0 -51
- package/tests/pricing.test.ts +0 -73
- package/tests/serve.test.ts +0 -29
- package/tsconfig.json +0 -19
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// token-pilot serve — 本地 HTTP 服务(默认 127.0.0.1:17895)
|
|
2
|
+
// 路由:
|
|
3
|
+
// GET / 完整看板(注入当前数据)
|
|
4
|
+
// GET /panel 菜单栏面板预览
|
|
5
|
+
// GET /api/data DashboardData JSON(前端轮询)
|
|
6
|
+
// GET /api/config 当前配置
|
|
7
|
+
// POST /api/config 保存配置(设置弹窗写回)
|
|
8
|
+
// 后台每 10s 增量采集 + 重聚合,同时写 ~/.token-pilot/dashboard-data.json 给 Tauri 菜单栏。
|
|
9
|
+
import http from 'node:http';
|
|
10
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { Collector } from '../core/collector.js';
|
|
15
|
+
import { autoOverrides } from '../core/price-fetch.js';
|
|
16
|
+
import { buildDashboardData } from '../core/aggregate.js';
|
|
17
|
+
import { loadPricingWithOverrides } from '../core/pricing.js';
|
|
18
|
+
import { loadConfig, saveConfig, fxRate, monthlyBudgetCny, DATA_PATH, CONFIG_DIR } from '../core/config.js';
|
|
19
|
+
import { notify } from '../core/alert.js';
|
|
20
|
+
import { applyClaudeAutoCompact } from '../core/autocompact.js';
|
|
21
|
+
import { costOf } from '../core/pricing.js';
|
|
22
|
+
import { detectRetryLoops } from '../core/retry-detector.js';
|
|
23
|
+
import { notifyRetryLoops } from '../core/breaker.js';
|
|
24
|
+
import { installRtk, uninstallRtk, rtkInstalled } from '../core/rtk.js';
|
|
25
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const WEB_DIR = path.resolve(__dirname, '../../web');
|
|
27
|
+
export function readWebAsset(name) {
|
|
28
|
+
const p = path.join(WEB_DIR, name);
|
|
29
|
+
if (!existsSync(p))
|
|
30
|
+
return null;
|
|
31
|
+
return readFileSync(p, 'utf8');
|
|
32
|
+
}
|
|
33
|
+
export function injectData(html, data) {
|
|
34
|
+
const json = JSON.stringify(data).replace(/</g, '\\u003c');
|
|
35
|
+
return html.replace('window.__TP_DATA__=null;', `window.__TP_DATA__=${json};`);
|
|
36
|
+
}
|
|
37
|
+
export function writeDashboardDataFile(data) {
|
|
38
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
39
|
+
writeFileSync(DATA_PATH, JSON.stringify(data), 'utf8');
|
|
40
|
+
}
|
|
41
|
+
export async function refreshOnce(state) {
|
|
42
|
+
const cfg = loadConfig();
|
|
43
|
+
const pricing = loadPricingWithOverrides({ ...autoOverrides(), ...(cfg.priceOverrides ?? {}) }, fxRate(cfg));
|
|
44
|
+
const { events, contentEvents } = await state.collector.sync();
|
|
45
|
+
const data = await buildDashboardData(events, cfg, pricing, contentEvents ?? []);
|
|
46
|
+
state.data = data;
|
|
47
|
+
writeDashboardDataFile(data);
|
|
48
|
+
// 预算告警(每跨一次阈值提醒一次)
|
|
49
|
+
if (data.budget) {
|
|
50
|
+
const threshold = cfg.alertOnPercent ?? 80;
|
|
51
|
+
const pct = data.budget.pct;
|
|
52
|
+
if (pct >= threshold && Math.floor(pct / 10) > Math.floor(state.lastAlertPct / 10)) {
|
|
53
|
+
notify('token-pilot 预算告警', `本月已用 ${pct.toFixed(0)}%(${data.budget.spentCny.toFixed(0)} / ${data.budget.monthlyCny.toFixed(0)} 元)`);
|
|
54
|
+
}
|
|
55
|
+
state.lastAlertPct = pct;
|
|
56
|
+
}
|
|
57
|
+
// 打转熔断:近 7 天检测到打转就主动通知(每会话每天一次)
|
|
58
|
+
try {
|
|
59
|
+
const weekAgo = Date.now() - 7 * 86400_000;
|
|
60
|
+
const weekEvents = (await state.collector.sync()).events.filter((e) => e.timestamp >= weekAgo);
|
|
61
|
+
const alerts = detectRetryLoops(weekEvents, (m, u) => costOf(m, u, pricing), { minCallsPerSession: 30, minCostUsd: 0.3 });
|
|
62
|
+
notifyRetryLoops(alerts);
|
|
63
|
+
}
|
|
64
|
+
catch { /* 不阻塞 */ }
|
|
65
|
+
// 自动压缩提醒(开关开着时):超阈值会话发系统通知,单会话 30min 冷却
|
|
66
|
+
const ac = cfg.autoCompact;
|
|
67
|
+
if (ac?.enabled && data.live.sessions.some((s) => s.needsCompact)) {
|
|
68
|
+
if (!state.compactNotifiedAt)
|
|
69
|
+
state.compactNotifiedAt = new Map();
|
|
70
|
+
const threshold = ac.thresholdTokens ?? 100_000;
|
|
71
|
+
for (const s of data.live.sessions) {
|
|
72
|
+
if (!s.needsCompact)
|
|
73
|
+
continue;
|
|
74
|
+
const last = state.compactNotifiedAt.get(s.key) ?? 0;
|
|
75
|
+
if (Date.now() - last < 30 * 60_000)
|
|
76
|
+
continue;
|
|
77
|
+
state.compactNotifiedAt.set(s.key, Date.now());
|
|
78
|
+
notify('token-pilot · 该压缩上下文了', `${s.agent} 会话上下文 ${Math.round((s.ctxTokens ?? threshold) / 1000)}K tokens,后续每轮都全量重发。到对应窗口跑 /compact 可立即降本。`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return data;
|
|
82
|
+
}
|
|
83
|
+
function readBody(req) {
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
let buf = '';
|
|
86
|
+
req.on('data', (c) => { buf += c; if (buf.length > 1_000_000)
|
|
87
|
+
req.destroy(); });
|
|
88
|
+
req.on('end', () => resolve(buf));
|
|
89
|
+
req.on('error', reject);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export async function serve(port = 17895) {
|
|
93
|
+
const state = { collector: new Collector(), data: null, refreshing: false, lastAlertPct: 0 };
|
|
94
|
+
const refresh = async () => {
|
|
95
|
+
if (state.refreshing)
|
|
96
|
+
return;
|
|
97
|
+
state.refreshing = true;
|
|
98
|
+
try {
|
|
99
|
+
await refreshOnce(state);
|
|
100
|
+
}
|
|
101
|
+
catch { /* 下一轮再来 */ }
|
|
102
|
+
finally {
|
|
103
|
+
state.refreshing = false;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
await refresh();
|
|
107
|
+
setInterval(refresh, 10_000);
|
|
108
|
+
const server = http.createServer(async (req, res) => {
|
|
109
|
+
const url = (req.url ?? '/').split('?')[0];
|
|
110
|
+
try {
|
|
111
|
+
if (url === '/api/data') {
|
|
112
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
113
|
+
res.end(JSON.stringify(state.data ?? {}));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (url === '/api/config' && req.method === 'GET') {
|
|
117
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
118
|
+
res.end(JSON.stringify(loadConfig()));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (url === '/api/config' && req.method === 'POST') {
|
|
122
|
+
const body = await readBody(req);
|
|
123
|
+
const patch = JSON.parse(body);
|
|
124
|
+
const cur = loadConfig();
|
|
125
|
+
const next = { ...cur, ...patch };
|
|
126
|
+
// 用户明示后,写 Claude Code 的 autoCompactEnabled
|
|
127
|
+
if (next.autoCompact?.applyClaudeCode) {
|
|
128
|
+
const r = applyClaudeAutoCompact(next.autoCompact.enabled);
|
|
129
|
+
if (!r.ok) {
|
|
130
|
+
res.writeHead(500, { 'content-type': 'application/json; charset=utf-8' });
|
|
131
|
+
res.end(JSON.stringify({ ok: false, error: r.message }));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// RTK 安装层:勾选且未装 → 装;取消勾选且已装 → 卸
|
|
136
|
+
if (next.autoCompact && typeof next.autoCompact.installRtk === 'boolean') {
|
|
137
|
+
const installed = await rtkInstalled();
|
|
138
|
+
if (next.autoCompact.installRtk && !installed) {
|
|
139
|
+
const r = await installRtk();
|
|
140
|
+
if (!r.ok) {
|
|
141
|
+
res.writeHead(500, { 'content-type': 'application/json; charset=utf-8' });
|
|
142
|
+
res.end(JSON.stringify({ ok: false, error: r.message }));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else if (!next.autoCompact.installRtk && installed) {
|
|
147
|
+
await uninstallRtk();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
saveConfig(next);
|
|
151
|
+
await refresh();
|
|
152
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
153
|
+
res.end(JSON.stringify({ ok: true }));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (url === '/' || url === '/index.html') {
|
|
157
|
+
const html = readWebAsset('dashboard.html');
|
|
158
|
+
if (!html || !state.data) {
|
|
159
|
+
res.writeHead(503, { 'content-type': 'text/plain; charset=utf-8' });
|
|
160
|
+
res.end('看板资源未就绪');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
164
|
+
res.end(injectData(html, state.data));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (url === '/panel') {
|
|
168
|
+
const html = readWebAsset('panel.html');
|
|
169
|
+
if (!html || !state.data) {
|
|
170
|
+
res.writeHead(503, { 'content-type': 'text/plain; charset=utf-8' });
|
|
171
|
+
res.end('面板资源未就绪');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
175
|
+
res.end(injectData(html, state.data));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
res.writeHead(404);
|
|
179
|
+
res.end('not found');
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
res.writeHead(500);
|
|
183
|
+
res.end(String(e));
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
await new Promise((resolve) => server.listen(port, '127.0.0.1', resolve));
|
|
187
|
+
console.log(`token-pilot serve · http://127.0.0.1:${port}(数据不出本机)`);
|
|
188
|
+
console.log(`菜单栏面板预览:http://127.0.0.1:${port}/panel`);
|
|
189
|
+
// 自动打开看板
|
|
190
|
+
try {
|
|
191
|
+
const opener = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
|
|
192
|
+
const args = process.platform === 'win32' ? ['/c', 'start', `http://127.0.0.1:${port}`] : [`http://127.0.0.1:${port}`];
|
|
193
|
+
spawn(opener, args, { detached: true, stdio: 'ignore' }).unref();
|
|
194
|
+
}
|
|
195
|
+
catch { /* 手动打开即可 */ }
|
|
196
|
+
}
|
|
197
|
+
// dash 命令:静态 bake 一份带数据的 HTML(不起服务)
|
|
198
|
+
export async function bakeDashboard() {
|
|
199
|
+
const state = { collector: new Collector(), data: null, refreshing: false, lastAlertPct: 0 };
|
|
200
|
+
const data = await refreshOnce(state);
|
|
201
|
+
const html = readWebAsset('dashboard.html');
|
|
202
|
+
if (!html)
|
|
203
|
+
throw new Error('web/dashboard.html 不存在');
|
|
204
|
+
const out = path.join(CONFIG_DIR, 'dashboard.html');
|
|
205
|
+
writeFileSync(out, injectData(html, data), 'utf8');
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
export function budgetLine(cfg) {
|
|
209
|
+
const b = monthlyBudgetCny(cfg);
|
|
210
|
+
return b ? `¥${b}` : '未设置';
|
|
211
|
+
}
|