@anthonyhan/token-pilot 0.0.0-stage → 0.2.3
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/.github/FUNDING.yml +3 -0
- package/.github/workflows/build-desktop.yml +68 -0
- package/.idea/go.imports.xml +10 -0
- package/.idea/modules.xml +8 -0
- package/.idea/sonarlint/vulnerabilitystore/default/index.pb +0 -0
- package/.idea/thriftCompilerAssist.xml +6 -0
- package/.idea/token-pilot.iml +9 -0
- package/.idea/vcs.xml +6 -0
- package/README-en.md +127 -0
- package/README.md +146 -2
- package/bin/token-pilot.js +15 -0
- 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 +40 -0
- package/menubar/package-lock.json +248 -0
- package/menubar/package.json +16 -0
- package/menubar/src/dashboard.html +1266 -0
- package/menubar/src/index.html +674 -0
- package/menubar/src-tauri/Cargo.lock +4963 -0
- package/menubar/src-tauri/Cargo.toml +15 -0
- package/menubar/src-tauri/build.rs +3 -0
- package/menubar/src-tauri/gen/schemas/acl-manifests.json +1 -0
- package/menubar/src-tauri/gen/schemas/capabilities.json +1 -0
- package/menubar/src-tauri/gen/schemas/desktop-schema.json +2526 -0
- package/menubar/src-tauri/gen/schemas/macOS-schema.json +2526 -0
- 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 +5 -0
- 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 +4 -0
- 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 +486 -0
- package/menubar/src-tauri/tauri.conf.json +56 -0
- package/package.json +20 -3
- package/pricing/models.json +311 -0
- package/scripts/gen-icons.py +153 -0
- package/scripts/reconcile.py +162 -0
- package/scripts/verify-dashboard.py +101 -0
- package/src/adapters/claude-code.ts +246 -0
- package/src/adapters/codex.ts +166 -0
- package/src/adapters/dsh.ts +156 -0
- package/src/adapters/kimi-code.ts +164 -0
- package/src/adapters/opencode.ts +120 -0
- package/src/adapters/pi.ts +99 -0
- package/src/adapters/qoder.ts +43 -0
- package/src/adapters/types.ts +39 -0
- package/src/adapters/volc-ark.ts +102 -0
- package/src/adapters/workbuddy.ts +135 -0
- package/src/cli/index.ts +294 -0
- package/src/cli/serve.ts +209 -0
- package/src/core/aggregate.ts +821 -0
- package/src/core/alert.ts +33 -0
- package/src/core/autocompact.ts +46 -0
- package/src/core/budget.ts +42 -0
- package/src/core/collector.ts +209 -0
- package/src/core/compression.ts +163 -0
- package/src/core/config.ts +71 -0
- package/src/core/contract.ts +181 -0
- package/src/core/metrics.ts +72 -0
- package/src/core/price-fetch.ts +112 -0
- package/src/core/pricing.ts +182 -0
- package/src/core/registry.ts +141 -0
- package/src/core/retry-detector.ts +64 -0
- package/src/core/rtk.ts +58 -0
- package/src/core/savings.ts +55 -0
- package/src/core/schema.ts +66 -0
- package/src/core/unknown-models.ts +64 -0
- package/tests/adapters-dsh.test.ts +35 -0
- package/tests/adapters-opencode.test.ts +39 -0
- package/tests/adapters-pi.test.ts +33 -0
- package/tests/adapters-workbuddy.test.ts +34 -0
- package/tests/adapters.test.ts +45 -0
- package/tests/aggregate.test.ts +153 -0
- package/tests/compression.test.ts +71 -0
- package/tests/fixtures/claude-session.jsonl +9 -0
- package/tests/fixtures/codex-rollout.jsonl +5 -0
- package/tests/fixtures/kimi-wire.jsonl +6 -0
- package/tests/pricing-timebased.test.ts +51 -0
- package/tests/pricing.test.ts +73 -0
- package/tests/serve.test.ts +29 -0
- package/tsconfig.json +19 -0
- package/web/dashboard.html +1266 -0
- package/web/panel.html +674 -0
package/src/cli/serve.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
|
|
10
|
+
import http from 'node:http';
|
|
11
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { spawn } from 'node:child_process';
|
|
15
|
+
import { Collector } from '../core/collector.ts';
|
|
16
|
+
import { autoOverrides } from '../core/price-fetch.ts';
|
|
17
|
+
import { buildDashboardData } from '../core/aggregate.ts';
|
|
18
|
+
import { loadPricingWithOverrides } from '../core/pricing.ts';
|
|
19
|
+
import { loadConfig, saveConfig, fxRate, monthlyBudgetCny, DATA_PATH, CONFIG_DIR } from '../core/config.ts';
|
|
20
|
+
import { notify } from '../core/alert.ts';
|
|
21
|
+
import { applyClaudeAutoCompact } from '../core/autocompact.ts';
|
|
22
|
+
import { installRtk, uninstallRtk, rtkInstalled } from '../core/rtk.ts';
|
|
23
|
+
import type { DashboardData } from '../core/contract.ts';
|
|
24
|
+
import type { TokenPilotConfig } from '../core/config.ts';
|
|
25
|
+
|
|
26
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
27
|
+
const WEB_DIR = path.resolve(__dirname, '../../web');
|
|
28
|
+
|
|
29
|
+
export function readWebAsset(name: string): string | null {
|
|
30
|
+
const p = path.join(WEB_DIR, name);
|
|
31
|
+
if (!existsSync(p)) return null;
|
|
32
|
+
return readFileSync(p, 'utf8');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function injectData(html: string, data: DashboardData): string {
|
|
36
|
+
const json = JSON.stringify(data).replace(/</g, '\\u003c');
|
|
37
|
+
return html.replace('window.__TP_DATA__=null;', `window.__TP_DATA__=${json};`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function writeDashboardDataFile(data: DashboardData): void {
|
|
41
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
42
|
+
writeFileSync(DATA_PATH, JSON.stringify(data), 'utf8');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ServeState {
|
|
46
|
+
collector: Collector;
|
|
47
|
+
data: DashboardData | null;
|
|
48
|
+
refreshing: boolean;
|
|
49
|
+
lastAlertPct: number;
|
|
50
|
+
compactNotifiedAt?: Map<string, number>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function refreshOnce(state: ServeState): Promise<DashboardData> {
|
|
54
|
+
const cfg = loadConfig();
|
|
55
|
+
const pricing = loadPricingWithOverrides({ ...autoOverrides(), ...(cfg.priceOverrides ?? {}) }, fxRate(cfg));
|
|
56
|
+
const { events, contentEvents } = await state.collector.sync();
|
|
57
|
+
const data = await buildDashboardData(events, cfg, pricing, contentEvents ?? []);
|
|
58
|
+
state.data = data;
|
|
59
|
+
writeDashboardDataFile(data);
|
|
60
|
+
// 预算告警(每跨一次阈值提醒一次)
|
|
61
|
+
if (data.budget) {
|
|
62
|
+
const threshold = cfg.alertOnPercent ?? 80;
|
|
63
|
+
const pct = data.budget.pct;
|
|
64
|
+
if (pct >= threshold && Math.floor(pct / 10) > Math.floor(state.lastAlertPct / 10)) {
|
|
65
|
+
notify('token-pilot 预算告警', `本月已用 ${pct.toFixed(0)}%(${data.budget.spentCny.toFixed(0)} / ${data.budget.monthlyCny.toFixed(0)} 元)`);
|
|
66
|
+
}
|
|
67
|
+
state.lastAlertPct = pct;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 自动压缩提醒(开关开着时):超阈值会话发系统通知,单会话 30min 冷却
|
|
71
|
+
const ac = cfg.autoCompact;
|
|
72
|
+
if (ac?.enabled && data.live.sessions.some((s) => s.needsCompact)) {
|
|
73
|
+
if (!state.compactNotifiedAt) state.compactNotifiedAt = new Map();
|
|
74
|
+
const threshold = ac.thresholdTokens ?? 100_000;
|
|
75
|
+
for (const s of data.live.sessions) {
|
|
76
|
+
if (!s.needsCompact) continue;
|
|
77
|
+
const last = state.compactNotifiedAt.get(s.key) ?? 0;
|
|
78
|
+
if (Date.now() - last < 30 * 60_000) continue;
|
|
79
|
+
state.compactNotifiedAt.set(s.key, Date.now());
|
|
80
|
+
notify(
|
|
81
|
+
'token-pilot · 该压缩上下文了',
|
|
82
|
+
`${s.agent} 会话上下文 ${Math.round((s.ctxTokens ?? threshold) / 1000)}K tokens,后续每轮都全量重发。到对应窗口跑 /compact 可立即降本。`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return data;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function readBody(req: http.IncomingMessage): Promise<string> {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
let buf = '';
|
|
92
|
+
req.on('data', (c) => { buf += c; if (buf.length > 1_000_000) req.destroy(); });
|
|
93
|
+
req.on('end', () => resolve(buf));
|
|
94
|
+
req.on('error', reject);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function serve(port = 17895): Promise<void> {
|
|
99
|
+
const state: ServeState = { collector: new Collector(), data: null, refreshing: false, lastAlertPct: 0 };
|
|
100
|
+
|
|
101
|
+
const refresh = async () => {
|
|
102
|
+
if (state.refreshing) return;
|
|
103
|
+
state.refreshing = true;
|
|
104
|
+
try { await refreshOnce(state); } catch { /* 下一轮再来 */ } finally { state.refreshing = false; }
|
|
105
|
+
};
|
|
106
|
+
await refresh();
|
|
107
|
+
setInterval(refresh, 10_000);
|
|
108
|
+
|
|
109
|
+
const server = http.createServer(async (req, res) => {
|
|
110
|
+
const url = (req.url ?? '/').split('?')[0];
|
|
111
|
+
try {
|
|
112
|
+
if (url === '/api/data') {
|
|
113
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
114
|
+
res.end(JSON.stringify(state.data ?? {}));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (url === '/api/config' && req.method === 'GET') {
|
|
118
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
119
|
+
res.end(JSON.stringify(loadConfig()));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (url === '/api/config' && req.method === 'POST') {
|
|
123
|
+
const body = await readBody(req);
|
|
124
|
+
const patch = JSON.parse(body) as Partial<TokenPilotConfig>;
|
|
125
|
+
const cur = loadConfig();
|
|
126
|
+
const next: TokenPilotConfig = { ...cur, ...patch };
|
|
127
|
+
// 用户明示后,写 Claude Code 的 autoCompactEnabled
|
|
128
|
+
if (next.autoCompact?.applyClaudeCode) {
|
|
129
|
+
const r = applyClaudeAutoCompact(next.autoCompact.enabled);
|
|
130
|
+
if (!r.ok) {
|
|
131
|
+
res.writeHead(500, { 'content-type': 'application/json; charset=utf-8' });
|
|
132
|
+
res.end(JSON.stringify({ ok: false, error: r.message }));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// RTK 安装层:勾选且未装 → 装;取消勾选且已装 → 卸
|
|
137
|
+
if (next.autoCompact && typeof next.autoCompact.installRtk === 'boolean') {
|
|
138
|
+
const installed = await rtkInstalled();
|
|
139
|
+
if (next.autoCompact.installRtk && !installed) {
|
|
140
|
+
const r = await installRtk();
|
|
141
|
+
if (!r.ok) {
|
|
142
|
+
res.writeHead(500, { 'content-type': 'application/json; charset=utf-8' });
|
|
143
|
+
res.end(JSON.stringify({ ok: false, error: r.message }));
|
|
144
|
+
return;
|
|
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); res.end('not found');
|
|
179
|
+
} catch (e) {
|
|
180
|
+
res.writeHead(500); res.end(String(e));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await new Promise<void>((resolve) => server.listen(port, '127.0.0.1', resolve));
|
|
185
|
+
console.log(`token-pilot serve · http://127.0.0.1:${port}(数据不出本机)`);
|
|
186
|
+
console.log(`菜单栏面板预览:http://127.0.0.1:${port}/panel`);
|
|
187
|
+
// 自动打开看板
|
|
188
|
+
try {
|
|
189
|
+
const opener = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
|
|
190
|
+
const args = process.platform === 'win32' ? ['/c', 'start', `http://127.0.0.1:${port}`] : [`http://127.0.0.1:${port}`];
|
|
191
|
+
spawn(opener, args, { detached: true, stdio: 'ignore' }).unref();
|
|
192
|
+
} catch { /* 手动打开即可 */ }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// dash 命令:静态 bake 一份带数据的 HTML(不起服务)
|
|
196
|
+
export async function bakeDashboard(): Promise<string> {
|
|
197
|
+
const state: ServeState = { collector: new Collector(), data: null, refreshing: false, lastAlertPct: 0 };
|
|
198
|
+
const data = await refreshOnce(state);
|
|
199
|
+
const html = readWebAsset('dashboard.html');
|
|
200
|
+
if (!html) throw new Error('web/dashboard.html 不存在');
|
|
201
|
+
const out = path.join(CONFIG_DIR, 'dashboard.html');
|
|
202
|
+
writeFileSync(out, injectData(html, data), 'utf8');
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function budgetLine(cfg: TokenPilotConfig): string {
|
|
207
|
+
const b = monthlyBudgetCny(cfg);
|
|
208
|
+
return b ? `¥${b}` : '未设置';
|
|
209
|
+
}
|