@anthonyhan/token-pilot 0.2.3 → 0.2.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.
- 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 +278 -0
- package/dist/cli/serve.js +200 -0
- package/dist/core/aggregate.js +821 -0
- package/dist/core/alert.js +32 -0
- package/dist/core/autocompact.js +40 -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 +41 -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 +16 -6
- 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/web/dashboard.html +0 -1266
- package/web/panel.html +0 -674
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
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// DeepSeek Harness adapter — 只读解析 ~/.dsh/sessions/**/session.v4.jsonl.zstd
|
|
2
|
+
// DSH 会话日志是 zstd 压缩的 JSONL;usage 在 data.usage(OpenAI 风格命名),
|
|
3
|
+
// model 在同一行的 message 内容里(正则提取),时间戳在顶层 time(ms)。
|
|
4
|
+
// 语义实测:inputTokens=新输入(不含缓存),cacheReadTokens=缓存读,totalTokens≈input+cacheRead。
|
|
5
|
+
// 零依赖原则:zstd 解压走系统 zstd CLI(macOS brew 常见),缺了就跳过并在检测里如实标注。
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { promises as fs } from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
const execFileP = promisify(execFile);
|
|
12
|
+
const HOME = os.homedir();
|
|
13
|
+
const SESSIONS_DIR = path.join(HOME, '.dsh', 'sessions');
|
|
14
|
+
let zstdOk = null;
|
|
15
|
+
async function hasZstd() {
|
|
16
|
+
if (zstdOk !== null)
|
|
17
|
+
return zstdOk;
|
|
18
|
+
try {
|
|
19
|
+
await execFileP('zstd', ['--version'], { timeout: 3000 });
|
|
20
|
+
zstdOk = true;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
zstdOk = false;
|
|
24
|
+
}
|
|
25
|
+
return zstdOk;
|
|
26
|
+
}
|
|
27
|
+
async function* walkSessions(dir) {
|
|
28
|
+
let entries;
|
|
29
|
+
try {
|
|
30
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
for (const e of entries) {
|
|
36
|
+
const full = path.join(dir, e.name);
|
|
37
|
+
if (e.isDirectory())
|
|
38
|
+
yield* walkSessions(full);
|
|
39
|
+
else if (e.isFile() && e.name === 'session.v4.jsonl.zstd')
|
|
40
|
+
yield full;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const MODEL_RE = /"model"\s*:\s*"([^"]+)"/;
|
|
44
|
+
function parseLine(line, file) {
|
|
45
|
+
if (!line.includes('"usage"'))
|
|
46
|
+
return null;
|
|
47
|
+
let d;
|
|
48
|
+
try {
|
|
49
|
+
d = JSON.parse(line);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const u = d.data?.usage;
|
|
55
|
+
if (!u || typeof u.inputTokens !== 'number')
|
|
56
|
+
return null;
|
|
57
|
+
const input = u.inputTokens ?? 0;
|
|
58
|
+
const output = u.outputTokens ?? 0;
|
|
59
|
+
const cacheRead = u.cacheReadTokens ?? 0;
|
|
60
|
+
if (input + output + cacheRead === 0)
|
|
61
|
+
return null;
|
|
62
|
+
const m = MODEL_RE.exec(line);
|
|
63
|
+
const sessionId = path.basename(path.dirname(file));
|
|
64
|
+
return {
|
|
65
|
+
agent: 'dsh',
|
|
66
|
+
sessionId,
|
|
67
|
+
// 一次 API 调用 = 一条 usage(turn+step 粒度,实测逐步独立非累计)
|
|
68
|
+
eventId: `${sessionId}#t${d.data?.turn ?? 0}s${d.data?.step ?? 0}q${d.seq ?? 0}`,
|
|
69
|
+
timestamp: d.time ?? 0,
|
|
70
|
+
provider: providerFromModel(m?.[1] ?? ''),
|
|
71
|
+
model: m?.[1] ?? 'unknown',
|
|
72
|
+
usage: {
|
|
73
|
+
inputTokens: input,
|
|
74
|
+
outputTokens: output,
|
|
75
|
+
cacheReadTokens: cacheRead,
|
|
76
|
+
cacheCreationTokens: 0,
|
|
77
|
+
totalTokens: input + output + cacheRead,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function providerFromModel(model) {
|
|
82
|
+
const m = model.toLowerCase();
|
|
83
|
+
if (m.includes('kimi'))
|
|
84
|
+
return 'kimi';
|
|
85
|
+
if (m.includes('deepseek'))
|
|
86
|
+
return 'deepseek';
|
|
87
|
+
if (m.includes('step'))
|
|
88
|
+
return 'stepfun';
|
|
89
|
+
if (m.includes('glm'))
|
|
90
|
+
return 'glm';
|
|
91
|
+
if (m.includes('claude'))
|
|
92
|
+
return 'anthropic';
|
|
93
|
+
if (m.includes('gpt'))
|
|
94
|
+
return 'openai';
|
|
95
|
+
return 'unknown';
|
|
96
|
+
}
|
|
97
|
+
async function readFileEvents(file, sinceMs = 0) {
|
|
98
|
+
let content;
|
|
99
|
+
try {
|
|
100
|
+
const r = await execFileP('zstd', ['-dc', file], { timeout: 60_000, maxBuffer: 512 * 1024 * 1024 });
|
|
101
|
+
content = r.stdout;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
const events = [];
|
|
107
|
+
for (const line of content.split('\n')) {
|
|
108
|
+
if (!line.trim())
|
|
109
|
+
continue;
|
|
110
|
+
const ev = parseLine(line, file);
|
|
111
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
112
|
+
events.push(ev);
|
|
113
|
+
}
|
|
114
|
+
// 同 seq 的流式快照去重(取最大 totalTokens)
|
|
115
|
+
const byId = new Map();
|
|
116
|
+
for (const ev of events) {
|
|
117
|
+
const prev = byId.get(ev.eventId);
|
|
118
|
+
if (!prev || ev.usage.totalTokens > prev.usage.totalTokens)
|
|
119
|
+
byId.set(ev.eventId, ev);
|
|
120
|
+
}
|
|
121
|
+
return [...byId.values()];
|
|
122
|
+
}
|
|
123
|
+
async function listFiles() {
|
|
124
|
+
const out = [];
|
|
125
|
+
for await (const f of walkSessions(SESSIONS_DIR))
|
|
126
|
+
out.push(f);
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
export const dshAdapter = {
|
|
130
|
+
meta: {
|
|
131
|
+
id: 'dsh',
|
|
132
|
+
displayName: 'DeepSeek Harness',
|
|
133
|
+
detectPaths: ['~/.dsh'],
|
|
134
|
+
},
|
|
135
|
+
async detect() {
|
|
136
|
+
try {
|
|
137
|
+
await fs.access(SESSIONS_DIR);
|
|
138
|
+
return await hasZstd();
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
listFiles,
|
|
145
|
+
readFileEvents,
|
|
146
|
+
async readEvents(sinceMs = 0) {
|
|
147
|
+
if (!(await hasZstd()))
|
|
148
|
+
return [];
|
|
149
|
+
const events = [];
|
|
150
|
+
for (const file of await listFiles()) {
|
|
151
|
+
try {
|
|
152
|
+
const st = await fs.stat(file);
|
|
153
|
+
if (st.mtimeMs < sinceMs)
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
events.push(...(await readFileEvents(file, sinceMs)));
|
|
160
|
+
}
|
|
161
|
+
return events;
|
|
162
|
+
},
|
|
163
|
+
};
|