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.
- package/CHANGELOG.md +19 -0
- package/LICENSE +22 -0
- package/README.md +94 -0
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/bin/tokenusage.js +69 -0
- package/package.json +59 -0
- package/src/cli.js +134 -0
- package/src/commands/device-login.js +205 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +110 -0
- package/src/commands/init.js +2436 -0
- package/src/commands/sessions.js +29 -0
- package/src/commands/status.js +1747 -0
- package/src/commands/sync.js +7934 -0
- package/src/commands/uninstall.js +312 -0
- package/src/lib/account-usage-dedup.js +267 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/ark-agent-plan-limits.js +438 -0
- package/src/lib/ark-coding-plan-limits.js +537 -0
- package/src/lib/async-queue-reader.js +138 -0
- package/src/lib/atomic-json.js +27 -0
- package/src/lib/bearer-token.js +11 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1343 -0
- package/src/lib/claude-config.js +279 -0
- package/src/lib/claude-user-identity.js +56 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/cloud-account.js +541 -0
- package/src/lib/cloud-device-token.js +141 -0
- package/src/lib/cloud-sync-prefs.js +14 -0
- package/src/lib/cloud-usage-reset.js +115 -0
- package/src/lib/codex-config.js +595 -0
- package/src/lib/codex-context-breakdown.js +806 -0
- package/src/lib/codex-model-attribution.js +128 -0
- package/src/lib/codex-rollout-parser.js +1471 -0
- package/src/lib/codex-service-tier.js +49 -0
- package/src/lib/codex-token-refresh.js +140 -0
- package/src/lib/codex-token-usage.js +195 -0
- package/src/lib/codex-usage-record.js +195 -0
- package/src/lib/command-runner.js +313 -0
- package/src/lib/commandcode-limits.js +317 -0
- package/src/lib/context-health.js +124 -0
- package/src/lib/cursor-config.js +605 -0
- package/src/lib/cursor-store.js +1918 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/devin-limits.js +249 -0
- package/src/lib/devin-usage.js +212 -0
- package/src/lib/diagnostics.js +383 -0
- package/src/lib/doctor.js +385 -0
- package/src/lib/dumate.js +95 -0
- package/src/lib/fs.js +455 -0
- package/src/lib/function-url.js +14 -0
- package/src/lib/gemini-config.js +291 -0
- package/src/lib/git-outcomes.js +243 -0
- package/src/lib/grok-context-breakdown.js +890 -0
- package/src/lib/grok-hook.js +360 -0
- package/src/lib/grok-limits.js +865 -0
- package/src/lib/grok-usage.js +98 -0
- package/src/lib/heatmap-compact.js +114 -0
- package/src/lib/ingest-sanitize.js +41 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/install-resolver.js +170 -0
- package/src/lib/jsonl-lines.js +134 -0
- package/src/lib/kimi-model-repair.js +173 -0
- package/src/lib/kimi-profile.js +61 -0
- package/src/lib/kimi-session.js +216 -0
- package/src/lib/local-api.js +3588 -0
- package/src/lib/machine-id.js +238 -0
- package/src/lib/multi-install-parser.js +113 -0
- package/src/lib/notify-command.js +38 -0
- package/src/lib/omp-hook.js +589 -0
- package/src/lib/openclaw-cli.js +104 -0
- package/src/lib/openclaw-hook.js +375 -0
- package/src/lib/openclaw-session-plugin.js +560 -0
- package/src/lib/opencode-config.js +121 -0
- package/src/lib/opencode-go-limits.js +807 -0
- package/src/lib/outcomes-engine.js +253 -0
- package/src/lib/passive-mode.js +219 -0
- package/src/lib/pet-packages.js +722 -0
- package/src/lib/pricing/curated-overrides.json +822 -0
- package/src/lib/pricing/index.js +379 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +456 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +117 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/provider-status.js +104 -0
- package/src/lib/proxy-env.js +489 -0
- package/src/lib/proxy-settings.js +163 -0
- package/src/lib/qoder-cn-cli-limits.js +119 -0
- package/src/lib/qoder-limits.js +880 -0
- package/src/lib/quiet-warnings.js +26 -0
- package/src/lib/rollout.js +25966 -0
- package/src/lib/runtime-config.js +259 -0
- package/src/lib/scan-roots.js +277 -0
- package/src/lib/sentry.js +231 -0
- package/src/lib/session-analytics.js +2972 -0
- package/src/lib/session-performance.js +237 -0
- package/src/lib/session-upload.js +379 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1591 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +215 -0
- package/src/lib/star-cta.js +73 -0
- package/src/lib/subscription-manager.js +293 -0
- package/src/lib/subscriptions.js +508 -0
- package/src/lib/telemetry.js +193 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/trae-cn-config.js +548 -0
- package/src/lib/trae-db.js +127 -0
- package/src/lib/trae-snapshot.js +230 -0
- package/src/lib/trae-sqlite.js +143 -0
- package/src/lib/trae-usage.js +121 -0
- package/src/lib/upload-queue.js +134 -0
- package/src/lib/upload-throttle.js +171 -0
- package/src/lib/usage-limits.js +4552 -0
- package/src/lib/wrapped-aggregator.js +236 -0
- package/src/lib/wsl-probe.js +281 -0
- package/src/lib/zcode-limits.js +1172 -0
- package/src/lib/zcode-reset-credits.js +66 -0
- package/test/cli-smoke.test.js +53 -0
- package/test/ingest-sanitize.test.js +162 -0
- package/test/quiet-warnings.test.js +53 -0
- 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
|
+
};
|