sigrank 0.18.5 → 1.0.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +158 -0
  2. package/MCP_SERVER_DESCRIPTION.md +172 -0
  3. package/README.md +94 -41
  4. package/adapters/index.mjs +1100 -0
  5. package/adapters/tokenpull.mjs +568 -0
  6. package/adapters.mjs +1 -857
  7. package/analytics/cascade.mjs +338 -0
  8. package/cascade.mjs +1 -274
  9. package/cli.mjs +1 -1853
  10. package/connect.mjs +1 -13
  11. package/identity/connect.mjs +13 -0
  12. package/identity/keystore.mjs +231 -0
  13. package/identity/sign.mjs +106 -0
  14. package/index.mjs +47 -162
  15. package/keystore.mjs +1 -242
  16. package/lib/constants.mjs +137 -0
  17. package/lib/pkg-version.mjs +59 -0
  18. package/manifest.json +46 -0
  19. package/narrate.mjs +1 -77
  20. package/package.json +41 -16
  21. package/preflight.mjs +1 -191
  22. package/presentation/cli.mjs +1926 -0
  23. package/presentation/narrate.mjs +219 -0
  24. package/presentation/tui-themes.mjs +189 -0
  25. package/presentation/tui.mjs +3316 -0
  26. package/prompts/check-my-efficiency.md +1 -0
  27. package/prompts/compare-with-leader.md +1 -0
  28. package/prompts/simulate-improvement.md +1 -0
  29. package/proxy.mjs +492 -0
  30. package/resources/class-tiers.md +81 -0
  31. package/resources/data-policy.md +31 -0
  32. package/resources/install-guide.md +59 -0
  33. package/resources/privacy-model.md +44 -0
  34. package/resources/scoring-formula.md +24 -0
  35. package/review.mjs +263 -0
  36. package/sign.mjs +1 -106
  37. package/submit/index.mjs +297 -0
  38. package/submit/preflight.mjs +191 -0
  39. package/submit.mjs +1 -303
  40. package/tokenpull.mjs +1 -576
  41. package/tokscale_analytics.mjs +864 -0
  42. package/tools/_framing.mjs +217 -0
  43. package/tools/_helpers.mjs +253 -0
  44. package/tools/_peer-matching.mjs +361 -0
  45. package/tools/_report.mjs +262 -0
  46. package/tools/_schemas.mjs +492 -0
  47. package/tools/_verifiers.mjs +148 -0
  48. package/tools/compare-operators.mjs +80 -0
  49. package/tools/compare-self.mjs +124 -0
  50. package/tools/describe-power-user.mjs +78 -0
  51. package/tools/diagnose-cascade.mjs +192 -0
  52. package/tools/discover-peers.mjs +198 -0
  53. package/tools/enroll.mjs +119 -0
  54. package/tools/get-best-operator.mjs +59 -0
  55. package/tools/get-leaderboard.mjs +27 -0
  56. package/tools/get-operator.mjs +33 -0
  57. package/tools/index.mjs +229 -0
  58. package/tools/optimize-efficiency.mjs +170 -0
  59. package/tools/rank-paste.mjs +51 -0
  60. package/tools/rank-windows.mjs +120 -0
  61. package/tools/self-improve.mjs +423 -0
  62. package/tools/simulate-change.mjs +207 -0
  63. package/tools/submit-paste.mjs +121 -0
  64. package/tools/submit-verified.mjs +153 -0
  65. package/tools/suggest-improvements.mjs +168 -0
  66. package/tools/tokenpull-compare.mjs +123 -0
  67. package/tools/tokenpull-submit.mjs +235 -0
  68. package/tools/tokenpull.mjs +113 -0
  69. package/tools/tokscale-breakdown.mjs +119 -0
  70. package/tools/tokscale-competitive-intel.mjs +45 -0
  71. package/tools/tokscale-cost-analysis.mjs +31 -0
  72. package/tools/tokscale-developer-profile.mjs +36 -0
  73. package/tools/tokscale-device-profile.mjs +35 -0
  74. package/tools/tokscale-market-share.mjs +38 -0
  75. package/tools/tokscale-mcp-usage.mjs +33 -0
  76. package/tools/tokscale-model-trends.mjs +32 -0
  77. package/tools/watch-tokenpull.mjs +134 -0
  78. package/tools.mjs +1 -3897
  79. package/tui.mjs +4 -2650
  80. package/banner.svg +0 -7
package/adapters.mjs CHANGED
@@ -1,857 +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, timeoutMs = 10_000) {
101
- try {
102
- const { stdout } = await execFileP("sqlite3", ["-json", dbPath, sql], {
103
- timeout: timeoutMs,
104
- maxBuffer: 256 * 1024 * 1024, // 256MB — Devin's sessions.db yields ~15MB JSON
105
- });
106
- return JSON.parse(stdout || "[]");
107
- } catch {
108
- return [];
109
- }
110
- }
111
-
112
- // ── Env-var helper ────────────────────────────────────────────────────────────
113
- /** Resolve roots from env var or default. Supports comma-separated list. */
114
- function roots(envVar, defaultPath) {
115
- const v = process.env[envVar];
116
- if (v)
117
- return v
118
- .split(",")
119
- .map((s) => s.trim())
120
- .filter(Boolean);
121
- return [defaultPath];
122
- }
123
-
124
- // ── 1. Amp ────────────────────────────────────────────────────────────────────
125
- // ~/.local/share/amp/threads/**/*.json
126
- // Fields: assistant message usage: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens
127
- export const ampAdapter = {
128
- platform: "amp",
129
- defaultRoot: () => join(homedir(), ".local", "share", "amp"),
130
- async *messages(root) {
131
- for (const r of roots("AMP_DATA_DIR", root)) {
132
- const threadsDir = join(r, "threads");
133
- for await (const path of walkFiles(threadsDir, isJson)) {
134
- const text = await readUtf8(path);
135
- if (!text) continue;
136
- let thread;
137
- try {
138
- thread = JSON.parse(text);
139
- } catch {
140
- continue;
141
- }
142
- // Amp thread is an array or object with messages
143
- const msgs = Array.isArray(thread) ? thread : thread.messages || [];
144
- for (const msg of msgs) {
145
- if (!msg || msg.role !== "assistant") continue;
146
- const u = msg.usage || (msg.metadata && msg.metadata.usage) || {};
147
- const input = Number(u.input_tokens || u.inputTokens || 0);
148
- const output = Number(u.output_tokens || u.outputTokens || 0);
149
- const cacheCreate = Number(
150
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
151
- );
152
- const cacheRead = Number(
153
- u.cache_read_tokens || u.cacheReadTokens || 0,
154
- );
155
- if (input + output + cacheCreate + cacheRead === 0) continue;
156
- yield {
157
- id: msg.id || null,
158
- sid: thread.id || null,
159
- ts: msg.timestamp || msg.created_at || null,
160
- input,
161
- output,
162
- cacheCreate,
163
- cacheRead,
164
- file: path,
165
- };
166
- }
167
- }
168
- }
169
- },
170
- };
171
-
172
- // ── 2. Kimi ───────────────────────────────────────────────────────────────────
173
- // ~/.kimi/sessions/<group-id>/<session-id>/wire.jsonl
174
- // StatusUpdate lines only; token_usage: { input_other, output, input_cache_read, input_cache_creation }
175
- export const kimiAdapter = {
176
- platform: "kimi",
177
- defaultRoot: () => join(homedir(), ".kimi"),
178
- async *messages(root) {
179
- const sessionsDir = join(roots("KIMI_DATA_DIR", root)[0], "sessions");
180
- for await (const path of walkFiles(sessionsDir, isJsonl)) {
181
- const text = await readUtf8(path);
182
- for (const [ev] of parseJsonl(text, path)) {
183
- if (!ev || ev.type !== "StatusUpdate") continue;
184
- const u = ev.token_usage || {};
185
- const input = Number(u.input_other || 0);
186
- const output = Number(u.output || 0);
187
- const cacheCreate = Number(u.input_cache_creation || 0);
188
- const cacheRead = Number(u.input_cache_read || 0);
189
- if (input + output + cacheCreate + cacheRead === 0) continue;
190
- yield {
191
- id: ev.id || null,
192
- sid: null,
193
- ts: ev.timestamp || ev.created_at || null,
194
- input,
195
- output,
196
- cacheCreate,
197
- cacheRead,
198
- file: path,
199
- };
200
- }
201
- }
202
- },
203
- };
204
-
205
- // ── 3. Qwen ───────────────────────────────────────────────────────────────────
206
- // ~/.qwen/projects/{project}/chats/*.jsonl
207
- // usageMetadata: { promptTokenCount, candidatesTokenCount, cachedContentTokenCount, thoughtsTokenCount }
208
- // No cache creation field. Reasoning (thoughtsTokenCount) → output.
209
- export const qwenAdapter = {
210
- platform: "qwen",
211
- estimated: true, // no cacheCreate in logs
212
- defaultRoot: () => join(homedir(), ".qwen"),
213
- async *messages(root) {
214
- const projectsDir = join(roots("QWEN_DATA_DIR", root)[0], "projects");
215
- for await (const path of walkFiles(projectsDir, isJsonl)) {
216
- const text = await readUtf8(path);
217
- for (const [ev] of parseJsonl(text, path)) {
218
- if (!ev || !ev.usageMetadata) continue;
219
- const u = ev.usageMetadata;
220
- const rawInput = Number(u.promptTokenCount || 0);
221
- const rawOutput = Number(u.candidatesTokenCount || 0);
222
- const thoughts = Number(u.thoughtsTokenCount || 0); // reasoning → output
223
- const cacheRead = Number(u.cachedContentTokenCount || 0);
224
- // promptTokenCount is inclusive of cached; subtract to get fresh input
225
- const input = Math.max(0, rawInput - cacheRead);
226
- const output = rawOutput + thoughts;
227
- if (input + output + cacheRead === 0) continue;
228
- yield {
229
- id: ev.id || null,
230
- sid: null,
231
- ts: ev.timestamp || null,
232
- input,
233
- output,
234
- cacheCreate: 0,
235
- cacheRead,
236
- file: path,
237
- };
238
- }
239
- }
240
- },
241
- };
242
-
243
- // ── 4. pi-agent ───────────────────────────────────────────────────────────────
244
- // ~/.pi/agent/sessions/**/*.jsonl
245
- // Fields: inputTokens, outputTokens, cacheCreationTokens, cacheReadTokens
246
- export const piAdapter = {
247
- platform: "pi",
248
- defaultRoot: () => join(homedir(), ".pi", "agent", "sessions"),
249
- async *messages(root) {
250
- for (const r of roots("PI_AGENT_DIR", root)) {
251
- for await (const path of walkFiles(r, isJsonl)) {
252
- const text = await readUtf8(path);
253
- for (const [ev] of parseJsonl(text, path)) {
254
- if (!ev) continue;
255
- // pi-agent stores usage in assistant messages or usage events
256
- const u = ev.usage || ev;
257
- const input = Number(u.inputTokens || u.input_tokens || 0);
258
- const output = Number(u.outputTokens || u.output_tokens || 0);
259
- const cacheCreate = Number(
260
- u.cacheCreationTokens || u.cache_creation_tokens || 0,
261
- );
262
- const cacheRead = Number(
263
- u.cacheReadTokens || u.cache_read_tokens || 0,
264
- );
265
- if (input + output + cacheCreate + cacheRead === 0) continue;
266
- yield {
267
- id: ev.id || null,
268
- sid: ev.sessionId || null,
269
- ts: ev.timestamp || null,
270
- input,
271
- output,
272
- cacheCreate,
273
- cacheRead,
274
- file: path,
275
- };
276
- }
277
- }
278
- }
279
- },
280
- };
281
-
282
- // ── 5. OpenClaw ───────────────────────────────────────────────────────────────
283
- // ~/.openclaw/ (also ~/.clawdbot/, ~/.moltbot/, ~/.moldbot/)
284
- // agents/<agentId>/sessions/<uuid>.jsonl (+ .deleted.<ts> and .reset.<ts> variants)
285
- // Per-message: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens
286
- export const openclawAdapter = {
287
- platform: "openclaw",
288
- defaultRoot: () => {
289
- const env = process.env.OPENCLAW_DIR;
290
- if (env) return env.split(",")[0].trim();
291
- return join(homedir(), ".openclaw");
292
- },
293
- async *messages(root) {
294
- const dirs = process.env.OPENCLAW_DIR
295
- ? process.env.OPENCLAW_DIR.split(",").map((s) => s.trim())
296
- : [
297
- join(homedir(), ".openclaw"),
298
- join(homedir(), ".clawdbot"),
299
- join(homedir(), ".moltbot"),
300
- join(homedir(), ".moldbot"),
301
- ];
302
- const pred = (n) => isJsonl(n) || n.endsWith(".json"); // covers archived variants
303
- for (const dir of dirs) {
304
- for await (const path of walkFiles(dir, pred)) {
305
- const text = await readUtf8(path);
306
- for (const [ev] of parseJsonl(text, path)) {
307
- if (!ev || ev.role !== "assistant") continue;
308
- const u = ev.usage || ev.tokens || {};
309
- const input = Number(u.input_tokens || u.inputTokens || 0);
310
- const output = Number(u.output_tokens || u.outputTokens || 0);
311
- const cacheCreate = Number(
312
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
313
- );
314
- const cacheRead = Number(
315
- u.cache_read_tokens || u.cacheReadTokens || 0,
316
- );
317
- if (input + output + cacheCreate + cacheRead === 0) continue;
318
- yield {
319
- id: ev.id || null,
320
- sid: null,
321
- ts: ev.timestamp || null,
322
- input,
323
- output,
324
- cacheCreate,
325
- cacheRead,
326
- file: path,
327
- };
328
- }
329
- }
330
- }
331
- },
332
- };
333
-
334
- // ── 6. Droid ──────────────────────────────────────────────────────────────────
335
- // ~/.factory/sessions/**/*.settings.json
336
- // Fields: input_tokens, output_tokens, cache_creation_tokens, cache_read_tokens, thinking_tokens
337
- // Thinking tokens → output. Per-settings-file (session-level granularity).
338
- // CUMULATIVE-COLUMN GUARD: settings files contain session-level totals. If the same session
339
- // appears in multiple files, summing double-counts. We set both id and sid to the session id
340
- // so tokenpull()'s (sid|id) keep-last dedup collapses duplicates to the latest values.
341
- export const droidAdapter = {
342
- platform: "droid",
343
- defaultRoot: () => join(homedir(), ".factory", "sessions"),
344
- async *messages(root) {
345
- for (const r of roots("DROID_SESSIONS_DIR", root)) {
346
- for await (const path of walkFiles(r, (n) =>
347
- n.endsWith(".settings.json"),
348
- )) {
349
- const text = await readUtf8(path);
350
- if (!text) continue;
351
- let s;
352
- try {
353
- s = JSON.parse(text);
354
- } catch {
355
- continue;
356
- }
357
- const input = Number(s.input_tokens || 0);
358
- const output =
359
- Number(s.output_tokens || 0) + Number(s.thinking_tokens || 0);
360
- const cacheCreate = Number(s.cache_creation_tokens || 0);
361
- const cacheRead = Number(s.cache_read_tokens || 0);
362
- if (input + output + cacheCreate + cacheRead === 0) continue;
363
- const sessionId = String(s.session_id || s.id || "");
364
- yield {
365
- id: sessionId,
366
- sid: sessionId,
367
- ts: s.updated_at || s.created_at || null,
368
- input,
369
- output,
370
- cacheCreate,
371
- cacheRead,
372
- file: path,
373
- };
374
- }
375
- }
376
- },
377
- };
378
-
379
- // ── 7. Codebuff ───────────────────────────────────────────────────────────────
380
- // ~/.config/manicode/projects/<project>/chats/<chat-id>/chat-messages.json
381
- // assistant messages: metadata.usage or metadata.codebuff.usage
382
- export const codebuffAdapter = {
383
- platform: "codebuff",
384
- defaultRoot: () => join(homedir(), ".config", "manicode"),
385
- async *messages(root) {
386
- const dirs = process.env.CODEBUFF_DATA_DIR
387
- ? process.env.CODEBUFF_DATA_DIR.split(",").map((s) => s.trim())
388
- : [
389
- join(homedir(), ".config", "manicode"),
390
- join(homedir(), ".config", "manicode-dev"),
391
- join(homedir(), ".config", "manicode-staging"),
392
- ];
393
- for (const dir of dirs) {
394
- for await (const path of walkFiles(
395
- dir,
396
- (n) => n === "chat-messages.json",
397
- )) {
398
- const text = await readUtf8(path);
399
- if (!text) continue;
400
- let msgs;
401
- try {
402
- msgs = JSON.parse(text);
403
- } catch {
404
- continue;
405
- }
406
- if (!Array.isArray(msgs)) continue;
407
- for (const msg of msgs) {
408
- if (!msg || msg.role !== "assistant") continue;
409
- const u =
410
- (msg.metadata &&
411
- (msg.metadata.usage || msg.metadata.codebuff?.usage)) ||
412
- {};
413
- const input = Number(u.input_tokens || u.inputTokens || 0);
414
- const output = Number(u.output_tokens || u.outputTokens || 0);
415
- const cacheCreate = Number(
416
- u.cache_creation_tokens || u.cacheCreationTokens || 0,
417
- );
418
- const cacheRead = Number(
419
- u.cache_read_tokens || u.cacheReadTokens || 0,
420
- );
421
- if (input + output + cacheCreate + cacheRead === 0) continue;
422
- yield {
423
- id: msg.id || null,
424
- sid: null,
425
- ts: msg.timestamp || msg.created_at || null,
426
- input,
427
- output,
428
- cacheCreate,
429
- cacheRead,
430
- file: path,
431
- };
432
- }
433
- }
434
- }
435
- },
436
- };
437
-
438
- // ── 8. Gemini CLI ─────────────────────────────────────────────────────────────
439
- // ~/.gemini/tmp/*/chats/*.json and *.jsonl
440
- // Fields: input, output, cached, thought (reasoning), tool, total
441
- // SigRank mapping: input = input−cached (fresh), cacheRead = cached, cacheCreate = 0 (not exposed),
442
- // output = output + thought (reasoning→output)
443
- export const geminiAdapter = {
444
- platform: "gemini",
445
- estimated: true, // no cacheCreate field in Gemini logs
446
- defaultRoot: () => join(homedir(), ".gemini", "tmp"),
447
- async *messages(root) {
448
- for (const r of roots("GEMINI_DATA_DIR", root)) {
449
- for await (const path of walkFiles(
450
- r,
451
- (n) => n.endsWith(".json") || n.endsWith(".jsonl"),
452
- )) {
453
- const text = await readUtf8(path);
454
- if (!text) continue;
455
- // Try JSONL first, then single JSON
456
- let parsed = [];
457
- try {
458
- parsed = text
459
- .trim()
460
- .split("\n")
461
- .filter(Boolean)
462
- .map((l) => JSON.parse(l));
463
- } catch {
464
- try {
465
- parsed = [JSON.parse(text)];
466
- } catch {
467
- continue;
468
- }
469
- }
470
- for (const ev of parsed) {
471
- if (!ev) continue;
472
- // Gemini usage may be at top level or nested in usageMetadata
473
- const u = ev.usageMetadata || ev.usage || ev;
474
- const rawInput = Number(u.input || u.promptTokenCount || 0);
475
- const rawOutput = Number(u.output || u.candidatesTokenCount || 0);
476
- const cached = Number(u.cached || u.cachedContentTokenCount || 0);
477
- const thought = Number(u.thought || u.thoughtsTokenCount || 0);
478
- if (rawInput + rawOutput + cached + thought === 0) continue;
479
- const input = Math.max(0, rawInput - cached); // strip cached from input
480
- const output = rawOutput + thought; // reasoning → output
481
- yield {
482
- id: ev.id || null,
483
- sid: null,
484
- ts: ev.timestamp || null,
485
- input,
486
- output,
487
- cacheCreate: 0,
488
- cacheRead: cached,
489
- file: path,
490
- };
491
- }
492
- }
493
- }
494
- },
495
- };
496
-
497
- // ── 9. GitHub Copilot CLI ─────────────────────────────────────────────────────
498
- // ~/.copilot/otel/*.jsonl (requires COPILOT_OTEL_ENABLED=true before session start)
499
- // OpenTelemetry spans; looks for llm.token_count.prompt / completion / cached attributes
500
- export const copilotAdapter = {
501
- platform: "copilot",
502
- defaultRoot: () => join(homedir(), ".copilot", "otel"),
503
- async *messages(root) {
504
- const dir = process.env.COPILOT_OTEL_FILE_EXPORTER_PATH
505
- ? dirname(process.env.COPILOT_OTEL_FILE_EXPORTER_PATH)
506
- : root;
507
- for await (const path of walkFiles(dir, isJsonl)) {
508
- const text = await readUtf8(path);
509
- for (const [ev] of parseJsonl(text, path)) {
510
- if (!ev) continue;
511
- // OTel span: attributes may be an object or array of {key,value} pairs
512
- const attrs = ev.attributes || ev.resource?.attributes || {};
513
- const get = (k) => {
514
- if (typeof attrs === "object" && !Array.isArray(attrs))
515
- return attrs[k];
516
- if (Array.isArray(attrs)) {
517
- const a = attrs.find((x) => x.key === k);
518
- return a?.value?.intValue ?? a?.value?.stringValue ?? null;
519
- }
520
- return null;
521
- };
522
- const input = Number(
523
- get("llm.token_count.prompt") ||
524
- get("gen_ai.usage.input_tokens") ||
525
- 0,
526
- );
527
- const output = Number(
528
- get("llm.token_count.completion") ||
529
- get("gen_ai.usage.output_tokens") ||
530
- 0,
531
- );
532
- const cacheCreate = Number(get("llm.token_count.cache_creation") || 0);
533
- const cacheRead = Number(
534
- get("llm.token_count.cache_read") ||
535
- get("gen_ai.usage.cache_read_input_tokens") ||
536
- 0,
537
- );
538
- if (input + output + cacheCreate + cacheRead === 0) continue;
539
- yield {
540
- id: ev.traceId || ev.spanId || null,
541
- sid: null,
542
- ts: ev.startTimeUnixNano
543
- ? new Date(Number(ev.startTimeUnixNano) / 1e6).toISOString()
544
- : null,
545
- input,
546
- output,
547
- cacheCreate,
548
- cacheRead,
549
- file: path,
550
- };
551
- }
552
- }
553
- },
554
- setupNote:
555
- "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.",
556
- };
557
-
558
- // ── 10. OpenCode ──────────────────────────────────────────────────────────────
559
- // ~/.local/share/opencode — JSON message files, but costs stored as 0 and token fields
560
- // are calculated via LiteLLM pricing (not stored in the log). No raw token fields.
561
- // SigRank cannot derive pillars from OpenCode logs with current log format.
562
- export const opencodeAdapter = {
563
- platform: "opencode",
564
- defaultRoot: () => join(homedir(), ".local", "share", "opencode"),
565
- estimated: true,
566
- dataGap:
567
- "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.",
568
- // eslint-disable-next-line require-yield
569
- async *messages() {
570
- /* no data available */
571
- },
572
- };
573
-
574
- // ── 11. Goose ─────────────────────────────────────────────────────────────────
575
- // SQLite: sessions.db at standard Goose data roots or $GOOSE_PATH_ROOT/data/sessions/sessions.db
576
- // Columns: accumulated_input_tokens (or input_tokens), accumulated_output_tokens (or output_tokens),
577
- // accumulated_total_tokens (or total_tokens). NO cache fields. Reasoning = total-input-output.
578
- // CUMULATIVE-COLUMN GUARD: accumulated_* columns are per-session running totals. If the sessions
579
- // table has multiple rows per session id, summing them downstream double-counts. We set both sid
580
- // and id to the session id and ORDER BY updated_at so tokenpull()'s (sid|id) keep-last dedup
581
- // collapses duplicate session rows to the latest (max accumulated) values.
582
- export const gooseAdapter = {
583
- platform: "goose",
584
- estimated: true, // no cacheCreate or cacheRead
585
- defaultRoot: () => {
586
- const env = process.env.GOOSE_PATH_ROOT;
587
- if (env) return env;
588
- // Standard locations (macOS first, then XDG)
589
- return join(homedir(), "Library", "Application Support", "goose");
590
- },
591
- async *messages(root) {
592
- const dbCandidates = process.env.GOOSE_PATH_ROOT
593
- ? [join(process.env.GOOSE_PATH_ROOT, "data", "sessions", "sessions.db")]
594
- : [
595
- join(
596
- homedir(),
597
- "Library",
598
- "Application Support",
599
- "goose",
600
- "sessions",
601
- "sessions.db",
602
- ),
603
- join(
604
- homedir(),
605
- ".local",
606
- "share",
607
- "goose",
608
- "sessions",
609
- "sessions.db",
610
- ),
611
- join(
612
- homedir(),
613
- ".local",
614
- "share",
615
- "Block",
616
- "goose",
617
- "sessions",
618
- "sessions.db",
619
- ),
620
- ];
621
- for (const db of dbCandidates) {
622
- // ORDER BY updated_at so the latest cumulative row per session is yielded last
623
- // (tokenpull keep-last dedup picks the final row = max accumulated values).
624
- const rows = await sqliteJson(
625
- db,
626
- "SELECT * FROM sessions ORDER BY updated_at",
627
- );
628
- for (const row of rows) {
629
- const input = Number(
630
- row.accumulated_input_tokens || row.input_tokens || 0,
631
- );
632
- const output = Number(
633
- row.accumulated_output_tokens || row.output_tokens || 0,
634
- );
635
- const total = Number(
636
- row.accumulated_total_tokens || row.total_tokens || 0,
637
- );
638
- const reasoning = Math.max(0, total - input - output); // folded into output
639
- if (input + output === 0) continue;
640
- const sessionId = String(row.id || row.session_id || "");
641
- yield {
642
- id: sessionId,
643
- sid: sessionId,
644
- ts: row.created_at || row.updated_at || null,
645
- input,
646
- output: output + reasoning,
647
- cacheCreate: 0,
648
- cacheRead: 0,
649
- file: db,
650
- };
651
- }
652
- }
653
- },
654
- };
655
-
656
- // ── 12. Kilo ──────────────────────────────────────────────────────────────────
657
- // SQLite: ~/.local/share/kilo/kilo.db
658
- // Per-message rows with model, input/output/cache token columns.
659
- export const kiloAdapter = {
660
- platform: "kilo",
661
- defaultRoot: () => join(homedir(), ".local", "share", "kilo"),
662
- async *messages(root) {
663
- const dbPath = join(roots("KILO_DATA_DIR", root)[0], "kilo.db");
664
- const rows = await sqliteJson(
665
- dbPath,
666
- 'SELECT * FROM messages WHERE role="assistant"',
667
- );
668
- for (const row of rows) {
669
- const input = Number(row.input_tokens || row.inputTokens || 0);
670
- const output = Number(row.output_tokens || row.outputTokens || 0);
671
- const cacheCreate = Number(
672
- row.cache_creation_tokens || row.cacheCreationTokens || 0,
673
- );
674
- const cacheRead = Number(
675
- row.cache_read_tokens || row.cacheReadTokens || 0,
676
- );
677
- if (input + output + cacheCreate + cacheRead === 0) continue;
678
- yield {
679
- id: String(row.id || ""),
680
- sid: String(row.session_id || row.sessionId || ""),
681
- ts: row.created_at || row.timestamp || null,
682
- input,
683
- output,
684
- cacheCreate,
685
- cacheRead,
686
- file: dbPath,
687
- };
688
- }
689
- },
690
- };
691
-
692
- // ── 13. Hermes Agent ─────────────────────────────────────────────────────────
693
- // SQLite: ~/.hermes/state.db
694
- // Per-session rows: input, output, cache_read, cache_write (=cacheCreate), reasoning_tokens → output
695
- // CUMULATIVE-COLUMN GUARD: session rows may contain cumulative totals. If the same session
696
- // appears in multiple rows, summing double-counts. We set both id and sid to the session id
697
- // and ORDER BY updated_at so tokenpull()'s (sid|id) keep-last dedup collapses duplicates
698
- // to the latest values.
699
- export const hermesAdapter = {
700
- platform: "hermes",
701
- defaultRoot: () => join(homedir(), ".hermes"),
702
- async *messages(root) {
703
- for (const r of roots("HERMES_HOME", root)) {
704
- const dbPath = join(r, "state.db");
705
- // ORDER BY updated_at so the latest cumulative row per session is yielded last.
706
- const rows = await sqliteJson(
707
- dbPath,
708
- "SELECT * FROM sessions ORDER BY updated_at",
709
- );
710
- for (const row of rows) {
711
- const input = Number(row.input || 0);
712
- const reasoning = Number(row.reasoning_tokens || 0);
713
- const output = Number(row.output || 0) + reasoning;
714
- const cacheCreate = Number(row.cache_write || row.cache_creation || 0);
715
- const cacheRead = Number(row.cache_read || 0);
716
- if (input + output + cacheCreate + cacheRead === 0) continue;
717
- const sessionId = String(row.id || row.session_id || "");
718
- yield {
719
- id: sessionId,
720
- sid: sessionId,
721
- ts: row.created_at || row.updated_at || null,
722
- input,
723
- output,
724
- cacheCreate,
725
- cacheRead,
726
- file: dbPath,
727
- };
728
- }
729
- }
730
- },
731
- };
732
-
733
- // ── 14. Devin CLI ────────────────────────────────────────────────────────────
734
- // SQLite: ~/.local/share/devin/cli/sessions.db
735
- // Same combined-input problem as Codex: input_tokens INCLUDES cache write, so we
736
- // yield { ts, output, cacheRead, uncached } and let tokenpullCodex() do the
737
- // ioRatio split (input = output × ioRatio, cacheCreate = uncached − input).
738
- // ioRatio comes from Claude (Beta) or the 7:1:2 average (Alpha = 0.5).
739
- export const devinAdapter = {
740
- platform: "devin",
741
- defaultRoot: () => join(homedir(), ".local", "share", "devin", "cli"),
742
- async *records(root) {
743
- for (const r of roots("DEVIN_HOME", root)) {
744
- const dbPath = join(r, "sessions.db");
745
- const rows = await sqliteJson(
746
- dbPath,
747
- `SELECT row_id, session_id,
748
- json_extract(chat_message, '$.metadata.metrics.input_tokens') as input_tokens,
749
- json_extract(chat_message, '$.metadata.metrics.output_tokens') as output_tokens,
750
- json_extract(chat_message, '$.metadata.metrics.cache_read_tokens') as cache_read_tokens,
751
- json_extract(chat_message, '$.metadata.created_at') as created_at
752
- FROM message_nodes
753
- WHERE json_extract(chat_message, '$.role') = 'assistant'
754
- AND json_extract(chat_message, '$.metadata.metrics.input_tokens') IS NOT NULL
755
- ORDER BY created_at`,
756
- 60_000,
757
- );
758
- for (const row of rows) {
759
- const inputIncl = Number(row.input_tokens || 0);
760
- const cached = Number(row.cache_read_tokens || 0);
761
- const output = Number(row.output_tokens || 0);
762
- if (inputIncl + output + cached === 0) continue;
763
- yield {
764
- ts: row.created_at || null,
765
- output,
766
- cacheRead: cached,
767
- uncached: Math.max(0, inputIncl - cached),
768
- file: dbPath,
769
- };
770
- }
771
- }
772
- },
773
- };
774
-
775
- // ── 15. Other (user-supplied JSON) ───────────────────────────────────────────
776
- // For platforms/models not yet on the adapter list. The user writes a JSON file
777
- // with the 4 pillars per window and points SIGRANK_OTHER_PATH at it. This lets
778
- // anyone with a new tool submit without needing a custom adapter.
779
- //
780
- // JSON format (all fields optional, missing windows are skipped):
781
- // {
782
- // "platform": "my-tool", // optional, defaults to "other"
783
- // "windows": {
784
- // "all": { "input": 1000, "output": 2000, "cacheCreate": 500, "cacheRead": 8000 },
785
- // "30d": { "input": 500, "output": 1000, "cacheCreate": 200, "cacheRead": 4000 },
786
- // "7d": { "input": 100, "output": 200, "cacheCreate": 50, "cacheRead": 800 }
787
- // }
788
- // }
789
- //
790
- // The adapter yields one "message" per window with the summed pillars. Since
791
- // there's no per-message timestamp, all data lands in the "all" window only
792
- // (same as null-timestamp records in other adapters).
793
- export const otherAdapter = {
794
- platform: "other",
795
- defaultRoot: () => process.env.SIGRANK_OTHER_PATH || "",
796
- async *messages(root) {
797
- const filePath = root || process.env.SIGRANK_OTHER_PATH;
798
- if (!filePath) {
799
- throw new Error(
800
- "other adapter: set SIGRANK_OTHER_PATH to a JSON file with your token pillars. " +
801
- 'Format: { "windows": { "all": { "input": N, "output": N, "cacheCreate": N, "cacheRead": N } } }',
802
- );
803
- }
804
- let text;
805
- try {
806
- text = await readFile(filePath, "utf8");
807
- } catch {
808
- throw new Error(`other adapter: cannot read ${filePath} — check SIGRANK_OTHER_PATH`);
809
- }
810
- let data;
811
- try {
812
- data = JSON.parse(text);
813
- } catch {
814
- throw new Error(`other adapter: invalid JSON in ${filePath}`);
815
- }
816
- const windows = data.windows || {};
817
- for (const [win, pillars] of Object.entries(windows)) {
818
- const input = Number(pillars.input || 0);
819
- const output = Number(pillars.output || 0);
820
- const cacheCreate = Number(pillars.cacheCreate || pillars.cache_create || 0);
821
- const cacheRead = Number(pillars.cacheRead || pillars.cache_read || 0);
822
- if (input + output + cacheCreate + cacheRead === 0) continue;
823
- yield {
824
- id: `other:${win}`,
825
- sid: `other-${win}`,
826
- ts: null, // no timestamp → lands in "all" window only
827
- input,
828
- output,
829
- cacheCreate,
830
- cacheRead,
831
- file: filePath,
832
- };
833
- }
834
- },
835
- };
836
-
837
- // ── Registry ──────────────────────────────────────────────────────────────────
838
- /** All non-Claude, non-Codex adapters keyed by platform ID. */
839
- export const ADAPTERS = {
840
- amp: ampAdapter,
841
- kimi: kimiAdapter,
842
- qwen: qwenAdapter,
843
- pi: piAdapter,
844
- openclaw: openclawAdapter,
845
- droid: droidAdapter,
846
- codebuff: codebuffAdapter,
847
- gemini: geminiAdapter,
848
- copilot: copilotAdapter,
849
- opencode: opencodeAdapter,
850
- goose: gooseAdapter,
851
- kilo: kiloAdapter,
852
- hermes: hermesAdapter,
853
- devin: devinAdapter,
854
- other: otherAdapter,
855
- };
856
-
857
- export const ALL_PLATFORMS = Object.keys(ADAPTERS).concat(["claude", "codex"]);
1
+ export * from './adapters/index.mjs';