@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
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
[](LICENSE)
|
|
12
12
|
[](#-状态栏应用)
|
|
13
|
-
[](tests/)
|
|
14
14
|
[](#-数据准确性)
|
|
15
15
|
|
|
16
16
|
[快速开始](#-快速开始) · [状态栏应用](#-状态栏应用) · [数据准确性](#-数据准确性) · [什么时候别用它](#-什么时候别用它) · [TODO](#-todo-list) · [English](README-en.md) · [llms.txt](llms.txt)
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
|
|
24
24
|
> 状态栏常驻 `⚡ ¥今日 · 预算%`。左键弹面板,「打开完整看板」看全部。我的真实 10 月:**810M tokens · ¥2,546 · 12,685 次调用**([怎么算的](#-数据准确性))。
|
|
25
25
|
|
|
26
|
-
> ⚠️
|
|
26
|
+
> ⚠️ npm 上另有一个**别人家的** `token-pilot`(MCP 代码导航工具,会往 Claude Code 写 hooks)。我们的包是 **`@anthonyhan/token-pilot`**,下面命令都带 scope,照抄即可。
|
|
27
27
|
|
|
28
28
|
## 🚀 快速开始
|
|
29
29
|
|
|
@@ -40,9 +40,25 @@ token-pilot month # 本月账单 + 月底预测 + 打转检测 + 省钱建
|
|
|
40
40
|
|
|
41
41
|
### 状态栏应用
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
安装启动后**只在状态栏显示一个闪电图标**——无 Dock 图标、无启动窗口;左键弹面板,右键出菜单。
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
**macOS(Apple Silicon / Intel)**:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# 方式一:Homebrew(推荐,自动处理 Gatekeeper,装完即用)
|
|
49
|
+
brew install --cask anthonyhann/token-pilot/token-pilot
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# 方式二:Releases 手动下载 .dmg,拖入「应用程序」后跑一次(解决未签名的「已损坏」误报)
|
|
54
|
+
xattr -dr com.apple.quarantine /Applications/token-pilot.app
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Windows**:Releases 下载 `.msi`,SmartScreen 选「仍要运行」。
|
|
58
|
+
|
|
59
|
+
> 长期解:Apple 开发者签名+公证($99/年)在 TODO 里,有赞助再开。
|
|
60
|
+
|
|
61
|
+
自己构建:`cd menubar && npm install && npx tauri build`
|
|
46
62
|
|
|
47
63
|
## ✨ 它能做什么
|
|
48
64
|
|
|
@@ -53,6 +69,7 @@ token-pilot month # 本月账单 + 月底预测 + 打转检测 + 省钱建
|
|
|
53
69
|
| Agent 打转 | 不用说话 | 自动揪出「单会话 ≥30 次调用」的死循环,算出浪费金额 |
|
|
54
70
|
| 想省钱 | 看「省钱建议」卡 | 贵模型干轻活 → 迁移建议+每月省多少;低缓存命中 → 优化空间;本月缓存帮你省了多少 |
|
|
55
71
|
| 上下文太长 | 自动提醒 / 点徽标 | 会话超 100K 上下文标记「该压缩」,可写 Claude Code autoCompact / 一键装 RTK 输出压缩层 |
|
|
72
|
+
| 想知道能省多少 | 看「压缩潜力」板块 | 本月陈旧输出/重复读取/超长输出各浪费多少钱,按模式拆分 + 列出最浪费的 5 个会话 |
|
|
56
73
|
| 核对价格 | `token-pilot prices` | 每个在用模型的单价来源(目录价/人工覆写/自动补价待核对) |
|
|
57
74
|
| 谁在烧 | 看「宿主/模型/项目榜」 | 哪个工具、哪个模型、哪个项目花最多,一目了然 |
|
|
58
75
|
| 国产模型 | 不用配置 | DeepSeek/GLM/Kimi/Qwen 走宿主日志 model 字段自动归一 |
|
|
@@ -66,7 +83,7 @@ token-pilot month # 本月账单 + 月底预测 + 打转检测 + 省钱建
|
|
|
66
83
|
```bash
|
|
67
84
|
python3 scripts/reconcile.py month # 第三方视角独立重算原始日志,与 CLI 逐项对账
|
|
68
85
|
python3 scripts/verify-dashboard.py # 看板 36 项算法不变量校验
|
|
69
|
-
npm test #
|
|
86
|
+
npm test # 42 个单元测试(定价/采集口径/聚合/注入/熔断)
|
|
70
87
|
```
|
|
71
88
|
|
|
72
89
|
实测修过的口径坑(2026-10-21,本机 4787 个日志文件):
|
|
@@ -80,7 +97,7 @@ npm test # 40 个单元测试(定价/采集口径
|
|
|
80
97
|
## 📊 凭什么信我
|
|
81
98
|
|
|
82
99
|
- 我自己的 AI 账单就是它管的:本月 **¥2,546 / 810M tokens**(2026-10-21,上方截图即本机实时数据)
|
|
83
|
-
- 36 项看板算法不变量 +
|
|
100
|
+
- 36 项看板算法不变量 + 42 个单测 + 独立对账脚本,全部开箱可跑
|
|
84
101
|
- 诚实披露速率口径:日志按「调用完成时刻」记账,生成中的调用完成前不计入(看板内有注明)
|
|
85
102
|
|
|
86
103
|
## ⚠️ 什么时候别用它
|
|
@@ -109,7 +126,14 @@ token-pilot 免费且 MIT。如果它帮你省下了 AI 账单,可以请我喝
|
|
|
109
126
|
|
|
110
127
|
## 📝 更新日志
|
|
111
128
|
|
|
112
|
-
|
|
129
|
+
完整分版本说明见 [RELEASE_NOTES.md](RELEASE_NOTES.md)(也是客户端自动更新弹窗的文案来源)。
|
|
130
|
+
|
|
131
|
+
- **v0.2.6** 激活自动更新链路 + 打转熔断主动通知(含防轰炸)
|
|
132
|
+
- **v0.2.5** npm scope 校正为 `@anthonyhann`,包可运行性修复
|
|
133
|
+
- **v0.2.3** 2B 压缩执行第一层(自动压缩 + autoCompact 写入 + RTK)、价格核对台
|
|
134
|
+
- **v0.2.1** 定价表对齐 8 家官方目录价、DeepSeek 分时、面板布局修复
|
|
135
|
+
- **v0.2.0** 压缩潜力量化、9 源采集覆盖、三处口径修复
|
|
136
|
+
- **v0.1.0** 首版 → CLI 账单 + 本地看板 + macOS/Windows 状态栏应用,三形态一次给齐
|
|
113
137
|
|
|
114
138
|
## ✅ TODO LIST
|
|
115
139
|
|
|
@@ -124,16 +148,17 @@ token-pilot 免费且 MIT。如果它帮你省下了 AI 账单,可以请我喝
|
|
|
124
148
|
**节流(Step 2)**
|
|
125
149
|
- [x] 2A 压缩潜力量化(规则引擎:陈旧输出/重复读取/超长输出 → 本月可省多少,实测 $63-95)
|
|
126
150
|
- [x] 2B 压缩执行第一层:自动压缩提醒 + Claude autoCompact 写入 + RTK 一键安装
|
|
151
|
+
- [x] 异常重试自动熔断提醒(`breaker.ts`:单会话 ≥30 次调用即主动通知,每轮最多提 1 个防首跑轰炸)
|
|
127
152
|
- [ ] 2B 压缩执行第二层:代理网关(LiteLLM/自研,视数据再定)
|
|
128
153
|
- [ ] 低价模型路由(本地代理层,可选开启,默认不动你任何配置)
|
|
129
|
-
- [ ] 异常重试自动熔断提醒(已有检测,加主动通知策略)
|
|
130
154
|
|
|
131
155
|
**平台与工程**
|
|
132
|
-
- [x] macOS 状态栏应用(arm64 .app/.dmg)
|
|
133
|
-
- [x] Windows 核查 + 分平台图标 + CI
|
|
134
|
-
- [
|
|
135
|
-
- [
|
|
136
|
-
- [ ]
|
|
156
|
+
- [x] macOS 状态栏应用(arm64 + Intel x64 双架构 .app/.dmg)
|
|
157
|
+
- [x] Windows 核查 + 分平台图标 + CI(`.msi` 由 Actions 产出)
|
|
158
|
+
- [x] 自动更新(tauri-updater,v0.2.6 起生效,三平台共用一份 `latest.json`)
|
|
159
|
+
- [x] CI 自动发布:打 tag 后三平台产物 + 合并版 `latest.json` 自动进 Release,并回写 brew cask
|
|
160
|
+
- [ ] Windows `.msi` 实机安装验证(目前只验证到 Actions 产物层)
|
|
161
|
+
- [ ] 统一二进制(universal2 单包替代 arm64/x64 两个 dmg)
|
|
137
162
|
- [ ] 签名与公证(Apple Developer $99/年,有赞助再开)
|
|
138
163
|
|
|
139
164
|
## English
|
package/bin/token-pilot.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// token-pilot
|
|
2
|
+
// token-pilot 全局入口:优先跑编译产物 dist/(npm 包形态);仓库内开发回退 tsx
|
|
3
3
|
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
4
5
|
import path from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
6
7
|
|
|
7
8
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
-
const
|
|
9
|
+
const distEntry = path.resolve(__dirname, '../dist/cli/index.js');
|
|
10
|
+
const srcEntry = path.resolve(__dirname, '../src/cli/index.ts');
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
);
|
|
12
|
+
let r;
|
|
13
|
+
if (existsSync(distEntry)) {
|
|
14
|
+
r = spawnSync(process.execPath, [distEntry, ...process.argv.slice(2)], { stdio: 'inherit' });
|
|
15
|
+
} else {
|
|
16
|
+
r = spawnSync('npx', ['tsx', srcEntry, ...process.argv.slice(2)], { stdio: 'inherit', shell: process.platform === 'win32' });
|
|
17
|
+
}
|
|
15
18
|
process.exit(r.status ?? 0);
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Claude Code adapter — 只读解析 ~/.claude/projects/**/*.jsonl
|
|
2
|
+
// 参考 ccusage 路线:不注入 hook,不改配置,纯只读
|
|
3
|
+
import { promises as fs } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
const HOME = os.homedir();
|
|
7
|
+
const PROJECTS_DIR = path.join(HOME, '.claude', 'projects');
|
|
8
|
+
async function* walkJsonl(dir) {
|
|
9
|
+
let entries;
|
|
10
|
+
try {
|
|
11
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (const e of entries) {
|
|
17
|
+
const full = path.join(dir, e.name);
|
|
18
|
+
if (e.isDirectory()) {
|
|
19
|
+
yield* walkJsonl(full);
|
|
20
|
+
}
|
|
21
|
+
else if (e.isFile() && e.name.endsWith('.jsonl')) {
|
|
22
|
+
yield full;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function parseLine(line, file, lineNo) {
|
|
27
|
+
if (!line.includes('"usage"'))
|
|
28
|
+
return null;
|
|
29
|
+
let d;
|
|
30
|
+
try {
|
|
31
|
+
d = JSON.parse(line);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const usage = d.message?.usage;
|
|
37
|
+
const model = d.message?.model;
|
|
38
|
+
if (!usage || !model || model === '<synthetic>' || model === 'error')
|
|
39
|
+
return null;
|
|
40
|
+
const input = usage.input_tokens ?? 0;
|
|
41
|
+
const output = usage.output_tokens ?? 0;
|
|
42
|
+
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
43
|
+
const cacheCreation = usage.cache_creation_input_tokens ?? 0;
|
|
44
|
+
// eventId 用 message.id 做去重键;缺 id 时退化为 文件+行号(不参与去重)
|
|
45
|
+
const msgId = d.message?.id;
|
|
46
|
+
const eventId = msgId ?? `${path.basename(file, '.jsonl')}#${lineNo}`;
|
|
47
|
+
return {
|
|
48
|
+
agent: 'claude-code',
|
|
49
|
+
sessionId: d.sessionId ?? path.basename(file, '.jsonl'),
|
|
50
|
+
eventId,
|
|
51
|
+
timestamp: d.timestamp ? new Date(d.timestamp).getTime() : 0,
|
|
52
|
+
provider: providerFromModel(model),
|
|
53
|
+
model,
|
|
54
|
+
usage: {
|
|
55
|
+
inputTokens: input,
|
|
56
|
+
outputTokens: output,
|
|
57
|
+
cacheReadTokens: cacheRead,
|
|
58
|
+
cacheCreationTokens: cacheCreation,
|
|
59
|
+
totalTokens: input + output + cacheRead + cacheCreation,
|
|
60
|
+
},
|
|
61
|
+
workspace: d.cwd,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// 从 model 名推 provider(国产模型识别在这)
|
|
65
|
+
function providerFromModel(model) {
|
|
66
|
+
const m = model.toLowerCase();
|
|
67
|
+
if (m.includes('claude'))
|
|
68
|
+
return 'anthropic';
|
|
69
|
+
if (m.includes('deepseek'))
|
|
70
|
+
return 'deepseek';
|
|
71
|
+
if (m.includes('glm'))
|
|
72
|
+
return 'glm';
|
|
73
|
+
if (m.includes('kimi'))
|
|
74
|
+
return 'kimi';
|
|
75
|
+
if (m.includes('qwen'))
|
|
76
|
+
return 'qwen';
|
|
77
|
+
if (m.includes('doubao') || m.includes('seed'))
|
|
78
|
+
return 'doubao';
|
|
79
|
+
if (m.includes('minimax'))
|
|
80
|
+
return 'minimax';
|
|
81
|
+
if (m.includes('hunyuan') || m.includes('hy'))
|
|
82
|
+
return 'hunyuan';
|
|
83
|
+
if (m.includes('mimo'))
|
|
84
|
+
return 'mimo';
|
|
85
|
+
if (m.includes('longcat'))
|
|
86
|
+
return 'longcat';
|
|
87
|
+
if (m.includes('gpt'))
|
|
88
|
+
return 'openai';
|
|
89
|
+
return 'unknown';
|
|
90
|
+
}
|
|
91
|
+
// 抓会话标题:首条用户文本消息,截 40 字符
|
|
92
|
+
function grabTitle(line, out) {
|
|
93
|
+
try {
|
|
94
|
+
const d = JSON.parse(line);
|
|
95
|
+
if (d?.type !== 'user' || !d?.sessionId)
|
|
96
|
+
return;
|
|
97
|
+
if (out.has(d.sessionId))
|
|
98
|
+
return;
|
|
99
|
+
const c = d.message?.content;
|
|
100
|
+
let text = '';
|
|
101
|
+
if (typeof c === 'string')
|
|
102
|
+
text = c;
|
|
103
|
+
else if (Array.isArray(c)) {
|
|
104
|
+
const t = c.find((b) => b?.type === 'text' && typeof b.text === 'string');
|
|
105
|
+
text = t?.text ?? '';
|
|
106
|
+
}
|
|
107
|
+
text = text.replace(/\s+/g, ' ').trim();
|
|
108
|
+
// 跳过系统注入块:xml 标签、[SYSTEM]/[MESSAGE FROM...] 等方括号标签
|
|
109
|
+
if (!text || text.startsWith('<') || text.startsWith('['))
|
|
110
|
+
return;
|
|
111
|
+
out.set(d.sessionId, text.slice(0, 40));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// 非 JSON 行忽略
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// 解析单个 jsonl 文件:文件内按 message.id 去重(取 totalTokens 最大),附会话标题
|
|
118
|
+
async function readFileEvents(file, sinceMs = 0) {
|
|
119
|
+
let content;
|
|
120
|
+
try {
|
|
121
|
+
content = await fs.readFile(file, 'utf8');
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
const byId = new Map();
|
|
127
|
+
const titleBySession = new Map();
|
|
128
|
+
const lines = content.split('\n');
|
|
129
|
+
for (let i = 0; i < lines.length; i++) {
|
|
130
|
+
const line = lines[i];
|
|
131
|
+
if (!line.trim())
|
|
132
|
+
continue;
|
|
133
|
+
// 顺手抓会话标题(首条用户文本消息),成本只是一次子串判断
|
|
134
|
+
if (line.includes('"type":"user"') && line.includes('"content"')) {
|
|
135
|
+
grabTitle(line, titleBySession);
|
|
136
|
+
}
|
|
137
|
+
const ev = parseLine(line, file, i);
|
|
138
|
+
if (!ev || ev.timestamp < sinceMs)
|
|
139
|
+
continue;
|
|
140
|
+
const prev = byId.get(ev.eventId);
|
|
141
|
+
if (!prev || ev.usage.totalTokens > prev.usage.totalTokens) {
|
|
142
|
+
byId.set(ev.eventId, ev);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const events = [...byId.values()];
|
|
146
|
+
for (const ev of events) {
|
|
147
|
+
const t = titleBySession.get(ev.sessionId);
|
|
148
|
+
if (t)
|
|
149
|
+
ev.sessionTitle = t;
|
|
150
|
+
}
|
|
151
|
+
return events;
|
|
152
|
+
}
|
|
153
|
+
async function listFiles() {
|
|
154
|
+
const out = [];
|
|
155
|
+
for await (const f of walkJsonl(PROJECTS_DIR))
|
|
156
|
+
out.push(f);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
// 内容流:tool_use(assistant)与 tool_result(user)只取尺寸与目标路径,不落内容
|
|
160
|
+
async function readFileContentEvents(file, sinceMs = 0) {
|
|
161
|
+
let content;
|
|
162
|
+
try {
|
|
163
|
+
content = await fs.readFile(file, 'utf8');
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
const events = [];
|
|
169
|
+
const sessionId = path.basename(file, '.jsonl');
|
|
170
|
+
for (const line of content.split('\n')) {
|
|
171
|
+
if (!line.includes('tool_use') && !line.includes('tool_result'))
|
|
172
|
+
continue;
|
|
173
|
+
let d;
|
|
174
|
+
try {
|
|
175
|
+
d = JSON.parse(line);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const ts = d.timestamp ? new Date(d.timestamp).getTime() : 0;
|
|
181
|
+
if (ts < sinceMs)
|
|
182
|
+
continue;
|
|
183
|
+
const items = d.message?.content;
|
|
184
|
+
if (!Array.isArray(items))
|
|
185
|
+
continue;
|
|
186
|
+
for (const c of items) {
|
|
187
|
+
if (!c || typeof c !== 'object')
|
|
188
|
+
continue;
|
|
189
|
+
const cc = c;
|
|
190
|
+
if (d.type === 'assistant' && cc.type === 'tool_use') {
|
|
191
|
+
const input = cc.input;
|
|
192
|
+
events.push({
|
|
193
|
+
agent: 'claude-code',
|
|
194
|
+
sessionId,
|
|
195
|
+
timestamp: ts,
|
|
196
|
+
kind: 'tool_use',
|
|
197
|
+
tool: cc.name,
|
|
198
|
+
targetPath: input?.file_path ?? input?.path,
|
|
199
|
+
sizeBytes: cc.input ? JSON.stringify(cc.input).length : 0,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
else if (d.type === 'user' && cc.type === 'tool_result') {
|
|
203
|
+
events.push({
|
|
204
|
+
agent: 'claude-code',
|
|
205
|
+
sessionId,
|
|
206
|
+
timestamp: ts,
|
|
207
|
+
kind: 'tool_result',
|
|
208
|
+
sizeBytes: cc.content ? JSON.stringify(cc.content).length : 0,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return events;
|
|
214
|
+
}
|
|
215
|
+
export const claudeCodeAdapter = {
|
|
216
|
+
meta: {
|
|
217
|
+
id: 'claude-code',
|
|
218
|
+
displayName: 'Claude Code',
|
|
219
|
+
detectPaths: ['~/.claude'],
|
|
220
|
+
detectCommands: ['claude'],
|
|
221
|
+
},
|
|
222
|
+
async detect() {
|
|
223
|
+
try {
|
|
224
|
+
await fs.access(PROJECTS_DIR);
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
listFiles,
|
|
232
|
+
readFileEvents,
|
|
233
|
+
readFileContentEvents,
|
|
234
|
+
async readEvents(sinceMs = 0) {
|
|
235
|
+
// 同一 message.id 只可能出现在一个会话文件里,按文件合并即可
|
|
236
|
+
const events = [];
|
|
237
|
+
for (const file of await listFiles()) {
|
|
238
|
+
// 文件 mtime 早于 since 就整个跳过(快计算)
|
|
239
|
+
try {
|
|
240
|
+
const st = await fs.stat(file);
|
|
241
|
+
if (st.mtimeMs < sinceMs)
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
events.push(...(await readFileEvents(file, sinceMs)));
|
|
248
|
+
}
|
|
249
|
+
return events;
|
|
250
|
+
},
|
|
251
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Codex adapter — 只读解析 ~/.codex/sessions/**/*.jsonl
|
|
2
|
+
// 格式:rollout jsonl,token 在 type=token_usage_record 事件的 payload.usage
|
|
3
|
+
import { promises as fs } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
const HOME = os.homedir();
|
|
7
|
+
const SESSIONS_DIR = path.join(HOME, '.codex', 'sessions');
|
|
8
|
+
async function* walkJsonl(dir) {
|
|
9
|
+
let entries;
|
|
10
|
+
try {
|
|
11
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (const e of entries) {
|
|
17
|
+
const full = path.join(dir, e.name);
|
|
18
|
+
if (e.isDirectory())
|
|
19
|
+
yield* walkJsonl(full);
|
|
20
|
+
else if (e.isFile() && e.name.endsWith('.jsonl'))
|
|
21
|
+
yield full;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function parseLine(line, file, lineNo) {
|
|
25
|
+
if (!line.includes('"token_usage_record"'))
|
|
26
|
+
return null;
|
|
27
|
+
let d;
|
|
28
|
+
try {
|
|
29
|
+
d = JSON.parse(line);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (d.type !== 'token_usage_record')
|
|
35
|
+
return null;
|
|
36
|
+
const u = d.payload?.usage;
|
|
37
|
+
if (!u)
|
|
38
|
+
return null;
|
|
39
|
+
// OpenAI 语义实测:input_tokens 包含 cached_input_tokens(cached 是子集),
|
|
40
|
+
// total_tokens = input_tokens + output_tokens。
|
|
41
|
+
// 新输入 = input − cached,缓存部分走 cacheRead 单价,不重复计费。
|
|
42
|
+
const inputRaw = u.input_tokens ?? 0;
|
|
43
|
+
const cacheRead = u.cached_input_tokens ?? 0;
|
|
44
|
+
const cacheCreation = u.cache_write_input_tokens ?? 0;
|
|
45
|
+
const output = u.output_tokens ?? 0;
|
|
46
|
+
const input = Math.max(inputRaw - cacheRead, 0);
|
|
47
|
+
return {
|
|
48
|
+
agent: 'codex',
|
|
49
|
+
sessionId: d.payload?.session_id ?? path.basename(file, '.jsonl'),
|
|
50
|
+
eventId: `${path.basename(file, '.jsonl')}#${lineNo}`,
|
|
51
|
+
timestamp: d.timestamp ? new Date(d.timestamp).getTime() : 0,
|
|
52
|
+
provider: 'openai',
|
|
53
|
+
model: 'codex', // rollout 里 model 名不稳定,先标 codex
|
|
54
|
+
usage: {
|
|
55
|
+
inputTokens: input,
|
|
56
|
+
outputTokens: output,
|
|
57
|
+
cacheReadTokens: cacheRead,
|
|
58
|
+
cacheCreationTokens: cacheCreation,
|
|
59
|
+
totalTokens: input + output + cacheRead + cacheCreation,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async function readFileEvents(file, sinceMs = 0) {
|
|
64
|
+
let content;
|
|
65
|
+
try {
|
|
66
|
+
content = await fs.readFile(file, 'utf8');
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
const events = [];
|
|
72
|
+
const lines = content.split('\n');
|
|
73
|
+
for (let i = 0; i < lines.length; i++) {
|
|
74
|
+
const line = lines[i];
|
|
75
|
+
if (!line.trim())
|
|
76
|
+
continue;
|
|
77
|
+
const ev = parseLine(line, file, i);
|
|
78
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
79
|
+
events.push(ev);
|
|
80
|
+
}
|
|
81
|
+
return events;
|
|
82
|
+
}
|
|
83
|
+
async function listFiles() {
|
|
84
|
+
const out = [];
|
|
85
|
+
for await (const f of walkJsonl(SESSIONS_DIR))
|
|
86
|
+
out.push(f);
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
export const codexAdapter = {
|
|
90
|
+
meta: {
|
|
91
|
+
id: 'codex',
|
|
92
|
+
displayName: 'Codex',
|
|
93
|
+
detectPaths: ['~/.codex'],
|
|
94
|
+
detectCommands: ['codex'],
|
|
95
|
+
},
|
|
96
|
+
async detect() {
|
|
97
|
+
try {
|
|
98
|
+
await fs.access(SESSIONS_DIR);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
listFiles,
|
|
106
|
+
readFileEvents,
|
|
107
|
+
readFileContentEvents: readCodexContentEvents,
|
|
108
|
+
async readEvents(sinceMs = 0) {
|
|
109
|
+
const events = [];
|
|
110
|
+
for (const file of await listFiles()) {
|
|
111
|
+
try {
|
|
112
|
+
const st = await fs.stat(file);
|
|
113
|
+
if (st.mtimeMs < sinceMs)
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
events.push(...(await readFileEvents(file, sinceMs)));
|
|
120
|
+
}
|
|
121
|
+
return events;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
// 内容流:response_item 的 function_call / function_call_output,只取尺寸
|
|
125
|
+
export async function readCodexContentEvents(file, sinceMs = 0) {
|
|
126
|
+
let content;
|
|
127
|
+
try {
|
|
128
|
+
content = await fs.readFile(file, 'utf8');
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
const events = [];
|
|
134
|
+
const sessionId = path.basename(file, '.jsonl');
|
|
135
|
+
for (const line of content.split('\n')) {
|
|
136
|
+
if (!line.includes('function_call'))
|
|
137
|
+
continue;
|
|
138
|
+
let d;
|
|
139
|
+
try {
|
|
140
|
+
d = JSON.parse(line);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const ts = d.timestamp ? new Date(d.timestamp).getTime() : 0;
|
|
146
|
+
if (ts < sinceMs)
|
|
147
|
+
continue;
|
|
148
|
+
const p = d.payload;
|
|
149
|
+
if (p?.type === 'function_call') {
|
|
150
|
+
events.push({
|
|
151
|
+
agent: 'codex', sessionId, timestamp: ts, kind: 'tool_use',
|
|
152
|
+
tool: p.name, sizeBytes: (p.arguments ?? '').length,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else if (p?.type === 'function_call_output') {
|
|
156
|
+
events.push({
|
|
157
|
+
agent: 'codex', sessionId, timestamp: ts, kind: 'tool_result',
|
|
158
|
+
sizeBytes: (p.output ?? '').length,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return events;
|
|
163
|
+
}
|