@anthonyhan/token-pilot 0.0.0-stage → 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/README.md +146 -2
- package/bin/token-pilot.js +18 -0
- 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 +30 -3
- package/pricing/models.json +311 -0
|
@@ -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
|
+
};
|
|
@@ -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
|
+
};
|