tokenusage-cli 0.3.0 → 0.4.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 +60 -0
- package/README.md +52 -11
- package/bin/tokenusage.js +14 -2
- package/package.json +1 -2
- package/src/cli.js +325 -78
- package/src/commands/device-login.js +3 -3
- package/src/commands/init.js +343 -153
- package/src/commands/pause.js +11 -23
- package/src/commands/reconcile.js +496 -0
- package/src/commands/sessions.js +57 -14
- package/src/commands/sync.js +69 -15
- package/src/commands/uninstall.js +6 -9
- package/src/commands/upload.js +147 -0
- package/src/lib/cli-errors.js +33 -0
- package/src/lib/cli-ui.js +93 -0
- package/src/lib/queue-row-normalize.js +55 -0
- package/src/lib/scheduled-task.js +597 -75
- package/src/lib/antigravity-paths.js +0 -48
- package/src/lib/async-queue-reader.js +0 -138
- package/src/lib/atomic-json.js +0 -27
- package/src/lib/bearer-token.js +0 -11
- package/src/lib/cloud-account.js +0 -541
- package/src/lib/codex-context-breakdown.js +0 -806
- package/src/lib/context-health.js +0 -124
- package/src/lib/grok-context-breakdown.js +0 -890
- package/src/lib/heatmap-compact.js +0 -114
- package/src/lib/local-api.js +0 -3574
- package/src/lib/outcomes-engine.js +0 -253
- package/src/lib/pet-packages.js +0 -722
- package/src/lib/skill-usage.js +0 -267
- package/src/lib/skills-manager.js +0 -1591
- package/src/lib/source-metadata.js +0 -48
- package/src/lib/subscription-manager.js +0 -293
- package/src/lib/wrapped-aggregator.js +0 -236
- package/test/audit-fixes.test.js +0 -249
- package/test/branding.test.js +0 -24
- package/test/cli-options.test.js +0 -43
- package/test/cli-smoke.test.js +0 -53
- package/test/cli-ui.test.js +0 -47
- package/test/collection-controls.test.js +0 -255
- package/test/ingest-sanitize.test.js +0 -162
- package/test/packaging.test.js +0 -107
- package/test/quiet-warnings.test.js +0 -53
- package/test/runtime-copy.test.js +0 -111
- package/test/session-upload.test.js +0 -427
- package/test/trae-db.test.js +0 -67
- package/test/zstd-native.test.js +0 -36
package/src/cli.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Command table + dispatch. Help text lives next to each command definition
|
|
4
|
+
// (`usage` / `options` / `notes`) so a new flag cannot be documented in one
|
|
5
|
+
// place and forgotten in another: `tokenusage --help` renders the one-line
|
|
6
|
+
// summary from here and `tokenusage <command> --help` renders the full block.
|
|
7
|
+
//
|
|
8
|
+
// Errors: user mistakes throw UsageError (printed without a stack, exit 2);
|
|
9
|
+
// everything else keeps the plain message and only prints a stack under --debug
|
|
10
|
+
// (bin/tokenusage.js owns that policy).
|
|
11
|
+
|
|
12
|
+
const { UsageError } = require("./lib/cli-errors");
|
|
13
|
+
|
|
1
14
|
const { cmdInit } = require("./commands/init");
|
|
2
|
-
const { cmdSync } = require("./commands/sync");
|
|
15
|
+
const { cmdSync, formatSyncSourceList } = require("./commands/sync");
|
|
16
|
+
const { cmdUpload } = require("./commands/upload");
|
|
17
|
+
const { cmdReconcile } = require("./commands/reconcile");
|
|
3
18
|
const { cmdStatus } = require("./commands/status");
|
|
4
19
|
const { cmdDiagnostics } = require("./commands/diagnostics");
|
|
5
20
|
const { cmdDoctor } = require("./commands/doctor");
|
|
@@ -8,65 +23,208 @@ const { cmdDeviceLogin } = require("./commands/device-login");
|
|
|
8
23
|
const { cmdSessions } = require("./commands/sessions");
|
|
9
24
|
const { cmdPause, cmdResume } = require("./commands/pause");
|
|
10
25
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
26
|
+
const DEFAULT_WATCH_INTERVAL_MS = 15 * 60_000;
|
|
27
|
+
const WATCH_MIN_INTERVAL_MS = 60_000;
|
|
28
|
+
// Failure backoff ladder (watch mode). A pass that fails (offline, backend 5xx,
|
|
29
|
+
// sync.lock contention) does not reset the schedule to the fastest interval:
|
|
30
|
+
// it steps through the same 1→2→5 ladder the juejin CLI uses, multiplied by
|
|
31
|
+
// the configured base interval. One successful pass drops straight back to the
|
|
32
|
+
// base interval.
|
|
33
|
+
const WATCH_BACKOFF_MULTIPLIERS = [1, 2, 5];
|
|
20
34
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const COMMANDS = [
|
|
36
|
+
{
|
|
37
|
+
name: "init",
|
|
38
|
+
summary: "Configure hooks, the runtime copy and the auto-sync schedule",
|
|
39
|
+
run: (argv) => cmdInit(argv),
|
|
40
|
+
usage: [
|
|
41
|
+
"tokenusage init [options]",
|
|
42
|
+
"",
|
|
43
|
+
"Options:",
|
|
44
|
+
" --base-url <url> Dashboard/backend base URL (defaults to the hosted instance)",
|
|
45
|
+
" --dashboard-url <url> Dashboard URL shown in the success message",
|
|
46
|
+
" --anon-key <key> Publishable key of your self-hosted instance",
|
|
47
|
+
" --anon-key-file <path> Read the publishable key from a file",
|
|
48
|
+
" --integrations <ids|all|none>",
|
|
49
|
+
" Only configure the listed tools (comma separated ids,",
|
|
50
|
+
" e.g. claude,codex,gemini). `none` == --no-integrations",
|
|
51
|
+
" --no-integrations Leave every tool config untouched (scheduled scanning only)",
|
|
52
|
+
" --no-task Do not register the auto-sync schedule",
|
|
53
|
+
" --run-report=on|off Persist the per-run telemetry preference (§2.11)",
|
|
54
|
+
" --yes Skip the interactive confirmation",
|
|
55
|
+
" --dry-run Preview without writing anything",
|
|
56
|
+
],
|
|
57
|
+
notes: [
|
|
58
|
+
"The hourly schedule uses Task Scheduler on Windows, launchd on macOS and a",
|
|
59
|
+
"systemd --user timer on Linux; `pause`/`resume` toggle whichever was registered.",
|
|
60
|
+
"Removed flags: --email, --password, --device-name, --link-code, --no-auth —",
|
|
61
|
+
"browser sign-in is gone; pair the machine with `tokenusage device-login`.",
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "sync",
|
|
66
|
+
summary: "Scan local AI-tool sessions and upload hourly token deltas",
|
|
67
|
+
run: async (argv) => {
|
|
68
|
+
const uploadOnly = argv.includes("--upload-only");
|
|
69
|
+
const rest = uploadOnly ? argv.filter((a) => a !== "--upload-only") : argv;
|
|
70
|
+
if (uploadOnly) {
|
|
71
|
+
await cmdUpload(rest);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
37
74
|
if (rest.includes("--watch")) {
|
|
38
75
|
await watchSync(rest);
|
|
39
76
|
return;
|
|
40
77
|
}
|
|
41
78
|
await cmdSync(rest);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
79
|
+
},
|
|
80
|
+
usage: [
|
|
81
|
+
"tokenusage sync [options]",
|
|
82
|
+
"",
|
|
83
|
+
"Options:",
|
|
84
|
+
" --watch Keep running and sync on an interval",
|
|
85
|
+
" --interval <ms> Watch interval in ms (floor 60000, default 900000)",
|
|
86
|
+
" --upload-only Skip scanning; only flush the pending queue",
|
|
87
|
+
" --source <id> Scan a single provider (ids are listed below)",
|
|
88
|
+
" --drain Upload the whole backlog (higher batch ceiling)",
|
|
89
|
+
" --no-sessions Skip the session metadata upload (hourly buckets only)",
|
|
90
|
+
" --publish-account Publish account-level sources (cursor / trae-cn) locally",
|
|
91
|
+
" --all-local-sources Scan every local source, including background-only ones",
|
|
92
|
+
" --auto Auto flavor (honors pause + cloud-sync preference)",
|
|
93
|
+
" --from-notify|--from-openclaw|--from-retry|--from-scheduled",
|
|
94
|
+
" Trigger tags used by the generated hooks/schedule",
|
|
95
|
+
" --wait-for-lock Wait for a concurrent sync to finish instead of skipping",
|
|
96
|
+
" --background|--lightweight Reduced-scan background flavor",
|
|
97
|
+
" --repair-grok Rebuild Grok rows from session snapshots",
|
|
98
|
+
],
|
|
99
|
+
notes: [
|
|
100
|
+
"Unknown --source values fail loudly (they used to fall back to a full scan,",
|
|
101
|
+
"which silently collected every provider instead of the one requested).",
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "upload",
|
|
106
|
+
summary: "Flush the pending queue without scanning provider sessions",
|
|
107
|
+
run: (argv) => cmdUpload(argv),
|
|
108
|
+
usage: [
|
|
109
|
+
"tokenusage upload [options]",
|
|
110
|
+
"",
|
|
111
|
+
"Options:",
|
|
112
|
+
" --drain, --force Upload the whole backlog (100 batches, ignores cooldown)",
|
|
113
|
+
" --json Machine-readable summary",
|
|
114
|
+
],
|
|
115
|
+
notes: [
|
|
116
|
+
"Uses the same sync.lock, queue offset and ingest path as `sync`, so the",
|
|
117
|
+
"last-wins reconciliation baseline is unaffected.",
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "reconcile",
|
|
122
|
+
summary: "Compare the local queue baseline against this device's cloud rows",
|
|
123
|
+
run: (argv) => cmdReconcile(argv),
|
|
124
|
+
usage: [
|
|
125
|
+
"tokenusage reconcile [options]",
|
|
126
|
+
"",
|
|
127
|
+
"Options:",
|
|
128
|
+
" --days <n> Window length in days (default 7, max 366)",
|
|
129
|
+
" --from <YYYY-MM-DD|ISO> Window start (inclusive, UTC)",
|
|
130
|
+
" --to <YYYY-MM-DD|ISO> Window end; a bare date includes the whole day",
|
|
131
|
+
" --limit <n> Rows per page (1–2000, default 1000)",
|
|
132
|
+
" --json Machine-readable summary",
|
|
133
|
+
],
|
|
134
|
+
notes: [
|
|
135
|
+
"Read-only: the command never uploads, rewrites the queue or moves the",
|
|
136
|
+
"upload offset. Drift is reported per (hour_start, source, model) key as",
|
|
137
|
+
"missing_on_server / server_only / mismatch, and exits 1 when any exists.",
|
|
138
|
+
"The baseline is the same last-wins reading of queue.jsonl that `sync`/`upload`",
|
|
139
|
+
"send, so a fully uploaded queue should report OK.",
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: "pause",
|
|
144
|
+
summary: "Stop automatic collection (manual sync still works)",
|
|
145
|
+
run: () => cmdPause(),
|
|
146
|
+
usage: ["tokenusage pause", "", "Writes the pause flag and stops the registered auto-sync schedule."],
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: "resume",
|
|
150
|
+
summary: "Restart automatic collection",
|
|
151
|
+
run: () => cmdResume(),
|
|
152
|
+
usage: ["tokenusage resume", "", "Clears the pause flag and re-enables the registered auto-sync schedule."],
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "status",
|
|
156
|
+
summary: "Show pairing, queue, hook and provider state",
|
|
157
|
+
run: (argv) => cmdStatus(argv),
|
|
158
|
+
usage: [
|
|
159
|
+
"tokenusage status [options]",
|
|
160
|
+
"",
|
|
161
|
+
"Options:",
|
|
162
|
+
" --diagnostics Include diagnostics (paths, cursors, WSL/Kiro probes)",
|
|
163
|
+
" --json Machine-readable output",
|
|
164
|
+
" --light Deterministic ASCII table (no ANSI, no emoji)",
|
|
165
|
+
" --no-spinner Disable the progress spinner",
|
|
166
|
+
" --probe-keychain Probe the macOS keychain (may prompt)",
|
|
167
|
+
" --probe-keychain-details Include per-item keychain details",
|
|
168
|
+
],
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: "diagnostics",
|
|
172
|
+
summary: "Dump a redacted environment report",
|
|
173
|
+
run: (argv) => cmdDiagnostics(argv),
|
|
174
|
+
usage: ["tokenusage diagnostics [--out <file>]", "", "Home paths are redacted to `~` before being printed."],
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "doctor",
|
|
178
|
+
summary: "Check the local setup and exit non-zero on critical failures",
|
|
179
|
+
run: (argv) => cmdDoctor(argv),
|
|
180
|
+
usage: [
|
|
181
|
+
"tokenusage doctor [options]",
|
|
182
|
+
"",
|
|
183
|
+
"Options:",
|
|
184
|
+
" --json Machine-readable report",
|
|
185
|
+
" --out <file> Also write the report to a file",
|
|
186
|
+
" --base-url <url> Backend to probe (defaults to the configured one)",
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: "uninstall",
|
|
191
|
+
summary: "Remove hooks, the auto-sync schedule and the runtime copy",
|
|
192
|
+
run: (argv) => cmdUninstall(argv),
|
|
193
|
+
usage: ["tokenusage uninstall [--purge]", "", " --purge Also delete ~/.tokenusage (keeps machine identity)"],
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: "device-login",
|
|
197
|
+
summary: "Pair this machine with the dashboard (device flow)",
|
|
198
|
+
run: (argv) => cmdDeviceLogin(argv),
|
|
199
|
+
usage: [
|
|
200
|
+
"tokenusage device-login [options]",
|
|
201
|
+
"",
|
|
202
|
+
"Options:",
|
|
203
|
+
" --json Machine-readable output",
|
|
204
|
+
" --base-url <url> Backend to pair against",
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "sessions",
|
|
209
|
+
summary: "Export metadata-only session efficiency analytics",
|
|
210
|
+
run: (argv) => cmdSessions(argv),
|
|
211
|
+
usage: [
|
|
212
|
+
"tokenusage sessions [options]",
|
|
213
|
+
"",
|
|
214
|
+
"Options:",
|
|
215
|
+
" --from <YYYY-MM-DD> Window start (inclusive)",
|
|
216
|
+
" --to <YYYY-MM-DD> Window end (inclusive)",
|
|
217
|
+
" --format <json|csv> Output format (default json)",
|
|
218
|
+
" --csv Shorthand for --format csv",
|
|
219
|
+
" --out <file> Write to a file (atomic, mode 0600) instead of stdout",
|
|
220
|
+
" --refresh Rebuild the analytics cache",
|
|
221
|
+
" --no-git Skip git outcome attribution",
|
|
222
|
+
],
|
|
223
|
+
},
|
|
224
|
+
];
|
|
225
|
+
|
|
226
|
+
function findCommand(name) {
|
|
227
|
+
return COMMANDS.find((command) => command.name === name) || null;
|
|
70
228
|
}
|
|
71
229
|
|
|
72
230
|
// Split `--interval <ms>` (or `--interval=<ms>`) off the sync args. Values
|
|
@@ -75,7 +233,7 @@ async function run(argv) {
|
|
|
75
233
|
// pass with "Unknown option: --interval" instead of the documented fallback.
|
|
76
234
|
function resolveWatchInterval(rest) {
|
|
77
235
|
const args = rest.filter((arg) => arg !== "--watch");
|
|
78
|
-
let intervalMs =
|
|
236
|
+
let intervalMs = DEFAULT_WATCH_INTERVAL_MS;
|
|
79
237
|
let value;
|
|
80
238
|
const spacedIndex = args.indexOf("--interval");
|
|
81
239
|
const equalsIndex = args.findIndex((arg) => arg.startsWith("--interval="));
|
|
@@ -87,10 +245,17 @@ function resolveWatchInterval(rest) {
|
|
|
87
245
|
args.splice(equalsIndex, 1);
|
|
88
246
|
}
|
|
89
247
|
const parsed = Number(value);
|
|
90
|
-
if (Number.isFinite(parsed) && parsed >=
|
|
248
|
+
if (Number.isFinite(parsed) && parsed >= WATCH_MIN_INTERVAL_MS) intervalMs = parsed;
|
|
91
249
|
return { args, intervalMs };
|
|
92
250
|
}
|
|
93
251
|
|
|
252
|
+
// Pure so the ladder is testable without sleeping: 0 failures → base interval,
|
|
253
|
+
// then base×2, base×5 (and stays at base×5 for further failures).
|
|
254
|
+
function resolveWatchDelay({ intervalMs, failureCount = 0 }) {
|
|
255
|
+
const step = Math.min(Math.max(0, Number(failureCount) || 0), WATCH_BACKOFF_MULTIPLIERS.length - 1);
|
|
256
|
+
return Math.max(WATCH_MIN_INTERVAL_MS, intervalMs * WATCH_BACKOFF_MULTIPLIERS[step]);
|
|
257
|
+
}
|
|
258
|
+
|
|
94
259
|
// --watch: keep the process alive and re-run sync on an interval. Each pass
|
|
95
260
|
// reuses the session-analytics sidecar cache and the upload watermarks, so a
|
|
96
261
|
// pass with nothing new is cheap (a directory signature stat + a no-op).
|
|
@@ -98,53 +263,135 @@ function resolveWatchInterval(rest) {
|
|
|
98
263
|
// `watch` runs distinctly from scheduled/manual/notify ones (§2.11).
|
|
99
264
|
async function watchSync(rest) {
|
|
100
265
|
const { args, intervalMs } = resolveWatchInterval(rest);
|
|
266
|
+
const minutes = (ms) => Math.round(ms / 60_000);
|
|
101
267
|
|
|
102
|
-
process.stdout.write(`tokenusage sync --watch: every ${
|
|
268
|
+
process.stdout.write(`tokenusage sync --watch: every ${minutes(intervalMs)} min (Ctrl+C to stop)\n`);
|
|
269
|
+
let failureCount = 0;
|
|
103
270
|
for (;;) {
|
|
104
271
|
try {
|
|
105
272
|
await cmdSync(args, { trigger: "watch" });
|
|
273
|
+
failureCount = 0;
|
|
106
274
|
} catch (error) {
|
|
107
275
|
// A failed pass (offline, backend 5xx) must not kill the watcher; the
|
|
108
276
|
// queue and watermarks retain everything for the next pass.
|
|
277
|
+
failureCount += 1;
|
|
109
278
|
process.stderr.write(`sync pass failed: ${error?.message || error}\n`);
|
|
110
279
|
}
|
|
111
|
-
|
|
280
|
+
const delayMs = resolveWatchDelay({ intervalMs, failureCount });
|
|
281
|
+
if (failureCount > 0) {
|
|
282
|
+
process.stderr.write(
|
|
283
|
+
`next pass in ${minutes(delayMs)} min (backoff step ${Math.min(failureCount, WATCH_BACKOFF_MULTIPLIERS.length)}/${WATCH_BACKOFF_MULTIPLIERS.length})\n`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
112
287
|
}
|
|
113
288
|
}
|
|
114
289
|
|
|
290
|
+
function formatCommandList() {
|
|
291
|
+
const width = COMMANDS.reduce((max, command) => Math.max(max, command.name.length), 0);
|
|
292
|
+
return COMMANDS.map((command) => ` ${command.name.padEnd(width)} ${command.summary}`);
|
|
293
|
+
}
|
|
294
|
+
|
|
115
295
|
function printHelp() {
|
|
116
|
-
|
|
296
|
+
const pkg = require("../package.json");
|
|
117
297
|
process.stdout.write(
|
|
118
298
|
[
|
|
119
|
-
|
|
299
|
+
`tokenusage v${pkg.version} — self-hosted AI token usage collector`,
|
|
120
300
|
"",
|
|
121
301
|
"Usage:",
|
|
122
|
-
" tokenusage
|
|
123
|
-
"
|
|
124
|
-
"
|
|
125
|
-
|
|
126
|
-
"
|
|
127
|
-
"
|
|
128
|
-
" tokenusage [--debug] diagnostics [--out diagnostics.json]",
|
|
129
|
-
" tokenusage [--debug] doctor [--json] [--out doctor.json] [--base-url <url>]",
|
|
130
|
-
" tokenusage [--debug] uninstall [--purge]",
|
|
131
|
-
" tokenusage [--debug] device-login [--json] [--base-url <url>]",
|
|
132
|
-
" tokenusage sessions [--from YYYY-MM-DD] [--to YYYY-MM-DD] [--format json|csv] [--out file] [--refresh] [--no-git]",
|
|
302
|
+
" tokenusage <command> [options]",
|
|
303
|
+
"",
|
|
304
|
+
"Commands:",
|
|
305
|
+
...formatCommandList(),
|
|
306
|
+
"",
|
|
307
|
+
"Run `tokenusage <command> --help` for that command's options.",
|
|
133
308
|
"",
|
|
134
309
|
"Notes:",
|
|
135
310
|
" - Pairing: device-login prints a code; approve it at the hosted /device page.",
|
|
136
311
|
" - sync parses local AI-tool session logs and uploads hourly token deltas",
|
|
137
312
|
" plus session metadata (no prompts, message bodies, paths or titles).",
|
|
138
|
-
" - --watch keeps running and syncs on an interval (increments only)
|
|
313
|
+
" - --watch keeps running and syncs on an interval (increments only);",
|
|
314
|
+
" failures back off 1x → 2x → 5x of the base interval and reset on success.",
|
|
139
315
|
" - --no-sessions skips the session metadata upload (hourly buckets only).",
|
|
140
|
-
" -
|
|
141
|
-
"
|
|
142
|
-
"
|
|
143
|
-
" - --debug shows original backend errors.",
|
|
144
|
-
" - sessions exports metadata-only efficiency analytics; no prompt or response text is retained.",
|
|
316
|
+
" - pause/resume stop and restart automatic collection (flag + schedule);",
|
|
317
|
+
" manual sync and upload always run.",
|
|
318
|
+
" - --debug shows original backend errors and full stack traces.",
|
|
145
319
|
"",
|
|
146
320
|
].join("\n"),
|
|
147
321
|
);
|
|
148
322
|
}
|
|
149
323
|
|
|
150
|
-
|
|
324
|
+
function printCommandHelp(name) {
|
|
325
|
+
const command = findCommand(name);
|
|
326
|
+
if (!command) {
|
|
327
|
+
throw new UsageError(`Unknown command: ${name}`, {
|
|
328
|
+
code: "UNKNOWN_COMMAND",
|
|
329
|
+
hint: "Run `tokenusage --help` to see supported commands.",
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const lines = [
|
|
333
|
+
...command.usage,
|
|
334
|
+
...(command.notes && command.notes.length ? ["", ...command.notes] : []),
|
|
335
|
+
...(command.name === "sync"
|
|
336
|
+
? ["", "Available --source ids:", ` ${formatSyncSourceList()}`]
|
|
337
|
+
: []),
|
|
338
|
+
"",
|
|
339
|
+
`Run \`tokenusage --help\` for the full command list.`,
|
|
340
|
+
"",
|
|
341
|
+
];
|
|
342
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function run(argv) {
|
|
346
|
+
const [command, ...rest] = argv;
|
|
347
|
+
|
|
348
|
+
// No args → help (the upstream local dashboard was removed in this fork;
|
|
349
|
+
// the hosted token-usage site is the dashboard).
|
|
350
|
+
if (!command) {
|
|
351
|
+
printHelp();
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (command === "-h" || command === "--help") {
|
|
356
|
+
printHelp();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (command === "-v" || command === "--version") {
|
|
361
|
+
const pkg = require("../package.json");
|
|
362
|
+
console.log(`v${pkg.version}`);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (command === "help") {
|
|
367
|
+
if (rest[0]) printCommandHelp(rest[0]);
|
|
368
|
+
else printHelp();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const spec = findCommand(command);
|
|
373
|
+
if (!spec) {
|
|
374
|
+
throw new UsageError(`Unknown command: ${command}`, {
|
|
375
|
+
code: "UNKNOWN_COMMAND",
|
|
376
|
+
hint: "Run `tokenusage --help` to see supported commands.",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// `tokenusage <command> --help` works for every command without every
|
|
381
|
+
// parser having to special-case it (a new flag can never make --help throw).
|
|
382
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
383
|
+
printCommandHelp(spec.name);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
await spec.run(rest);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
module.exports = {
|
|
391
|
+
run,
|
|
392
|
+
printHelp,
|
|
393
|
+
printCommandHelp,
|
|
394
|
+
resolveWatchInterval,
|
|
395
|
+
resolveWatchDelay,
|
|
396
|
+
COMMANDS,
|
|
397
|
+
};
|
|
@@ -76,9 +76,9 @@ async function cmdDeviceLogin(argv = [], options = {}) {
|
|
|
76
76
|
const baseUrl = runtime.baseUrl;
|
|
77
77
|
|
|
78
78
|
const clientInfo = `${os.platform()}-${os.arch()} ${os.hostname()}`;
|
|
79
|
-
// Same machineId
|
|
80
|
-
//
|
|
81
|
-
const { getOrCreateMachineId } = require("../lib/
|
|
79
|
+
// Same machineId sync uploads with — both paths must resolve to the SAME
|
|
80
|
+
// cloud device row for this machine.
|
|
81
|
+
const { getOrCreateMachineId } = require("../lib/machine-id");
|
|
82
82
|
const machineId = getOrCreateMachineId(path.join(trackerDir, "queue.jsonl"));
|
|
83
83
|
process.stdout.write(`Requesting device code from ${baseUrl}...\n`);
|
|
84
84
|
const authResp = await authorize({ baseUrl, clientInfo, machineId, anonKey: runtime.anonKey, timeoutMs: runtime.httpTimeoutMs });
|