tokenusage-cli 0.1.0 → 0.2.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 (131) hide show
  1. package/CHANGELOG.md +46 -19
  2. package/LICENSE +22 -22
  3. package/README.md +123 -94
  4. package/THIRD_PARTY_NOTICES.md +140 -140
  5. package/bin/tokenusage.js +69 -69
  6. package/package.json +59 -59
  7. package/src/cli.js +146 -134
  8. package/src/commands/device-login.js +205 -205
  9. package/src/commands/diagnostics.js +38 -38
  10. package/src/commands/doctor.js +110 -110
  11. package/src/commands/init.js +2484 -2436
  12. package/src/commands/pause.js +77 -0
  13. package/src/commands/sessions.js +29 -29
  14. package/src/commands/status.js +1758 -1747
  15. package/src/commands/sync.js +8048 -7934
  16. package/src/commands/uninstall.js +322 -312
  17. package/src/lib/account-usage-dedup.js +267 -267
  18. package/src/lib/antigravity-paths.js +48 -48
  19. package/src/lib/ark-agent-plan-limits.js +438 -438
  20. package/src/lib/ark-coding-plan-limits.js +537 -537
  21. package/src/lib/async-queue-reader.js +138 -138
  22. package/src/lib/atomic-json.js +27 -27
  23. package/src/lib/bearer-token.js +11 -11
  24. package/src/lib/categorizer-utils.js +232 -232
  25. package/src/lib/claude-categorizer.js +1343 -1343
  26. package/src/lib/claude-config.js +279 -279
  27. package/src/lib/claude-user-identity.js +56 -56
  28. package/src/lib/cli-ui.js +179 -179
  29. package/src/lib/cloud-account.js +541 -541
  30. package/src/lib/cloud-device-token.js +141 -141
  31. package/src/lib/cloud-sync-prefs.js +14 -14
  32. package/src/lib/cloud-usage-reset.js +115 -115
  33. package/src/lib/codex-config.js +595 -595
  34. package/src/lib/codex-context-breakdown.js +806 -806
  35. package/src/lib/codex-model-attribution.js +128 -128
  36. package/src/lib/codex-rollout-parser.js +1471 -1471
  37. package/src/lib/codex-service-tier.js +49 -49
  38. package/src/lib/codex-token-refresh.js +140 -140
  39. package/src/lib/codex-token-usage.js +195 -195
  40. package/src/lib/codex-usage-record.js +195 -195
  41. package/src/lib/collection-pause.js +77 -0
  42. package/src/lib/command-runner.js +313 -313
  43. package/src/lib/commandcode-limits.js +317 -317
  44. package/src/lib/context-health.js +124 -124
  45. package/src/lib/cursor-config.js +605 -605
  46. package/src/lib/cursor-store.js +1918 -1918
  47. package/src/lib/debug-flags.js +7 -7
  48. package/src/lib/devin-limits.js +249 -249
  49. package/src/lib/devin-usage.js +212 -212
  50. package/src/lib/diagnostics.js +383 -383
  51. package/src/lib/doctor.js +385 -385
  52. package/src/lib/dumate.js +95 -95
  53. package/src/lib/fs.js +455 -455
  54. package/src/lib/function-url.js +14 -14
  55. package/src/lib/gemini-config.js +291 -291
  56. package/src/lib/git-outcomes.js +243 -243
  57. package/src/lib/grok-context-breakdown.js +890 -890
  58. package/src/lib/grok-hook.js +360 -360
  59. package/src/lib/grok-limits.js +865 -865
  60. package/src/lib/grok-usage.js +98 -98
  61. package/src/lib/heatmap-compact.js +114 -114
  62. package/src/lib/ingest-sanitize.js +41 -41
  63. package/src/lib/init-flow.js +42 -42
  64. package/src/lib/install-resolver.js +170 -170
  65. package/src/lib/jsonl-lines.js +134 -134
  66. package/src/lib/kimi-model-repair.js +173 -173
  67. package/src/lib/kimi-profile.js +61 -61
  68. package/src/lib/kimi-session.js +216 -216
  69. package/src/lib/local-api.js +3588 -3588
  70. package/src/lib/machine-id.js +238 -238
  71. package/src/lib/multi-install-parser.js +113 -113
  72. package/src/lib/notify-command.js +38 -38
  73. package/src/lib/omp-hook.js +589 -589
  74. package/src/lib/openclaw-cli.js +104 -104
  75. package/src/lib/openclaw-hook.js +375 -375
  76. package/src/lib/openclaw-session-plugin.js +560 -560
  77. package/src/lib/opencode-config.js +121 -121
  78. package/src/lib/opencode-go-limits.js +807 -807
  79. package/src/lib/outcomes-engine.js +253 -253
  80. package/src/lib/passive-mode.js +219 -219
  81. package/src/lib/pet-packages.js +722 -722
  82. package/src/lib/pricing/curated-overrides.json +822 -822
  83. package/src/lib/pricing/index.js +379 -379
  84. package/src/lib/pricing/litellm-fetcher.js +172 -172
  85. package/src/lib/pricing/matcher.js +456 -456
  86. package/src/lib/pricing/seed-snapshot.json +1 -1
  87. package/src/lib/progress.js +76 -76
  88. package/src/lib/project-usage-purge.js +117 -117
  89. package/src/lib/prompt.js +24 -24
  90. package/src/lib/provider-status.js +104 -104
  91. package/src/lib/proxy-env.js +489 -489
  92. package/src/lib/proxy-settings.js +163 -163
  93. package/src/lib/qoder-cn-cli-limits.js +119 -119
  94. package/src/lib/qoder-limits.js +880 -880
  95. package/src/lib/quiet-warnings.js +26 -26
  96. package/src/lib/rollout.js +26005 -25966
  97. package/src/lib/runtime-config.js +9 -4
  98. package/src/lib/scan-roots.js +277 -277
  99. package/src/lib/scheduled-task.js +145 -0
  100. package/src/lib/sentry.js +231 -231
  101. package/src/lib/session-analytics.js +2972 -2972
  102. package/src/lib/session-performance.js +237 -237
  103. package/src/lib/session-upload.js +379 -379
  104. package/src/lib/skill-usage.js +267 -267
  105. package/src/lib/skills-manager.js +1591 -1591
  106. package/src/lib/source-metadata.js +48 -48
  107. package/src/lib/sqlite-reader.js +215 -215
  108. package/src/lib/star-cta.js +73 -73
  109. package/src/lib/subscription-manager.js +293 -293
  110. package/src/lib/subscriptions.js +508 -508
  111. package/src/lib/sync-report.js +138 -0
  112. package/src/lib/telemetry.js +193 -193
  113. package/src/lib/tracker-paths.js +16 -16
  114. package/src/lib/trae-cn-config.js +548 -548
  115. package/src/lib/trae-db.js +127 -127
  116. package/src/lib/trae-snapshot.js +230 -230
  117. package/src/lib/trae-sqlite.js +143 -143
  118. package/src/lib/trae-usage.js +121 -121
  119. package/src/lib/upload-queue.js +134 -134
  120. package/src/lib/upload-throttle.js +171 -171
  121. package/src/lib/usage-limits.js +4552 -4552
  122. package/src/lib/wrapped-aggregator.js +236 -236
  123. package/src/lib/wsl-probe.js +281 -281
  124. package/src/lib/zcode-limits.js +1172 -1172
  125. package/src/lib/zcode-reset-credits.js +66 -66
  126. package/test/audit-fixes.test.js +200 -0
  127. package/test/cli-smoke.test.js +53 -53
  128. package/test/collection-controls.test.js +210 -0
  129. package/test/ingest-sanitize.test.js +162 -162
  130. package/test/quiet-warnings.test.js +53 -53
  131. package/test/session-upload.test.js +427 -427
@@ -1,98 +1,98 @@
1
- "use strict";
2
-
3
- // Grok's ACP turn_completed.usage uses camelCase fields. In that shape,
4
- // inputTokens is the whole prompt (cache reads/writes included) and
5
- // outputTokens includes reasoningTokens. Token Usage stores mutually
6
- // exclusive columns, so both reported totals need to be split before pricing.
7
- //
8
- // Grok's headless/CLI summaries may use snake_case fields whose input_tokens is
9
- // already the non-cached portion. Keep the two shapes distinct instead of
10
- // subtracting cache tokens twice.
11
-
12
- const USD_TICKS_PER_USD = 10_000_000_000;
13
-
14
- function nonNegative(value) {
15
- // Floor: Grok's ACP reports have been observed with fractional JSON
16
- // numbers; the ingest edge requires non-negative INTEGERS and would 400
17
- // the whole upload batch on one fractional counter.
18
- const n = Number(value);
19
- return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
20
- }
21
-
22
- function firstBoolean(...values) {
23
- for (const value of values) {
24
- if (typeof value === "boolean") return value;
25
- }
26
- return false;
27
- }
28
-
29
- function reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }) {
30
- if (costIsPartial || usageIsIncomplete) return null;
31
-
32
- const ticks = Number(
33
- usage.costUsdTicks ??
34
- usage.totalCostUsdTicks ??
35
- usage.cost_usd_ticks ??
36
- usage.total_cost_usd_ticks,
37
- );
38
- if (Number.isFinite(ticks) && ticks >= 0) return ticks / USD_TICKS_PER_USD;
39
-
40
- const usd = Number(usage.costUsd ?? usage.totalCostUsd ?? usage.cost_usd ?? usage.total_cost_usd);
41
- return Number.isFinite(usd) && usd >= 0 ? usd : null;
42
- }
43
-
44
- function normalizeGrokUsage(usage) {
45
- if (!usage || typeof usage !== "object") return null;
46
-
47
- const camelCaseInput = usage.inputTokens != null;
48
- const reportedInput = nonNegative(usage.inputTokens ?? usage.input_tokens);
49
- const cachedInput = nonNegative(
50
- usage.cachedReadTokens ?? usage.cacheReadInputTokens ?? usage.cache_read_input_tokens ?? usage.cached_input_tokens,
51
- );
52
- const cacheCreationInput = nonNegative(
53
- usage.cacheCreationTokens ??
54
- usage.cachedWriteTokens ??
55
- usage.cacheWriteInputTokens ??
56
- usage.cache_creation_input_tokens,
57
- );
58
- const rawOutput = nonNegative(usage.outputTokens ?? usage.output_tokens);
59
- const reasoningOutput = nonNegative(usage.reasoningTokens ?? usage.reasoning_output_tokens);
60
-
61
- const inputTokens = camelCaseInput
62
- ? Math.max(0, reportedInput - cachedInput - cacheCreationInput)
63
- : reportedInput;
64
- const outputTokens = Math.max(0, rawOutput - reasoningOutput);
65
-
66
- let totalTokens = nonNegative(usage.totalTokens ?? usage.total_tokens);
67
- if (totalTokens <= 0) {
68
- totalTokens = inputTokens
69
- + cachedInput
70
- + cacheCreationInput
71
- + outputTokens
72
- + reasoningOutput;
73
- }
74
- if (totalTokens <= 0) return null;
75
-
76
- const costIsPartial = firstBoolean(usage.costIsPartial, usage.cost_is_partial);
77
- const usageIsIncomplete = firstBoolean(usage.usageIsIncomplete, usage.usage_is_incomplete);
78
-
79
- return {
80
- input_tokens: inputTokens,
81
- cached_input_tokens: cachedInput,
82
- cache_creation_input_tokens: cacheCreationInput,
83
- output_tokens: outputTokens,
84
- reasoning_output_tokens: reasoningOutput,
85
- total_tokens: totalTokens,
86
- billable_total_tokens: totalTokens,
87
- total_cost_usd: reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }),
88
- cost_is_partial: costIsPartial,
89
- usage_is_incomplete: usageIsIncomplete,
90
- model_calls: nonNegative(usage.modelCalls ?? usage.model_calls),
91
- api_duration_ms: nonNegative(usage.apiDurationMs ?? usage.api_duration_ms),
92
- };
93
- }
94
-
95
- module.exports = {
96
- USD_TICKS_PER_USD,
97
- normalizeGrokUsage,
98
- };
1
+ "use strict";
2
+
3
+ // Grok's ACP turn_completed.usage uses camelCase fields. In that shape,
4
+ // inputTokens is the whole prompt (cache reads/writes included) and
5
+ // outputTokens includes reasoningTokens. Token Usage stores mutually
6
+ // exclusive columns, so both reported totals need to be split before pricing.
7
+ //
8
+ // Grok's headless/CLI summaries may use snake_case fields whose input_tokens is
9
+ // already the non-cached portion. Keep the two shapes distinct instead of
10
+ // subtracting cache tokens twice.
11
+
12
+ const USD_TICKS_PER_USD = 10_000_000_000;
13
+
14
+ function nonNegative(value) {
15
+ // Floor: Grok's ACP reports have been observed with fractional JSON
16
+ // numbers; the ingest edge requires non-negative INTEGERS and would 400
17
+ // the whole upload batch on one fractional counter.
18
+ const n = Number(value);
19
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
20
+ }
21
+
22
+ function firstBoolean(...values) {
23
+ for (const value of values) {
24
+ if (typeof value === "boolean") return value;
25
+ }
26
+ return false;
27
+ }
28
+
29
+ function reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }) {
30
+ if (costIsPartial || usageIsIncomplete) return null;
31
+
32
+ const ticks = Number(
33
+ usage.costUsdTicks ??
34
+ usage.totalCostUsdTicks ??
35
+ usage.cost_usd_ticks ??
36
+ usage.total_cost_usd_ticks,
37
+ );
38
+ if (Number.isFinite(ticks) && ticks >= 0) return ticks / USD_TICKS_PER_USD;
39
+
40
+ const usd = Number(usage.costUsd ?? usage.totalCostUsd ?? usage.cost_usd ?? usage.total_cost_usd);
41
+ return Number.isFinite(usd) && usd >= 0 ? usd : null;
42
+ }
43
+
44
+ function normalizeGrokUsage(usage) {
45
+ if (!usage || typeof usage !== "object") return null;
46
+
47
+ const camelCaseInput = usage.inputTokens != null;
48
+ const reportedInput = nonNegative(usage.inputTokens ?? usage.input_tokens);
49
+ const cachedInput = nonNegative(
50
+ usage.cachedReadTokens ?? usage.cacheReadInputTokens ?? usage.cache_read_input_tokens ?? usage.cached_input_tokens,
51
+ );
52
+ const cacheCreationInput = nonNegative(
53
+ usage.cacheCreationTokens ??
54
+ usage.cachedWriteTokens ??
55
+ usage.cacheWriteInputTokens ??
56
+ usage.cache_creation_input_tokens,
57
+ );
58
+ const rawOutput = nonNegative(usage.outputTokens ?? usage.output_tokens);
59
+ const reasoningOutput = nonNegative(usage.reasoningTokens ?? usage.reasoning_output_tokens);
60
+
61
+ const inputTokens = camelCaseInput
62
+ ? Math.max(0, reportedInput - cachedInput - cacheCreationInput)
63
+ : reportedInput;
64
+ const outputTokens = Math.max(0, rawOutput - reasoningOutput);
65
+
66
+ let totalTokens = nonNegative(usage.totalTokens ?? usage.total_tokens);
67
+ if (totalTokens <= 0) {
68
+ totalTokens = inputTokens
69
+ + cachedInput
70
+ + cacheCreationInput
71
+ + outputTokens
72
+ + reasoningOutput;
73
+ }
74
+ if (totalTokens <= 0) return null;
75
+
76
+ const costIsPartial = firstBoolean(usage.costIsPartial, usage.cost_is_partial);
77
+ const usageIsIncomplete = firstBoolean(usage.usageIsIncomplete, usage.usage_is_incomplete);
78
+
79
+ return {
80
+ input_tokens: inputTokens,
81
+ cached_input_tokens: cachedInput,
82
+ cache_creation_input_tokens: cacheCreationInput,
83
+ output_tokens: outputTokens,
84
+ reasoning_output_tokens: reasoningOutput,
85
+ total_tokens: totalTokens,
86
+ billable_total_tokens: totalTokens,
87
+ total_cost_usd: reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }),
88
+ cost_is_partial: costIsPartial,
89
+ usage_is_incomplete: usageIsIncomplete,
90
+ model_calls: nonNegative(usage.modelCalls ?? usage.model_calls),
91
+ api_duration_ms: nonNegative(usage.apiDurationMs ?? usage.api_duration_ms),
92
+ };
93
+ }
94
+
95
+ module.exports = {
96
+ USD_TICKS_PER_USD,
97
+ normalizeGrokUsage,
98
+ };
@@ -1,114 +1,114 @@
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 };
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 };
@@ -1,41 +1,41 @@
1
- "use strict";
2
-
3
- // The ingest edge (docs/cli-contract.md §2.4) validates every usage bucket
4
- // BEFORE writing and rejects the WHOLE batch with 400 on the first invalid
5
- // row. The upload path drains queue.jsonl verbatim, where any producer bug
6
- // (fractional counters from a vendor API, a mangled line) would otherwise
7
- // poison the batch and stall the queue head-of-line forever: the offset only
8
- // advances on success, so every retry re-reads the identical rejected batch.
9
- // Rows are therefore coerced into the contract shape here, and dropped
10
- // (return null) when they cannot be repaired — losing one row beats losing
11
- // the whole queue behind it.
12
-
13
- const { TOKEN_COLUMNS } = require("./account-usage-dedup");
14
-
15
- function nonNegativeInteger(value) {
16
- const n = Number(value);
17
- return Number.isFinite(n) ? Math.max(0, Math.floor(n)) : 0;
18
- }
19
-
20
- function nonNegativeNumber(value) {
21
- const n = Number(value);
22
- return Number.isFinite(n) && n > 0 ? n : 0;
23
- }
24
-
25
- function sanitizeBucketForUpload(bucket) {
26
- if (!bucket || typeof bucket !== "object" || Array.isArray(bucket)) return null;
27
- if (typeof bucket.hour_start !== "string" || !Number.isFinite(Date.parse(bucket.hour_start))) return null;
28
- const sanitized = { ...bucket };
29
- for (const column of TOKEN_COLUMNS) {
30
- sanitized[column] = nonNegativeInteger(bucket[column]);
31
- }
32
- sanitized.billable_total_tokens = bucket.billable_total_tokens == null
33
- ? null
34
- : nonNegativeInteger(bucket.billable_total_tokens);
35
- if (bucket.conversation_count !== undefined) sanitized.conversation_count = nonNegativeInteger(bucket.conversation_count);
36
- if (bucket.conversations !== undefined) sanitized.conversations = nonNegativeInteger(bucket.conversations);
37
- sanitized.total_cost_usd = bucket.total_cost_usd == null ? 0 : nonNegativeNumber(bucket.total_cost_usd);
38
- return sanitized;
39
- }
40
-
41
- module.exports = { sanitizeBucketForUpload };
1
+ "use strict";
2
+
3
+ // The ingest edge (docs/cli-contract.md §2.4) validates every usage bucket
4
+ // BEFORE writing and rejects the WHOLE batch with 400 on the first invalid
5
+ // row. The upload path drains queue.jsonl verbatim, where any producer bug
6
+ // (fractional counters from a vendor API, a mangled line) would otherwise
7
+ // poison the batch and stall the queue head-of-line forever: the offset only
8
+ // advances on success, so every retry re-reads the identical rejected batch.
9
+ // Rows are therefore coerced into the contract shape here, and dropped
10
+ // (return null) when they cannot be repaired — losing one row beats losing
11
+ // the whole queue behind it.
12
+
13
+ const { TOKEN_COLUMNS } = require("./account-usage-dedup");
14
+
15
+ function nonNegativeInteger(value) {
16
+ const n = Number(value);
17
+ return Number.isFinite(n) ? Math.max(0, Math.floor(n)) : 0;
18
+ }
19
+
20
+ function nonNegativeNumber(value) {
21
+ const n = Number(value);
22
+ return Number.isFinite(n) && n > 0 ? n : 0;
23
+ }
24
+
25
+ function sanitizeBucketForUpload(bucket) {
26
+ if (!bucket || typeof bucket !== "object" || Array.isArray(bucket)) return null;
27
+ if (typeof bucket.hour_start !== "string" || !Number.isFinite(Date.parse(bucket.hour_start))) return null;
28
+ const sanitized = { ...bucket };
29
+ for (const column of TOKEN_COLUMNS) {
30
+ sanitized[column] = nonNegativeInteger(bucket[column]);
31
+ }
32
+ sanitized.billable_total_tokens = bucket.billable_total_tokens == null
33
+ ? null
34
+ : nonNegativeInteger(bucket.billable_total_tokens);
35
+ if (bucket.conversation_count !== undefined) sanitized.conversation_count = nonNegativeInteger(bucket.conversation_count);
36
+ if (bucket.conversations !== undefined) sanitized.conversations = nonNegativeInteger(bucket.conversations);
37
+ sanitized.total_cost_usd = bucket.total_cost_usd == null ? 0 : nonNegativeNumber(bucket.total_cost_usd);
38
+ return sanitized;
39
+ }
40
+
41
+ module.exports = { sanitizeBucketForUpload };
@@ -1,42 +1,42 @@
1
- "use strict";
2
-
3
- const { formatSummaryLine, renderBox, underline } = require("./cli-ui");
4
-
5
- const DIVIDER = "----------------------------------------------";
6
-
7
- function renderLocalReport({ summary, isDryRun }) {
8
- const header = isDryRun
9
- ? "Dry run complete. Preview only; no changes were applied."
10
- : "Local configuration complete.";
11
- const lines = [header, "", "Integration Status:"];
12
- for (const item of summary || []) lines.push(formatSummaryLine(item));
13
- process.stdout.write(`${lines.join("\n")}\n`);
14
- }
15
-
16
- function renderAuthTransition({ authUrl, canAutoOpen }) {
17
- const lines = ["", DIVIDER, "", "Next: Registering device..."];
18
- if (canAutoOpen) {
19
- lines.push("Opening your browser to link account...");
20
- if (authUrl) lines.push(`If it does not open, visit: ${underline(authUrl)}`);
21
- } else {
22
- lines.push("Open the link below to sign in.");
23
- if (authUrl) lines.push(`Visit: ${underline(authUrl)}`);
24
- }
25
- lines.push("");
26
- process.stdout.write(`${lines.join("\n")}\n`);
27
- }
28
-
29
- function renderSuccessBox({ configPath, dashboardUrl }) {
30
- const identityLine = "Account linked.";
31
- const lines = ["You are all set!", "", identityLine, `Token saved to: ${configPath}`, ""];
32
- if (dashboardUrl) lines.push(`View your stats at: ${dashboardUrl}`);
33
- lines.push("You can close this terminal window.");
34
- process.stdout.write(`${renderBox(lines)}\n`);
35
- }
36
-
37
- module.exports = {
38
- DIVIDER,
39
- renderLocalReport,
40
- renderAuthTransition,
41
- renderSuccessBox,
42
- };
1
+ "use strict";
2
+
3
+ const { formatSummaryLine, renderBox, underline } = require("./cli-ui");
4
+
5
+ const DIVIDER = "----------------------------------------------";
6
+
7
+ function renderLocalReport({ summary, isDryRun }) {
8
+ const header = isDryRun
9
+ ? "Dry run complete. Preview only; no changes were applied."
10
+ : "Local configuration complete.";
11
+ const lines = [header, "", "Integration Status:"];
12
+ for (const item of summary || []) lines.push(formatSummaryLine(item));
13
+ process.stdout.write(`${lines.join("\n")}\n`);
14
+ }
15
+
16
+ function renderAuthTransition({ authUrl, canAutoOpen }) {
17
+ const lines = ["", DIVIDER, "", "Next: Registering device..."];
18
+ if (canAutoOpen) {
19
+ lines.push("Opening your browser to link account...");
20
+ if (authUrl) lines.push(`If it does not open, visit: ${underline(authUrl)}`);
21
+ } else {
22
+ lines.push("Open the link below to sign in.");
23
+ if (authUrl) lines.push(`Visit: ${underline(authUrl)}`);
24
+ }
25
+ lines.push("");
26
+ process.stdout.write(`${lines.join("\n")}\n`);
27
+ }
28
+
29
+ function renderSuccessBox({ configPath, dashboardUrl }) {
30
+ const identityLine = "Account linked.";
31
+ const lines = ["You are all set!", "", identityLine, `Token saved to: ${configPath}`, ""];
32
+ if (dashboardUrl) lines.push(`View your stats at: ${dashboardUrl}`);
33
+ lines.push("You can close this terminal window.");
34
+ process.stdout.write(`${renderBox(lines)}\n`);
35
+ }
36
+
37
+ module.exports = {
38
+ DIVIDER,
39
+ renderLocalReport,
40
+ renderAuthTransition,
41
+ renderSuccessBox,
42
+ };