tokenusage-cli 0.3.0 → 0.4.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 (47) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +52 -11
  3. package/bin/tokenusage.js +14 -2
  4. package/package.json +1 -2
  5. package/src/cli.js +325 -78
  6. package/src/commands/device-login.js +3 -3
  7. package/src/commands/init.js +343 -153
  8. package/src/commands/pause.js +11 -23
  9. package/src/commands/reconcile.js +496 -0
  10. package/src/commands/sessions.js +57 -14
  11. package/src/commands/sync.js +69 -15
  12. package/src/commands/uninstall.js +6 -9
  13. package/src/commands/upload.js +147 -0
  14. package/src/lib/cli-errors.js +33 -0
  15. package/src/lib/cli-ui.js +93 -0
  16. package/src/lib/queue-row-normalize.js +55 -0
  17. package/src/lib/scheduled-task.js +597 -75
  18. package/src/lib/antigravity-paths.js +0 -48
  19. package/src/lib/async-queue-reader.js +0 -138
  20. package/src/lib/atomic-json.js +0 -27
  21. package/src/lib/bearer-token.js +0 -11
  22. package/src/lib/cloud-account.js +0 -541
  23. package/src/lib/codex-context-breakdown.js +0 -806
  24. package/src/lib/context-health.js +0 -124
  25. package/src/lib/grok-context-breakdown.js +0 -890
  26. package/src/lib/heatmap-compact.js +0 -114
  27. package/src/lib/local-api.js +0 -3574
  28. package/src/lib/outcomes-engine.js +0 -253
  29. package/src/lib/pet-packages.js +0 -722
  30. package/src/lib/skill-usage.js +0 -267
  31. package/src/lib/skills-manager.js +0 -1591
  32. package/src/lib/source-metadata.js +0 -48
  33. package/src/lib/subscription-manager.js +0 -293
  34. package/src/lib/wrapped-aggregator.js +0 -236
  35. package/test/audit-fixes.test.js +0 -249
  36. package/test/branding.test.js +0 -24
  37. package/test/cli-options.test.js +0 -43
  38. package/test/cli-smoke.test.js +0 -53
  39. package/test/cli-ui.test.js +0 -47
  40. package/test/collection-controls.test.js +0 -255
  41. package/test/ingest-sanitize.test.js +0 -162
  42. package/test/packaging.test.js +0 -107
  43. package/test/quiet-warnings.test.js +0 -53
  44. package/test/runtime-copy.test.js +0 -111
  45. package/test/session-upload.test.js +0 -427
  46. package/test/trae-db.test.js +0 -67
  47. package/test/zstd-native.test.js +0 -36
@@ -1,114 +0,0 @@
1
- "use strict";
2
-
3
- // Expand the `format=compact` activity-heatmap payload back into the schema the
4
- // dashboard, popover and widget already render.
5
- //
6
- // The heatmap is the single largest cloud read we serve: 2.55 GB/day on
7
- // 2026-09-21, 31% of all egress, and 94% of it proxied through this CLI. The
8
- // bytes were never the data — `account_heatmap_compact` hands the edge ~12 KB
9
- // of sparse rows, and the edge blew that up to ~42 KB by materialising a
10
- // 52-week × 7-day grid of five-key objects, where `billable_total_tokens`
11
- // simply repeats `total_tokens` and `level` is derived from the window maximum.
12
- //
13
- // So the grid gets rebuilt here instead, one hop later, off the same sparse
14
- // rows. The rendered payload is identical; it just stops crossing the network.
15
- //
16
- // Model names are the other half of it. A year of daily rows repeats the same
17
- // ~160 names ~1900 times, which was 57% of even the sparse payload, so they are
18
- // sent once in `model_names` and referenced by index from flat
19
- // [index, tokens, index, tokens, ...] pairs.
20
- //
21
- // This mirrors the tail of dashboard/edge-patches/token-usage-account-heatmap.ts
22
- // step for step, including the details that look incidental but are not:
23
- // coercion through Number(), last-row-wins on a repeated day, and `{}` (not
24
- // null) for a present row whose models map is empty.
25
-
26
- /** Next YYYY-MM-DD, treating the key as a UTC calendar day (same as the edge). */
27
- function nextDayKey(dayKey) {
28
- const d = new Date(`${dayKey}T00:00:00Z`);
29
- d.setUTCDate(d.getUTCDate() + 1);
30
- return d.toISOString().slice(0, 10);
31
- }
32
-
33
- const DAY_KEY_RE = /^\d{4}-\d{2}-\d{2}$/;
34
-
35
- // 104 weeks is the edge's own `weeks` clamp; the guard is here so a malformed
36
- // `to` cannot spin the grid loop forever.
37
- const MAX_GRID_DAYS = 104 * 7 + 7;
38
-
39
- /**
40
- * @param {unknown} payload a `token-usage-account-heatmap` response.
41
- * @returns {unknown} the dense payload, or `payload` itself when it is not a
42
- * well-formed compact response — which is also how a new CLI stays correct
43
- * against an edge that predates the compact branch.
44
- */
45
- function expandHeatmapCompact(payload) {
46
- if (!payload || typeof payload !== "object") return payload;
47
- if (payload.format !== "compact") return payload;
48
- if (!Array.isArray(payload.days)) return payload;
49
- // Without the dictionary the indices resolve to nothing and every model
50
- // breakdown would render empty, so refuse the payload instead of silently
51
- // dropping data.
52
- if (!Array.isArray(payload.model_names)) return payload;
53
- if (!DAY_KEY_RE.test(String(payload.from)) || !DAY_KEY_RE.test(String(payload.to))) return payload;
54
-
55
- const names = payload.model_names;
56
- const byDay = new Map();
57
- for (const row of payload.days) {
58
- if (!Array.isArray(row)) continue;
59
- const [day, total, pairs] = row;
60
- if (!DAY_KEY_RE.test(String(day))) continue;
61
- const mdl = {};
62
- if (Array.isArray(pairs)) {
63
- // `i + 1 < length` drops a trailing index with no value rather than
64
- // recording it as 0.
65
- for (let i = 0; i + 1 < pairs.length; i += 2) {
66
- const name = names[pairs[i]];
67
- if (typeof name === "string") mdl[name] = Number(pairs[i + 1]) || 0;
68
- }
69
- }
70
- // Last row wins, as Map.set did on the edge.
71
- byDay.set(String(day), { total: Number(total) || 0, models: mdl });
72
- }
73
-
74
- const maxValue = Number(payload.max_value) || 0;
75
- const levelOf = (v) => {
76
- if (v <= 0) return 0;
77
- if (maxValue === 0) return 1;
78
- const r = v / maxValue;
79
- if (r <= 0.25) return 1;
80
- if (r <= 0.5) return 2;
81
- if (r <= 0.75) return 3;
82
- return 4;
83
- };
84
-
85
- const cells = [];
86
- const to = String(payload.to);
87
- let cursor = String(payload.from);
88
- while (cursor <= to && cells.length < MAX_GRID_DAYS) {
89
- const hit = byDay.get(cursor);
90
- const total = hit ? hit.total : 0;
91
- cells.push({
92
- day: cursor,
93
- total_tokens: total,
94
- billable_total_tokens: total,
95
- level: levelOf(total),
96
- models: hit ? hit.models : null,
97
- });
98
- cursor = nextDayKey(cursor);
99
- }
100
-
101
- const weeks = [];
102
- for (let i = 0; i < cells.length; i += 7) weeks.push(cells.slice(i, i + 7));
103
-
104
- return {
105
- from: payload.from,
106
- to: payload.to,
107
- week_starts_on: payload.week_starts_on,
108
- active_days: payload.active_days,
109
- streak_days: payload.streak_days,
110
- weeks,
111
- };
112
- }
113
-
114
- module.exports = { expandHeatmapCompact };