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.
- package/CHANGELOG.md +46 -19
- package/LICENSE +22 -22
- package/README.md +123 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +210 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
package/src/lib/trae-db.js
CHANGED
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
const fs = require("node:fs");
|
|
2
|
-
const os = require("node:os");
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
|
|
5
|
-
const { openTraeSnapshot } = require("./trae-snapshot");
|
|
6
|
-
const { withTraeSqlite } = require("./trae-sqlite");
|
|
7
|
-
|
|
8
|
-
const TOKEN_FIELDS = [
|
|
9
|
-
"prompt_tokens", "prompt_tokens_total", "input_tokens",
|
|
10
|
-
"completion_tokens", "completion_tokens_total", "output_tokens",
|
|
11
|
-
"total_tokens", "cache_read_input_tokens", "cache_creation_input_tokens",
|
|
12
|
-
"reasoning_tokens", "reasoning_output_tokens",
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
function resolveTraeDbPaths(env = process.env, { platform = process.platform, home = os.homedir() } = {}) {
|
|
16
|
-
const explicit = String(env.TOKENUSAGE_TRAE_DB || "").trim();
|
|
17
|
-
if (explicit) return [path.resolve(explicit)];
|
|
18
|
-
const customHome = String(env.TOKENUSAGE_TRAE_HOME || "").trim();
|
|
19
|
-
const parent = platform === "win32"
|
|
20
|
-
? env.APPDATA || path.join(home, "AppData", "Roaming")
|
|
21
|
-
: platform === "darwin"
|
|
22
|
-
? path.join(home, "Library", "Application Support")
|
|
23
|
-
: env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
24
|
-
const roots = customHome ? [customHome] : [path.join(parent, "Trae"), path.join(parent, "TRAE SOLO")];
|
|
25
|
-
const result = [];
|
|
26
|
-
for (const root of roots) {
|
|
27
|
-
// A decrypted sibling may be an old export. Only an explicit DB override
|
|
28
|
-
// opts into one; normal discovery always reads the current application DB.
|
|
29
|
-
for (const module of ["ai-agent", "ai-chat"]) {
|
|
30
|
-
const candidate = path.resolve(root, "ModularData", module, "database.db");
|
|
31
|
-
let present;
|
|
32
|
-
try { present = fs.statSync(candidate).isFile(); }
|
|
33
|
-
catch (err) {
|
|
34
|
-
// Permission failures must reach the reader's diagnostics, not masquerade
|
|
35
|
-
// as an absent installation and silently drop an entire usage history.
|
|
36
|
-
present = err.code !== "ENOENT" && err.code !== "ENOTDIR";
|
|
37
|
-
}
|
|
38
|
-
if (present) {
|
|
39
|
-
result.push(candidate);
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return [...new Set(result)];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function error(message) {
|
|
48
|
-
return new Error(`Cannot read TRAE usage: ${message}`);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function projectUsage(queryRows) {
|
|
52
|
-
const tables = await queryRows("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'chat_turn'");
|
|
53
|
-
// history_v2.token_usage can be a context-size snapshot rather than billed
|
|
54
|
-
// usage. Do not synthesize input tokens from those scalar values.
|
|
55
|
-
if (!tables.length) throw error("unsupported database schema: chat_turn table is missing.");
|
|
56
|
-
const columns = new Set((await queryRows("PRAGMA table_info(chat_turn)")).map((row) => row.name));
|
|
57
|
-
if (!columns.has("context") || (!columns.has("created_at") && !columns.has("updated_at"))) {
|
|
58
|
-
throw error("unsupported chat_turn schema.");
|
|
59
|
-
}
|
|
60
|
-
const column = (name, fallback = "NULL") => columns.has(name) ? `"${name}"` : fallback;
|
|
61
|
-
const date = columns.has("created_at") && columns.has("updated_at")
|
|
62
|
-
? 'COALESCE("created_at", "updated_at")' : column("created_at", column("updated_at"));
|
|
63
|
-
const modelPaths = [
|
|
64
|
-
"persist_user_message_context.model_info.display_model_name",
|
|
65
|
-
"persist_user_message_context.model_info.model_name",
|
|
66
|
-
"persist_user_message_context.model_info.config_name", "model_name", "model",
|
|
67
|
-
"agent_model", "selected_model.name", "selectedModel.name",
|
|
68
|
-
];
|
|
69
|
-
const model = modelPaths.map((field) => `CASE WHEN json_type(context, '$.${field}') = 'text'
|
|
70
|
-
THEN NULLIF(NULLIF(TRIM(json_extract(context, '$.${field}')), ''), '-') END`);
|
|
71
|
-
const fields = TOKEN_FIELDS.map((field) => `CASE
|
|
72
|
-
WHEN json_type(context, '$.token_usage.${field}') IN ('integer', 'real')
|
|
73
|
-
THEN json_extract(context, '$.token_usage.${field}') END AS "${field}"`);
|
|
74
|
-
const invalidFields = TOKEN_FIELDS.map((field) =>
|
|
75
|
-
`COALESCE(json_type(context, '$.token_usage.${field}') NOT IN ('integer', 'real', 'null'), 0)`);
|
|
76
|
-
// Project inside SQLite: prompts, messages, session titles, fee/account data,
|
|
77
|
-
// and the rest of the context never cross into JS results or cursor state.
|
|
78
|
-
const query = `SELECT CAST(${column("id", "rowid")} AS TEXT) AS id,
|
|
79
|
-
${column("session_id")} AS session_id, ${column("turn_id")} AS turn_id,
|
|
80
|
-
${date} AS created_at, ${column("updated_at")} AS updated_at,
|
|
81
|
-
COALESCE(${model.join(", ")}, 'trae-unknown') AS model,
|
|
82
|
-
${fields.join(", ")}, (${invalidFields.join(" OR ")}) AS invalid_usage
|
|
83
|
-
FROM chat_turn WHERE CASE WHEN json_valid(context)
|
|
84
|
-
THEN json_type(context, '$.token_usage') = 'object' ELSE 0 END`;
|
|
85
|
-
return (await queryRows(query)).map((row) => {
|
|
86
|
-
const usage = {};
|
|
87
|
-
let invalid = Boolean(row.invalid_usage);
|
|
88
|
-
for (const field of TOKEN_FIELDS) {
|
|
89
|
-
const value = row[field];
|
|
90
|
-
if (value == null) continue;
|
|
91
|
-
if (!Number.isSafeInteger(value) || value < 0) invalid = true;
|
|
92
|
-
else usage[field] = value;
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
id: row.id, session_id: row.session_id, turn_id: row.turn_id,
|
|
96
|
-
created_at: row.created_at, updated_at: row.updated_at,
|
|
97
|
-
model: row.model, usage: invalid ? null : usage,
|
|
98
|
-
};
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async function readTraeUsageRows(dbPath, { env = process.env } = {}) {
|
|
103
|
-
for (let attempt = 0; attempt < 3; attempt++) {
|
|
104
|
-
let snapshot;
|
|
105
|
-
try { snapshot = openTraeSnapshot(dbPath, env); }
|
|
106
|
-
catch (err) {
|
|
107
|
-
// A writer can also change the files while the snapshot opens. Retry
|
|
108
|
-
// that too, and report the specific failure once attempts run out.
|
|
109
|
-
if (err?.transient && attempt < 2) continue;
|
|
110
|
-
throw err;
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
let result;
|
|
114
|
-
try { result = await withTraeSqlite(snapshot, projectUsage); }
|
|
115
|
-
catch (err) {
|
|
116
|
-
// A writer may have changed pages while SQLite was reading. Discard
|
|
117
|
-
// every projected row and retry; never publish a mixed snapshot.
|
|
118
|
-
if (snapshot.isStable()) throw err;
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (snapshot.isStable()) return result;
|
|
122
|
-
} finally { snapshot.close(); }
|
|
123
|
-
}
|
|
124
|
-
throw error("database changed during the read; retry after TRAE finishes its current turn.");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
module.exports = { resolveTraeDbPaths, readTraeUsageRows };
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const os = require("node:os");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
|
+
const { openTraeSnapshot } = require("./trae-snapshot");
|
|
6
|
+
const { withTraeSqlite } = require("./trae-sqlite");
|
|
7
|
+
|
|
8
|
+
const TOKEN_FIELDS = [
|
|
9
|
+
"prompt_tokens", "prompt_tokens_total", "input_tokens",
|
|
10
|
+
"completion_tokens", "completion_tokens_total", "output_tokens",
|
|
11
|
+
"total_tokens", "cache_read_input_tokens", "cache_creation_input_tokens",
|
|
12
|
+
"reasoning_tokens", "reasoning_output_tokens",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
function resolveTraeDbPaths(env = process.env, { platform = process.platform, home = os.homedir() } = {}) {
|
|
16
|
+
const explicit = String(env.TOKENUSAGE_TRAE_DB || "").trim();
|
|
17
|
+
if (explicit) return [path.resolve(explicit)];
|
|
18
|
+
const customHome = String(env.TOKENUSAGE_TRAE_HOME || "").trim();
|
|
19
|
+
const parent = platform === "win32"
|
|
20
|
+
? env.APPDATA || path.join(home, "AppData", "Roaming")
|
|
21
|
+
: platform === "darwin"
|
|
22
|
+
? path.join(home, "Library", "Application Support")
|
|
23
|
+
: env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
24
|
+
const roots = customHome ? [customHome] : [path.join(parent, "Trae"), path.join(parent, "TRAE SOLO")];
|
|
25
|
+
const result = [];
|
|
26
|
+
for (const root of roots) {
|
|
27
|
+
// A decrypted sibling may be an old export. Only an explicit DB override
|
|
28
|
+
// opts into one; normal discovery always reads the current application DB.
|
|
29
|
+
for (const module of ["ai-agent", "ai-chat"]) {
|
|
30
|
+
const candidate = path.resolve(root, "ModularData", module, "database.db");
|
|
31
|
+
let present;
|
|
32
|
+
try { present = fs.statSync(candidate).isFile(); }
|
|
33
|
+
catch (err) {
|
|
34
|
+
// Permission failures must reach the reader's diagnostics, not masquerade
|
|
35
|
+
// as an absent installation and silently drop an entire usage history.
|
|
36
|
+
present = err.code !== "ENOENT" && err.code !== "ENOTDIR";
|
|
37
|
+
}
|
|
38
|
+
if (present) {
|
|
39
|
+
result.push(candidate);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return [...new Set(result)];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function error(message) {
|
|
48
|
+
return new Error(`Cannot read TRAE usage: ${message}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function projectUsage(queryRows) {
|
|
52
|
+
const tables = await queryRows("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'chat_turn'");
|
|
53
|
+
// history_v2.token_usage can be a context-size snapshot rather than billed
|
|
54
|
+
// usage. Do not synthesize input tokens from those scalar values.
|
|
55
|
+
if (!tables.length) throw error("unsupported database schema: chat_turn table is missing.");
|
|
56
|
+
const columns = new Set((await queryRows("PRAGMA table_info(chat_turn)")).map((row) => row.name));
|
|
57
|
+
if (!columns.has("context") || (!columns.has("created_at") && !columns.has("updated_at"))) {
|
|
58
|
+
throw error("unsupported chat_turn schema.");
|
|
59
|
+
}
|
|
60
|
+
const column = (name, fallback = "NULL") => columns.has(name) ? `"${name}"` : fallback;
|
|
61
|
+
const date = columns.has("created_at") && columns.has("updated_at")
|
|
62
|
+
? 'COALESCE("created_at", "updated_at")' : column("created_at", column("updated_at"));
|
|
63
|
+
const modelPaths = [
|
|
64
|
+
"persist_user_message_context.model_info.display_model_name",
|
|
65
|
+
"persist_user_message_context.model_info.model_name",
|
|
66
|
+
"persist_user_message_context.model_info.config_name", "model_name", "model",
|
|
67
|
+
"agent_model", "selected_model.name", "selectedModel.name",
|
|
68
|
+
];
|
|
69
|
+
const model = modelPaths.map((field) => `CASE WHEN json_type(context, '$.${field}') = 'text'
|
|
70
|
+
THEN NULLIF(NULLIF(TRIM(json_extract(context, '$.${field}')), ''), '-') END`);
|
|
71
|
+
const fields = TOKEN_FIELDS.map((field) => `CASE
|
|
72
|
+
WHEN json_type(context, '$.token_usage.${field}') IN ('integer', 'real')
|
|
73
|
+
THEN json_extract(context, '$.token_usage.${field}') END AS "${field}"`);
|
|
74
|
+
const invalidFields = TOKEN_FIELDS.map((field) =>
|
|
75
|
+
`COALESCE(json_type(context, '$.token_usage.${field}') NOT IN ('integer', 'real', 'null'), 0)`);
|
|
76
|
+
// Project inside SQLite: prompts, messages, session titles, fee/account data,
|
|
77
|
+
// and the rest of the context never cross into JS results or cursor state.
|
|
78
|
+
const query = `SELECT CAST(${column("id", "rowid")} AS TEXT) AS id,
|
|
79
|
+
${column("session_id")} AS session_id, ${column("turn_id")} AS turn_id,
|
|
80
|
+
${date} AS created_at, ${column("updated_at")} AS updated_at,
|
|
81
|
+
COALESCE(${model.join(", ")}, 'trae-unknown') AS model,
|
|
82
|
+
${fields.join(", ")}, (${invalidFields.join(" OR ")}) AS invalid_usage
|
|
83
|
+
FROM chat_turn WHERE CASE WHEN json_valid(context)
|
|
84
|
+
THEN json_type(context, '$.token_usage') = 'object' ELSE 0 END`;
|
|
85
|
+
return (await queryRows(query)).map((row) => {
|
|
86
|
+
const usage = {};
|
|
87
|
+
let invalid = Boolean(row.invalid_usage);
|
|
88
|
+
for (const field of TOKEN_FIELDS) {
|
|
89
|
+
const value = row[field];
|
|
90
|
+
if (value == null) continue;
|
|
91
|
+
if (!Number.isSafeInteger(value) || value < 0) invalid = true;
|
|
92
|
+
else usage[field] = value;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
id: row.id, session_id: row.session_id, turn_id: row.turn_id,
|
|
96
|
+
created_at: row.created_at, updated_at: row.updated_at,
|
|
97
|
+
model: row.model, usage: invalid ? null : usage,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function readTraeUsageRows(dbPath, { env = process.env } = {}) {
|
|
103
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
104
|
+
let snapshot;
|
|
105
|
+
try { snapshot = openTraeSnapshot(dbPath, env); }
|
|
106
|
+
catch (err) {
|
|
107
|
+
// A writer can also change the files while the snapshot opens. Retry
|
|
108
|
+
// that too, and report the specific failure once attempts run out.
|
|
109
|
+
if (err?.transient && attempt < 2) continue;
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
let result;
|
|
114
|
+
try { result = await withTraeSqlite(snapshot, projectUsage); }
|
|
115
|
+
catch (err) {
|
|
116
|
+
// A writer may have changed pages while SQLite was reading. Discard
|
|
117
|
+
// every projected row and retry; never publish a mixed snapshot.
|
|
118
|
+
if (snapshot.isStable()) throw err;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (snapshot.isStable()) return result;
|
|
122
|
+
} finally { snapshot.close(); }
|
|
123
|
+
}
|
|
124
|
+
throw error("database changed during the read; retry after TRAE finishes its current turn.");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { resolveTraeDbPaths, readTraeUsageRows };
|