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,48 +1,48 @@
1
- const ACCOUNT_LEVEL_SOURCES = new Set(["cursor", "trae-cn"]);
2
-
3
- function normalizeSource(value) {
4
- return String(value || "").trim().toLowerCase();
5
- }
6
-
7
- function getSourceScope(source) {
8
- return ACCOUNT_LEVEL_SOURCES.has(normalizeSource(source)) ? "account" : "local";
9
- }
10
-
11
- function isAccountLevelSource(source) {
12
- return getSourceScope(source) === "account";
13
- }
14
-
15
- function normalizeUsageScope(value) {
16
- const raw = String(value || "").trim().toLowerCase();
17
- if (!raw || raw === "all" || raw === "raw") return "all";
18
- if (raw === "personal" || raw === "local") return "personal";
19
- return "all";
20
- }
21
-
22
- function filterRowsByUsageScope(rows, scope = "all") {
23
- const normalizedScope = normalizeUsageScope(scope);
24
- if (normalizedScope === "all") return Array.isArray(rows) ? rows : [];
25
- return (Array.isArray(rows) ? rows : []).filter((row) => !isAccountLevelSource(row?.source));
26
- }
27
-
28
- function listExcludedSources(rows, scope = "all") {
29
- const normalizedScope = normalizeUsageScope(scope);
30
- if (normalizedScope === "all") return [];
31
- const seen = new Set();
32
- const out = [];
33
- for (const row of Array.isArray(rows) ? rows : []) {
34
- const source = normalizeSource(row?.source);
35
- if (!source || seen.has(source) || !isAccountLevelSource(source)) continue;
36
- seen.add(source);
37
- out.push({ source, source_scope: getSourceScope(source), reason: "account_level_source" });
38
- }
39
- return out.sort((a, b) => a.source.localeCompare(b.source));
40
- }
41
-
42
- module.exports = {
43
- getSourceScope,
44
- isAccountLevelSource,
45
- normalizeUsageScope,
46
- filterRowsByUsageScope,
47
- listExcludedSources,
48
- };
1
+ const ACCOUNT_LEVEL_SOURCES = new Set(["cursor", "trae-cn"]);
2
+
3
+ function normalizeSource(value) {
4
+ return String(value || "").trim().toLowerCase();
5
+ }
6
+
7
+ function getSourceScope(source) {
8
+ return ACCOUNT_LEVEL_SOURCES.has(normalizeSource(source)) ? "account" : "local";
9
+ }
10
+
11
+ function isAccountLevelSource(source) {
12
+ return getSourceScope(source) === "account";
13
+ }
14
+
15
+ function normalizeUsageScope(value) {
16
+ const raw = String(value || "").trim().toLowerCase();
17
+ if (!raw || raw === "all" || raw === "raw") return "all";
18
+ if (raw === "personal" || raw === "local") return "personal";
19
+ return "all";
20
+ }
21
+
22
+ function filterRowsByUsageScope(rows, scope = "all") {
23
+ const normalizedScope = normalizeUsageScope(scope);
24
+ if (normalizedScope === "all") return Array.isArray(rows) ? rows : [];
25
+ return (Array.isArray(rows) ? rows : []).filter((row) => !isAccountLevelSource(row?.source));
26
+ }
27
+
28
+ function listExcludedSources(rows, scope = "all") {
29
+ const normalizedScope = normalizeUsageScope(scope);
30
+ if (normalizedScope === "all") return [];
31
+ const seen = new Set();
32
+ const out = [];
33
+ for (const row of Array.isArray(rows) ? rows : []) {
34
+ const source = normalizeSource(row?.source);
35
+ if (!source || seen.has(source) || !isAccountLevelSource(source)) continue;
36
+ seen.add(source);
37
+ out.push({ source, source_scope: getSourceScope(source), reason: "account_level_source" });
38
+ }
39
+ return out.sort((a, b) => a.source.localeCompare(b.source));
40
+ }
41
+
42
+ module.exports = {
43
+ getSourceScope,
44
+ isAccountLevelSource,
45
+ normalizeUsageScope,
46
+ filterRowsByUsageScope,
47
+ listExcludedSources,
48
+ };
@@ -1,215 +1,215 @@
1
- const cp = require("node:child_process");
2
- const fssync = require("node:fs");
3
- const { promisify } = require("node:util");
4
-
5
- const warnedSqliteReadFailures = new Set();
6
-
7
- function isDebugEnabled(env = process.env) {
8
- const value = String((env && env.TOKENUSAGE_DEBUG) || "").toLowerCase();
9
- return value === "1" || value === "true";
10
- }
11
-
12
- function formatError(err) {
13
- if (!err) return "unknown error";
14
- return err && err.message ? err.message : String(err);
15
- }
16
-
17
- function warnSqliteUnavailable({ dbPath, label, cliError, nodeSqliteError, env, stderr }) {
18
- const key = `${label || "SQLite"}:${dbPath || ""}`;
19
- if (warnedSqliteReadFailures.has(key)) return;
20
- warnedSqliteReadFailures.add(key);
21
-
22
- const out = stderr && typeof stderr.write === "function" ? stderr : process.stderr;
23
- const displayLabel = label || "local";
24
- out.write(
25
- `[tokenusage] Cannot read ${displayLabel} SQLite database. Install sqlite3 CLI and add it to PATH, or use Node.js 22+ with node:sqlite support. Path: ${dbPath}\n`,
26
- );
27
- if (isDebugEnabled(env)) {
28
- out.write(`[tokenusage] sqlite3 CLI failed: ${formatError(cliError)}\n`);
29
- out.write(`[tokenusage] node:sqlite failed: ${formatError(nodeSqliteError)}\n`);
30
- }
31
- }
32
-
33
- function readSqliteRowsWithCli(dbPath, sql, { execFileSync, timeout, maxBuffer, readOnly }) {
34
- const args = readOnly ? ["-readonly", "-json", dbPath, sql] : ["-json", dbPath, sql];
35
- const raw = execFileSync("sqlite3", args, {
36
- windowsHide: true,
37
- encoding: "utf8",
38
- maxBuffer,
39
- timeout,
40
- stdio: ["pipe", "pipe", "pipe"],
41
- });
42
- if (!raw || !raw.trim()) return [];
43
- const rows = JSON.parse(raw);
44
- return Array.isArray(rows) ? rows : [];
45
- }
46
-
47
- function isSqliteCliUnavailable(err) {
48
- const message = formatError(err).toLowerCase();
49
- return (
50
- err?.code === "ENOENT" ||
51
- message.includes("spawn sqlite3 enoent") ||
52
- message.includes("sqlite3 enoent") ||
53
- message.includes("not recognized as an internal or external command")
54
- );
55
- }
56
-
57
- function isNodeSqliteUnavailable(err) {
58
- const message = formatError(err).toLowerCase();
59
- return (
60
- message.includes("no such built-in module") ||
61
- message.includes("cannot find module 'node:sqlite'") ||
62
- message.includes('cannot find module "node:sqlite"') ||
63
- message.includes("node:sqlite databasesync is unavailable")
64
- );
65
- }
66
-
67
- function readSqliteRowsWithNode(dbPath, sql, { requireFn }) {
68
- const { DatabaseSync } = requireFn("node:sqlite");
69
- if (typeof DatabaseSync !== "function") {
70
- throw new Error("node:sqlite DatabaseSync is unavailable");
71
- }
72
- const db = new DatabaseSync(dbPath, { readOnly: true });
73
- try {
74
- const rows = db.prepare(sql).all();
75
- return Array.isArray(rows) ? rows : [];
76
- } finally {
77
- db.close();
78
- }
79
- }
80
-
81
- function readSqliteJsonRows(dbPath, sql, options = {}) {
82
- if (!dbPath || !sql) return [];
83
- try {
84
- if (!fssync.existsSync(dbPath)) return [];
85
- } catch (_e) {
86
- return [];
87
- }
88
- const execFileSync = options.execFileSync || cp.execFileSync;
89
- const requireFn = options.requireFn || require;
90
- const env = options.env || process.env;
91
- const timeout = Number.isFinite(options.timeout) ? options.timeout : 30_000;
92
- const maxBuffer = Number.isFinite(options.maxBuffer) ? options.maxBuffer : 50 * 1024 * 1024;
93
- const label = options.label || "local";
94
-
95
- let cliError = null;
96
- try {
97
- return readSqliteRowsWithCli(dbPath, sql, { execFileSync, timeout, maxBuffer, readOnly: Boolean(options.readOnly) });
98
- } catch (err) {
99
- cliError = err;
100
- }
101
-
102
- let nodeSqliteError = null;
103
- try {
104
- return readSqliteRowsWithNode(dbPath, sql, { requireFn });
105
- } catch (err) {
106
- nodeSqliteError = err;
107
- }
108
-
109
- if (isSqliteCliUnavailable(cliError) && isNodeSqliteUnavailable(nodeSqliteError)) {
110
- warnSqliteUnavailable({
111
- dbPath,
112
- label,
113
- cliError,
114
- nodeSqliteError,
115
- env,
116
- stderr: options.stderr,
117
- });
118
- }
119
- if (options.throwOnReadFailure) {
120
- const err = new Error(
121
- `Cannot read ${label} SQLite database at ${dbPath}: ${formatError(cliError)}; ${formatError(nodeSqliteError)}`,
122
- );
123
- err.cliError = cliError;
124
- err.nodeSqliteError = nodeSqliteError;
125
- throw err;
126
- }
127
- return [];
128
- }
129
-
130
- async function readSqliteRowsWithCliAsync(dbPath, sql, { execFile, timeout, maxBuffer, readOnly }) {
131
- const args = readOnly ? ["-readonly", "-json", dbPath, sql] : ["-json", dbPath, sql];
132
- const { stdout } = await execFile("sqlite3", args, {
133
- windowsHide: true,
134
- encoding: "utf8",
135
- maxBuffer,
136
- timeout,
137
- });
138
- if (!stdout || !stdout.trim()) return [];
139
- const rows = JSON.parse(stdout);
140
- return Array.isArray(rows) ? rows : [];
141
- }
142
-
143
- // Async twin of readSqliteJsonRows for hot paths that must not block the event
144
- // loop (e.g. the usage-limits poll — see the "limits 路径 spawnSync 冻结全端点"
145
- // lesson). The CLI path runs via async execFile; the node:sqlite fallback is
146
- // synchronous (no async API exists), but it only runs when the sqlite3 CLI is
147
- // absent — uncommon on macOS/Linux, where the async path keeps the loop free.
148
- async function readSqliteJsonRowsAsync(dbPath, sql, options = {}) {
149
- if (!dbPath || !sql) return [];
150
- try {
151
- if (!fssync.existsSync(dbPath)) return [];
152
- } catch (_e) {
153
- return [];
154
- }
155
- const execFile = options.execFile || promisify(cp.execFile);
156
- const requireFn = options.requireFn || require;
157
- const env = options.env || process.env;
158
- const timeout = Number.isFinite(options.timeout) ? options.timeout : 30_000;
159
- const maxBuffer = Number.isFinite(options.maxBuffer) ? options.maxBuffer : 50 * 1024 * 1024;
160
- const label = options.label || "local";
161
-
162
- let cliError = null;
163
- try {
164
- return await readSqliteRowsWithCliAsync(dbPath, sql, { execFile, timeout, maxBuffer, readOnly: Boolean(options.readOnly) });
165
- } catch (err) {
166
- cliError = err;
167
- }
168
-
169
- let nodeSqliteError = null;
170
- try {
171
- return readSqliteRowsWithNode(dbPath, sql, { requireFn });
172
- } catch (err) {
173
- nodeSqliteError = err;
174
- }
175
-
176
- if (isSqliteCliUnavailable(cliError) && isNodeSqliteUnavailable(nodeSqliteError)) {
177
- warnSqliteUnavailable({
178
- dbPath,
179
- label,
180
- cliError,
181
- nodeSqliteError,
182
- env,
183
- stderr: options.stderr,
184
- });
185
- }
186
- if (options.throwOnReadFailure) {
187
- const err = new Error(
188
- `Cannot read ${label} SQLite database at ${dbPath}: ${formatError(cliError)}; ${formatError(nodeSqliteError)}`,
189
- );
190
- err.cliError = cliError;
191
- err.nodeSqliteError = nodeSqliteError;
192
- throw err;
193
- }
194
- return [];
195
- }
196
-
197
- function readSqliteFirstValue(dbPath, sql, column, options = {}) {
198
- const rows = readSqliteJsonRows(dbPath, sql, options);
199
- const row = rows[0];
200
- if (!row || typeof row !== "object") return null;
201
- const key = typeof column === "string" && column.length > 0 ? column : Object.keys(row)[0];
202
- const value = row[key];
203
- return typeof value === "string" ? value.trim() : value == null ? null : String(value).trim();
204
- }
205
-
206
- function resetSqliteReaderWarningsForTests() {
207
- warnedSqliteReadFailures.clear();
208
- }
209
-
210
- module.exports = {
211
- readSqliteJsonRows,
212
- readSqliteJsonRowsAsync,
213
- readSqliteFirstValue,
214
- resetSqliteReaderWarningsForTests,
215
- };
1
+ const cp = require("node:child_process");
2
+ const fssync = require("node:fs");
3
+ const { promisify } = require("node:util");
4
+
5
+ const warnedSqliteReadFailures = new Set();
6
+
7
+ function isDebugEnabled(env = process.env) {
8
+ const value = String((env && env.TOKENUSAGE_DEBUG) || "").toLowerCase();
9
+ return value === "1" || value === "true";
10
+ }
11
+
12
+ function formatError(err) {
13
+ if (!err) return "unknown error";
14
+ return err && err.message ? err.message : String(err);
15
+ }
16
+
17
+ function warnSqliteUnavailable({ dbPath, label, cliError, nodeSqliteError, env, stderr }) {
18
+ const key = `${label || "SQLite"}:${dbPath || ""}`;
19
+ if (warnedSqliteReadFailures.has(key)) return;
20
+ warnedSqliteReadFailures.add(key);
21
+
22
+ const out = stderr && typeof stderr.write === "function" ? stderr : process.stderr;
23
+ const displayLabel = label || "local";
24
+ out.write(
25
+ `[tokenusage] Cannot read ${displayLabel} SQLite database. Install sqlite3 CLI and add it to PATH, or use Node.js 22+ with node:sqlite support. Path: ${dbPath}\n`,
26
+ );
27
+ if (isDebugEnabled(env)) {
28
+ out.write(`[tokenusage] sqlite3 CLI failed: ${formatError(cliError)}\n`);
29
+ out.write(`[tokenusage] node:sqlite failed: ${formatError(nodeSqliteError)}\n`);
30
+ }
31
+ }
32
+
33
+ function readSqliteRowsWithCli(dbPath, sql, { execFileSync, timeout, maxBuffer, readOnly }) {
34
+ const args = readOnly ? ["-readonly", "-json", dbPath, sql] : ["-json", dbPath, sql];
35
+ const raw = execFileSync("sqlite3", args, {
36
+ windowsHide: true,
37
+ encoding: "utf8",
38
+ maxBuffer,
39
+ timeout,
40
+ stdio: ["pipe", "pipe", "pipe"],
41
+ });
42
+ if (!raw || !raw.trim()) return [];
43
+ const rows = JSON.parse(raw);
44
+ return Array.isArray(rows) ? rows : [];
45
+ }
46
+
47
+ function isSqliteCliUnavailable(err) {
48
+ const message = formatError(err).toLowerCase();
49
+ return (
50
+ err?.code === "ENOENT" ||
51
+ message.includes("spawn sqlite3 enoent") ||
52
+ message.includes("sqlite3 enoent") ||
53
+ message.includes("not recognized as an internal or external command")
54
+ );
55
+ }
56
+
57
+ function isNodeSqliteUnavailable(err) {
58
+ const message = formatError(err).toLowerCase();
59
+ return (
60
+ message.includes("no such built-in module") ||
61
+ message.includes("cannot find module 'node:sqlite'") ||
62
+ message.includes('cannot find module "node:sqlite"') ||
63
+ message.includes("node:sqlite databasesync is unavailable")
64
+ );
65
+ }
66
+
67
+ function readSqliteRowsWithNode(dbPath, sql, { requireFn }) {
68
+ const { DatabaseSync } = requireFn("node:sqlite");
69
+ if (typeof DatabaseSync !== "function") {
70
+ throw new Error("node:sqlite DatabaseSync is unavailable");
71
+ }
72
+ const db = new DatabaseSync(dbPath, { readOnly: true });
73
+ try {
74
+ const rows = db.prepare(sql).all();
75
+ return Array.isArray(rows) ? rows : [];
76
+ } finally {
77
+ db.close();
78
+ }
79
+ }
80
+
81
+ function readSqliteJsonRows(dbPath, sql, options = {}) {
82
+ if (!dbPath || !sql) return [];
83
+ try {
84
+ if (!fssync.existsSync(dbPath)) return [];
85
+ } catch (_e) {
86
+ return [];
87
+ }
88
+ const execFileSync = options.execFileSync || cp.execFileSync;
89
+ const requireFn = options.requireFn || require;
90
+ const env = options.env || process.env;
91
+ const timeout = Number.isFinite(options.timeout) ? options.timeout : 30_000;
92
+ const maxBuffer = Number.isFinite(options.maxBuffer) ? options.maxBuffer : 50 * 1024 * 1024;
93
+ const label = options.label || "local";
94
+
95
+ let cliError = null;
96
+ try {
97
+ return readSqliteRowsWithCli(dbPath, sql, { execFileSync, timeout, maxBuffer, readOnly: Boolean(options.readOnly) });
98
+ } catch (err) {
99
+ cliError = err;
100
+ }
101
+
102
+ let nodeSqliteError = null;
103
+ try {
104
+ return readSqliteRowsWithNode(dbPath, sql, { requireFn });
105
+ } catch (err) {
106
+ nodeSqliteError = err;
107
+ }
108
+
109
+ if (isSqliteCliUnavailable(cliError) && isNodeSqliteUnavailable(nodeSqliteError)) {
110
+ warnSqliteUnavailable({
111
+ dbPath,
112
+ label,
113
+ cliError,
114
+ nodeSqliteError,
115
+ env,
116
+ stderr: options.stderr,
117
+ });
118
+ }
119
+ if (options.throwOnReadFailure) {
120
+ const err = new Error(
121
+ `Cannot read ${label} SQLite database at ${dbPath}: ${formatError(cliError)}; ${formatError(nodeSqliteError)}`,
122
+ );
123
+ err.cliError = cliError;
124
+ err.nodeSqliteError = nodeSqliteError;
125
+ throw err;
126
+ }
127
+ return [];
128
+ }
129
+
130
+ async function readSqliteRowsWithCliAsync(dbPath, sql, { execFile, timeout, maxBuffer, readOnly }) {
131
+ const args = readOnly ? ["-readonly", "-json", dbPath, sql] : ["-json", dbPath, sql];
132
+ const { stdout } = await execFile("sqlite3", args, {
133
+ windowsHide: true,
134
+ encoding: "utf8",
135
+ maxBuffer,
136
+ timeout,
137
+ });
138
+ if (!stdout || !stdout.trim()) return [];
139
+ const rows = JSON.parse(stdout);
140
+ return Array.isArray(rows) ? rows : [];
141
+ }
142
+
143
+ // Async twin of readSqliteJsonRows for hot paths that must not block the event
144
+ // loop (e.g. the usage-limits poll — see the "limits 路径 spawnSync 冻结全端点"
145
+ // lesson). The CLI path runs via async execFile; the node:sqlite fallback is
146
+ // synchronous (no async API exists), but it only runs when the sqlite3 CLI is
147
+ // absent — uncommon on macOS/Linux, where the async path keeps the loop free.
148
+ async function readSqliteJsonRowsAsync(dbPath, sql, options = {}) {
149
+ if (!dbPath || !sql) return [];
150
+ try {
151
+ if (!fssync.existsSync(dbPath)) return [];
152
+ } catch (_e) {
153
+ return [];
154
+ }
155
+ const execFile = options.execFile || promisify(cp.execFile);
156
+ const requireFn = options.requireFn || require;
157
+ const env = options.env || process.env;
158
+ const timeout = Number.isFinite(options.timeout) ? options.timeout : 30_000;
159
+ const maxBuffer = Number.isFinite(options.maxBuffer) ? options.maxBuffer : 50 * 1024 * 1024;
160
+ const label = options.label || "local";
161
+
162
+ let cliError = null;
163
+ try {
164
+ return await readSqliteRowsWithCliAsync(dbPath, sql, { execFile, timeout, maxBuffer, readOnly: Boolean(options.readOnly) });
165
+ } catch (err) {
166
+ cliError = err;
167
+ }
168
+
169
+ let nodeSqliteError = null;
170
+ try {
171
+ return readSqliteRowsWithNode(dbPath, sql, { requireFn });
172
+ } catch (err) {
173
+ nodeSqliteError = err;
174
+ }
175
+
176
+ if (isSqliteCliUnavailable(cliError) && isNodeSqliteUnavailable(nodeSqliteError)) {
177
+ warnSqliteUnavailable({
178
+ dbPath,
179
+ label,
180
+ cliError,
181
+ nodeSqliteError,
182
+ env,
183
+ stderr: options.stderr,
184
+ });
185
+ }
186
+ if (options.throwOnReadFailure) {
187
+ const err = new Error(
188
+ `Cannot read ${label} SQLite database at ${dbPath}: ${formatError(cliError)}; ${formatError(nodeSqliteError)}`,
189
+ );
190
+ err.cliError = cliError;
191
+ err.nodeSqliteError = nodeSqliteError;
192
+ throw err;
193
+ }
194
+ return [];
195
+ }
196
+
197
+ function readSqliteFirstValue(dbPath, sql, column, options = {}) {
198
+ const rows = readSqliteJsonRows(dbPath, sql, options);
199
+ const row = rows[0];
200
+ if (!row || typeof row !== "object") return null;
201
+ const key = typeof column === "string" && column.length > 0 ? column : Object.keys(row)[0];
202
+ const value = row[key];
203
+ return typeof value === "string" ? value.trim() : value == null ? null : String(value).trim();
204
+ }
205
+
206
+ function resetSqliteReaderWarningsForTests() {
207
+ warnedSqliteReadFailures.clear();
208
+ }
209
+
210
+ module.exports = {
211
+ readSqliteJsonRows,
212
+ readSqliteJsonRowsAsync,
213
+ readSqliteFirstValue,
214
+ resetSqliteReaderWarningsForTests,
215
+ };
@@ -1,73 +1,73 @@
1
- "use strict";
2
-
3
- const path = require("node:path");
4
-
5
- const pkg = require("../../package.json");
6
- const { readJson, writeJson } = require("./fs");
7
-
8
- const STAR_CTA_URL = "https://github.com/baiwumm/token-usage";
9
- const STAR_CTA_STATE_FILE = "star-cta.json";
10
-
11
- function isTruthyFlag(value) {
12
- if (typeof value !== "string") return false;
13
- const normalized = value.trim().toLowerCase();
14
- return normalized === "1" || normalized === "true" || normalized === "yes";
15
- }
16
-
17
- function isInteractiveCli({ stdout = process.stdout, env = process.env } = {}) {
18
- if (!stdout?.isTTY) return false;
19
- if (env?.NODE_TEST_CONTEXT !== undefined) return false;
20
- if (isTruthyFlag(env?.CI)) return false;
21
- if (isTruthyFlag(env?.TOKENUSAGE_NO_STAR_PROMPT)) return false;
22
-
23
- const shell = String(env?.TOKENUSAGE_APP_SHELL || "").trim().toLowerCase();
24
- if (shell && shell !== "cli") return false;
25
- return true;
26
- }
27
-
28
- async function maybeShowStarCta({
29
- trackerDir,
30
- stdout = process.stdout,
31
- env = process.env,
32
- nowMs = Date.now(),
33
- version = pkg.version,
34
- readJsonFn = readJson,
35
- writeJsonFn = writeJson,
36
- } = {}) {
37
- if (!trackerDir) return { shown: false, reason: "no-tracker-dir" };
38
- if (!isInteractiveCli({ stdout, env })) {
39
- return { shown: false, reason: "non-interactive" };
40
- }
41
-
42
- const statePath = path.join(trackerDir, STAR_CTA_STATE_FILE);
43
- const state = await readJsonFn(statePath);
44
- if (state?.shown_at) return { shown: false, reason: "already-shown" };
45
-
46
- try {
47
- await writeJsonFn(statePath, {
48
- shown_at: new Date(nowMs).toISOString(),
49
- version: String(version || "").slice(0, 32),
50
- });
51
- } catch (_error) {
52
- // Do not print a prompt that we cannot remember; otherwise a read-only
53
- // install would show it on every launch.
54
- return { shown: false, reason: "state-write-failed" };
55
- }
56
-
57
- stdout.write(
58
- [
59
- " ⭐ If Token Usage helps you understand where your tokens go,",
60
- " a GitHub Star helps more developers find it:",
61
- ` ${STAR_CTA_URL}`,
62
- "",
63
- ].join("\n"),
64
- );
65
- return { shown: true, reason: "shown" };
66
- }
67
-
68
- module.exports = {
69
- STAR_CTA_URL,
70
- STAR_CTA_STATE_FILE,
71
- isInteractiveCli,
72
- maybeShowStarCta,
73
- };
1
+ "use strict";
2
+
3
+ const path = require("node:path");
4
+
5
+ const pkg = require("../../package.json");
6
+ const { readJson, writeJson } = require("./fs");
7
+
8
+ const STAR_CTA_URL = "https://github.com/baiwumm/token-usage";
9
+ const STAR_CTA_STATE_FILE = "star-cta.json";
10
+
11
+ function isTruthyFlag(value) {
12
+ if (typeof value !== "string") return false;
13
+ const normalized = value.trim().toLowerCase();
14
+ return normalized === "1" || normalized === "true" || normalized === "yes";
15
+ }
16
+
17
+ function isInteractiveCli({ stdout = process.stdout, env = process.env } = {}) {
18
+ if (!stdout?.isTTY) return false;
19
+ if (env?.NODE_TEST_CONTEXT !== undefined) return false;
20
+ if (isTruthyFlag(env?.CI)) return false;
21
+ if (isTruthyFlag(env?.TOKENUSAGE_NO_STAR_PROMPT)) return false;
22
+
23
+ const shell = String(env?.TOKENUSAGE_APP_SHELL || "").trim().toLowerCase();
24
+ if (shell && shell !== "cli") return false;
25
+ return true;
26
+ }
27
+
28
+ async function maybeShowStarCta({
29
+ trackerDir,
30
+ stdout = process.stdout,
31
+ env = process.env,
32
+ nowMs = Date.now(),
33
+ version = pkg.version,
34
+ readJsonFn = readJson,
35
+ writeJsonFn = writeJson,
36
+ } = {}) {
37
+ if (!trackerDir) return { shown: false, reason: "no-tracker-dir" };
38
+ if (!isInteractiveCli({ stdout, env })) {
39
+ return { shown: false, reason: "non-interactive" };
40
+ }
41
+
42
+ const statePath = path.join(trackerDir, STAR_CTA_STATE_FILE);
43
+ const state = await readJsonFn(statePath);
44
+ if (state?.shown_at) return { shown: false, reason: "already-shown" };
45
+
46
+ try {
47
+ await writeJsonFn(statePath, {
48
+ shown_at: new Date(nowMs).toISOString(),
49
+ version: String(version || "").slice(0, 32),
50
+ });
51
+ } catch (_error) {
52
+ // Do not print a prompt that we cannot remember; otherwise a read-only
53
+ // install would show it on every launch.
54
+ return { shown: false, reason: "state-write-failed" };
55
+ }
56
+
57
+ stdout.write(
58
+ [
59
+ " ⭐ If Token Usage helps you understand where your tokens go,",
60
+ " a GitHub Star helps more developers find it:",
61
+ ` ${STAR_CTA_URL}`,
62
+ "",
63
+ ].join("\n"),
64
+ );
65
+ return { shown: true, reason: "shown" };
66
+ }
67
+
68
+ module.exports = {
69
+ STAR_CTA_URL,
70
+ STAR_CTA_STATE_FILE,
71
+ isInteractiveCli,
72
+ maybeShowStarCta,
73
+ };