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,121 +1,121 @@
1
- const os = require("node:os");
2
- const path = require("node:path");
3
- const fs = require("node:fs/promises");
4
-
5
- const { ensureDir } = require("./fs");
6
-
7
- const DEFAULT_PLUGIN_NAME = "tokenusage.js";
8
- const PLUGIN_MARKER = "TOKENUSAGE_PLUGIN";
9
- const DEFAULT_EVENT = "session.updated";
10
-
11
- function resolveOpencodeConfigDir({ home = os.homedir(), env = process.env } = {}) {
12
- const explicit =
13
- typeof env.OPENCODE_CONFIG_DIR === "string" ? env.OPENCODE_CONFIG_DIR.trim() : "";
14
- if (explicit) return path.resolve(explicit);
15
- const xdg = typeof env.XDG_CONFIG_HOME === "string" ? env.XDG_CONFIG_HOME.trim() : "";
16
- const base = xdg || path.join(home, ".config");
17
- return path.join(base, "opencode");
18
- }
19
-
20
- function resolveOpencodePluginDir({ configDir }) {
21
- return path.join(configDir, "plugin");
22
- }
23
-
24
- function buildOpencodePlugin({ notifyPath }) {
25
- const safeNotifyPath = typeof notifyPath === "string" ? notifyPath : "";
26
- return (
27
- `// ${PLUGIN_MARKER}\n` +
28
- `const notifyPath = ${JSON.stringify(safeNotifyPath)};\n` +
29
- `const server = async ({ $ }) => {\n` +
30
- ` return {\n` +
31
- ` event: async ({ event }) => {\n` +
32
- ` if (!event || event.type !== ${JSON.stringify(DEFAULT_EVENT)}) return;\n` +
33
- ` try {\n` +
34
- ` if (!notifyPath) return;\n` +
35
- // `node` (not `/usr/bin/env node`): /usr/bin/env does not exist on Windows, so the Bun
36
- // shell exec fails and its error leaks into the OpenCode TUI input box (issue #189).
37
- // `.quiet()` keeps any subprocess output out of the TUI even on success/warnings.
38
- ` const proc = $\`node ${"${notifyPath}"} --source=opencode\`.quiet();\n` +
39
- ` if (proc && typeof proc.catch === 'function') proc.catch(() => {});\n` +
40
- ` } catch (_) {}\n` +
41
- ` }\n` +
42
- ` };\n` +
43
- `};\n` +
44
- // Three loaders read this one file (issue #646):
45
- // * older opencode v1 takes the named export;
46
- // * current v1 (verified on 1.18.30) requires a default export carrying
47
- // `id` and `server()`, and rejects the file outright without them;
48
- // * v2 requires a default export carrying `id` and `setup()`/`effect()`,
49
- // and ignores `server()`.
50
- // Emitting all three keeps a single generated file valid everywhere. v2's
51
- // plugin context exposes no event stream and no shell (`@opencode-ai/plugin`
52
- // v2 PluginContext is agent/aisdk/catalog/command/integration/reference/skill
53
- // only), so there is nothing for `setup` to subscribe to -- it exists to make
54
- // the plugin load instead of erroring on every startup. Usage itself does not
55
- // depend on this hook: opencode rows are parsed from the on-disk storage and
56
- // opencode.db, and the hook only makes a sync happen sooner.
57
- `export const TokenUsagePlugin = server;\n` +
58
- `export default {\n` +
59
- ` id: "tokenusage",\n` +
60
- ` server,\n` +
61
- ` setup: async () => {},\n` +
62
- `};\n`
63
- );
64
- }
65
-
66
- async function upsertOpencodePlugin({ configDir, notifyPath, pluginName = DEFAULT_PLUGIN_NAME }) {
67
- if (!configDir) return { changed: false, pluginPath: null, skippedReason: "config-missing" };
68
- const pluginDir = resolveOpencodePluginDir({ configDir });
69
- const pluginPath = path.join(pluginDir, pluginName);
70
- const next = buildOpencodePlugin({ notifyPath });
71
- const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
72
-
73
- if (existing === next) {
74
- return { changed: false, pluginPath, skippedReason: null };
75
- }
76
-
77
- await ensureDir(pluginDir);
78
-
79
- let backupPath = null;
80
- if (existing != null) {
81
- backupPath = `${pluginPath}.bak.${new Date().toISOString().replace(/[:.]/g, "-")}`;
82
- await fs.copyFile(pluginPath, backupPath).catch(() => {});
83
- }
84
-
85
- await fs.writeFile(pluginPath, next, "utf8");
86
- return { changed: true, pluginPath, backupPath, skippedReason: null };
87
- }
88
-
89
- async function removeOpencodePlugin({ configDir, pluginName = DEFAULT_PLUGIN_NAME }) {
90
- if (!configDir) return { removed: false, skippedReason: "config-missing" };
91
- const pluginPath = path.join(resolveOpencodePluginDir({ configDir }), pluginName);
92
- const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
93
- if (existing == null) return { removed: false, skippedReason: "plugin-missing" };
94
- if (!hasPluginMarker(existing)) return { removed: false, skippedReason: "unexpected-content" };
95
- await fs.unlink(pluginPath).catch(() => {});
96
- return { removed: true, skippedReason: null };
97
- }
98
-
99
- async function isOpencodePluginInstalled({ configDir, pluginName = DEFAULT_PLUGIN_NAME }) {
100
- if (!configDir) return false;
101
- const pluginPath = path.join(resolveOpencodePluginDir({ configDir }), pluginName);
102
- const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
103
- if (!existing) return false;
104
- return hasPluginMarker(existing);
105
- }
106
-
107
- function hasPluginMarker(text) {
108
- return typeof text === "string" && text.includes(PLUGIN_MARKER);
109
- }
110
-
111
- module.exports = {
112
- DEFAULT_EVENT,
113
- DEFAULT_PLUGIN_NAME,
114
- PLUGIN_MARKER,
115
- resolveOpencodeConfigDir,
116
- resolveOpencodePluginDir,
117
- buildOpencodePlugin,
118
- upsertOpencodePlugin,
119
- removeOpencodePlugin,
120
- isOpencodePluginInstalled,
121
- };
1
+ const os = require("node:os");
2
+ const path = require("node:path");
3
+ const fs = require("node:fs/promises");
4
+
5
+ const { ensureDir } = require("./fs");
6
+
7
+ const DEFAULT_PLUGIN_NAME = "tokenusage.js";
8
+ const PLUGIN_MARKER = "TOKENUSAGE_PLUGIN";
9
+ const DEFAULT_EVENT = "session.updated";
10
+
11
+ function resolveOpencodeConfigDir({ home = os.homedir(), env = process.env } = {}) {
12
+ const explicit =
13
+ typeof env.OPENCODE_CONFIG_DIR === "string" ? env.OPENCODE_CONFIG_DIR.trim() : "";
14
+ if (explicit) return path.resolve(explicit);
15
+ const xdg = typeof env.XDG_CONFIG_HOME === "string" ? env.XDG_CONFIG_HOME.trim() : "";
16
+ const base = xdg || path.join(home, ".config");
17
+ return path.join(base, "opencode");
18
+ }
19
+
20
+ function resolveOpencodePluginDir({ configDir }) {
21
+ return path.join(configDir, "plugin");
22
+ }
23
+
24
+ function buildOpencodePlugin({ notifyPath }) {
25
+ const safeNotifyPath = typeof notifyPath === "string" ? notifyPath : "";
26
+ return (
27
+ `// ${PLUGIN_MARKER}\n` +
28
+ `const notifyPath = ${JSON.stringify(safeNotifyPath)};\n` +
29
+ `const server = async ({ $ }) => {\n` +
30
+ ` return {\n` +
31
+ ` event: async ({ event }) => {\n` +
32
+ ` if (!event || event.type !== ${JSON.stringify(DEFAULT_EVENT)}) return;\n` +
33
+ ` try {\n` +
34
+ ` if (!notifyPath) return;\n` +
35
+ // `node` (not `/usr/bin/env node`): /usr/bin/env does not exist on Windows, so the Bun
36
+ // shell exec fails and its error leaks into the OpenCode TUI input box (issue #189).
37
+ // `.quiet()` keeps any subprocess output out of the TUI even on success/warnings.
38
+ ` const proc = $\`node ${"${notifyPath}"} --source=opencode\`.quiet();\n` +
39
+ ` if (proc && typeof proc.catch === 'function') proc.catch(() => {});\n` +
40
+ ` } catch (_) {}\n` +
41
+ ` }\n` +
42
+ ` };\n` +
43
+ `};\n` +
44
+ // Three loaders read this one file (issue #646):
45
+ // * older opencode v1 takes the named export;
46
+ // * current v1 (verified on 1.18.30) requires a default export carrying
47
+ // `id` and `server()`, and rejects the file outright without them;
48
+ // * v2 requires a default export carrying `id` and `setup()`/`effect()`,
49
+ // and ignores `server()`.
50
+ // Emitting all three keeps a single generated file valid everywhere. v2's
51
+ // plugin context exposes no event stream and no shell (`@opencode-ai/plugin`
52
+ // v2 PluginContext is agent/aisdk/catalog/command/integration/reference/skill
53
+ // only), so there is nothing for `setup` to subscribe to -- it exists to make
54
+ // the plugin load instead of erroring on every startup. Usage itself does not
55
+ // depend on this hook: opencode rows are parsed from the on-disk storage and
56
+ // opencode.db, and the hook only makes a sync happen sooner.
57
+ `export const TokenUsagePlugin = server;\n` +
58
+ `export default {\n` +
59
+ ` id: "tokenusage",\n` +
60
+ ` server,\n` +
61
+ ` setup: async () => {},\n` +
62
+ `};\n`
63
+ );
64
+ }
65
+
66
+ async function upsertOpencodePlugin({ configDir, notifyPath, pluginName = DEFAULT_PLUGIN_NAME }) {
67
+ if (!configDir) return { changed: false, pluginPath: null, skippedReason: "config-missing" };
68
+ const pluginDir = resolveOpencodePluginDir({ configDir });
69
+ const pluginPath = path.join(pluginDir, pluginName);
70
+ const next = buildOpencodePlugin({ notifyPath });
71
+ const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
72
+
73
+ if (existing === next) {
74
+ return { changed: false, pluginPath, skippedReason: null };
75
+ }
76
+
77
+ await ensureDir(pluginDir);
78
+
79
+ let backupPath = null;
80
+ if (existing != null) {
81
+ backupPath = `${pluginPath}.bak.${new Date().toISOString().replace(/[:.]/g, "-")}`;
82
+ await fs.copyFile(pluginPath, backupPath).catch(() => {});
83
+ }
84
+
85
+ await fs.writeFile(pluginPath, next, "utf8");
86
+ return { changed: true, pluginPath, backupPath, skippedReason: null };
87
+ }
88
+
89
+ async function removeOpencodePlugin({ configDir, pluginName = DEFAULT_PLUGIN_NAME }) {
90
+ if (!configDir) return { removed: false, skippedReason: "config-missing" };
91
+ const pluginPath = path.join(resolveOpencodePluginDir({ configDir }), pluginName);
92
+ const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
93
+ if (existing == null) return { removed: false, skippedReason: "plugin-missing" };
94
+ if (!hasPluginMarker(existing)) return { removed: false, skippedReason: "unexpected-content" };
95
+ await fs.unlink(pluginPath).catch(() => {});
96
+ return { removed: true, skippedReason: null };
97
+ }
98
+
99
+ async function isOpencodePluginInstalled({ configDir, pluginName = DEFAULT_PLUGIN_NAME }) {
100
+ if (!configDir) return false;
101
+ const pluginPath = path.join(resolveOpencodePluginDir({ configDir }), pluginName);
102
+ const existing = await fs.readFile(pluginPath, "utf8").catch(() => null);
103
+ if (!existing) return false;
104
+ return hasPluginMarker(existing);
105
+ }
106
+
107
+ function hasPluginMarker(text) {
108
+ return typeof text === "string" && text.includes(PLUGIN_MARKER);
109
+ }
110
+
111
+ module.exports = {
112
+ DEFAULT_EVENT,
113
+ DEFAULT_PLUGIN_NAME,
114
+ PLUGIN_MARKER,
115
+ resolveOpencodeConfigDir,
116
+ resolveOpencodePluginDir,
117
+ buildOpencodePlugin,
118
+ upsertOpencodePlugin,
119
+ removeOpencodePlugin,
120
+ isOpencodePluginInstalled,
121
+ };