sigrank 0.0.177 → 0.0.179

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/MCP_SERVER_DESCRIPTION.md +141 -0
  3. package/README.md +23 -3
  4. package/adapters/index.mjs +865 -0
  5. package/adapters/tokenpull.mjs +576 -0
  6. package/adapters.mjs +1 -750
  7. package/analytics/cascade.mjs +274 -0
  8. package/cascade.mjs +1 -274
  9. package/cli.mjs +1 -1850
  10. package/connect.mjs +1 -13
  11. package/identity/connect.mjs +13 -0
  12. package/identity/keystore.mjs +242 -0
  13. package/identity/sign.mjs +106 -0
  14. package/index.mjs +30 -148
  15. package/keystore.mjs +1 -242
  16. package/narrate.mjs +1 -77
  17. package/package.json +31 -15
  18. package/preflight.mjs +1 -191
  19. package/presentation/cli.mjs +1874 -0
  20. package/presentation/narrate.mjs +77 -0
  21. package/presentation/tui.mjs +2668 -0
  22. package/prompts/check-my-efficiency.md +1 -0
  23. package/prompts/compare-with-leader.md +1 -0
  24. package/prompts/simulate-improvement.md +1 -0
  25. package/resources/class-tiers.md +23 -0
  26. package/resources/data-policy.md +27 -0
  27. package/resources/install-guide.md +59 -0
  28. package/resources/privacy-model.md +30 -0
  29. package/resources/scoring-formula.md +28 -0
  30. package/review.mjs +263 -0
  31. package/sign.mjs +1 -106
  32. package/submit/index.mjs +309 -0
  33. package/submit/preflight.mjs +191 -0
  34. package/submit.mjs +1 -303
  35. package/tokenpull.mjs +1 -584
  36. package/tools/_framing.mjs +139 -0
  37. package/tools/_helpers.mjs +258 -0
  38. package/tools/_report.mjs +262 -0
  39. package/tools/_schemas.mjs +394 -0
  40. package/tools/_verifiers.mjs +148 -0
  41. package/tools/compare-operators.mjs +79 -0
  42. package/tools/compare-self.mjs +122 -0
  43. package/tools/describe-power-user.mjs +68 -0
  44. package/tools/diagnose-cascade.mjs +192 -0
  45. package/tools/enroll.mjs +121 -0
  46. package/tools/get-best-operator.mjs +58 -0
  47. package/tools/get-leaderboard.mjs +26 -0
  48. package/tools/get-operator.mjs +33 -0
  49. package/tools/index.mjs +257 -0
  50. package/tools/optimize-efficiency.mjs +160 -0
  51. package/tools/rank-paste.mjs +51 -0
  52. package/tools/rank-windows.mjs +120 -0
  53. package/tools/self-improve.mjs +423 -0
  54. package/tools/simulate-change.mjs +207 -0
  55. package/tools/submit-paste.mjs +118 -0
  56. package/tools/submit-verified.mjs +153 -0
  57. package/tools/suggest-improvements.mjs +168 -0
  58. package/tools/tokenpull-compare.mjs +123 -0
  59. package/tools/tokenpull-submit.mjs +234 -0
  60. package/tools/tokenpull.mjs +113 -0
  61. package/tools/tokscale-breakdown.mjs +159 -0
  62. package/tools/tokscale-competitive-intel.mjs +45 -0
  63. package/tools/tokscale-cost-analysis.mjs +31 -0
  64. package/tools/tokscale-developer-profile.mjs +36 -0
  65. package/tools/tokscale-device-profile.mjs +35 -0
  66. package/tools/tokscale-market-share.mjs +38 -0
  67. package/tools/tokscale-mcp-usage.mjs +33 -0
  68. package/tools/tokscale-model-trends.mjs +32 -0
  69. package/tools/watch-tokenpull.mjs +134 -0
  70. package/tools.mjs +1 -3385
  71. package/tui.mjs +4 -2650
  72. package/banner.svg +0 -7
package/adapters.mjs CHANGED
@@ -1,750 +1 @@
1
- /**
2
- * adapters.mjs — SigRank tokenpull adapters for all supported platforms.
3
- *
4
- * OKF (code-file form — fields live in this comment, not raw frontmatter, so the .mjs
5
- * still parses as valid JS):
6
- * type: Reference
7
- * title: SigRank tokenpull adapters
8
- * description: Per-platform adapters implementing the tokenpull contract — async-generate
9
- * {id,sid,ts,input,output,cacheCreate,cacheRead,file} from local logs. Reasoning→output,
10
- * cost fields dropped, missing cacheCreate → estimated:true. Token-only, read-only.
11
- * tags: [sigrank, mcp, tokenpull, adapters, reference]
12
- * timestamp: 2026-06-23
13
- *
14
- * Each adapter implements the tokenpull contract:
15
- * messages(root): async generator → { id?, sid?, ts, input, output, cacheCreate, cacheRead, file }
16
- *
17
- * SigRank-specific mapping rules (applied consistently across all adapters):
18
- * - Reasoning / thinking tokens → folded into `output` (they are output-side spend)
19
- * - No cache-creation data available → cacheCreate: 0 + adapter sets `estimated: true`
20
- * - Cost fields (USD) → NEVER used or forwarded (SigRank scores cost efficiency from
21
- * token ratios, not from dollar amounts — cost efficiency is derived, not ingested)
22
- * - Credits / provider-specific fields → dropped
23
- *
24
- * "estimated" flag: set on the ADAPTER OBJECT (not per-record) when the adapter
25
- * cannot provide native cacheCreate values. tokenpull() and tokenpullCodex() already
26
- * handle this pattern; new adapters with estimated=true get the same treatment.
27
- *
28
- * SQLite adapters shell out to `sqlite3 -json` (macOS/Linux system tool, no npm dep).
29
- * If sqlite3 is unavailable the adapter returns an empty generator with a dataGap note.
30
- *
31
- * Data-gap notes (sources that can't provide full 4-pillar data) are attached on the
32
- * adapter object as `dataGap: string` so tokenpull() can surface them to the user.
33
- */
34
-
35
- import { readdir, readFile } from "node:fs/promises";
36
- import { join, dirname } from "node:path";
37
- import { homedir } from "node:os";
38
- import { execFile as execFileCb } from "node:child_process";
39
- import { promisify } from "node:util";
40
-
41
- const execFileP = promisify(execFileCb);
42
- const DAY_MS = 86_400_000; // shared with tokenpull.mjs but kept local to avoid circular import
43
-
44
- // ── File-system helpers ───────────────────────────────────────────────────────
45
-
46
- /** Recursively yield every file whose name matches `pred` under dir (skips symlink dirs). */
47
- async function* walkFiles(dir, pred, counter = { n: 0 }, max = 10_000) {
48
- if (counter.n >= max) return;
49
- let entries;
50
- try {
51
- entries = await readdir(dir, { withFileTypes: true });
52
- } catch {
53
- return;
54
- }
55
- for (const e of entries) {
56
- if (counter.n >= max) return;
57
- const full = join(dir, e.name);
58
- if (e.isSymbolicLink()) continue;
59
- if (e.isDirectory()) {
60
- yield* walkFiles(full, pred, counter, max);
61
- } else if (e.isFile() && pred(e.name)) {
62
- counter.n++;
63
- yield full;
64
- }
65
- }
66
- }
67
-
68
- const isJsonl = (n) =>
69
- n.endsWith(".jsonl") ||
70
- n.endsWith(".jsonl.deleted") ||
71
- n.match(/\.jsonl\.reset\.\d+$/);
72
- const isJson = (n) => n.endsWith(".json") && !n.endsWith(".jsonl");
73
-
74
- /** Read a file as UTF-8, return null on error. */
75
- async function readUtf8(path) {
76
- try {
77
- return await readFile(path, "utf8");
78
- } catch {
79
- return null;
80
- }
81
- }
82
-
83
- /** Parse each newline-delimited JSON line, yield parsed objects silently skipping bad lines. */
84
- function* parseJsonl(text, filePath) {
85
- if (!text) return;
86
- for (const line of text.split("\n")) {
87
- const s = line.trim();
88
- if (!s) continue;
89
- try {
90
- yield [JSON.parse(s), filePath];
91
- } catch {
92
- /* skip malformed */
93
- }
94
- }
95
- }
96
-
97
- /** Run sqlite3 -json and return parsed rows, or [] on error/unavailability.
98
- * execFile with an args array — no shell, so dbPath/sql need no quoting/escaping
99
- * (matches the execFile hardening used in tools.mjs / tokenpull.mjs). */
100
- async function sqliteJson(dbPath, sql) {
101
- try {
102
- const { stdout } = await execFileP("sqlite3", ["-json", dbPath, sql], {
103
- timeout: 10_000,
104
- });
105
- return JSON.parse(stdout || "[]");
106
- } catch {
107
- return [];
108
- }
109
- }
110
-
111
- // ── Env-var helper ────────────────────────────────────────────────────────────
112
- /** Resolve roots from env var or default. Supports comma-separated list. */
113
- function roots(envVar, defaultPath) {
114
- const v = process.env[envVar];
115
- if (v)
116
- return v
117
- .split(",")
118
- .map((s) => s.trim())
119
- .filter(Boolean);
120
- return [defaultPath];
121
- }
122
-
123
- // ── 1. Amp ────────────────────────────────────────────────────────────────────
124
- // ~/.local/share/amp/threads/**/*.json
125
- // Fields: assistant message usage: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens
126
- export const ampAdapter = {
127
- platform: "amp",
128
- defaultRoot: () => join(homedir(), ".local", "share", "amp"),
129
- async *messages(root) {
130
- for (const r of roots("AMP_DATA_DIR", root)) {
131
- const threadsDir = join(r, "threads");
132
- for await (const path of walkFiles(threadsDir, isJson)) {
133
- const text = await readUtf8(path);
134
- if (!text) continue;
135
- let thread;
136
- try {
137
- thread = JSON.parse(text);
138
- } catch {
139
- continue;
140
- }
141
- // Amp thread is an array or object with messages
142
- const msgs = Array.isArray(thread) ? thread : thread.messages || [];
143
- for (const msg of msgs) {
144
- if (!msg || msg.role !== "assistant") continue;
145
- const u = msg.usage || (msg.metadata && msg.metadata.usage) || {};
146
- const input = Number(u.input_tokens || u.inputTokens || 0);
147
- const output = Number(u.output_tokens || u.outputTokens || 0);
148
- const cacheCreate = Number(
149
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
150
- );
151
- const cacheRead = Number(
152
- u.cache_read_tokens || u.cacheReadTokens || 0,
153
- );
154
- if (input + output + cacheCreate + cacheRead === 0) continue;
155
- yield {
156
- id: msg.id || null,
157
- sid: thread.id || null,
158
- ts: msg.timestamp || msg.created_at || null,
159
- input,
160
- output,
161
- cacheCreate,
162
- cacheRead,
163
- file: path,
164
- };
165
- }
166
- }
167
- }
168
- },
169
- };
170
-
171
- // ── 2. Kimi ───────────────────────────────────────────────────────────────────
172
- // ~/.kimi/sessions/<group-id>/<session-id>/wire.jsonl
173
- // StatusUpdate lines only; token_usage: { input_other, output, input_cache_read, input_cache_creation }
174
- export const kimiAdapter = {
175
- platform: "kimi",
176
- defaultRoot: () => join(homedir(), ".kimi"),
177
- async *messages(root) {
178
- const sessionsDir = join(roots("KIMI_DATA_DIR", root)[0], "sessions");
179
- for await (const path of walkFiles(sessionsDir, isJsonl)) {
180
- const text = await readUtf8(path);
181
- for (const [ev] of parseJsonl(text, path)) {
182
- if (!ev || ev.type !== "StatusUpdate") continue;
183
- const u = ev.token_usage || {};
184
- const input = Number(u.input_other || 0);
185
- const output = Number(u.output || 0);
186
- const cacheCreate = Number(u.input_cache_creation || 0);
187
- const cacheRead = Number(u.input_cache_read || 0);
188
- if (input + output + cacheCreate + cacheRead === 0) continue;
189
- yield {
190
- id: ev.id || null,
191
- sid: null,
192
- ts: ev.timestamp || ev.created_at || null,
193
- input,
194
- output,
195
- cacheCreate,
196
- cacheRead,
197
- file: path,
198
- };
199
- }
200
- }
201
- },
202
- };
203
-
204
- // ── 3. Qwen ───────────────────────────────────────────────────────────────────
205
- // ~/.qwen/projects/{project}/chats/*.jsonl
206
- // usageMetadata: { promptTokenCount, candidatesTokenCount, cachedContentTokenCount, thoughtsTokenCount }
207
- // No cache creation field. Reasoning (thoughtsTokenCount) → output.
208
- export const qwenAdapter = {
209
- platform: "qwen",
210
- estimated: true, // no cacheCreate in logs
211
- defaultRoot: () => join(homedir(), ".qwen"),
212
- async *messages(root) {
213
- const projectsDir = join(roots("QWEN_DATA_DIR", root)[0], "projects");
214
- for await (const path of walkFiles(projectsDir, isJsonl)) {
215
- const text = await readUtf8(path);
216
- for (const [ev] of parseJsonl(text, path)) {
217
- if (!ev || !ev.usageMetadata) continue;
218
- const u = ev.usageMetadata;
219
- const rawInput = Number(u.promptTokenCount || 0);
220
- const rawOutput = Number(u.candidatesTokenCount || 0);
221
- const thoughts = Number(u.thoughtsTokenCount || 0); // reasoning → output
222
- const cacheRead = Number(u.cachedContentTokenCount || 0);
223
- // promptTokenCount is inclusive of cached; subtract to get fresh input
224
- const input = Math.max(0, rawInput - cacheRead);
225
- const output = rawOutput + thoughts;
226
- if (input + output + cacheRead === 0) continue;
227
- yield {
228
- id: ev.id || null,
229
- sid: null,
230
- ts: ev.timestamp || null,
231
- input,
232
- output,
233
- cacheCreate: 0,
234
- cacheRead,
235
- file: path,
236
- };
237
- }
238
- }
239
- },
240
- };
241
-
242
- // ── 4. pi-agent ───────────────────────────────────────────────────────────────
243
- // ~/.pi/agent/sessions/**/*.jsonl
244
- // Fields: inputTokens, outputTokens, cacheCreationTokens, cacheReadTokens
245
- export const piAdapter = {
246
- platform: "pi",
247
- defaultRoot: () => join(homedir(), ".pi", "agent", "sessions"),
248
- async *messages(root) {
249
- for (const r of roots("PI_AGENT_DIR", root)) {
250
- for await (const path of walkFiles(r, isJsonl)) {
251
- const text = await readUtf8(path);
252
- for (const [ev] of parseJsonl(text, path)) {
253
- if (!ev) continue;
254
- // pi-agent stores usage in assistant messages or usage events
255
- const u = ev.usage || ev;
256
- const input = Number(u.inputTokens || u.input_tokens || 0);
257
- const output = Number(u.outputTokens || u.output_tokens || 0);
258
- const cacheCreate = Number(
259
- u.cacheCreationTokens || u.cache_creation_tokens || 0,
260
- );
261
- const cacheRead = Number(
262
- u.cacheReadTokens || u.cache_read_tokens || 0,
263
- );
264
- if (input + output + cacheCreate + cacheRead === 0) continue;
265
- yield {
266
- id: ev.id || null,
267
- sid: ev.sessionId || null,
268
- ts: ev.timestamp || null,
269
- input,
270
- output,
271
- cacheCreate,
272
- cacheRead,
273
- file: path,
274
- };
275
- }
276
- }
277
- }
278
- },
279
- };
280
-
281
- // ── 5. OpenClaw ───────────────────────────────────────────────────────────────
282
- // ~/.openclaw/ (also ~/.clawdbot/, ~/.moltbot/, ~/.moldbot/)
283
- // agents/<agentId>/sessions/<uuid>.jsonl (+ .deleted.<ts> and .reset.<ts> variants)
284
- // Per-message: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens
285
- export const openclawAdapter = {
286
- platform: "openclaw",
287
- defaultRoot: () => {
288
- const env = process.env.OPENCLAW_DIR;
289
- if (env) return env.split(",")[0].trim();
290
- return join(homedir(), ".openclaw");
291
- },
292
- async *messages(root) {
293
- const dirs = process.env.OPENCLAW_DIR
294
- ? process.env.OPENCLAW_DIR.split(",").map((s) => s.trim())
295
- : [
296
- join(homedir(), ".openclaw"),
297
- join(homedir(), ".clawdbot"),
298
- join(homedir(), ".moltbot"),
299
- join(homedir(), ".moldbot"),
300
- ];
301
- const pred = (n) => isJsonl(n) || n.endsWith(".json"); // covers archived variants
302
- for (const dir of dirs) {
303
- for await (const path of walkFiles(dir, pred)) {
304
- const text = await readUtf8(path);
305
- for (const [ev] of parseJsonl(text, path)) {
306
- if (!ev || ev.role !== "assistant") continue;
307
- const u = ev.usage || ev.tokens || {};
308
- const input = Number(u.input_tokens || u.inputTokens || 0);
309
- const output = Number(u.output_tokens || u.outputTokens || 0);
310
- const cacheCreate = Number(
311
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
312
- );
313
- const cacheRead = Number(
314
- u.cache_read_tokens || u.cacheReadTokens || 0,
315
- );
316
- if (input + output + cacheCreate + cacheRead === 0) continue;
317
- yield {
318
- id: ev.id || null,
319
- sid: null,
320
- ts: ev.timestamp || null,
321
- input,
322
- output,
323
- cacheCreate,
324
- cacheRead,
325
- file: path,
326
- };
327
- }
328
- }
329
- }
330
- },
331
- };
332
-
333
- // ── 6. Droid ──────────────────────────────────────────────────────────────────
334
- // ~/.factory/sessions/**/*.settings.json
335
- // Fields: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens, thinking_tokens
336
- // Thinking tokens → output. Per-settings-file (session-level granularity).
337
- // CUMULATIVE-COLUMN GUARD: settings files contain session-level totals. If the same session
338
- // appears in multiple files, summing double-counts. We set both id and sid to the session id
339
- // so tokenpull()'s (sid|id) keep-last dedup collapses duplicates to the latest values.
340
- export const droidAdapter = {
341
- platform: "droid",
342
- defaultRoot: () => join(homedir(), ".factory", "sessions"),
343
- async *messages(root) {
344
- for (const r of roots("DROID_SESSIONS_DIR", root)) {
345
- for await (const path of walkFiles(r, (n) =>
346
- n.endsWith(".settings.json"),
347
- )) {
348
- const text = await readUtf8(path);
349
- if (!text) continue;
350
- let s;
351
- try {
352
- s = JSON.parse(text);
353
- } catch {
354
- continue;
355
- }
356
- const input = Number(s.input_tokens || 0);
357
- const output =
358
- Number(s.output_tokens || 0) + Number(s.thinking_tokens || 0);
359
- const cacheCreate = Number(s.cache_creation_tokens || 0);
360
- const cacheRead = Number(s.cache_read_tokens || 0);
361
- if (input + output + cacheCreate + cacheRead === 0) continue;
362
- const sessionId = String(s.session_id || s.id || "");
363
- yield {
364
- id: sessionId,
365
- sid: sessionId,
366
- ts: s.updated_at || s.created_at || null,
367
- input,
368
- output,
369
- cacheCreate,
370
- cacheRead,
371
- file: path,
372
- };
373
- }
374
- }
375
- },
376
- };
377
-
378
- // ── 7. Codebuff ───────────────────────────────────────────────────────────────
379
- // ~/.config/manicode/projects/<project>/chats/<chat-id>/chat-messages.json
380
- // assistant messages: metadata.usage or metadata.codebuff.usage
381
- export const codebuffAdapter = {
382
- platform: "codebuff",
383
- defaultRoot: () => join(homedir(), ".config", "manicode"),
384
- async *messages(root) {
385
- const dirs = process.env.CODEBUFF_DATA_DIR
386
- ? process.env.CODEBUFF_DATA_DIR.split(",").map((s) => s.trim())
387
- : [
388
- join(homedir(), ".config", "manicode"),
389
- join(homedir(), ".config", "manicode-dev"),
390
- join(homedir(), ".config", "manicode-staging"),
391
- ];
392
- for (const dir of dirs) {
393
- for await (const path of walkFiles(
394
- dir,
395
- (n) => n === "chat-messages.json",
396
- )) {
397
- const text = await readUtf8(path);
398
- if (!text) continue;
399
- let msgs;
400
- try {
401
- msgs = JSON.parse(text);
402
- } catch {
403
- continue;
404
- }
405
- if (!Array.isArray(msgs)) continue;
406
- for (const msg of msgs) {
407
- if (!msg || msg.role !== "assistant") continue;
408
- const u =
409
- (msg.metadata &&
410
- (msg.metadata.usage || msg.metadata.codebuff?.usage)) ||
411
- {};
412
- const input = Number(u.input_tokens || u.inputTokens || 0);
413
- const output = Number(u.output_tokens || u.outputTokens || 0);
414
- const cacheCreate = Number(
415
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
416
- );
417
- const cacheRead = Number(
418
- u.cache_read_tokens || u.cacheReadTokens || 0,
419
- );
420
- if (input + output + cacheCreate + cacheRead === 0) continue;
421
- yield {
422
- id: msg.id || null,
423
- sid: null,
424
- ts: msg.timestamp || msg.created_at || null,
425
- input,
426
- output,
427
- cacheCreate,
428
- cacheRead,
429
- file: path,
430
- };
431
- }
432
- }
433
- }
434
- },
435
- };
436
-
437
- // ── 8. Gemini CLI ─────────────────────────────────────────────────────────────
438
- // ~/.gemini/tmp/*/chats/*.json and *.jsonl
439
- // Fields: input, output, cached, thought (reasoning), tool, total
440
- // SigRank mapping: input = input−cached (fresh), cacheRead = cached, cacheCreate = 0 (not exposed),
441
- // output = output + thought (reasoning→output)
442
- export const geminiAdapter = {
443
- platform: "gemini",
444
- estimated: true, // no cacheCreate field in Gemini logs
445
- defaultRoot: () => join(homedir(), ".gemini", "tmp"),
446
- async *messages(root) {
447
- for (const r of roots("GEMINI_DATA_DIR", root)) {
448
- for await (const path of walkFiles(
449
- r,
450
- (n) => n.endsWith(".json") || n.endsWith(".jsonl"),
451
- )) {
452
- const text = await readUtf8(path);
453
- if (!text) continue;
454
- // Try JSONL first, then single JSON
455
- let parsed = [];
456
- try {
457
- parsed = text
458
- .trim()
459
- .split("\n")
460
- .filter(Boolean)
461
- .map((l) => JSON.parse(l));
462
- } catch {
463
- try {
464
- parsed = [JSON.parse(text)];
465
- } catch {
466
- continue;
467
- }
468
- }
469
- for (const ev of parsed) {
470
- if (!ev) continue;
471
- // Gemini usage may be at top level or nested in usageMetadata
472
- const u = ev.usageMetadata || ev.usage || ev;
473
- const rawInput = Number(u.input || u.promptTokenCount || 0);
474
- const rawOutput = Number(u.output || u.candidatesTokenCount || 0);
475
- const cached = Number(u.cached || u.cachedContentTokenCount || 0);
476
- const thought = Number(u.thought || u.thoughtsTokenCount || 0);
477
- if (rawInput + rawOutput + cached + thought === 0) continue;
478
- const input = Math.max(0, rawInput - cached); // strip cached from input
479
- const output = rawOutput + thought; // reasoning → output
480
- yield {
481
- id: ev.id || null,
482
- sid: null,
483
- ts: ev.timestamp || null,
484
- input,
485
- output,
486
- cacheCreate: 0,
487
- cacheRead: cached,
488
- file: path,
489
- };
490
- }
491
- }
492
- }
493
- },
494
- };
495
-
496
- // ── 9. GitHub Copilot CLI ─────────────────────────────────────────────────────
497
- // ~/.copilot/otel/*.jsonl (requires COPILOT_OTEL_ENABLED=true before session start)
498
- // OpenTelemetry spans; looks for llm.token_count.prompt / completion / cached attributes
499
- export const copilotAdapter = {
500
- platform: "copilot",
501
- defaultRoot: () => join(homedir(), ".copilot", "otel"),
502
- async *messages(root) {
503
- const dir = process.env.COPILOT_OTEL_FILE_EXPORTER_PATH
504
- ? dirname(process.env.COPILOT_OTEL_FILE_EXPORTER_PATH)
505
- : root;
506
- for await (const path of walkFiles(dir, isJsonl)) {
507
- const text = await readUtf8(path);
508
- for (const [ev] of parseJsonl(text, path)) {
509
- if (!ev) continue;
510
- // OTel span: attributes may be an object or array of {key,value} pairs
511
- const attrs = ev.attributes || ev.resource?.attributes || {};
512
- const get = (k) => {
513
- if (typeof attrs === "object" && !Array.isArray(attrs))
514
- return attrs[k];
515
- if (Array.isArray(attrs)) {
516
- const a = attrs.find((x) => x.key === k);
517
- return a?.value?.intValue ?? a?.value?.stringValue ?? null;
518
- }
519
- return null;
520
- };
521
- const input = Number(
522
- get("llm.token_count.prompt") ||
523
- get("gen_ai.usage.input_tokens") ||
524
- 0,
525
- );
526
- const output = Number(
527
- get("llm.token_count.completion") ||
528
- get("gen_ai.usage.output_tokens") ||
529
- 0,
530
- );
531
- const cacheCreate = Number(get("llm.token_count.cache_creation") || 0);
532
- const cacheRead = Number(
533
- get("llm.token_count.cache_read") ||
534
- get("gen_ai.usage.cache_read_input_tokens") ||
535
- 0,
536
- );
537
- if (input + output + cacheCreate + cacheRead === 0) continue;
538
- yield {
539
- id: ev.traceId || ev.spanId || null,
540
- sid: null,
541
- ts: ev.startTimeUnixNano
542
- ? new Date(Number(ev.startTimeUnixNano) / 1e6).toISOString()
543
- : null,
544
- input,
545
- output,
546
- cacheCreate,
547
- cacheRead,
548
- file: path,
549
- };
550
- }
551
- }
552
- },
553
- setupNote:
554
- "Requires COPILOT_OTEL_ENABLED=true and COPILOT_OTEL_EXPORTER_TYPE=file set BEFORE starting the Copilot session. Without this, no local token logs are written.",
555
- };
556
-
557
- // ── 10. OpenCode ──────────────────────────────────────────────────────────────
558
- // ~/.local/share/opencode — JSON message files, but costs stored as 0 and token fields
559
- // are calculated via LiteLLM pricing (not stored in the log). No raw token fields.
560
- // SigRank cannot derive pillars from OpenCode logs with current log format.
561
- export const opencodeAdapter = {
562
- platform: "opencode",
563
- defaultRoot: () => join(homedir(), ".local", "share", "opencode"),
564
- estimated: true,
565
- dataGap:
566
- "OpenCode logs store cost:0 and derive tokens via LiteLLM at runtime — raw token counts are not persisted. SigRank cannot read pillars from OpenCode logs with the current format. Track https://github.com/ccusage/ccusage for format changes.",
567
- // eslint-disable-next-line require-yield
568
- async *messages() {
569
- /* no data available */
570
- },
571
- };
572
-
573
- // ── 11. Goose ─────────────────────────────────────────────────────────────────
574
- // SQLite: sessions.db at standard Goose data roots or $GOOSE_PATH_ROOT/data/sessions/sessions.db
575
- // Columns: accumulated_input_tokens (or input_tokens), accumulated_output_tokens (or output_tokens),
576
- // accumulated_total_tokens (or total_tokens). NO cache fields. Reasoning = total-input-output.
577
- // CUMULATIVE-COLUMN GUARD: accumulated_* columns are per-session running totals. If the sessions
578
- // table has multiple rows per session id, summing them downstream double-counts. We set both sid
579
- // and id to the session id and ORDER BY updated_at so tokenpull()'s (sid|id) keep-last dedup
580
- // collapses duplicate session rows to the latest (max accumulated) values.
581
- export const gooseAdapter = {
582
- platform: "goose",
583
- estimated: true, // no cacheCreate or cacheRead
584
- defaultRoot: () => {
585
- const env = process.env.GOOSE_PATH_ROOT;
586
- if (env) return env;
587
- // Standard locations (macOS first, then XDG)
588
- return join(homedir(), "Library", "Application Support", "goose");
589
- },
590
- async *messages(root) {
591
- const dbCandidates = process.env.GOOSE_PATH_ROOT
592
- ? [join(process.env.GOOSE_PATH_ROOT, "data", "sessions", "sessions.db")]
593
- : [
594
- join(
595
- homedir(),
596
- "Library",
597
- "Application Support",
598
- "goose",
599
- "sessions",
600
- "sessions.db",
601
- ),
602
- join(
603
- homedir(),
604
- ".local",
605
- "share",
606
- "goose",
607
- "sessions",
608
- "sessions.db",
609
- ),
610
- join(
611
- homedir(),
612
- ".local",
613
- "share",
614
- "Block",
615
- "goose",
616
- "sessions",
617
- "sessions.db",
618
- ),
619
- ];
620
- for (const db of dbCandidates) {
621
- // ORDER BY updated_at so the latest cumulative row per session is yielded last
622
- // (tokenpull keep-last dedup picks the final row = max accumulated values).
623
- const rows = await sqliteJson(
624
- db,
625
- "SELECT * FROM sessions ORDER BY updated_at",
626
- );
627
- for (const row of rows) {
628
- const input = Number(
629
- row.accumulated_input_tokens || row.input_tokens || 0,
630
- );
631
- const output = Number(
632
- row.accumulated_output_tokens || row.output_tokens || 0,
633
- );
634
- const total = Number(
635
- row.accumulated_total_tokens || row.total_tokens || 0,
636
- );
637
- const reasoning = Math.max(0, total - input - output); // folded into output
638
- if (input + output === 0) continue;
639
- const sessionId = String(row.id || row.session_id || "");
640
- yield {
641
- id: sessionId,
642
- sid: sessionId,
643
- ts: row.created_at || row.updated_at || null,
644
- input,
645
- output: output + reasoning,
646
- cacheCreate: 0,
647
- cacheRead: 0,
648
- file: db,
649
- };
650
- }
651
- }
652
- },
653
- };
654
-
655
- // ── 12. Kilo ──────────────────────────────────────────────────────────────────
656
- // SQLite: ~/.local/share/kilo/kilo.db
657
- // Per-message rows with model, input/output/cache token columns.
658
- export const kiloAdapter = {
659
- platform: "kilo",
660
- defaultRoot: () => join(homedir(), ".local", "share", "kilo"),
661
- async *messages(root) {
662
- const dbPath = join(roots("KILO_DATA_DIR", root)[0], "kilo.db");
663
- const rows = await sqliteJson(
664
- dbPath,
665
- 'SELECT * FROM messages WHERE role="assistant"',
666
- );
667
- for (const row of rows) {
668
- const input = Number(row.input_tokens || row.inputTokens || 0);
669
- const output = Number(row.output_tokens || row.outputTokens || 0);
670
- const cacheCreate = Number(
671
- row.cache_creation_tokens || row.cacheCreationTokens || 0,
672
- );
673
- const cacheRead = Number(
674
- row.cache_read_tokens || row.cacheReadTokens || 0,
675
- );
676
- if (input + output + cacheCreate + cacheRead === 0) continue;
677
- yield {
678
- id: String(row.id || ""),
679
- sid: String(row.session_id || row.sessionId || ""),
680
- ts: row.created_at || row.timestamp || null,
681
- input,
682
- output,
683
- cacheCreate,
684
- cacheRead,
685
- file: dbPath,
686
- };
687
- }
688
- },
689
- };
690
-
691
- // ── 13. Hermes Agent ─────────────────────────────────────────────────────────
692
- // SQLite: ~/.hermes/state.db
693
- // Per-session rows: input, output, cache_read, cache_write (=cacheCreate), reasoning_tokens → output
694
- // CUMULATIVE-COLUMN GUARD: session rows may contain cumulative totals. If the same session
695
- // appears in multiple rows, summing double-counts. We set both id and sid to the session id
696
- // and ORDER BY updated_at so tokenpull()'s (sid|id) keep-last dedup collapses duplicates
697
- // to the latest values.
698
- export const hermesAdapter = {
699
- platform: "hermes",
700
- defaultRoot: () => join(homedir(), ".hermes"),
701
- async *messages(root) {
702
- for (const r of roots("HERMES_HOME", root)) {
703
- const dbPath = join(r, "state.db");
704
- // ORDER BY updated_at so the latest cumulative row per session is yielded last.
705
- const rows = await sqliteJson(
706
- dbPath,
707
- "SELECT * FROM sessions ORDER BY updated_at",
708
- );
709
- for (const row of rows) {
710
- const input = Number(row.input || 0);
711
- const reasoning = Number(row.reasoning_tokens || 0);
712
- const output = Number(row.output || 0) + reasoning;
713
- const cacheCreate = Number(row.cache_write || row.cache_creation || 0);
714
- const cacheRead = Number(row.cache_read || 0);
715
- if (input + output + cacheCreate + cacheRead === 0) continue;
716
- const sessionId = String(row.id || row.session_id || "");
717
- yield {
718
- id: sessionId,
719
- sid: sessionId,
720
- ts: row.created_at || row.updated_at || null,
721
- input,
722
- output,
723
- cacheCreate,
724
- cacheRead,
725
- file: dbPath,
726
- };
727
- }
728
- }
729
- },
730
- };
731
-
732
- // ── Registry ──────────────────────────────────────────────────────────────────
733
- /** All non-Claude, non-Codex adapters keyed by platform ID. */
734
- export const ADAPTERS = {
735
- amp: ampAdapter,
736
- kimi: kimiAdapter,
737
- qwen: qwenAdapter,
738
- pi: piAdapter,
739
- openclaw: openclawAdapter,
740
- droid: droidAdapter,
741
- codebuff: codebuffAdapter,
742
- gemini: geminiAdapter,
743
- copilot: copilotAdapter,
744
- opencode: opencodeAdapter,
745
- goose: gooseAdapter,
746
- kilo: kiloAdapter,
747
- hermes: hermesAdapter,
748
- };
749
-
750
- export const ALL_PLATFORMS = Object.keys(ADAPTERS).concat(["claude", "codex"]);
1
+ export * from './adapters/index.mjs';