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.
Files changed (47) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +52 -11
  3. package/bin/tokenusage.js +14 -2
  4. package/package.json +1 -2
  5. package/src/cli.js +325 -78
  6. package/src/commands/device-login.js +3 -3
  7. package/src/commands/init.js +343 -153
  8. package/src/commands/pause.js +11 -23
  9. package/src/commands/reconcile.js +496 -0
  10. package/src/commands/sessions.js +57 -14
  11. package/src/commands/sync.js +69 -15
  12. package/src/commands/uninstall.js +6 -9
  13. package/src/commands/upload.js +147 -0
  14. package/src/lib/cli-errors.js +33 -0
  15. package/src/lib/cli-ui.js +93 -0
  16. package/src/lib/queue-row-normalize.js +55 -0
  17. package/src/lib/scheduled-task.js +597 -75
  18. package/src/lib/antigravity-paths.js +0 -48
  19. package/src/lib/async-queue-reader.js +0 -138
  20. package/src/lib/atomic-json.js +0 -27
  21. package/src/lib/bearer-token.js +0 -11
  22. package/src/lib/cloud-account.js +0 -541
  23. package/src/lib/codex-context-breakdown.js +0 -806
  24. package/src/lib/context-health.js +0 -124
  25. package/src/lib/grok-context-breakdown.js +0 -890
  26. package/src/lib/heatmap-compact.js +0 -114
  27. package/src/lib/local-api.js +0 -3574
  28. package/src/lib/outcomes-engine.js +0 -253
  29. package/src/lib/pet-packages.js +0 -722
  30. package/src/lib/skill-usage.js +0 -267
  31. package/src/lib/skills-manager.js +0 -1591
  32. package/src/lib/source-metadata.js +0 -48
  33. package/src/lib/subscription-manager.js +0 -293
  34. package/src/lib/wrapped-aggregator.js +0 -236
  35. package/test/audit-fixes.test.js +0 -249
  36. package/test/branding.test.js +0 -24
  37. package/test/cli-options.test.js +0 -43
  38. package/test/cli-smoke.test.js +0 -53
  39. package/test/cli-ui.test.js +0 -47
  40. package/test/collection-controls.test.js +0 -255
  41. package/test/ingest-sanitize.test.js +0 -162
  42. package/test/packaging.test.js +0 -107
  43. package/test/quiet-warnings.test.js +0 -53
  44. package/test/runtime-copy.test.js +0 -111
  45. package/test/session-upload.test.js +0 -427
  46. package/test/trae-db.test.js +0 -67
  47. package/test/zstd-native.test.js +0 -36
@@ -1,124 +0,0 @@
1
- "use strict";
2
-
3
- // Estimates fixed context overhead without returning file contents, prompts,
4
- // tool schemas, secrets, or command arguments.
5
-
6
- const fs = require("node:fs");
7
- const os = require("node:os");
8
- const path = require("node:path");
9
- const contextCache = new Map();
10
-
11
- function estimateTokens(text) {
12
- const value = String(text || "");
13
- let cjk = 0;
14
- for (const char of value) if (/\p{Script=Han}|\p{Script=Hiragana}|\p{Script=Katakana}/u.test(char)) cjk += 1;
15
- return Math.ceil(cjk + (value.length - cjk) / 4);
16
- }
17
-
18
- function readMetadata(filePath, kind, root) {
19
- try {
20
- const text = fs.readFileSync(filePath, "utf8");
21
- return { kind, name: path.relative(root, filePath) || path.basename(filePath), bytes: Buffer.byteLength(text), estimated_tokens: estimateTokens(text) };
22
- } catch { return null; }
23
- }
24
-
25
- function walkNamedFiles(root, targetName, limit = 500) {
26
- const out = [];
27
- const stack = [root];
28
- while (stack.length && out.length < limit) {
29
- const dir = stack.pop();
30
- let entries;
31
- try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { continue; }
32
- for (const entry of entries) {
33
- if ([".git", "node_modules", "dist", "build"].includes(entry.name)) continue;
34
- const filePath = path.join(dir, entry.name);
35
- if (entry.isDirectory()) stack.push(filePath);
36
- else if (entry.isFile() && entry.name === targetName) out.push(filePath);
37
- if (out.length >= limit) break;
38
- }
39
- }
40
- return out;
41
- }
42
-
43
- function countMcpServers(home, cwd) {
44
- const files = [
45
- path.join(home, ".claude.json"),
46
- path.join(home, ".claude", "settings.json"),
47
- path.join(cwd, ".mcp.json"),
48
- ];
49
- const names = new Set();
50
- for (const filePath of files) {
51
- try {
52
- const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
53
- const servers = data.mcpServers || data.mcp_servers || {};
54
- for (const name of Object.keys(servers)) names.add(name);
55
- } catch { /* absent or not JSON */ }
56
- }
57
- try {
58
- const toml = fs.readFileSync(path.join(home, ".codex", "config.toml"), "utf8");
59
- for (const match of toml.matchAll(/^\s*\[mcp_servers\.([^\]]+)\]/gm)) names.add(match[1]);
60
- } catch { /* absent */ }
61
- return names.size;
62
- }
63
-
64
- function computeContextHealth({ home = os.homedir(), cwd = process.cwd(), env = process.env, cacheTtlMs = 5 * 60_000 } = {}) {
65
- const cacheKey = `${home}\0${cwd}\0${env.TOKENUSAGE_MCP_TOOL_SCHEMA_TOKENS || ""}\0${env.TOKENUSAGE_MCP_TOOLS_PER_SERVER || ""}`;
66
- const cached = contextCache.get(cacheKey);
67
- if (cached && Date.now() - cached.at < cacheTtlMs) return cached.value;
68
- const files = [];
69
- const known = [
70
- [path.join(home, ".claude", "CLAUDE.md"), "instruction", home],
71
- [path.join(home, ".codex", "AGENTS.md"), "instruction", home],
72
- [path.join(cwd, "CLAUDE.md"), "instruction", cwd],
73
- [path.join(cwd, "AGENTS.md"), "instruction", cwd],
74
- ];
75
- for (const [filePath, kind, root] of known) {
76
- const item = readMetadata(filePath, kind, root);
77
- if (item) files.push(item);
78
- }
79
- const skillRoots = [
80
- path.join(home, ".claude", "skills"),
81
- path.join(home, ".codex", "skills"),
82
- path.join(cwd, ".claude", "skills"),
83
- path.join(cwd, ".agents", "skills"),
84
- ];
85
- for (const root of skillRoots) {
86
- for (const filePath of walkNamedFiles(root, "SKILL.md")) {
87
- const item = readMetadata(filePath, "skill", root);
88
- if (item) files.push(item);
89
- }
90
- }
91
- const instructionTokens = files.filter((row) => row.kind === "instruction").reduce((sum, row) => sum + row.estimated_tokens, 0);
92
- const skillTokens = files.filter((row) => row.kind === "skill").reduce((sum, row) => sum + row.estimated_tokens, 0);
93
- const mcpServers = countMcpServers(home, cwd);
94
- const perToolSchemaTokens = Math.max(0, Number(env.TOKENUSAGE_MCP_TOOL_SCHEMA_TOKENS || 400));
95
- const assumedToolsPerServer = Math.max(1, Number(env.TOKENUSAGE_MCP_TOOLS_PER_SERVER || 5));
96
- const mcpSchemaTokens = mcpServers * perToolSchemaTokens * assumedToolsPerServer;
97
- const total = instructionTokens + skillTokens + mcpSchemaTokens;
98
- const severity = total >= 50_000 ? "high" : total >= 20_000 ? "medium" : "low";
99
- const result = {
100
- generated_at: new Date().toISOString(),
101
- estimated_fixed_tokens: total,
102
- severity,
103
- breakdown: {
104
- instruction_files: files.filter((row) => row.kind === "instruction").length,
105
- instruction_tokens: instructionTokens,
106
- skills: files.filter((row) => row.kind === "skill").length,
107
- skill_tokens: skillTokens,
108
- mcp_servers: mcpServers,
109
- mcp_schema_tokens: mcpSchemaTokens,
110
- },
111
- largest_items: files.sort((a, b) => b.estimated_tokens - a.estimated_tokens).slice(0, 10),
112
- provenance: {
113
- confidence: "inferred",
114
- source: "local-config-metadata",
115
- mcp_assumption: { tools_per_server: assumedToolsPerServer, tokens_per_tool_schema: perToolSchemaTokens },
116
- content_retained: false,
117
- },
118
- };
119
- contextCache.set(cacheKey, { at: Date.now(), value: result });
120
- if (contextCache.size > 8) contextCache.delete(contextCache.keys().next().value);
121
- return result;
122
- }
123
-
124
- module.exports = { estimateTokens, computeContextHealth, countMcpServers };