@anthonyhan/token-pilot 0.4.12 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -2
- package/dist/adapters/claude-code.js +63 -68
- package/dist/adapters/codex.js +38 -43
- package/dist/adapters/dsh.js +9 -11
- package/dist/adapters/kimi-code.js +41 -46
- package/dist/adapters/lines.js +50 -0
- package/dist/adapters/opencode.js +2 -2
- package/dist/adapters/pi.js +9 -12
- package/dist/adapters/workbuddy.js +9 -10
- package/dist/cli/index.js +97 -2
- package/dist/cli/serve.js +110 -0
- package/dist/core/collector.js +6 -2
- package/dist/core/doctor.js +17 -1
- package/dist/core/optimizer/bili.js +123 -0
- package/dist/core/optimizer/hosts.js +367 -0
- package/dist/core/optimizer/index.js +588 -0
- package/dist/core/optimizer/state.js +169 -0
- package/dist/core/optimizer/types.js +5 -0
- package/dist/core/rtk.js +202 -28
- package/package.json +2 -2
- package/web/dashboard.html +452 -16
package/README.md
CHANGED
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
[](#-状态栏应用)
|
|
13
13
|
[](tests/)
|
|
14
14
|
[](#-数据准确性)
|
|
15
|
+
[](#状态栏应用)
|
|
16
|
+
|
|
17
|
+
> 它**不是**云服务,也不是代理:不注册、不上传、不改你的请求——只在你本机读日志算账。账单算不准,一切免谈,所以[对账脚本逐项一致](#-数据准确性)是本项目第一优先级。
|
|
15
18
|
|
|
16
19
|
[快速开始](#-快速开始) · [状态栏应用](#-状态栏应用) · [数据准确性](#-数据准确性) · [什么时候别用它](#-什么时候别用它) · [讨论](https://github.com/anthonyhann/token-pilot/discussions) · [贡献](CONTRIBUTING.md) · [English](README-en.md)
|
|
17
20
|
|
|
@@ -92,6 +95,26 @@ xattr -dr com.apple.quarantine /Applications/token-pilot.app
|
|
|
92
95
|
|
|
93
96
|
**兼容你的工具链:** Claude Code ✅ · Codex ✅ · Kimi Code ✅ · OpenCode ✅(全量 token)· DeepSeek/GLM/Kimi/Qwen/MiniMax/混元/MiMo/LongCat/Doubao ✅(模型归一)· 其余 12 款仅检测 · 桌面订阅端记固定月费
|
|
94
97
|
|
|
98
|
+
## 🧰 省 token 控制台
|
|
99
|
+
|
|
100
|
+
看板里的「省 token 控制台」把两件事接在一起:
|
|
101
|
+
|
|
102
|
+
- **RTK**:在工具输出进入上下文前压缩命令、测试、搜索和文件读取结果。
|
|
103
|
+
- **billion-context**:在长会话中折叠旧上下文,支持搜索/回读,降低每轮重发成本。
|
|
104
|
+
|
|
105
|
+
入口:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
token-pilot optimize scan # 扫描本机 Agent / CLI / 桌面端
|
|
109
|
+
token-pilot optimize plan # 生成可审阅的接入计划
|
|
110
|
+
token-pilot optimize apply # 执行计划(宿主配置按事务备份)
|
|
111
|
+
token-pilot optimize rollback # 一键恢复接入前配置
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
看板里可以勾选宿主、预览每一步会写什么、失败只回滚对应宿主。**外部已有的 RTK/bili 只使用、不擅自卸载**;桌面端没有稳定接入缝的宿主会明确标为「不可接管」,不会把失败包装成成功。
|
|
115
|
+
|
|
116
|
+
> 边界:billion-context 的 MIT 许可证带额外署名条款;RTK 的代理/引擎部分有 BUSL-1.1。token-pilot 按需调用上游安装命令,不把它们的二进制打包进 npm/cask。
|
|
117
|
+
|
|
95
118
|
## 📈 数据准确性
|
|
96
119
|
|
|
97
120
|
账单工具不准就是误导。所以准确性**可验证**:
|
|
@@ -99,7 +122,7 @@ xattr -dr com.apple.quarantine /Applications/token-pilot.app
|
|
|
99
122
|
```bash
|
|
100
123
|
python3 scripts/reconcile.py month # 第三方视角独立重算原始日志,与 CLI 逐项对账
|
|
101
124
|
python3 scripts/verify-dashboard.py # 看板 36 项算法不变量校验
|
|
102
|
-
npm test #
|
|
125
|
+
npm test # 151 个单元测试(定价/采集口径/聚合/注入/熔断/RTK/额度/并发/省 token)
|
|
103
126
|
```
|
|
104
127
|
|
|
105
128
|
实测修过的口径坑(2026-10-21,本机 4787 个日志文件):
|
|
@@ -113,7 +136,7 @@ npm test # 69 个单元测试(定价/采集口径
|
|
|
113
136
|
## 📊 凭什么信我
|
|
114
137
|
|
|
115
138
|
- 我自己的 AI 账单就是它管的:本月 **¥8,265 / 47.8 亿 tokens**(2026-10-29 本机实时数据)
|
|
116
|
-
- 36 项看板算法不变量 +
|
|
139
|
+
- 36 项看板算法不变量 + 151 个单测 + 独立对账脚本,全部开箱可跑
|
|
117
140
|
- 诚实披露速率口径:日志按「调用完成时刻」记账,生成中的调用完成前不计入(看板内有注明)
|
|
118
141
|
|
|
119
142
|
## ⚠️ 什么时候别用它
|
|
@@ -282,6 +305,7 @@ token-pilot 免费且 MIT。如果它帮你省下了 AI 账单,可以请我喝
|
|
|
282
305
|
**节流(Step 2)**
|
|
283
306
|
- [x] 2A 压缩潜力量化(规则引擎:陈旧输出/重复读取/超长输出 → 本月可省多少,实测 $63-95)
|
|
284
307
|
- [x] 2B 压缩执行第一层:自动压缩提醒 + Claude autoCompact 写入 + RTK 安装(含 engine 的完整版;自动接 hook,改动 settings.json 前备份、失败还原)
|
|
308
|
+
- [x] 省 token 控制台:RTK / billion-context 宿主扫描、接入计划、配置事务回滚、桌面端 Tauri 命令桥
|
|
285
309
|
- [x] 异常重试自动熔断提醒(`breaker.ts`:单会话 ≥30 次调用即主动通知,每轮最多提 1 个防首跑轰炸)
|
|
286
310
|
- [⏸] 2B 压缩执行第二层:代理网关(LiteLLM / 自研)
|
|
287
311
|
- 卡点不是技术,是**边界**:本地代理会接管你所有 AI 工具的请求链路。要做就得先回答「失败怎么无感回退」「改了网络出问题怎么一键还原」「凭什么让用户信它不劫持 key」。这三条没有令人信服的答案之前不做——**宁可不做,也不能做一个会偷偷影响你网络的中间层**
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { promises as fs } from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import os from 'node:os';
|
|
6
|
+
import { forEachLine } from './lines.js';
|
|
6
7
|
const HOME = os.homedir();
|
|
7
8
|
const PROJECTS_DIR = path.join(HOME, '.claude', 'projects');
|
|
8
9
|
async function* walkJsonl(dir) {
|
|
@@ -116,32 +117,28 @@ function grabTitle(line, out) {
|
|
|
116
117
|
}
|
|
117
118
|
// 解析单个 jsonl 文件:文件内按 message.id 去重(取 totalTokens 最大),附会话标题
|
|
118
119
|
async function readFileEvents(file, sinceMs = 0) {
|
|
119
|
-
|
|
120
|
+
const byId = new Map();
|
|
121
|
+
const titleBySession = new Map();
|
|
120
122
|
try {
|
|
121
|
-
|
|
123
|
+
await forEachLine(file, (line, i) => {
|
|
124
|
+
if (!line.trim())
|
|
125
|
+
return;
|
|
126
|
+
// 顺手抓会话标题(首条用户文本消息),成本只是一次子串判断
|
|
127
|
+
if (line.includes('"type":"user"') && line.includes('"content"')) {
|
|
128
|
+
grabTitle(line, titleBySession);
|
|
129
|
+
}
|
|
130
|
+
const ev = parseLine(line, file, i);
|
|
131
|
+
if (!ev || ev.timestamp < sinceMs)
|
|
132
|
+
return;
|
|
133
|
+
const prev = byId.get(ev.eventId);
|
|
134
|
+
if (!prev || ev.usage.totalTokens > prev.usage.totalTokens) {
|
|
135
|
+
byId.set(ev.eventId, ev);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
122
138
|
}
|
|
123
139
|
catch {
|
|
124
140
|
return [];
|
|
125
141
|
}
|
|
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
142
|
const events = [...byId.values()];
|
|
146
143
|
for (const ev of events) {
|
|
147
144
|
const t = titleBySession.get(ev.sessionId);
|
|
@@ -158,57 +155,55 @@ async function listFiles() {
|
|
|
158
155
|
}
|
|
159
156
|
// 内容流:tool_use(assistant)与 tool_result(user)只取尺寸与目标路径,不落内容
|
|
160
157
|
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
158
|
const events = [];
|
|
169
159
|
const sessionId = path.basename(file, '.jsonl');
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
});
|
|
160
|
+
try {
|
|
161
|
+
await forEachLine(file, (line) => {
|
|
162
|
+
if (!line.includes('tool_use') && !line.includes('tool_result'))
|
|
163
|
+
return;
|
|
164
|
+
let d;
|
|
165
|
+
try {
|
|
166
|
+
d = JSON.parse(line);
|
|
201
167
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
agent: 'claude-code',
|
|
205
|
-
sessionId,
|
|
206
|
-
timestamp: ts,
|
|
207
|
-
kind: 'tool_result',
|
|
208
|
-
sizeBytes: cc.content ? JSON.stringify(cc.content).length : 0,
|
|
209
|
-
});
|
|
168
|
+
catch {
|
|
169
|
+
return;
|
|
210
170
|
}
|
|
211
|
-
|
|
171
|
+
const ts = d.timestamp ? new Date(d.timestamp).getTime() : 0;
|
|
172
|
+
if (ts < sinceMs)
|
|
173
|
+
return;
|
|
174
|
+
const items = d.message?.content;
|
|
175
|
+
if (!Array.isArray(items))
|
|
176
|
+
return;
|
|
177
|
+
for (const c of items) {
|
|
178
|
+
if (!c || typeof c !== 'object')
|
|
179
|
+
continue;
|
|
180
|
+
const cc = c;
|
|
181
|
+
if (d.type === 'assistant' && cc.type === 'tool_use') {
|
|
182
|
+
const input = cc.input;
|
|
183
|
+
events.push({
|
|
184
|
+
agent: 'claude-code',
|
|
185
|
+
sessionId,
|
|
186
|
+
timestamp: ts,
|
|
187
|
+
kind: 'tool_use',
|
|
188
|
+
tool: cc.name,
|
|
189
|
+
targetPath: input?.file_path ?? input?.path,
|
|
190
|
+
sizeBytes: cc.input ? JSON.stringify(cc.input).length : 0,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
else if (d.type === 'user' && cc.type === 'tool_result') {
|
|
194
|
+
events.push({
|
|
195
|
+
agent: 'claude-code',
|
|
196
|
+
sessionId,
|
|
197
|
+
timestamp: ts,
|
|
198
|
+
kind: 'tool_result',
|
|
199
|
+
sizeBytes: cc.content ? JSON.stringify(cc.content).length : 0,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return [];
|
|
212
207
|
}
|
|
213
208
|
return events;
|
|
214
209
|
}
|
package/dist/adapters/codex.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { promises as fs } from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import os from 'node:os';
|
|
6
|
+
import { forEachLine } from './lines.js';
|
|
6
7
|
const HOME = os.homedir();
|
|
7
8
|
const SESSIONS_DIR = path.join(HOME, '.codex', 'sessions');
|
|
8
9
|
async function* walkJsonl(dir) {
|
|
@@ -61,23 +62,19 @@ function parseLine(line, file, lineNo) {
|
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
64
|
async function readFileEvents(file, sinceMs = 0) {
|
|
64
|
-
|
|
65
|
+
const events = [];
|
|
65
66
|
try {
|
|
66
|
-
|
|
67
|
+
await forEachLine(file, (line, lineNo) => {
|
|
68
|
+
if (!line.trim())
|
|
69
|
+
return;
|
|
70
|
+
const ev = parseLine(line, file, lineNo);
|
|
71
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
72
|
+
events.push(ev);
|
|
73
|
+
});
|
|
67
74
|
}
|
|
68
75
|
catch {
|
|
69
76
|
return [];
|
|
70
77
|
}
|
|
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
78
|
return events;
|
|
82
79
|
}
|
|
83
80
|
async function listFiles() {
|
|
@@ -123,41 +120,39 @@ export const codexAdapter = {
|
|
|
123
120
|
};
|
|
124
121
|
// 内容流:response_item 的 function_call / function_call_output,只取尺寸
|
|
125
122
|
export async function readCodexContentEvents(file, sinceMs = 0) {
|
|
126
|
-
|
|
123
|
+
const events = [];
|
|
124
|
+
const sessionId = path.basename(file, '.jsonl');
|
|
127
125
|
try {
|
|
128
|
-
|
|
126
|
+
await forEachLine(file, (line) => {
|
|
127
|
+
if (!line.includes('function_call'))
|
|
128
|
+
return;
|
|
129
|
+
let d;
|
|
130
|
+
try {
|
|
131
|
+
d = JSON.parse(line);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const ts = d.timestamp ? new Date(d.timestamp).getTime() : 0;
|
|
137
|
+
if (ts < sinceMs)
|
|
138
|
+
return;
|
|
139
|
+
const p = d.payload;
|
|
140
|
+
if (p?.type === 'function_call') {
|
|
141
|
+
events.push({
|
|
142
|
+
agent: 'codex', sessionId, timestamp: ts, kind: 'tool_use',
|
|
143
|
+
tool: p.name, sizeBytes: (p.arguments ?? '').length,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
else if (p?.type === 'function_call_output') {
|
|
147
|
+
events.push({
|
|
148
|
+
agent: 'codex', sessionId, timestamp: ts, kind: 'tool_result',
|
|
149
|
+
sizeBytes: (p.output ?? '').length,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
});
|
|
129
153
|
}
|
|
130
154
|
catch {
|
|
131
155
|
return [];
|
|
132
156
|
}
|
|
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
157
|
return events;
|
|
163
158
|
}
|
package/dist/adapters/dsh.js
CHANGED
|
@@ -8,6 +8,7 @@ import { promisify } from 'node:util';
|
|
|
8
8
|
import { promises as fs } from 'node:fs';
|
|
9
9
|
import path from 'node:path';
|
|
10
10
|
import os from 'node:os';
|
|
11
|
+
import { forEachZstdLine } from './lines.js';
|
|
11
12
|
const execFileP = promisify(execFile);
|
|
12
13
|
const HOME = os.homedir();
|
|
13
14
|
const SESSIONS_DIR = path.join(HOME, '.dsh', 'sessions');
|
|
@@ -95,22 +96,19 @@ function providerFromModel(model) {
|
|
|
95
96
|
return 'unknown';
|
|
96
97
|
}
|
|
97
98
|
async function readFileEvents(file, sinceMs = 0) {
|
|
98
|
-
|
|
99
|
+
const events = [];
|
|
99
100
|
try {
|
|
100
|
-
|
|
101
|
-
|
|
101
|
+
await forEachZstdLine(file, (line) => {
|
|
102
|
+
if (!line.trim())
|
|
103
|
+
return;
|
|
104
|
+
const ev = parseLine(line, file);
|
|
105
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
106
|
+
events.push(ev);
|
|
107
|
+
});
|
|
102
108
|
}
|
|
103
109
|
catch {
|
|
104
110
|
return [];
|
|
105
111
|
}
|
|
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
112
|
// 同 seq 的流式快照去重(取最大 totalTokens)
|
|
115
113
|
const byId = new Map();
|
|
116
114
|
for (const ev of events) {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { promises as fs } from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import os from 'node:os';
|
|
6
|
+
import { forEachLine } from './lines.js';
|
|
6
7
|
const HOME = os.homedir();
|
|
7
8
|
const SESSIONS_DIR = path.join(HOME, '.kimi-code', 'sessions');
|
|
8
9
|
async function* walkJsonl(dir) {
|
|
@@ -59,23 +60,19 @@ function parseLine(line, file, lineNo) {
|
|
|
59
60
|
};
|
|
60
61
|
}
|
|
61
62
|
async function readFileEvents(file, sinceMs = 0) {
|
|
62
|
-
|
|
63
|
+
const events = [];
|
|
63
64
|
try {
|
|
64
|
-
|
|
65
|
+
await forEachLine(file, (line, i) => {
|
|
66
|
+
if (!line.trim())
|
|
67
|
+
return;
|
|
68
|
+
const ev = parseLine(line, file, i);
|
|
69
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
70
|
+
events.push(ev);
|
|
71
|
+
});
|
|
65
72
|
}
|
|
66
73
|
catch {
|
|
67
74
|
return [];
|
|
68
75
|
}
|
|
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
76
|
return events;
|
|
80
77
|
}
|
|
81
78
|
async function listFiles() {
|
|
@@ -121,45 +118,43 @@ export const kimiCodeAdapter = {
|
|
|
121
118
|
};
|
|
122
119
|
// 内容流:agent.message.appended 里的 toolCalls / role=tool 结果,只取尺寸
|
|
123
120
|
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
121
|
const events = [];
|
|
132
122
|
const sessionId = path.basename(path.dirname(path.dirname(file)));
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
123
|
+
try {
|
|
124
|
+
await forEachLine(file, (line) => {
|
|
125
|
+
if (!line.includes('agent.message.appended'))
|
|
126
|
+
return;
|
|
127
|
+
let d;
|
|
128
|
+
try {
|
|
129
|
+
d = JSON.parse(line);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const ts = d.time ?? 0;
|
|
135
|
+
if (ts < sinceMs)
|
|
136
|
+
return;
|
|
137
|
+
const msg = d.message?.message;
|
|
138
|
+
if (!msg)
|
|
139
|
+
return;
|
|
140
|
+
if (Array.isArray(msg.toolCalls)) {
|
|
141
|
+
for (const tc of msg.toolCalls) {
|
|
142
|
+
events.push({
|
|
143
|
+
agent: 'kimi-code', sessionId, timestamp: ts, kind: 'tool_use',
|
|
144
|
+
tool: tc.name, sizeBytes: (tc.arguments ?? '').length,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (msg.role === 'tool' && msg.content) {
|
|
151
149
|
events.push({
|
|
152
|
-
agent: 'kimi-code', sessionId, timestamp: ts, kind: '
|
|
153
|
-
|
|
150
|
+
agent: 'kimi-code', sessionId, timestamp: ts, kind: 'tool_result',
|
|
151
|
+
sizeBytes: JSON.stringify(msg.content).length,
|
|
154
152
|
});
|
|
155
153
|
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
sizeBytes: JSON.stringify(msg.content).length,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return [];
|
|
163
158
|
}
|
|
164
159
|
return events;
|
|
165
160
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// 大日志逐行读取工具
|
|
2
|
+
//
|
|
3
|
+
// 教训:一个 281MB 的 rollout 直接 readFile + split('\n') 会同时持有
|
|
4
|
+
// 整文件字符串、行数组和解析对象;采集器又并发开 32 个文件,4GB 堆会被打爆。
|
|
5
|
+
// 这里统一用流式行读取,内存上限降到「单行 + 单文件解析结果」。
|
|
6
|
+
import { createReadStream } from 'node:fs';
|
|
7
|
+
import { createInterface } from 'node:readline';
|
|
8
|
+
import { spawn } from 'node:child_process';
|
|
9
|
+
export async function forEachLine(file, onLine) {
|
|
10
|
+
const stream = createReadStream(file, { encoding: 'utf8' });
|
|
11
|
+
const rl = createInterface({ input: stream, crlfDelay: Infinity });
|
|
12
|
+
let lineNo = 0;
|
|
13
|
+
try {
|
|
14
|
+
for await (const line of rl)
|
|
15
|
+
onLine(line, lineNo++);
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
rl.close();
|
|
19
|
+
stream.destroy();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function forEachZstdLine(file, onLine) {
|
|
23
|
+
const child = spawn('zstd', ['-dc', file], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
24
|
+
if (!child.stdout)
|
|
25
|
+
throw new Error('zstd stdout unavailable');
|
|
26
|
+
const exited = new Promise((resolve, reject) => {
|
|
27
|
+
child.once('error', reject);
|
|
28
|
+
child.once('close', (code) => {
|
|
29
|
+
if (code === 0)
|
|
30
|
+
resolve();
|
|
31
|
+
else
|
|
32
|
+
reject(new Error(`zstd exited ${code ?? 'unknown'}`));
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
const rl = createInterface({ input: child.stdout, crlfDelay: Infinity });
|
|
36
|
+
let lineNo = 0;
|
|
37
|
+
try {
|
|
38
|
+
for await (const line of rl)
|
|
39
|
+
onLine(line, lineNo++);
|
|
40
|
+
await exited;
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
rl.close();
|
|
44
|
+
child.stdout.destroy();
|
|
45
|
+
try {
|
|
46
|
+
child.kill();
|
|
47
|
+
}
|
|
48
|
+
catch { /* 已退出 */ }
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -67,7 +67,7 @@ export const opencodeAdapter = {
|
|
|
67
67
|
db = new DatabaseSync(file, { readOnly: true });
|
|
68
68
|
const rows = db.prepare(`SELECT id, session_id, time_created, data FROM message
|
|
69
69
|
WHERE time_created >= ? AND json_extract(data,'$.tokens') IS NOT NULL
|
|
70
|
-
AND json_extract(data,'$.role') = 'assistant'`).
|
|
70
|
+
AND json_extract(data,'$.role') = 'assistant'`).iterate(sinceMs);
|
|
71
71
|
for (const r of rows) {
|
|
72
72
|
const ev = toEvent(r.id, r.session_id, r.time_created, r.data);
|
|
73
73
|
if (ev)
|
|
@@ -96,7 +96,7 @@ export async function readOpencodeContentEvents(file, sinceMs = 0) {
|
|
|
96
96
|
try {
|
|
97
97
|
db = new DatabaseSync(file, { readOnly: true });
|
|
98
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'`).
|
|
99
|
+
FROM part p WHERE p.time_created >= ? AND json_extract(p.data,'$.type') = 'tool'`).iterate(sinceMs);
|
|
100
100
|
for (const r of rows) {
|
|
101
101
|
let d;
|
|
102
102
|
try {
|
package/dist/adapters/pi.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { promises as fs } from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import os from 'node:os';
|
|
7
|
+
import { forEachLine } from './lines.js';
|
|
7
8
|
const HOME = os.homedir();
|
|
8
9
|
const SESSIONS_DIR = path.join(HOME, '.pi', 'agent', 'sessions');
|
|
9
10
|
async function* walkJsonl(dir) {
|
|
@@ -58,23 +59,19 @@ function parseLine(line, file, lineNo) {
|
|
|
58
59
|
};
|
|
59
60
|
}
|
|
60
61
|
async function readFileEvents(file, sinceMs = 0) {
|
|
61
|
-
|
|
62
|
+
const events = [];
|
|
62
63
|
try {
|
|
63
|
-
|
|
64
|
+
await forEachLine(file, (line, i) => {
|
|
65
|
+
if (!line.trim())
|
|
66
|
+
return;
|
|
67
|
+
const ev = parseLine(line, file, i);
|
|
68
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
69
|
+
events.push(ev);
|
|
70
|
+
});
|
|
64
71
|
}
|
|
65
72
|
catch {
|
|
66
73
|
return [];
|
|
67
74
|
}
|
|
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
75
|
return events;
|
|
79
76
|
}
|
|
80
77
|
async function listFiles() {
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { promises as fs } from 'node:fs';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import os from 'node:os';
|
|
8
|
+
import { forEachLine } from './lines.js';
|
|
8
9
|
const HOME = os.homedir();
|
|
9
10
|
const ROOTS = [
|
|
10
11
|
[path.join(HOME, '.workbuddy', 'traces'), 'workbuddy'],
|
|
@@ -82,21 +83,19 @@ function providerFromModel(model) {
|
|
|
82
83
|
return 'unknown';
|
|
83
84
|
}
|
|
84
85
|
async function readFileEvents(file, sinceMs, agent) {
|
|
85
|
-
|
|
86
|
+
const events = [];
|
|
86
87
|
try {
|
|
87
|
-
|
|
88
|
+
await forEachLine(file, (line) => {
|
|
89
|
+
if (!line.trim())
|
|
90
|
+
return;
|
|
91
|
+
const ev = parseTraceEnd(line, file, agent);
|
|
92
|
+
if (ev && ev.timestamp >= sinceMs)
|
|
93
|
+
events.push(ev);
|
|
94
|
+
});
|
|
88
95
|
}
|
|
89
96
|
catch {
|
|
90
97
|
return [];
|
|
91
98
|
}
|
|
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
99
|
return events;
|
|
101
100
|
}
|
|
102
101
|
function makeAdapter(root, agentId, displayName) {
|