tokenusage-cli 0.1.0

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 (125) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/LICENSE +22 -0
  3. package/README.md +94 -0
  4. package/THIRD_PARTY_NOTICES.md +140 -0
  5. package/bin/tokenusage.js +69 -0
  6. package/package.json +59 -0
  7. package/src/cli.js +134 -0
  8. package/src/commands/device-login.js +205 -0
  9. package/src/commands/diagnostics.js +38 -0
  10. package/src/commands/doctor.js +110 -0
  11. package/src/commands/init.js +2436 -0
  12. package/src/commands/sessions.js +29 -0
  13. package/src/commands/status.js +1747 -0
  14. package/src/commands/sync.js +7934 -0
  15. package/src/commands/uninstall.js +312 -0
  16. package/src/lib/account-usage-dedup.js +267 -0
  17. package/src/lib/antigravity-paths.js +48 -0
  18. package/src/lib/ark-agent-plan-limits.js +438 -0
  19. package/src/lib/ark-coding-plan-limits.js +537 -0
  20. package/src/lib/async-queue-reader.js +138 -0
  21. package/src/lib/atomic-json.js +27 -0
  22. package/src/lib/bearer-token.js +11 -0
  23. package/src/lib/categorizer-utils.js +232 -0
  24. package/src/lib/claude-categorizer.js +1343 -0
  25. package/src/lib/claude-config.js +279 -0
  26. package/src/lib/claude-user-identity.js +56 -0
  27. package/src/lib/cli-ui.js +179 -0
  28. package/src/lib/cloud-account.js +541 -0
  29. package/src/lib/cloud-device-token.js +141 -0
  30. package/src/lib/cloud-sync-prefs.js +14 -0
  31. package/src/lib/cloud-usage-reset.js +115 -0
  32. package/src/lib/codex-config.js +595 -0
  33. package/src/lib/codex-context-breakdown.js +806 -0
  34. package/src/lib/codex-model-attribution.js +128 -0
  35. package/src/lib/codex-rollout-parser.js +1471 -0
  36. package/src/lib/codex-service-tier.js +49 -0
  37. package/src/lib/codex-token-refresh.js +140 -0
  38. package/src/lib/codex-token-usage.js +195 -0
  39. package/src/lib/codex-usage-record.js +195 -0
  40. package/src/lib/command-runner.js +313 -0
  41. package/src/lib/commandcode-limits.js +317 -0
  42. package/src/lib/context-health.js +124 -0
  43. package/src/lib/cursor-config.js +605 -0
  44. package/src/lib/cursor-store.js +1918 -0
  45. package/src/lib/debug-flags.js +7 -0
  46. package/src/lib/devin-limits.js +249 -0
  47. package/src/lib/devin-usage.js +212 -0
  48. package/src/lib/diagnostics.js +383 -0
  49. package/src/lib/doctor.js +385 -0
  50. package/src/lib/dumate.js +95 -0
  51. package/src/lib/fs.js +455 -0
  52. package/src/lib/function-url.js +14 -0
  53. package/src/lib/gemini-config.js +291 -0
  54. package/src/lib/git-outcomes.js +243 -0
  55. package/src/lib/grok-context-breakdown.js +890 -0
  56. package/src/lib/grok-hook.js +360 -0
  57. package/src/lib/grok-limits.js +865 -0
  58. package/src/lib/grok-usage.js +98 -0
  59. package/src/lib/heatmap-compact.js +114 -0
  60. package/src/lib/ingest-sanitize.js +41 -0
  61. package/src/lib/init-flow.js +42 -0
  62. package/src/lib/install-resolver.js +170 -0
  63. package/src/lib/jsonl-lines.js +134 -0
  64. package/src/lib/kimi-model-repair.js +173 -0
  65. package/src/lib/kimi-profile.js +61 -0
  66. package/src/lib/kimi-session.js +216 -0
  67. package/src/lib/local-api.js +3588 -0
  68. package/src/lib/machine-id.js +238 -0
  69. package/src/lib/multi-install-parser.js +113 -0
  70. package/src/lib/notify-command.js +38 -0
  71. package/src/lib/omp-hook.js +589 -0
  72. package/src/lib/openclaw-cli.js +104 -0
  73. package/src/lib/openclaw-hook.js +375 -0
  74. package/src/lib/openclaw-session-plugin.js +560 -0
  75. package/src/lib/opencode-config.js +121 -0
  76. package/src/lib/opencode-go-limits.js +807 -0
  77. package/src/lib/outcomes-engine.js +253 -0
  78. package/src/lib/passive-mode.js +219 -0
  79. package/src/lib/pet-packages.js +722 -0
  80. package/src/lib/pricing/curated-overrides.json +822 -0
  81. package/src/lib/pricing/index.js +379 -0
  82. package/src/lib/pricing/litellm-fetcher.js +172 -0
  83. package/src/lib/pricing/matcher.js +456 -0
  84. package/src/lib/pricing/seed-snapshot.json +1 -0
  85. package/src/lib/progress.js +76 -0
  86. package/src/lib/project-usage-purge.js +117 -0
  87. package/src/lib/prompt.js +24 -0
  88. package/src/lib/provider-status.js +104 -0
  89. package/src/lib/proxy-env.js +489 -0
  90. package/src/lib/proxy-settings.js +163 -0
  91. package/src/lib/qoder-cn-cli-limits.js +119 -0
  92. package/src/lib/qoder-limits.js +880 -0
  93. package/src/lib/quiet-warnings.js +26 -0
  94. package/src/lib/rollout.js +25966 -0
  95. package/src/lib/runtime-config.js +259 -0
  96. package/src/lib/scan-roots.js +277 -0
  97. package/src/lib/sentry.js +231 -0
  98. package/src/lib/session-analytics.js +2972 -0
  99. package/src/lib/session-performance.js +237 -0
  100. package/src/lib/session-upload.js +379 -0
  101. package/src/lib/skill-usage.js +267 -0
  102. package/src/lib/skills-manager.js +1591 -0
  103. package/src/lib/source-metadata.js +48 -0
  104. package/src/lib/sqlite-reader.js +215 -0
  105. package/src/lib/star-cta.js +73 -0
  106. package/src/lib/subscription-manager.js +293 -0
  107. package/src/lib/subscriptions.js +508 -0
  108. package/src/lib/telemetry.js +193 -0
  109. package/src/lib/tracker-paths.js +16 -0
  110. package/src/lib/trae-cn-config.js +548 -0
  111. package/src/lib/trae-db.js +127 -0
  112. package/src/lib/trae-snapshot.js +230 -0
  113. package/src/lib/trae-sqlite.js +143 -0
  114. package/src/lib/trae-usage.js +121 -0
  115. package/src/lib/upload-queue.js +134 -0
  116. package/src/lib/upload-throttle.js +171 -0
  117. package/src/lib/usage-limits.js +4552 -0
  118. package/src/lib/wrapped-aggregator.js +236 -0
  119. package/src/lib/wsl-probe.js +281 -0
  120. package/src/lib/zcode-limits.js +1172 -0
  121. package/src/lib/zcode-reset-credits.js +66 -0
  122. package/test/cli-smoke.test.js +53 -0
  123. package/test/ingest-sanitize.test.js +162 -0
  124. package/test/quiet-warnings.test.js +53 -0
  125. package/test/session-upload.test.js +427 -0
@@ -0,0 +1,171 @@
1
+ const DEFAULTS = {
2
+ intervalMs: 30 * 60_000,
3
+ jitterMsMax: 60_000,
4
+ backlogBytes: 1_000_000,
5
+ batchSize: 300,
6
+ maxBatchesSmall: 2,
7
+ maxBatchesLarge: 4,
8
+ backoffInitialMs: 60_000,
9
+ backoffMaxMs: 30 * 60_000,
10
+ };
11
+
12
+ function normalizeState(raw) {
13
+ const s = raw && typeof raw === "object" ? raw : {};
14
+ return {
15
+ version: 1,
16
+ lastSuccessMs: toSafeInt(s.lastSuccessMs),
17
+ lastSuccessAttemptId: typeof s.lastSuccessAttemptId === "string" ? s.lastSuccessAttemptId : null,
18
+ nextAllowedAtMs: toSafeInt(s.nextAllowedAtMs),
19
+ serverNextAllowedAtMs: toSafeInt(s.serverNextAllowedAtMs),
20
+ backoffUntilMs: toSafeInt(s.backoffUntilMs),
21
+ backoffStep: toSafeInt(s.backoffStep),
22
+ lastErrorAt: typeof s.lastErrorAt === "string" ? s.lastErrorAt : null,
23
+ lastError: typeof s.lastError === "string" ? s.lastError : null,
24
+ lastErrorStatus: toSafeInt(s.lastErrorStatus),
25
+ lastErrorCode: typeof s.lastErrorCode === "string" ? s.lastErrorCode : null,
26
+ lastErrorAttemptId: typeof s.lastErrorAttemptId === "string" ? s.lastErrorAttemptId : null,
27
+ updatedAt: typeof s.updatedAt === "string" ? s.updatedAt : null,
28
+ };
29
+ }
30
+
31
+ function decideAutoUpload({ nowMs, pendingBytes, state, config }) {
32
+ const cfg = { ...DEFAULTS, ...(config || {}) };
33
+ const s = normalizeState(state);
34
+ const pending = Number(pendingBytes || 0);
35
+
36
+ if (pending <= 0) {
37
+ return {
38
+ allowed: false,
39
+ reason: "no-pending",
40
+ maxBatches: 0,
41
+ batchSize: cfg.batchSize,
42
+ blockedUntilMs: 0,
43
+ };
44
+ }
45
+
46
+ const blockedUntilMs = Math.max(s.nextAllowedAtMs || 0, s.backoffUntilMs || 0);
47
+ if (blockedUntilMs > 0 && nowMs < blockedUntilMs) {
48
+ return {
49
+ allowed: false,
50
+ reason: "throttled",
51
+ maxBatches: 0,
52
+ batchSize: cfg.batchSize,
53
+ blockedUntilMs,
54
+ };
55
+ }
56
+
57
+ const maxBatches = pending >= cfg.backlogBytes ? cfg.maxBatchesLarge : cfg.maxBatchesSmall;
58
+ return {
59
+ allowed: true,
60
+ reason: "allowed",
61
+ maxBatches,
62
+ batchSize: cfg.batchSize,
63
+ blockedUntilMs: 0,
64
+ };
65
+ }
66
+
67
+ function recordUploadSuccess({ nowMs, state, config, randInt, cloudAccess, attemptId }) {
68
+ const cfg = { ...DEFAULTS, ...(config || {}) };
69
+ const s = normalizeState(state);
70
+ const jitter =
71
+ typeof randInt === "function" ? randInt(0, cfg.jitterMsMax) : randomInt(0, cfg.jitterMsMax);
72
+ const serverNextAllowedAtMs = toSafeInt(Date.parse(cloudAccess?.next_allowed_at));
73
+ const rawInterval = cloudAccess?.sync_interval_seconds;
74
+ const seconds = typeof rawInterval === "number" ? rawInterval
75
+ : typeof rawInterval === "string" && /^\d+$/.test(rawInterval.trim()) ? Number(rawInterval) : NaN;
76
+ const intervalMs = Number.isFinite(seconds) && seconds >= 0 ? Math.floor(seconds) * 1000 : cfg.intervalMs;
77
+ const nextAllowedAtMs = Math.max(serverNextAllowedAtMs, nowMs + intervalMs + jitter);
78
+
79
+ return {
80
+ ...s,
81
+ lastSuccessMs: nowMs,
82
+ lastSuccessAttemptId: typeof attemptId === "string" && attemptId ? attemptId : null,
83
+ nextAllowedAtMs,
84
+ serverNextAllowedAtMs,
85
+ backoffUntilMs: 0,
86
+ backoffStep: 0,
87
+ lastErrorAt: null,
88
+ lastError: null,
89
+ lastErrorStatus: 0,
90
+ lastErrorCode: null,
91
+ lastErrorAttemptId: null,
92
+ updatedAt: new Date(nowMs).toISOString(),
93
+ };
94
+ }
95
+
96
+ function recordUploadFailure({ nowMs, state, error, config, attemptId }) {
97
+ const cfg = { ...DEFAULTS, ...(config || {}) };
98
+ const s = normalizeState(state);
99
+
100
+ const retryAfterMs = toSafeInt(error?.retryAfterMs);
101
+ const status = toSafeInt(error?.status);
102
+
103
+ let backoffMs = 0;
104
+ if (retryAfterMs > 0) {
105
+ // Retry-After is the server's deadline. The configured cap applies only
106
+ // to exponential retries without an explicit server deadline.
107
+ backoffMs = Math.max(cfg.backoffInitialMs, retryAfterMs);
108
+ } else {
109
+ const step = Math.min(10, Math.max(0, s.backoffStep || 0));
110
+ backoffMs = Math.min(cfg.backoffMaxMs, cfg.backoffInitialMs * Math.pow(2, step));
111
+ }
112
+
113
+ const backoffUntilMs = nowMs + backoffMs;
114
+ const hasServerDeadline = status === 429 && error?.code === "cloud_sync_throttled" && retryAfterMs > 0;
115
+ const nextAllowedAtMs = hasServerDeadline
116
+ ? backoffUntilMs : Math.max(s.nextAllowedAtMs || 0, backoffUntilMs);
117
+
118
+ return {
119
+ ...s,
120
+ nextAllowedAtMs,
121
+ serverNextAllowedAtMs: hasServerDeadline ? backoffUntilMs : s.serverNextAllowedAtMs,
122
+ backoffUntilMs,
123
+ backoffStep: Math.min(20, (s.backoffStep || 0) + 1),
124
+ lastErrorAt: new Date(nowMs).toISOString(),
125
+ lastError: truncate(String(error?.message || "upload failed"), 200),
126
+ lastErrorStatus: status,
127
+ lastErrorCode: typeof error?.code === "string" ? error.code : null,
128
+ lastErrorAttemptId: typeof attemptId === "string" && attemptId ? attemptId : null,
129
+ updatedAt: new Date(nowMs).toISOString(),
130
+ };
131
+ }
132
+
133
+ function parseRetryAfterMs(headerValue, nowMs = Date.now()) {
134
+ if (typeof headerValue !== "string" || headerValue.trim().length === 0) return null;
135
+ const v = headerValue.trim();
136
+ const seconds = Number(v);
137
+ if (Number.isFinite(seconds) && seconds >= 0) return Math.floor(seconds * 1000);
138
+ const d = new Date(v);
139
+ if (Number.isNaN(d.getTime())) return null;
140
+ const delta = d.getTime() - nowMs;
141
+ return delta > 0 ? delta : 0;
142
+ }
143
+
144
+ function randomInt(min, maxInclusive) {
145
+ const lo = Math.floor(min);
146
+ const hi = Math.floor(maxInclusive);
147
+ if (hi <= lo) return lo;
148
+ return lo + Math.floor(Math.random() * (hi - lo + 1));
149
+ }
150
+
151
+ function toSafeInt(v) {
152
+ const n = Number(v);
153
+ if (!Number.isFinite(n)) return 0;
154
+ if (n <= 0) return 0;
155
+ return Math.floor(n);
156
+ }
157
+
158
+ function truncate(s, maxLen) {
159
+ if (typeof s !== "string") return "";
160
+ if (s.length <= maxLen) return s;
161
+ return s.slice(0, maxLen - 1) + "…";
162
+ }
163
+
164
+ module.exports = {
165
+ DEFAULTS,
166
+ normalizeState,
167
+ decideAutoUpload,
168
+ recordUploadSuccess,
169
+ recordUploadFailure,
170
+ parseRetryAfterMs,
171
+ };