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,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Optional Sentry error reporting for the Node CLI / EmbeddedServer runtime.
|
|
5
|
+
*
|
|
6
|
+
* Privacy contract (keep in sync with docs/PRIVACY.md and dashboard/src/lib/sentry.js):
|
|
7
|
+
* - Off unless TOKENUSAGE_SENTRY_DSN (or SENTRY_DSN) is set.
|
|
8
|
+
* - Honors the same telemetry opt-out as the daily heartbeat
|
|
9
|
+
* (TOKENUSAGE_NO_TELEMETRY / DO_NOT_TRACK / config.json `"telemetry": false`).
|
|
10
|
+
* - Fail-closed in the node test runner.
|
|
11
|
+
* - Never intended to carry prompts, code, or provider secrets; beforeSend
|
|
12
|
+
* scrubs common secret patterns from event payloads.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const path = require("node:path");
|
|
16
|
+
const pkg = require("../../package.json");
|
|
17
|
+
const { readJson } = require("./fs");
|
|
18
|
+
const { isTelemetryDisabled } = require("./telemetry");
|
|
19
|
+
|
|
20
|
+
const SECRET_PATTERN =
|
|
21
|
+
/(?:api[_-]?key|access[_-]?token|refresh[_-]?token|cookie|secret|password|dsn)[=:\s]+["']?[^\s"'\\]{8,}/gi;
|
|
22
|
+
const BEARER_PATTERN = /\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi;
|
|
23
|
+
const JWT_PATTERN = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,}\.[A-Za-z0-9._-]{10,}\b/g;
|
|
24
|
+
|
|
25
|
+
let initialized = false;
|
|
26
|
+
|
|
27
|
+
function resolveDsn(env = process.env) {
|
|
28
|
+
const raw = env?.TOKENUSAGE_SENTRY_DSN || env?.SENTRY_DSN || "";
|
|
29
|
+
return typeof raw === "string" ? raw.trim() : "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function resolveEnvironment(env = process.env) {
|
|
33
|
+
const raw = typeof env?.TOKENUSAGE_SENTRY_ENVIRONMENT === "string"
|
|
34
|
+
? env.TOKENUSAGE_SENTRY_ENVIRONMENT.trim()
|
|
35
|
+
: "";
|
|
36
|
+
if (raw) return raw;
|
|
37
|
+
if (env?.NODE_ENV === "production") return "production";
|
|
38
|
+
return "development";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function resolveShell(env = process.env) {
|
|
42
|
+
const raw = typeof env?.TOKENUSAGE_APP_SHELL === "string"
|
|
43
|
+
? env.TOKENUSAGE_APP_SHELL.trim().toLowerCase()
|
|
44
|
+
: "";
|
|
45
|
+
if (raw === "macos" || raw === "windows" || raw === "linux" || raw === "cli") return raw;
|
|
46
|
+
return "cli";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function scrubString(value) {
|
|
50
|
+
if (typeof value !== "string" || !value) return value;
|
|
51
|
+
return value
|
|
52
|
+
.replace(BEARER_PATTERN, "Bearer [Filtered]")
|
|
53
|
+
.replace(JWT_PATTERN, "[Filtered]")
|
|
54
|
+
.replace(SECRET_PATTERN, "[Filtered]");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function scrubValue(value, depth = 0) {
|
|
58
|
+
if (depth > 6 || value == null) return value;
|
|
59
|
+
if (typeof value === "string") return scrubString(value);
|
|
60
|
+
if (Array.isArray(value)) return value.map((item) => scrubValue(item, depth + 1));
|
|
61
|
+
if (typeof value === "object") {
|
|
62
|
+
const out = {};
|
|
63
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
64
|
+
const lower = key.toLowerCase();
|
|
65
|
+
if (
|
|
66
|
+
lower.includes("authorization") ||
|
|
67
|
+
lower.includes("cookie") ||
|
|
68
|
+
lower.includes("password") ||
|
|
69
|
+
lower.includes("secret") ||
|
|
70
|
+
lower.includes("token") ||
|
|
71
|
+
lower.includes("api_key") ||
|
|
72
|
+
lower.includes("apikey")
|
|
73
|
+
) {
|
|
74
|
+
out[key] = "[Filtered]";
|
|
75
|
+
} else {
|
|
76
|
+
out[key] = scrubValue(nested, depth + 1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function truncateMaybe(value, max = 240) {
|
|
85
|
+
if (typeof value !== "string") return value;
|
|
86
|
+
if (value.length <= max) return value;
|
|
87
|
+
return `${value.slice(0, max)}…[truncated]`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function scrubEvent(event) {
|
|
91
|
+
if (!event || typeof event !== "object") return event;
|
|
92
|
+
const next = { ...event };
|
|
93
|
+
if (next.message) next.message = truncateMaybe(scrubString(next.message));
|
|
94
|
+
if (next.extra) next.extra = scrubValue(next.extra);
|
|
95
|
+
if (next.contexts) next.contexts = scrubValue(next.contexts);
|
|
96
|
+
if (next.request) next.request = scrubValue(next.request);
|
|
97
|
+
// Console breadcrumbs can carry parser / JSON.parse excerpts of local logs
|
|
98
|
+
// (prompt text). Drop them entirely; keep only non-console crumbs and scrub.
|
|
99
|
+
if (Array.isArray(next.breadcrumbs)) {
|
|
100
|
+
next.breadcrumbs = next.breadcrumbs
|
|
101
|
+
.filter((crumb) => crumb && crumb.category !== "console")
|
|
102
|
+
.map((crumb) => scrubValue(crumb));
|
|
103
|
+
}
|
|
104
|
+
if (next.exception?.values) {
|
|
105
|
+
next.exception = {
|
|
106
|
+
...next.exception,
|
|
107
|
+
values: next.exception.values.map((item) => ({
|
|
108
|
+
...item,
|
|
109
|
+
value: truncateMaybe(scrubString(item?.value)),
|
|
110
|
+
})),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// Never attach IP / user PII by default.
|
|
114
|
+
if (next.user) {
|
|
115
|
+
next.user = { id: typeof next.user.id === "string" ? next.user.id : undefined };
|
|
116
|
+
}
|
|
117
|
+
return next;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Initialise Sentry once for this process. Never throws.
|
|
122
|
+
* @returns {Promise<{ enabled: boolean, reason?: string }>}
|
|
123
|
+
*/
|
|
124
|
+
async function initSentry({
|
|
125
|
+
env = process.env,
|
|
126
|
+
trackerDir,
|
|
127
|
+
version = pkg.version,
|
|
128
|
+
} = {}) {
|
|
129
|
+
try {
|
|
130
|
+
if (initialized) return { enabled: true, reason: "already-initialized" };
|
|
131
|
+
if (env?.NODE_TEST_CONTEXT !== undefined) {
|
|
132
|
+
return { enabled: false, reason: "test-env" };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const dsn = resolveDsn(env);
|
|
136
|
+
if (!dsn) return { enabled: false, reason: "no-dsn" };
|
|
137
|
+
|
|
138
|
+
let config = {};
|
|
139
|
+
if (trackerDir) {
|
|
140
|
+
config = (await readJson(path.join(trackerDir, "config.json"))) || {};
|
|
141
|
+
}
|
|
142
|
+
if (isTelemetryDisabled({ env, config })) {
|
|
143
|
+
return { enabled: false, reason: "opt-out" };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Honor the same outbound proxy / fail-closed policy as the rest of the CLI.
|
|
147
|
+
// Sentry's Node transport uses node:https (not undici), so pass proxy explicitly.
|
|
148
|
+
const {
|
|
149
|
+
readPersistedProxyConfig,
|
|
150
|
+
resolveEffectiveProxySource,
|
|
151
|
+
} = require("./proxy-env");
|
|
152
|
+
const proxyConfig = readPersistedProxyConfig();
|
|
153
|
+
const proxyResolved = resolveEffectiveProxySource({ env, proxyConfig });
|
|
154
|
+
if (proxyResolved?.source === "blocked") {
|
|
155
|
+
return { enabled: false, reason: "proxy-blocked" };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Lazy require keeps unit tests that never call init from paying the
|
|
159
|
+
// module graph cost at import time of unrelated modules.
|
|
160
|
+
const Sentry = require("@sentry/node");
|
|
161
|
+
Sentry.init({
|
|
162
|
+
dsn,
|
|
163
|
+
environment: resolveEnvironment(env),
|
|
164
|
+
release: `tokenusage-cli@${String(version || "").trim() || "0.0.0"}`,
|
|
165
|
+
sendDefaultPii: false,
|
|
166
|
+
tracesSampleRate: 0,
|
|
167
|
+
// Errors only: drop process-session + console; replace Http so request
|
|
168
|
+
// sessions stay off (Http defaults sessions:true and bypasses beforeSend).
|
|
169
|
+
integrations(defaults) {
|
|
170
|
+
const filtered = defaults.filter((integration) => {
|
|
171
|
+
const name = integration?.name || "";
|
|
172
|
+
return name !== "ProcessSession" && name !== "Console" && name !== "Http";
|
|
173
|
+
});
|
|
174
|
+
filtered.push(Sentry.httpIntegration({ sessions: false }));
|
|
175
|
+
return filtered;
|
|
176
|
+
},
|
|
177
|
+
transportOptions: proxyResolved?.proxyUrl
|
|
178
|
+
? { proxy: proxyResolved.proxyUrl }
|
|
179
|
+
: undefined,
|
|
180
|
+
beforeSend(event) {
|
|
181
|
+
return scrubEvent(event);
|
|
182
|
+
},
|
|
183
|
+
initialScope: {
|
|
184
|
+
tags: {
|
|
185
|
+
shell: resolveShell(env),
|
|
186
|
+
runtime: "node",
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
initialized = true;
|
|
191
|
+
return { enabled: true };
|
|
192
|
+
} catch {
|
|
193
|
+
return { enabled: false, reason: "init-failed" };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function captureException(error, context) {
|
|
198
|
+
if (!initialized) return;
|
|
199
|
+
try {
|
|
200
|
+
const Sentry = require("@sentry/node");
|
|
201
|
+
if (context) {
|
|
202
|
+
Sentry.withScope((scope) => {
|
|
203
|
+
if (context.tags) {
|
|
204
|
+
for (const [key, value] of Object.entries(context.tags)) {
|
|
205
|
+
scope.setTag(key, String(value));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (context.extra) scope.setExtras(scrubValue(context.extra));
|
|
209
|
+
Sentry.captureException(error);
|
|
210
|
+
});
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
Sentry.captureException(error);
|
|
214
|
+
} catch {
|
|
215
|
+
/* never break the CLI */
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = {
|
|
220
|
+
initSentry,
|
|
221
|
+
captureException,
|
|
222
|
+
resolveDsn,
|
|
223
|
+
resolveEnvironment,
|
|
224
|
+
resolveShell,
|
|
225
|
+
scrubEvent,
|
|
226
|
+
scrubString,
|
|
227
|
+
// test helpers
|
|
228
|
+
_resetForTests() {
|
|
229
|
+
initialized = false;
|
|
230
|
+
},
|
|
231
|
+
};
|