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