tokenusage-cli 0.1.0 → 0.2.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 (131) hide show
  1. package/CHANGELOG.md +39 -19
  2. package/LICENSE +22 -22
  3. package/README.md +94 -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 +181 -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,143 +1,143 @@
1
- const fs = require("node:fs");
2
-
3
- let enginePromise;
4
- let queryQueue = Promise.resolve();
5
-
6
- async function createEngine() {
7
- const [{ default: initialize }, sqlite, { FacadeVFS }] = await Promise.all([
8
- import("@journeyapps/wa-sqlite/dist/wa-sqlite.mjs"),
9
- import("@journeyapps/wa-sqlite"),
10
- import("@journeyapps/wa-sqlite/src/FacadeVFS.js"),
11
- ]);
12
- const module = await initialize({
13
- wasmBinary: fs.readFileSync(require.resolve("@journeyapps/wa-sqlite/dist/wa-sqlite.wasm")),
14
- });
15
- const api = sqlite.Factory(module);
16
- const snapshots = new Map();
17
- const handles = new Map();
18
-
19
- // Register one VFS for the lifetime of the engine. Each query gets a distinct
20
- // virtual filename, without accumulating WASM callbacks or touching disk.
21
- class TraeVFS extends FacadeVFS {
22
- constructor() {
23
- super("tokenusage-trae", module);
24
- }
25
-
26
- jOpen(name, fileId, flags, outputFlags) {
27
- const state = snapshots.get(name);
28
- if (!state || !(flags & sqlite.SQLITE_OPEN_MAIN_DB)) return sqlite.SQLITE_CANTOPEN;
29
- handles.set(fileId, state);
30
- outputFlags.setInt32(0, sqlite.SQLITE_OPEN_READONLY, true);
31
- return sqlite.SQLITE_OK;
32
- }
33
-
34
- jRead(fileId, output, offset) {
35
- const state = handles.get(fileId);
36
- if (!state) return sqlite.SQLITE_IOERR_READ;
37
- // FacadeVFS passes a proxy over WASM memory, not a native Uint8Array.
38
- // Keep the temporary buffer small and erase it after copying its page.
39
- const buffer = Buffer.alloc(output.byteLength);
40
- try {
41
- const count = state.snapshot.read(buffer, offset);
42
- if (!Number.isSafeInteger(count) || count < 0 || count > buffer.length) {
43
- throw new Error("Invalid TRAE snapshot read length.");
44
- }
45
- buffer.fill(0, count);
46
- output.set(buffer);
47
- return count === buffer.length ? sqlite.SQLITE_OK : sqlite.SQLITE_IOERR_SHORT_READ;
48
- } catch (err) {
49
- state.error ||= err;
50
- output.fill(0);
51
- return sqlite.SQLITE_IOERR_READ;
52
- } finally {
53
- buffer.fill(0);
54
- }
55
- }
56
-
57
- jFileSize(fileId, output) {
58
- const state = handles.get(fileId);
59
- if (!state) return sqlite.SQLITE_IOERR_FSTAT;
60
- output.setBigInt64(0, BigInt(state.snapshot.byteLength), true);
61
- return sqlite.SQLITE_OK;
62
- }
63
-
64
- jClose(fileId) {
65
- handles.delete(fileId);
66
- return sqlite.SQLITE_OK;
67
- }
68
-
69
- jAccess(name, flags, output) {
70
- output.setInt32(0, snapshots.has(name) ? 1 : 0, true);
71
- return sqlite.SQLITE_OK;
72
- }
73
-
74
- jWrite() { return sqlite.SQLITE_READONLY; }
75
- jTruncate() { return sqlite.SQLITE_READONLY; }
76
- jDelete() { return sqlite.SQLITE_READONLY; }
77
- }
78
-
79
- const vfs = new TraeVFS();
80
- api.vfs_register(vfs, false);
81
- return { api, sqlite, snapshots, handles, vfs, nextId: 0 };
82
- }
83
-
84
- /**
85
- * Query a read-only, random-access snapshot without materializing its database.
86
- * The caller owns snapshot cleanup; only the projected SQL rows leave this VFS.
87
- */
88
- async function querySnapshot(snapshot, callback) {
89
- if (!Number.isSafeInteger(snapshot.byteLength) || snapshot.byteLength < 0) {
90
- throw new Error("Invalid TRAE snapshot size.");
91
- }
92
- enginePromise ||= createEngine().catch((err) => {
93
- enginePromise = undefined;
94
- throw err;
95
- });
96
- const engine = await enginePromise;
97
- const { api, sqlite, snapshots, handles, vfs } = engine;
98
- const name = `trae-${++engine.nextId}.db`;
99
- const state = { snapshot, error: null };
100
- snapshots.set(name, state);
101
- let db;
102
- let failure;
103
- try {
104
- db = await api.open_v2(name, sqlite.SQLITE_OPEN_READONLY, vfs.name);
105
- const query = async (sql) => {
106
- const rows = [];
107
- try {
108
- await api.exec(db, sql, (values, columns) => {
109
- rows.push(Object.fromEntries(columns.map((column, index) => [column, values[index]])));
110
- });
111
- } catch (err) {
112
- throw state.error || err;
113
- }
114
- return rows;
115
- };
116
- await query("PRAGMA query_only=ON; PRAGMA cache_size=-4096; PRAGMA temp_store=MEMORY;");
117
- return await callback(query);
118
- } catch (err) {
119
- failure = state.error || err;
120
- throw failure;
121
- } finally {
122
- try {
123
- if (db) await api.close(db);
124
- } catch (err) {
125
- if (!failure) throw state.error || err;
126
- } finally {
127
- snapshots.delete(name);
128
- for (const [fileId, opened] of handles) {
129
- if (opened === state) handles.delete(fileId);
130
- }
131
- }
132
- }
133
- }
134
-
135
- function withTraeSqlite(snapshot, callback) {
136
- // The package's async convenience API shares temporary WASM allocations.
137
- // Serialize access to the single engine even when callers read in parallel.
138
- const operation = queryQueue.then(() => querySnapshot(snapshot, callback));
139
- queryQueue = operation.catch(() => {});
140
- return operation;
141
- }
142
-
143
- module.exports = { withTraeSqlite };
1
+ const fs = require("node:fs");
2
+
3
+ let enginePromise;
4
+ let queryQueue = Promise.resolve();
5
+
6
+ async function createEngine() {
7
+ const [{ default: initialize }, sqlite, { FacadeVFS }] = await Promise.all([
8
+ import("@journeyapps/wa-sqlite/dist/wa-sqlite.mjs"),
9
+ import("@journeyapps/wa-sqlite"),
10
+ import("@journeyapps/wa-sqlite/src/FacadeVFS.js"),
11
+ ]);
12
+ const module = await initialize({
13
+ wasmBinary: fs.readFileSync(require.resolve("@journeyapps/wa-sqlite/dist/wa-sqlite.wasm")),
14
+ });
15
+ const api = sqlite.Factory(module);
16
+ const snapshots = new Map();
17
+ const handles = new Map();
18
+
19
+ // Register one VFS for the lifetime of the engine. Each query gets a distinct
20
+ // virtual filename, without accumulating WASM callbacks or touching disk.
21
+ class TraeVFS extends FacadeVFS {
22
+ constructor() {
23
+ super("tokenusage-trae", module);
24
+ }
25
+
26
+ jOpen(name, fileId, flags, outputFlags) {
27
+ const state = snapshots.get(name);
28
+ if (!state || !(flags & sqlite.SQLITE_OPEN_MAIN_DB)) return sqlite.SQLITE_CANTOPEN;
29
+ handles.set(fileId, state);
30
+ outputFlags.setInt32(0, sqlite.SQLITE_OPEN_READONLY, true);
31
+ return sqlite.SQLITE_OK;
32
+ }
33
+
34
+ jRead(fileId, output, offset) {
35
+ const state = handles.get(fileId);
36
+ if (!state) return sqlite.SQLITE_IOERR_READ;
37
+ // FacadeVFS passes a proxy over WASM memory, not a native Uint8Array.
38
+ // Keep the temporary buffer small and erase it after copying its page.
39
+ const buffer = Buffer.alloc(output.byteLength);
40
+ try {
41
+ const count = state.snapshot.read(buffer, offset);
42
+ if (!Number.isSafeInteger(count) || count < 0 || count > buffer.length) {
43
+ throw new Error("Invalid TRAE snapshot read length.");
44
+ }
45
+ buffer.fill(0, count);
46
+ output.set(buffer);
47
+ return count === buffer.length ? sqlite.SQLITE_OK : sqlite.SQLITE_IOERR_SHORT_READ;
48
+ } catch (err) {
49
+ state.error ||= err;
50
+ output.fill(0);
51
+ return sqlite.SQLITE_IOERR_READ;
52
+ } finally {
53
+ buffer.fill(0);
54
+ }
55
+ }
56
+
57
+ jFileSize(fileId, output) {
58
+ const state = handles.get(fileId);
59
+ if (!state) return sqlite.SQLITE_IOERR_FSTAT;
60
+ output.setBigInt64(0, BigInt(state.snapshot.byteLength), true);
61
+ return sqlite.SQLITE_OK;
62
+ }
63
+
64
+ jClose(fileId) {
65
+ handles.delete(fileId);
66
+ return sqlite.SQLITE_OK;
67
+ }
68
+
69
+ jAccess(name, flags, output) {
70
+ output.setInt32(0, snapshots.has(name) ? 1 : 0, true);
71
+ return sqlite.SQLITE_OK;
72
+ }
73
+
74
+ jWrite() { return sqlite.SQLITE_READONLY; }
75
+ jTruncate() { return sqlite.SQLITE_READONLY; }
76
+ jDelete() { return sqlite.SQLITE_READONLY; }
77
+ }
78
+
79
+ const vfs = new TraeVFS();
80
+ api.vfs_register(vfs, false);
81
+ return { api, sqlite, snapshots, handles, vfs, nextId: 0 };
82
+ }
83
+
84
+ /**
85
+ * Query a read-only, random-access snapshot without materializing its database.
86
+ * The caller owns snapshot cleanup; only the projected SQL rows leave this VFS.
87
+ */
88
+ async function querySnapshot(snapshot, callback) {
89
+ if (!Number.isSafeInteger(snapshot.byteLength) || snapshot.byteLength < 0) {
90
+ throw new Error("Invalid TRAE snapshot size.");
91
+ }
92
+ enginePromise ||= createEngine().catch((err) => {
93
+ enginePromise = undefined;
94
+ throw err;
95
+ });
96
+ const engine = await enginePromise;
97
+ const { api, sqlite, snapshots, handles, vfs } = engine;
98
+ const name = `trae-${++engine.nextId}.db`;
99
+ const state = { snapshot, error: null };
100
+ snapshots.set(name, state);
101
+ let db;
102
+ let failure;
103
+ try {
104
+ db = await api.open_v2(name, sqlite.SQLITE_OPEN_READONLY, vfs.name);
105
+ const query = async (sql) => {
106
+ const rows = [];
107
+ try {
108
+ await api.exec(db, sql, (values, columns) => {
109
+ rows.push(Object.fromEntries(columns.map((column, index) => [column, values[index]])));
110
+ });
111
+ } catch (err) {
112
+ throw state.error || err;
113
+ }
114
+ return rows;
115
+ };
116
+ await query("PRAGMA query_only=ON; PRAGMA cache_size=-4096; PRAGMA temp_store=MEMORY;");
117
+ return await callback(query);
118
+ } catch (err) {
119
+ failure = state.error || err;
120
+ throw failure;
121
+ } finally {
122
+ try {
123
+ if (db) await api.close(db);
124
+ } catch (err) {
125
+ if (!failure) throw state.error || err;
126
+ } finally {
127
+ snapshots.delete(name);
128
+ for (const [fileId, opened] of handles) {
129
+ if (opened === state) handles.delete(fileId);
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ function withTraeSqlite(snapshot, callback) {
136
+ // The package's async convenience API shares temporary WASM allocations.
137
+ // Serialize access to the single engine even when callers read in parallel.
138
+ const operation = queryQueue.then(() => querySnapshot(snapshot, callback));
139
+ queryQueue = operation.catch(() => {});
140
+ return operation;
141
+ }
142
+
143
+ module.exports = { withTraeSqlite };
@@ -1,121 +1,121 @@
1
- "use strict";
2
-
3
- // TRAE stores the last request's usage alongside optional whole-turn prompt
4
- // and completion counters. Reconcile the request first: its total cannot
5
- // validate a whole-turn count, and its cache count is not a whole-turn count.
6
- function normalizeTraeUsage(raw, { model } = {}) {
7
- let usage = raw;
8
- if (typeof usage === "string") {
9
- try { usage = JSON.parse(usage); } catch { return null; }
10
- }
11
- if (!usage || typeof usage !== "object" || Array.isArray(usage)) return null;
12
- const fields = [
13
- "prompt_tokens", "prompt_tokens_total", "input_tokens",
14
- "completion_tokens", "completion_tokens_total", "output_tokens",
15
- "cache_read_input_tokens", "cache_creation_input_tokens",
16
- "reasoning_tokens", "reasoning_output_tokens", "total_tokens",
17
- ];
18
- for (const field of fields) {
19
- if (usage[field] != null && (!Number.isSafeInteger(usage[field]) || usage[field] < 0)) {
20
- return null;
21
- }
22
- }
23
- for (const [left, right] of [["prompt_tokens", "input_tokens"],
24
- ["completion_tokens", "output_tokens"], ["reasoning_tokens", "reasoning_output_tokens"]]) {
25
- if (usage[left] != null && usage[right] != null && usage[left] !== usage[right]) return null;
26
- }
27
- if (!["prompt_tokens", "prompt_tokens_total", "input_tokens", "completion_tokens",
28
- "completion_tokens_total", "output_tokens"].some((field) => usage[field] != null)) {
29
- return null;
30
- }
31
- const input = usage.prompt_tokens ?? usage.input_tokens ?? usage.prompt_tokens_total ?? 0;
32
- const output = usage.completion_tokens ?? usage.output_tokens ?? usage.completion_tokens_total ?? 0;
33
- const aggregateInput = usage.prompt_tokens_total > 0 ? usage.prompt_tokens_total : input;
34
- const aggregateOutput = usage.completion_tokens_total > 0 ? usage.completion_tokens_total : output;
35
- if (aggregateInput < input || aggregateOutput < output) return null;
36
- const cached = usage.cache_read_input_tokens ?? 0;
37
- let written = usage.cache_creation_input_tokens ?? 0;
38
- let reasoning = usage.reasoning_tokens ?? usage.reasoning_output_tokens ?? 0;
39
- let estimated = false;
40
- const isGemini = /^gemini(?:-|$)/i.test(normalizeTraeModel(model ?? usage.name));
41
- // Some TRAE Gemini rows copy the cache-read counter into the write field,
42
- // even when their sum exceeds the entire prompt. Gemini's GenerateContent
43
- // metadata reports cachedContentTokenCount, not an additional write count.
44
- // Keep the reported read and mark this narrow compatibility repair estimated.
45
- if (isGemini && cached > 0 && written === cached && cached + written > input) {
46
- written = 0;
47
- estimated = true;
48
- }
49
- // Older TRAE Gemini rows omit thoughts while retaining Gemini's total
50
- // (prompt + candidates + thoughts). Infer only that documented residual,
51
- // never an arbitrary provider's unexplained total.
52
- if (isGemini && usage.reasoning_tokens == null && usage.reasoning_output_tokens == null &&
53
- usage.total_tokens > input + output && written === 0 && cached <= input) {
54
- reasoning = usage.total_tokens - input - output;
55
- estimated = true;
56
- }
57
- // A total is necessary to distinguish inclusive/exclusive counters when
58
- // cache or reasoning is present. Missing metadata must not imply $0 usage
59
- // or guessed input. A zero *_total field means unset, not an empty turn.
60
- if (usage.total_tokens == null && (cached || written || reasoning)) return null;
61
- const total = usage.total_tokens ?? (input + output);
62
- if (!Number.isSafeInteger(total)) return null;
63
- const candidates = new Map();
64
- for (const includedCache of new Set([cached + written, cached, 0])) {
65
- for (const includedReasoning of new Set([reasoning, 0])) {
66
- const noncached = input - includedCache;
67
- const nonreasoning = output - includedReasoning;
68
- if (noncached < 0 || nonreasoning < 0) continue;
69
- if (noncached + cached + written + nonreasoning + reasoning !== total) continue;
70
- const totals = {
71
- input_tokens: noncached,
72
- cached_input_tokens: cached,
73
- cache_creation_input_tokens: written,
74
- output_tokens: nonreasoning,
75
- reasoning_output_tokens: reasoning,
76
- total_tokens: total,
77
- };
78
- candidates.set(JSON.stringify(totals), totals);
79
- }
80
- }
81
- if (candidates.size !== 1) return null;
82
- const totals = candidates.values().next().value;
83
- if (aggregateInput !== input || aggregateOutput !== output) {
84
- // The *_total counters cover the whole turn; the cache and reasoning fields
85
- // describe its final request. The difference in prompt tokens has no cache
86
- // split, so it counts toward total_tokens but no priced column. Completion
87
- // tokens are priced the same with or without caching and stay in output.
88
- const unpriced = aggregateInput - input;
89
- totals.output_tokens += aggregateOutput - output;
90
- totals.total_tokens += unpriced + aggregateOutput - output;
91
- if (unpriced > 0) totals.unpriced_input_tokens = unpriced;
92
- }
93
- if (Object.values(totals).some((value) => !Number.isSafeInteger(value))) return null;
94
- if (estimated) totals.usage_precision = "estimated";
95
- return totals;
96
- }
97
-
98
- function normalizeTraeModel(value) {
99
- if (typeof value !== "string") return "trae-unknown";
100
- const model = value.trim().split("__")[0].toLowerCase().replace(/\s+/g, "-");
101
- // TRAE's display alias otherwise fuzzy-matches a regional Sonnet SKU and
102
- // loses cache pricing. Sonnet 4 has the fixed 2025-05-14 API model id.
103
- if (model === "claude-4-sonnet") return "claude-sonnet-4-20250514";
104
- return model && model !== "-" && !model.includes("|") ? model : "trae-unknown";
105
- }
106
-
107
- function traeTimestamp(value) {
108
- if (value == null || value === "") return null;
109
- let millis;
110
- if (typeof value === "number" || (typeof value === "string" && /^\d+(\.\d+)?$/.test(value))) {
111
- const numeric = Number(value);
112
- millis = numeric < 100_000_000_000 ? numeric * 1000 : numeric;
113
- } else if (typeof value === "string") {
114
- millis = Date.parse(value);
115
- }
116
- if (!Number.isFinite(millis) || millis <= 0) return null;
117
- const date = new Date(millis);
118
- return Number.isFinite(date.getTime()) ? date.toISOString() : null;
119
- }
120
-
121
- module.exports = { normalizeTraeUsage, normalizeTraeModel, traeTimestamp };
1
+ "use strict";
2
+
3
+ // TRAE stores the last request's usage alongside optional whole-turn prompt
4
+ // and completion counters. Reconcile the request first: its total cannot
5
+ // validate a whole-turn count, and its cache count is not a whole-turn count.
6
+ function normalizeTraeUsage(raw, { model } = {}) {
7
+ let usage = raw;
8
+ if (typeof usage === "string") {
9
+ try { usage = JSON.parse(usage); } catch { return null; }
10
+ }
11
+ if (!usage || typeof usage !== "object" || Array.isArray(usage)) return null;
12
+ const fields = [
13
+ "prompt_tokens", "prompt_tokens_total", "input_tokens",
14
+ "completion_tokens", "completion_tokens_total", "output_tokens",
15
+ "cache_read_input_tokens", "cache_creation_input_tokens",
16
+ "reasoning_tokens", "reasoning_output_tokens", "total_tokens",
17
+ ];
18
+ for (const field of fields) {
19
+ if (usage[field] != null && (!Number.isSafeInteger(usage[field]) || usage[field] < 0)) {
20
+ return null;
21
+ }
22
+ }
23
+ for (const [left, right] of [["prompt_tokens", "input_tokens"],
24
+ ["completion_tokens", "output_tokens"], ["reasoning_tokens", "reasoning_output_tokens"]]) {
25
+ if (usage[left] != null && usage[right] != null && usage[left] !== usage[right]) return null;
26
+ }
27
+ if (!["prompt_tokens", "prompt_tokens_total", "input_tokens", "completion_tokens",
28
+ "completion_tokens_total", "output_tokens"].some((field) => usage[field] != null)) {
29
+ return null;
30
+ }
31
+ const input = usage.prompt_tokens ?? usage.input_tokens ?? usage.prompt_tokens_total ?? 0;
32
+ const output = usage.completion_tokens ?? usage.output_tokens ?? usage.completion_tokens_total ?? 0;
33
+ const aggregateInput = usage.prompt_tokens_total > 0 ? usage.prompt_tokens_total : input;
34
+ const aggregateOutput = usage.completion_tokens_total > 0 ? usage.completion_tokens_total : output;
35
+ if (aggregateInput < input || aggregateOutput < output) return null;
36
+ const cached = usage.cache_read_input_tokens ?? 0;
37
+ let written = usage.cache_creation_input_tokens ?? 0;
38
+ let reasoning = usage.reasoning_tokens ?? usage.reasoning_output_tokens ?? 0;
39
+ let estimated = false;
40
+ const isGemini = /^gemini(?:-|$)/i.test(normalizeTraeModel(model ?? usage.name));
41
+ // Some TRAE Gemini rows copy the cache-read counter into the write field,
42
+ // even when their sum exceeds the entire prompt. Gemini's GenerateContent
43
+ // metadata reports cachedContentTokenCount, not an additional write count.
44
+ // Keep the reported read and mark this narrow compatibility repair estimated.
45
+ if (isGemini && cached > 0 && written === cached && cached + written > input) {
46
+ written = 0;
47
+ estimated = true;
48
+ }
49
+ // Older TRAE Gemini rows omit thoughts while retaining Gemini's total
50
+ // (prompt + candidates + thoughts). Infer only that documented residual,
51
+ // never an arbitrary provider's unexplained total.
52
+ if (isGemini && usage.reasoning_tokens == null && usage.reasoning_output_tokens == null &&
53
+ usage.total_tokens > input + output && written === 0 && cached <= input) {
54
+ reasoning = usage.total_tokens - input - output;
55
+ estimated = true;
56
+ }
57
+ // A total is necessary to distinguish inclusive/exclusive counters when
58
+ // cache or reasoning is present. Missing metadata must not imply $0 usage
59
+ // or guessed input. A zero *_total field means unset, not an empty turn.
60
+ if (usage.total_tokens == null && (cached || written || reasoning)) return null;
61
+ const total = usage.total_tokens ?? (input + output);
62
+ if (!Number.isSafeInteger(total)) return null;
63
+ const candidates = new Map();
64
+ for (const includedCache of new Set([cached + written, cached, 0])) {
65
+ for (const includedReasoning of new Set([reasoning, 0])) {
66
+ const noncached = input - includedCache;
67
+ const nonreasoning = output - includedReasoning;
68
+ if (noncached < 0 || nonreasoning < 0) continue;
69
+ if (noncached + cached + written + nonreasoning + reasoning !== total) continue;
70
+ const totals = {
71
+ input_tokens: noncached,
72
+ cached_input_tokens: cached,
73
+ cache_creation_input_tokens: written,
74
+ output_tokens: nonreasoning,
75
+ reasoning_output_tokens: reasoning,
76
+ total_tokens: total,
77
+ };
78
+ candidates.set(JSON.stringify(totals), totals);
79
+ }
80
+ }
81
+ if (candidates.size !== 1) return null;
82
+ const totals = candidates.values().next().value;
83
+ if (aggregateInput !== input || aggregateOutput !== output) {
84
+ // The *_total counters cover the whole turn; the cache and reasoning fields
85
+ // describe its final request. The difference in prompt tokens has no cache
86
+ // split, so it counts toward total_tokens but no priced column. Completion
87
+ // tokens are priced the same with or without caching and stay in output.
88
+ const unpriced = aggregateInput - input;
89
+ totals.output_tokens += aggregateOutput - output;
90
+ totals.total_tokens += unpriced + aggregateOutput - output;
91
+ if (unpriced > 0) totals.unpriced_input_tokens = unpriced;
92
+ }
93
+ if (Object.values(totals).some((value) => !Number.isSafeInteger(value))) return null;
94
+ if (estimated) totals.usage_precision = "estimated";
95
+ return totals;
96
+ }
97
+
98
+ function normalizeTraeModel(value) {
99
+ if (typeof value !== "string") return "trae-unknown";
100
+ const model = value.trim().split("__")[0].toLowerCase().replace(/\s+/g, "-");
101
+ // TRAE's display alias otherwise fuzzy-matches a regional Sonnet SKU and
102
+ // loses cache pricing. Sonnet 4 has the fixed 2025-05-14 API model id.
103
+ if (model === "claude-4-sonnet") return "claude-sonnet-4-20250514";
104
+ return model && model !== "-" && !model.includes("|") ? model : "trae-unknown";
105
+ }
106
+
107
+ function traeTimestamp(value) {
108
+ if (value == null || value === "") return null;
109
+ let millis;
110
+ if (typeof value === "number" || (typeof value === "string" && /^\d+(\.\d+)?$/.test(value))) {
111
+ const numeric = Number(value);
112
+ millis = numeric < 100_000_000_000 ? numeric * 1000 : numeric;
113
+ } else if (typeof value === "string") {
114
+ millis = Date.parse(value);
115
+ }
116
+ if (!Number.isFinite(millis) || millis <= 0) return null;
117
+ const date = new Date(millis);
118
+ return Number.isFinite(date.getTime()) ? date.toISOString() : null;
119
+ }
120
+
121
+ module.exports = { normalizeTraeUsage, normalizeTraeModel, traeTimestamp };