@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
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Kimi Code adapter — 只读解析 ~/.kimi-code/sessions/*/agents/main/wire.jsonl
|
|
2
|
+
// 格式:wire.jsonl,usage 字段是 {inputOther, output, inputCacheRead, inputCacheCreation}
|
|
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, '.kimi-code', '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 === 'wire.jsonl')
|
|
21
|
+
yield full;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function parseLine(line, file, lineNo) {
|
|
25
|
+
if (!line.includes('"usage.record"'))
|
|
26
|
+
return null;
|
|
27
|
+
let d;
|
|
28
|
+
try {
|
|
29
|
+
d = JSON.parse(line);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
// 只认 usage.record;其他类型(context.append_loop_event / agent.message.appended)带 usage 字样但不是计费记录
|
|
35
|
+
if (d.type !== 'usage.record')
|
|
36
|
+
return null;
|
|
37
|
+
const u = d.usage;
|
|
38
|
+
if (!u || !d.model)
|
|
39
|
+
return null;
|
|
40
|
+
const inputOther = u.inputOther ?? 0;
|
|
41
|
+
const output = u.output ?? 0;
|
|
42
|
+
const cacheRead = u.inputCacheRead ?? 0;
|
|
43
|
+
const cacheCreation = u.inputCacheCreation ?? 0;
|
|
44
|
+
// inputOther 是不走缓存的新输入;缓存部分交给 cacheRead/cacheCreation 单价,不能并入 input 重复计费
|
|
45
|
+
return {
|
|
46
|
+
agent: 'kimi-code',
|
|
47
|
+
sessionId: path.basename(path.dirname(path.dirname(file))),
|
|
48
|
+
eventId: `${path.basename(path.dirname(path.dirname(file)))}#${lineNo}`,
|
|
49
|
+
timestamp: d.time ?? 0,
|
|
50
|
+
provider: 'kimi',
|
|
51
|
+
model: d.model,
|
|
52
|
+
usage: {
|
|
53
|
+
inputTokens: inputOther,
|
|
54
|
+
outputTokens: output,
|
|
55
|
+
cacheReadTokens: cacheRead,
|
|
56
|
+
cacheCreationTokens: cacheCreation,
|
|
57
|
+
totalTokens: inputOther + output + cacheRead + cacheCreation,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async function readFileEvents(file, sinceMs = 0) {
|
|
62
|
+
let content;
|
|
63
|
+
try {
|
|
64
|
+
content = await fs.readFile(file, 'utf8');
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
const events = [];
|
|
70
|
+
const lines = content.split('\n');
|
|
71
|
+
for (let i = 0; i < lines.length; i++) {
|
|
72
|
+
const line = lines[i];
|
|
73
|
+
if (!line.trim())
|
|
74
|
+
continue;
|
|
75
|
+
const ev = parseLine(line, file, i);
|
|
76
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
77
|
+
events.push(ev);
|
|
78
|
+
}
|
|
79
|
+
return events;
|
|
80
|
+
}
|
|
81
|
+
async function listFiles() {
|
|
82
|
+
const out = [];
|
|
83
|
+
for await (const f of walkJsonl(SESSIONS_DIR))
|
|
84
|
+
out.push(f);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
export const kimiCodeAdapter = {
|
|
88
|
+
meta: {
|
|
89
|
+
id: 'kimi-code',
|
|
90
|
+
displayName: 'Kimi Code',
|
|
91
|
+
detectPaths: ['~/.kimi-code'],
|
|
92
|
+
detectCommands: ['kimi'],
|
|
93
|
+
},
|
|
94
|
+
async detect() {
|
|
95
|
+
try {
|
|
96
|
+
await fs.access(SESSIONS_DIR);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
listFiles,
|
|
104
|
+
readFileEvents,
|
|
105
|
+
readFileContentEvents: readKimiContentEvents,
|
|
106
|
+
async readEvents(sinceMs = 0) {
|
|
107
|
+
const events = [];
|
|
108
|
+
for (const file of await listFiles()) {
|
|
109
|
+
try {
|
|
110
|
+
const st = await fs.stat(file);
|
|
111
|
+
if (st.mtimeMs < sinceMs)
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
events.push(...(await readFileEvents(file, sinceMs)));
|
|
118
|
+
}
|
|
119
|
+
return events;
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
// 内容流:agent.message.appended 里的 toolCalls / role=tool 结果,只取尺寸
|
|
123
|
+
export async function readKimiContentEvents(file, sinceMs = 0) {
|
|
124
|
+
let content;
|
|
125
|
+
try {
|
|
126
|
+
content = await fs.readFile(file, 'utf8');
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const events = [];
|
|
132
|
+
const sessionId = path.basename(path.dirname(path.dirname(file)));
|
|
133
|
+
for (const line of content.split('\n')) {
|
|
134
|
+
if (!line.includes('agent.message.appended'))
|
|
135
|
+
continue;
|
|
136
|
+
let d;
|
|
137
|
+
try {
|
|
138
|
+
d = JSON.parse(line);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const ts = d.time ?? 0;
|
|
144
|
+
if (ts < sinceMs)
|
|
145
|
+
continue;
|
|
146
|
+
const msg = d.message?.message;
|
|
147
|
+
if (!msg)
|
|
148
|
+
continue;
|
|
149
|
+
if (Array.isArray(msg.toolCalls)) {
|
|
150
|
+
for (const tc of msg.toolCalls) {
|
|
151
|
+
events.push({
|
|
152
|
+
agent: 'kimi-code', sessionId, timestamp: ts, kind: 'tool_use',
|
|
153
|
+
tool: tc.name, sizeBytes: (tc.arguments ?? '').length,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (msg.role === 'tool' && msg.content) {
|
|
158
|
+
events.push({
|
|
159
|
+
agent: 'kimi-code', sessionId, timestamp: ts, kind: 'tool_result',
|
|
160
|
+
sizeBytes: JSON.stringify(msg.content).length,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return events;
|
|
165
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// OpenCode adapter — 只读解析 ~/.local/share/opencode/opencode.db(SQLite)
|
|
2
|
+
// 结构:message 表 data JSON 内含 role/tokens{input,output,cache{read,write}}/modelID/providerID/path.cwd
|
|
3
|
+
// 只取 role=assistant 且有 tokens 的行(计费等价物)。零依赖:node:sqlite 内置。
|
|
4
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
5
|
+
import { promises as fs } from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
const HOME = os.homedir();
|
|
9
|
+
const DB_PATH = path.join(HOME, '.local', 'share', 'opencode', 'opencode.db');
|
|
10
|
+
function toEvent(id, sessionId, timeCreated, data) {
|
|
11
|
+
let d;
|
|
12
|
+
try {
|
|
13
|
+
d = JSON.parse(data);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
if (d.role !== 'assistant' || !d.tokens)
|
|
19
|
+
return null;
|
|
20
|
+
const t = d.tokens;
|
|
21
|
+
const input = t.input ?? 0;
|
|
22
|
+
const output = t.output ?? 0;
|
|
23
|
+
const cacheRead = t.cache?.read ?? 0;
|
|
24
|
+
const cacheCreation = t.cache?.write ?? 0;
|
|
25
|
+
if (input + output + cacheRead + cacheCreation === 0)
|
|
26
|
+
return null;
|
|
27
|
+
return {
|
|
28
|
+
agent: 'opencode',
|
|
29
|
+
sessionId,
|
|
30
|
+
eventId: id,
|
|
31
|
+
timestamp: timeCreated,
|
|
32
|
+
provider: d.providerID ?? 'unknown',
|
|
33
|
+
model: d.modelID ?? 'unknown',
|
|
34
|
+
usage: {
|
|
35
|
+
inputTokens: input,
|
|
36
|
+
outputTokens: output,
|
|
37
|
+
cacheReadTokens: cacheRead,
|
|
38
|
+
cacheCreationTokens: cacheCreation,
|
|
39
|
+
totalTokens: input + output + cacheRead + cacheCreation,
|
|
40
|
+
},
|
|
41
|
+
workspace: d.path?.cwd,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export const opencodeAdapter = {
|
|
45
|
+
meta: {
|
|
46
|
+
id: 'opencode',
|
|
47
|
+
displayName: 'OpenCode',
|
|
48
|
+
detectPaths: ['~/.local/share/opencode'],
|
|
49
|
+
detectCommands: ['opencode'],
|
|
50
|
+
},
|
|
51
|
+
async detect() {
|
|
52
|
+
try {
|
|
53
|
+
await fs.access(DB_PATH);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
async listFiles() {
|
|
61
|
+
return [DB_PATH];
|
|
62
|
+
},
|
|
63
|
+
async readFileEvents(file, sinceMs = 0) {
|
|
64
|
+
const events = [];
|
|
65
|
+
let db = null;
|
|
66
|
+
try {
|
|
67
|
+
db = new DatabaseSync(file, { readOnly: true });
|
|
68
|
+
const rows = db.prepare(`SELECT id, session_id, time_created, data FROM message
|
|
69
|
+
WHERE time_created >= ? AND json_extract(data,'$.tokens') IS NOT NULL
|
|
70
|
+
AND json_extract(data,'$.role') = 'assistant'`).all(sinceMs);
|
|
71
|
+
for (const r of rows) {
|
|
72
|
+
const ev = toEvent(r.id, r.session_id, r.time_created, r.data);
|
|
73
|
+
if (ev)
|
|
74
|
+
events.push(ev);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// db 被锁或损坏时静默跳过(只读不该影响 OpenCode 本体)
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
try {
|
|
82
|
+
db?.close();
|
|
83
|
+
}
|
|
84
|
+
catch { /* 忽略 */ }
|
|
85
|
+
}
|
|
86
|
+
return events;
|
|
87
|
+
},
|
|
88
|
+
async readEvents(sinceMs = 0) {
|
|
89
|
+
return this.readFileEvents(DB_PATH, sinceMs);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
// 内容流:part 表 type=tool 的 state.input/output,只取尺寸
|
|
93
|
+
export async function readOpencodeContentEvents(file, sinceMs = 0) {
|
|
94
|
+
const events = [];
|
|
95
|
+
let db = null;
|
|
96
|
+
try {
|
|
97
|
+
db = new DatabaseSync(file, { readOnly: true });
|
|
98
|
+
const rows = db.prepare(`SELECT p.session_id AS sid, p.time_created AS ts, p.data AS data
|
|
99
|
+
FROM part p WHERE p.time_created >= ? AND json_extract(p.data,'$.type') = 'tool'`).all(sinceMs);
|
|
100
|
+
for (const r of rows) {
|
|
101
|
+
let d;
|
|
102
|
+
try {
|
|
103
|
+
d = JSON.parse(r.data);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const inSize = d.state?.input ? JSON.stringify(d.state.input).length : 0;
|
|
109
|
+
const outSize = d.state?.output ? JSON.stringify(d.state.output).length : 0;
|
|
110
|
+
events.push({ agent: 'opencode', sessionId: r.sid, timestamp: r.ts, kind: 'tool_use', tool: d.tool, sizeBytes: inSize });
|
|
111
|
+
if (outSize > 0) {
|
|
112
|
+
events.push({ agent: 'opencode', sessionId: r.sid, timestamp: r.ts, kind: 'tool_result', tool: d.tool, sizeBytes: outSize });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// 锁定或损坏时跳过
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
try {
|
|
121
|
+
db?.close();
|
|
122
|
+
}
|
|
123
|
+
catch { /* 忽略 */ }
|
|
124
|
+
}
|
|
125
|
+
return events;
|
|
126
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// pi adapter — 只读解析 ~/.pi/agent/sessions/**/*.jsonl
|
|
2
|
+
// 结构:type=message 且 message.role=assistant 且 message.usage {input,output,cacheRead,cacheWrite}
|
|
3
|
+
// 全零 usage(coding-plan 代理常报 0)过滤掉,不计费不误导。
|
|
4
|
+
import { promises as fs } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
const HOME = os.homedir();
|
|
8
|
+
const SESSIONS_DIR = path.join(HOME, '.pi', 'agent', 'sessions');
|
|
9
|
+
async function* walkJsonl(dir) {
|
|
10
|
+
let entries;
|
|
11
|
+
try {
|
|
12
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const e of entries) {
|
|
18
|
+
const full = path.join(dir, e.name);
|
|
19
|
+
if (e.isDirectory())
|
|
20
|
+
yield* walkJsonl(full);
|
|
21
|
+
else if (e.isFile() && e.name.endsWith('.jsonl'))
|
|
22
|
+
yield full;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function parseLine(line, file, lineNo) {
|
|
26
|
+
if (!line.includes('"usage"'))
|
|
27
|
+
return null;
|
|
28
|
+
let d;
|
|
29
|
+
try {
|
|
30
|
+
d = JSON.parse(line);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (d.type !== 'message' || d.message?.role !== 'assistant' || !d.message.usage)
|
|
36
|
+
return null;
|
|
37
|
+
const u = d.message.usage;
|
|
38
|
+
const input = u.input ?? 0;
|
|
39
|
+
const output = u.output ?? 0;
|
|
40
|
+
const cacheRead = u.cacheRead ?? 0;
|
|
41
|
+
const cacheCreation = u.cacheWrite ?? 0;
|
|
42
|
+
if (input + output + cacheRead + cacheCreation === 0)
|
|
43
|
+
return null; // 全零不计
|
|
44
|
+
return {
|
|
45
|
+
agent: 'pi',
|
|
46
|
+
sessionId: path.basename(file, '.jsonl'),
|
|
47
|
+
eventId: `${path.basename(file, '.jsonl')}#${d.id ?? lineNo}`,
|
|
48
|
+
timestamp: d.timestamp ? new Date(d.timestamp).getTime() : 0,
|
|
49
|
+
provider: d.message.provider ?? 'unknown',
|
|
50
|
+
model: d.message.model ?? 'unknown',
|
|
51
|
+
usage: {
|
|
52
|
+
inputTokens: input,
|
|
53
|
+
outputTokens: output,
|
|
54
|
+
cacheReadTokens: cacheRead,
|
|
55
|
+
cacheCreationTokens: cacheCreation,
|
|
56
|
+
totalTokens: input + output + cacheRead + cacheCreation,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function readFileEvents(file, sinceMs = 0) {
|
|
61
|
+
let content;
|
|
62
|
+
try {
|
|
63
|
+
content = await fs.readFile(file, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
const events = [];
|
|
69
|
+
const lines = content.split('\n');
|
|
70
|
+
for (let i = 0; i < lines.length; i++) {
|
|
71
|
+
const line = lines[i];
|
|
72
|
+
if (!line.trim())
|
|
73
|
+
continue;
|
|
74
|
+
const ev = parseLine(line, file, i);
|
|
75
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
76
|
+
events.push(ev);
|
|
77
|
+
}
|
|
78
|
+
return events;
|
|
79
|
+
}
|
|
80
|
+
async function listFiles() {
|
|
81
|
+
const out = [];
|
|
82
|
+
for await (const f of walkJsonl(SESSIONS_DIR))
|
|
83
|
+
out.push(f);
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
export const piAdapter = {
|
|
87
|
+
meta: {
|
|
88
|
+
id: 'pi',
|
|
89
|
+
displayName: 'pi',
|
|
90
|
+
detectPaths: ['~/.pi'],
|
|
91
|
+
detectCommands: ['pi'],
|
|
92
|
+
},
|
|
93
|
+
async detect() {
|
|
94
|
+
try {
|
|
95
|
+
await fs.access(SESSIONS_DIR);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
listFiles,
|
|
103
|
+
readFileEvents,
|
|
104
|
+
async readEvents(sinceMs = 0) {
|
|
105
|
+
const events = [];
|
|
106
|
+
for (const file of await listFiles()) {
|
|
107
|
+
try {
|
|
108
|
+
const st = await fs.stat(file);
|
|
109
|
+
if (st.mtimeMs < sinceMs)
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
events.push(...(await readFileEvents(file, sinceMs)));
|
|
116
|
+
}
|
|
117
|
+
return events;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Qoder adapter — 仅会话数(Qoder 不写 token/模型数据,model 全为 auto,实测确认)
|
|
2
|
+
// 只提供 sessionCount 能力供状态区展示,不产生计费事件(避免污染调用数/成本口径)。
|
|
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 QODER_DIR = path.join(HOME, '.qoder');
|
|
8
|
+
async function countSessionDirs(dir, depth) {
|
|
9
|
+
if (depth > 5)
|
|
10
|
+
return 0;
|
|
11
|
+
let n = 0;
|
|
12
|
+
let entries;
|
|
13
|
+
try {
|
|
14
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
for (const e of entries) {
|
|
20
|
+
if (!e.isDirectory())
|
|
21
|
+
continue;
|
|
22
|
+
if (/^sess|^session/i.test(e.name))
|
|
23
|
+
n++;
|
|
24
|
+
n += await countSessionDirs(path.join(dir, e.name), depth + 1);
|
|
25
|
+
}
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
export const qoderAdapter = {
|
|
29
|
+
meta: {
|
|
30
|
+
id: 'qoder',
|
|
31
|
+
displayName: 'Qoder',
|
|
32
|
+
detectPaths: ['~/.qoder'],
|
|
33
|
+
detectCommands: ['qoder'],
|
|
34
|
+
},
|
|
35
|
+
async detect() {
|
|
36
|
+
try {
|
|
37
|
+
await fs.access(QODER_DIR);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
// Qoder 无 token 数据:只报会话数
|
|
45
|
+
async sessionCount() {
|
|
46
|
+
return countSessionDirs(QODER_DIR, 0);
|
|
47
|
+
},
|
|
48
|
+
async readEvents() {
|
|
49
|
+
return []; // 故意为空:无 token 数据可采
|
|
50
|
+
},
|
|
51
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Doubao · 火山 adapter — 云端用量 API(经 arkcli usage stats,日粒度)
|
|
2
|
+
// 与本地日志粒度不同:云端是「每天×模型」的合计,一个 record 是一天总量(非单次调用)。
|
|
3
|
+
// 事件时间戳取当天中午;调用次数字段无法精确(记录的是 ReqCnt 汇总),会话维度不适用。
|
|
4
|
+
import { execFile } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
const execFileP = promisify(execFile);
|
|
7
|
+
// arkcli 输出尾部可能跟非 JSON 行(升级提示等),只解析第一个完整 JSON 对象
|
|
8
|
+
function extractJson(raw) {
|
|
9
|
+
const start = raw.indexOf('{');
|
|
10
|
+
if (start < 0)
|
|
11
|
+
return null;
|
|
12
|
+
let depth = 0;
|
|
13
|
+
for (let i = start; i < raw.length; i++) {
|
|
14
|
+
if (raw[i] === '{')
|
|
15
|
+
depth++;
|
|
16
|
+
else if (raw[i] === '}') {
|
|
17
|
+
depth--;
|
|
18
|
+
if (depth === 0) {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(raw.slice(start, i + 1));
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
function ymd(d) {
|
|
31
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
32
|
+
}
|
|
33
|
+
export const volcArkAdapter = {
|
|
34
|
+
meta: {
|
|
35
|
+
id: 'volc-ark',
|
|
36
|
+
displayName: 'Doubao · 火山',
|
|
37
|
+
detectPaths: [],
|
|
38
|
+
detectCommands: ['arkcli'],
|
|
39
|
+
},
|
|
40
|
+
async detect() {
|
|
41
|
+
try {
|
|
42
|
+
await execFileP('arkcli', ['--version'], { timeout: 5000 });
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
async readEvents(sinceMs = 0) {
|
|
50
|
+
const start = ymd(new Date(sinceMs > 0 ? sinceMs : Date.now() - 40 * 86400_000));
|
|
51
|
+
let out;
|
|
52
|
+
try {
|
|
53
|
+
const r = await execFileP('arkcli', ['usage', 'stats', '--start', start, '--format', 'json'], { timeout: 30_000 });
|
|
54
|
+
out = r.stdout;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
const parsed = extractJson(out);
|
|
60
|
+
if (!parsed?.records)
|
|
61
|
+
return [];
|
|
62
|
+
const events = [];
|
|
63
|
+
for (const rec of parsed.records) {
|
|
64
|
+
const input = Math.max((rec.InputTokens ?? 0) - (rec.CacheTokensHit ?? 0), 0);
|
|
65
|
+
const cacheRead = rec.CacheTokensHit ?? 0;
|
|
66
|
+
const output = rec.OutputTokens ?? 0;
|
|
67
|
+
if (input + cacheRead + output === 0)
|
|
68
|
+
continue;
|
|
69
|
+
const model = rec.Model ?? rec.ModelID ?? rec.Endpoint ?? 'doubao-cloud';
|
|
70
|
+
// 日粒度:时间戳定当天中午
|
|
71
|
+
const ts = rec.Day ? new Date(rec.Day + 'T12:00:00').getTime() : Date.now();
|
|
72
|
+
if (ts < sinceMs)
|
|
73
|
+
continue;
|
|
74
|
+
events.push({
|
|
75
|
+
agent: 'volc-ark',
|
|
76
|
+
sessionId: `cloud-${rec.Day}`,
|
|
77
|
+
eventId: `volc-${rec.Day}-${model}`,
|
|
78
|
+
timestamp: ts,
|
|
79
|
+
provider: 'doubao',
|
|
80
|
+
model,
|
|
81
|
+
usage: {
|
|
82
|
+
inputTokens: input,
|
|
83
|
+
outputTokens: output,
|
|
84
|
+
cacheReadTokens: cacheRead,
|
|
85
|
+
cacheCreationTokens: 0,
|
|
86
|
+
totalTokens: input + output + cacheRead,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return events;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// WorkBuddy / WorkBuddy AI adapter — 只读解析 ~/.workbuddy{,-ai}/traces/*/**\/*.jsonl
|
|
2
|
+
// 结构:OTel 风格 trace 文件;trace_end.summary.modelInfo 有完整用量:
|
|
3
|
+
// { models, totalInputTokens, totalOutputTokens, totalCachedTokens, callCount }
|
|
4
|
+
// 口径:trace 级聚合(一个 trace = 一次 Agent 工作流),逐调用粒度不可得——如实标注。
|
|
5
|
+
import { promises as fs } from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
const HOME = os.homedir();
|
|
9
|
+
const ROOTS = [
|
|
10
|
+
[path.join(HOME, '.workbuddy', 'traces'), 'workbuddy'],
|
|
11
|
+
[path.join(HOME, '.workbuddy-ai', 'traces'), 'workbuddy-ai'],
|
|
12
|
+
];
|
|
13
|
+
async function* walkJsonl(dir) {
|
|
14
|
+
let entries;
|
|
15
|
+
try {
|
|
16
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
for (const e of entries) {
|
|
22
|
+
const full = path.join(dir, e.name);
|
|
23
|
+
if (e.isDirectory())
|
|
24
|
+
yield* walkJsonl(full);
|
|
25
|
+
else if (e.isFile() && e.name.endsWith('.jsonl'))
|
|
26
|
+
yield full;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function parseTraceEnd(line, file, agent) {
|
|
30
|
+
if (!line.includes('"trace_end"') || !line.includes('modelInfo'))
|
|
31
|
+
return null;
|
|
32
|
+
let d;
|
|
33
|
+
try {
|
|
34
|
+
d = JSON.parse(line);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (d.type !== 'trace_end')
|
|
40
|
+
return null;
|
|
41
|
+
const s = d.summary;
|
|
42
|
+
const mi = s?.modelInfo;
|
|
43
|
+
if (!s || !mi || !mi.models?.length)
|
|
44
|
+
return null;
|
|
45
|
+
const totalIn = mi.totalInputTokens ?? 0;
|
|
46
|
+
const cacheRead = mi.totalCachedTokens ?? 0;
|
|
47
|
+
const input = Math.max(totalIn - cacheRead, 0);
|
|
48
|
+
const output = mi.totalOutputTokens ?? 0;
|
|
49
|
+
if (input + cacheRead + output === 0)
|
|
50
|
+
return null;
|
|
51
|
+
const model = mi.models[0];
|
|
52
|
+
return {
|
|
53
|
+
agent,
|
|
54
|
+
sessionId: s.sessionId ?? path.basename(file, '.jsonl'),
|
|
55
|
+
eventId: path.basename(file, '.jsonl'),
|
|
56
|
+
timestamp: s.endedAt ? new Date(s.endedAt).getTime() : 0,
|
|
57
|
+
provider: providerFromModel(model),
|
|
58
|
+
model,
|
|
59
|
+
usage: {
|
|
60
|
+
inputTokens: input,
|
|
61
|
+
outputTokens: output,
|
|
62
|
+
cacheReadTokens: cacheRead,
|
|
63
|
+
cacheCreationTokens: 0,
|
|
64
|
+
totalTokens: input + output + cacheRead,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function providerFromModel(model) {
|
|
69
|
+
const m = model.toLowerCase();
|
|
70
|
+
if (m.startsWith('hy') || m.includes('hunyuan'))
|
|
71
|
+
return 'hunyuan';
|
|
72
|
+
if (m.includes('glm'))
|
|
73
|
+
return 'glm';
|
|
74
|
+
if (m.includes('deepseek'))
|
|
75
|
+
return 'deepseek';
|
|
76
|
+
if (m.includes('kimi'))
|
|
77
|
+
return 'kimi';
|
|
78
|
+
if (m.includes('claude'))
|
|
79
|
+
return 'anthropic';
|
|
80
|
+
if (m.includes('gpt'))
|
|
81
|
+
return 'openai';
|
|
82
|
+
return 'unknown';
|
|
83
|
+
}
|
|
84
|
+
async function readFileEvents(file, sinceMs, agent) {
|
|
85
|
+
let content;
|
|
86
|
+
try {
|
|
87
|
+
content = await fs.readFile(file, 'utf8');
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
const events = [];
|
|
93
|
+
for (const line of content.split('\n')) {
|
|
94
|
+
if (!line.trim())
|
|
95
|
+
continue;
|
|
96
|
+
const ev = parseTraceEnd(line, file, agent);
|
|
97
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
98
|
+
events.push(ev);
|
|
99
|
+
}
|
|
100
|
+
return events;
|
|
101
|
+
}
|
|
102
|
+
function makeAdapter(root, agentId, displayName) {
|
|
103
|
+
return {
|
|
104
|
+
meta: { id: agentId, displayName, detectPaths: [path.dirname(root)] },
|
|
105
|
+
async detect() {
|
|
106
|
+
try {
|
|
107
|
+
await fs.access(root);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
async listFiles() {
|
|
115
|
+
const out = [];
|
|
116
|
+
for await (const f of walkJsonl(root))
|
|
117
|
+
out.push(f);
|
|
118
|
+
return out;
|
|
119
|
+
},
|
|
120
|
+
async readFileEvents(file, sinceMs = 0) {
|
|
121
|
+
return readFileEvents(file, sinceMs, agentId);
|
|
122
|
+
},
|
|
123
|
+
async readEvents(sinceMs = 0) {
|
|
124
|
+
return readFileEventsList(await this.listFiles(), sinceMs, agentId);
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function readFileEventsList(files, sinceMs, agent) {
|
|
129
|
+
const events = [];
|
|
130
|
+
for (const file of files) {
|
|
131
|
+
try {
|
|
132
|
+
const st = await fs.stat(file);
|
|
133
|
+
if (st.mtimeMs < sinceMs)
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
events.push(...(await readFileEvents(file, sinceMs, agent)));
|
|
140
|
+
}
|
|
141
|
+
return events;
|
|
142
|
+
}
|
|
143
|
+
export const workbuddyAdapter = makeAdapter(ROOTS[0][0], 'workbuddy', 'WorkBuddy');
|
|
144
|
+
export const workbuddyAiAdapter = makeAdapter(ROOTS[1][0], 'workbuddy-ai', 'WorkBuddy AI');
|