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.
- package/CHANGELOG.md +39 -19
- package/LICENSE +22 -22
- package/README.md +94 -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 +181 -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
|
@@ -1,232 +1,232 @@
|
|
|
1
|
-
// Shared helpers for claude-categorizer.js and codex-context-breakdown.js.
|
|
2
|
-
// Extracted to eliminate copy-paste between the two files.
|
|
3
|
-
|
|
4
|
-
"use strict";
|
|
5
|
-
|
|
6
|
-
const path = require("node:path");
|
|
7
|
-
|
|
8
|
-
// ---------------------------------------------------------------------------
|
|
9
|
-
// Tool categorizer
|
|
10
|
-
// ---------------------------------------------------------------------------
|
|
11
|
-
|
|
12
|
-
function categorizeTool(name) {
|
|
13
|
-
if (name === "text_response") return "Text Response";
|
|
14
|
-
if (name === "Malformed") return "Malformed";
|
|
15
|
-
|
|
16
|
-
if (name.startsWith("mcp__")) {
|
|
17
|
-
const parts = name.split("__");
|
|
18
|
-
if (parts.length >= 3) {
|
|
19
|
-
const serverRaw = parts[1];
|
|
20
|
-
let server;
|
|
21
|
-
const pluginMatch = serverRaw.match(/^plugin_(.+)$/);
|
|
22
|
-
if (pluginMatch) {
|
|
23
|
-
const inner = pluginMatch[1];
|
|
24
|
-
const segments = inner.split("_");
|
|
25
|
-
const half = Math.floor(segments.length / 2);
|
|
26
|
-
const firstHalf = segments.slice(0, half).join("_");
|
|
27
|
-
const secondHalf = segments.slice(half).join("_");
|
|
28
|
-
if (firstHalf && firstHalf === secondHalf) {
|
|
29
|
-
server = firstHalf;
|
|
30
|
-
} else {
|
|
31
|
-
server = inner;
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
server = serverRaw;
|
|
35
|
-
}
|
|
36
|
-
server = server.replace(/_/g, "-");
|
|
37
|
-
return `MCP: ${server}`;
|
|
38
|
-
}
|
|
39
|
-
return "MCP: Unknown";
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (/^Task(Create|Update|Get|List|Output|Stop)$/.test(name)) return "Task Mgmt";
|
|
43
|
-
if (/^Todo/.test(name)) return "Task Mgmt";
|
|
44
|
-
if (/Plan/.test(name)) return "Planning";
|
|
45
|
-
if (name === "Agent") return "Agent";
|
|
46
|
-
if (/^Web(Fetch|Search)$/.test(name)) return "Web";
|
|
47
|
-
if (name === "Skill") return "Skill";
|
|
48
|
-
if (name === "LSP") return "IDE";
|
|
49
|
-
if (name === "AskUserQuestion") return "Interaction";
|
|
50
|
-
|
|
51
|
-
if (name === "exec_command" || name === "write_stdin") return "Execution";
|
|
52
|
-
if (name === "update_plan") return "Planning";
|
|
53
|
-
if (/_agent$/.test(name)) return "Agent";
|
|
54
|
-
if (/^list_mcp/.test(name)) return "MCP Mgmt";
|
|
55
|
-
if (
|
|
56
|
-
/^(navigate_page|click|select_page|new_page|take_snapshot|take_screenshot|evaluate_script|list_pages|list_console_messages|view_image|emulate|resize_page|wait_for|close_page|get_console_message|get_network_request|list_network_requests|performance_)/.test(
|
|
57
|
-
name,
|
|
58
|
-
)
|
|
59
|
-
)
|
|
60
|
-
return "Browser";
|
|
61
|
-
|
|
62
|
-
// Claude Code built-in tools (also harmless to match from Codex side).
|
|
63
|
-
if (/^(Read|Write|Edit|Glob)$/.test(name)) return "File Ops";
|
|
64
|
-
if (name === "Grep") return "Search";
|
|
65
|
-
if (name === "Bash") return "Execution";
|
|
66
|
-
|
|
67
|
-
if (name.includes("<tool_call>") || name.includes("<arg_")) return "Malformed";
|
|
68
|
-
|
|
69
|
-
return "Other";
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// ---------------------------------------------------------------------------
|
|
73
|
-
// Shell command helpers
|
|
74
|
-
// ---------------------------------------------------------------------------
|
|
75
|
-
|
|
76
|
-
function inferExecCommandKind(command) {
|
|
77
|
-
const cmd = String(command || "").trim();
|
|
78
|
-
if (/^(npm|yarn|pnpm)\s+(run\s+)?(build|build:|.*:build\b)/.test(cmd)) return "build";
|
|
79
|
-
if (/^(npm|yarn|pnpm)\s+(test|run\s+test\b|run\s+.*test\b)/.test(cmd)) return "test";
|
|
80
|
-
if (/^(npm|yarn|pnpm)\s+run\s+typecheck\b/.test(cmd)) return "typecheck";
|
|
81
|
-
if (/^(npm|yarn|pnpm)\s+(install|add|ci)\b/.test(cmd)) return "dependency";
|
|
82
|
-
if (/^(npm|yarn|pnpm)\s+(pack|publish|version)\b/.test(cmd)) return "package";
|
|
83
|
-
if (/^(npm|yarn|pnpm)\s+run\s+(dev|serve|start|.*dev.*)\b/.test(cmd)) return "dev_server";
|
|
84
|
-
if (/^node\s+--check\b/.test(cmd) || /\bnode\s+--check\b/.test(cmd)) return "syntax_check";
|
|
85
|
-
if (/^node\s+--input-type=module\s+-e\b/.test(cmd) || /^node\s+-e\b/.test(cmd)) return "node_eval";
|
|
86
|
-
if (/^node\s+.*\b(query|analyze|report)\b/.test(cmd)) return "node_cli";
|
|
87
|
-
if (/^git\s+status\b/.test(cmd)) return "git_status";
|
|
88
|
-
if (/^git\s+(push|pull|fetch|clone)\b/.test(cmd) || /\bgit\s+(push|pull|fetch|clone)\b/.test(cmd)) return "git_remote";
|
|
89
|
-
if (/^git\s+(add|commit|branch|config|remote|restore)\b/.test(cmd) || /\bgit\s+(add|commit|branch|config|remote|restore)\b/.test(cmd)) return "git_local";
|
|
90
|
-
if (/^(curl|wget)\b/.test(cmd) || /\b(curl|wget)\b/.test(cmd)) return "http";
|
|
91
|
-
if (/^(ps|pgrep|pkill|kill|lsof)\b/.test(cmd)) return "process";
|
|
92
|
-
if (/^tmux\b/.test(cmd)) return "terminal";
|
|
93
|
-
if (/^(open|osascript)\b/.test(cmd)) return "browser_control";
|
|
94
|
-
if (/^(rm|mkdir|touch|chmod|cp|mv)\b/.test(cmd)) return "file_mutation";
|
|
95
|
-
if (/^(pwd|ls|test)\b/.test(cmd) || /^(pwd|ls)\s*[;&|]/.test(cmd)) return "shell_inspect";
|
|
96
|
-
if (/[;&|]{1,2}/.test(cmd)) return "compound";
|
|
97
|
-
return "unknown";
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function shellWords(command) {
|
|
101
|
-
const out = [];
|
|
102
|
-
const re = /"([^"\\]*(?:\\.[^"\\]*)*)"|'([^']*)'|(\S+)/g;
|
|
103
|
-
let match;
|
|
104
|
-
while ((match = re.exec(String(command || ""))) !== null) {
|
|
105
|
-
out.push(match[1] ?? match[2] ?? match[3] ?? "");
|
|
106
|
-
}
|
|
107
|
-
return out.filter(Boolean);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function unwrapShellCommand(words) {
|
|
111
|
-
if (words.length >= 3 && /^(bash|sh|zsh|fish)$/.test(words[0]) && words[1] === "-lc") {
|
|
112
|
-
return shellWords(words.slice(2).join(" "));
|
|
113
|
-
}
|
|
114
|
-
if (words.length >= 3 && /^(rtk|env|command|xcrun)$/.test(words[0])) {
|
|
115
|
-
return unwrapShellCommand(words.slice(1));
|
|
116
|
-
}
|
|
117
|
-
return words;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function sanitizeCommandSignature(command) {
|
|
121
|
-
const words = unwrapShellCommand(shellWords(command));
|
|
122
|
-
if (words.length === 0) return "unknown";
|
|
123
|
-
const executable = path.basename(words[0] || "unknown");
|
|
124
|
-
const subcommand = words.find((word, idx) => {
|
|
125
|
-
if (idx === 0) return false;
|
|
126
|
-
if (!word || word.startsWith("-")) return false;
|
|
127
|
-
if (/^[A-Z_][A-Z0-9_]*=/.test(word)) return false;
|
|
128
|
-
return true;
|
|
129
|
-
});
|
|
130
|
-
return subcommand ? `${executable} ${subcommand}` : executable;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function getExecutableName(command) {
|
|
134
|
-
const words = unwrapShellCommand(shellWords(command));
|
|
135
|
-
if (words.length === 0) return "unknown";
|
|
136
|
-
return path.basename(words[0] || "unknown") || "unknown";
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// ---------------------------------------------------------------------------
|
|
140
|
-
// Token totals helpers
|
|
141
|
-
// ---------------------------------------------------------------------------
|
|
142
|
-
|
|
143
|
-
function emptyTotals() {
|
|
144
|
-
return {
|
|
145
|
-
input_tokens: 0,
|
|
146
|
-
cached_input_tokens: 0,
|
|
147
|
-
cache_creation_input_tokens: 0,
|
|
148
|
-
output_tokens: 0,
|
|
149
|
-
reasoning_output_tokens: 0,
|
|
150
|
-
total_tokens: 0,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function addInto(target, source) {
|
|
155
|
-
target.input_tokens += source.input_tokens || 0;
|
|
156
|
-
target.cached_input_tokens += source.cached_input_tokens || 0;
|
|
157
|
-
target.cache_creation_input_tokens += source.cache_creation_input_tokens || 0;
|
|
158
|
-
target.output_tokens += source.output_tokens || 0;
|
|
159
|
-
target.reasoning_output_tokens += source.reasoning_output_tokens || 0;
|
|
160
|
-
target.total_tokens += source.total_tokens || 0;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function roundTotals(totals) {
|
|
164
|
-
return {
|
|
165
|
-
input_tokens: Math.round(totals?.input_tokens || 0),
|
|
166
|
-
cached_input_tokens: Math.round(totals?.cached_input_tokens || 0),
|
|
167
|
-
cache_creation_input_tokens: Math.round(totals?.cache_creation_input_tokens || 0),
|
|
168
|
-
output_tokens: Math.round(totals?.output_tokens || 0),
|
|
169
|
-
reasoning_output_tokens: Math.round(totals?.reasoning_output_tokens || 0),
|
|
170
|
-
total_tokens: Math.round(totals?.total_tokens || 0),
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function buildExecStatsEntry() {
|
|
175
|
-
return {
|
|
176
|
-
calls: 0,
|
|
177
|
-
failures: 0,
|
|
178
|
-
duration_ms: 0,
|
|
179
|
-
max_duration_ms: 0,
|
|
180
|
-
output_chars: 0,
|
|
181
|
-
output_lines: 0,
|
|
182
|
-
totals: emptyTotals(),
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// ---------------------------------------------------------------------------
|
|
187
|
-
// Integer allocation
|
|
188
|
-
// ---------------------------------------------------------------------------
|
|
189
|
-
|
|
190
|
-
function allocateByLargestRemainder(total, weights, order) {
|
|
191
|
-
const out = {};
|
|
192
|
-
if (!Number.isFinite(total) || total <= 0) {
|
|
193
|
-
for (const key of order) out[key] = 0;
|
|
194
|
-
return out;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
let totalWeight = 0;
|
|
198
|
-
for (const key of order) {
|
|
199
|
-
const w = Number(weights[key] || 0);
|
|
200
|
-
if (Number.isFinite(w) && w > 0) totalWeight += w;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (totalWeight <= 0) {
|
|
204
|
-
for (const key of order) out[key] = 0;
|
|
205
|
-
return out;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const exact = order.map((key) => (Number(weights[key] || 0) / totalWeight) * total);
|
|
209
|
-
const floored = exact.map((x) => Math.floor(x));
|
|
210
|
-
const remainder = total - floored.reduce((a, b) => a + b, 0);
|
|
211
|
-
const remainders = exact
|
|
212
|
-
.map((x, i) => ({ i, frac: x - Math.floor(x) }))
|
|
213
|
-
.sort((a, b) => b.frac - a.frac);
|
|
214
|
-
for (let k = 0; k < remainder; k++) floored[remainders[k % order.length].i] += 1;
|
|
215
|
-
|
|
216
|
-
for (let i = 0; i < order.length; i++) out[order[i]] = floored[i];
|
|
217
|
-
return out;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
module.exports = {
|
|
221
|
-
categorizeTool,
|
|
222
|
-
inferExecCommandKind,
|
|
223
|
-
shellWords,
|
|
224
|
-
unwrapShellCommand,
|
|
225
|
-
sanitizeCommandSignature,
|
|
226
|
-
getExecutableName,
|
|
227
|
-
emptyTotals,
|
|
228
|
-
addInto,
|
|
229
|
-
roundTotals,
|
|
230
|
-
buildExecStatsEntry,
|
|
231
|
-
allocateByLargestRemainder,
|
|
232
|
-
};
|
|
1
|
+
// Shared helpers for claude-categorizer.js and codex-context-breakdown.js.
|
|
2
|
+
// Extracted to eliminate copy-paste between the two files.
|
|
3
|
+
|
|
4
|
+
"use strict";
|
|
5
|
+
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Tool categorizer
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
function categorizeTool(name) {
|
|
13
|
+
if (name === "text_response") return "Text Response";
|
|
14
|
+
if (name === "Malformed") return "Malformed";
|
|
15
|
+
|
|
16
|
+
if (name.startsWith("mcp__")) {
|
|
17
|
+
const parts = name.split("__");
|
|
18
|
+
if (parts.length >= 3) {
|
|
19
|
+
const serverRaw = parts[1];
|
|
20
|
+
let server;
|
|
21
|
+
const pluginMatch = serverRaw.match(/^plugin_(.+)$/);
|
|
22
|
+
if (pluginMatch) {
|
|
23
|
+
const inner = pluginMatch[1];
|
|
24
|
+
const segments = inner.split("_");
|
|
25
|
+
const half = Math.floor(segments.length / 2);
|
|
26
|
+
const firstHalf = segments.slice(0, half).join("_");
|
|
27
|
+
const secondHalf = segments.slice(half).join("_");
|
|
28
|
+
if (firstHalf && firstHalf === secondHalf) {
|
|
29
|
+
server = firstHalf;
|
|
30
|
+
} else {
|
|
31
|
+
server = inner;
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
server = serverRaw;
|
|
35
|
+
}
|
|
36
|
+
server = server.replace(/_/g, "-");
|
|
37
|
+
return `MCP: ${server}`;
|
|
38
|
+
}
|
|
39
|
+
return "MCP: Unknown";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (/^Task(Create|Update|Get|List|Output|Stop)$/.test(name)) return "Task Mgmt";
|
|
43
|
+
if (/^Todo/.test(name)) return "Task Mgmt";
|
|
44
|
+
if (/Plan/.test(name)) return "Planning";
|
|
45
|
+
if (name === "Agent") return "Agent";
|
|
46
|
+
if (/^Web(Fetch|Search)$/.test(name)) return "Web";
|
|
47
|
+
if (name === "Skill") return "Skill";
|
|
48
|
+
if (name === "LSP") return "IDE";
|
|
49
|
+
if (name === "AskUserQuestion") return "Interaction";
|
|
50
|
+
|
|
51
|
+
if (name === "exec_command" || name === "write_stdin") return "Execution";
|
|
52
|
+
if (name === "update_plan") return "Planning";
|
|
53
|
+
if (/_agent$/.test(name)) return "Agent";
|
|
54
|
+
if (/^list_mcp/.test(name)) return "MCP Mgmt";
|
|
55
|
+
if (
|
|
56
|
+
/^(navigate_page|click|select_page|new_page|take_snapshot|take_screenshot|evaluate_script|list_pages|list_console_messages|view_image|emulate|resize_page|wait_for|close_page|get_console_message|get_network_request|list_network_requests|performance_)/.test(
|
|
57
|
+
name,
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
return "Browser";
|
|
61
|
+
|
|
62
|
+
// Claude Code built-in tools (also harmless to match from Codex side).
|
|
63
|
+
if (/^(Read|Write|Edit|Glob)$/.test(name)) return "File Ops";
|
|
64
|
+
if (name === "Grep") return "Search";
|
|
65
|
+
if (name === "Bash") return "Execution";
|
|
66
|
+
|
|
67
|
+
if (name.includes("<tool_call>") || name.includes("<arg_")) return "Malformed";
|
|
68
|
+
|
|
69
|
+
return "Other";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Shell command helpers
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
function inferExecCommandKind(command) {
|
|
77
|
+
const cmd = String(command || "").trim();
|
|
78
|
+
if (/^(npm|yarn|pnpm)\s+(run\s+)?(build|build:|.*:build\b)/.test(cmd)) return "build";
|
|
79
|
+
if (/^(npm|yarn|pnpm)\s+(test|run\s+test\b|run\s+.*test\b)/.test(cmd)) return "test";
|
|
80
|
+
if (/^(npm|yarn|pnpm)\s+run\s+typecheck\b/.test(cmd)) return "typecheck";
|
|
81
|
+
if (/^(npm|yarn|pnpm)\s+(install|add|ci)\b/.test(cmd)) return "dependency";
|
|
82
|
+
if (/^(npm|yarn|pnpm)\s+(pack|publish|version)\b/.test(cmd)) return "package";
|
|
83
|
+
if (/^(npm|yarn|pnpm)\s+run\s+(dev|serve|start|.*dev.*)\b/.test(cmd)) return "dev_server";
|
|
84
|
+
if (/^node\s+--check\b/.test(cmd) || /\bnode\s+--check\b/.test(cmd)) return "syntax_check";
|
|
85
|
+
if (/^node\s+--input-type=module\s+-e\b/.test(cmd) || /^node\s+-e\b/.test(cmd)) return "node_eval";
|
|
86
|
+
if (/^node\s+.*\b(query|analyze|report)\b/.test(cmd)) return "node_cli";
|
|
87
|
+
if (/^git\s+status\b/.test(cmd)) return "git_status";
|
|
88
|
+
if (/^git\s+(push|pull|fetch|clone)\b/.test(cmd) || /\bgit\s+(push|pull|fetch|clone)\b/.test(cmd)) return "git_remote";
|
|
89
|
+
if (/^git\s+(add|commit|branch|config|remote|restore)\b/.test(cmd) || /\bgit\s+(add|commit|branch|config|remote|restore)\b/.test(cmd)) return "git_local";
|
|
90
|
+
if (/^(curl|wget)\b/.test(cmd) || /\b(curl|wget)\b/.test(cmd)) return "http";
|
|
91
|
+
if (/^(ps|pgrep|pkill|kill|lsof)\b/.test(cmd)) return "process";
|
|
92
|
+
if (/^tmux\b/.test(cmd)) return "terminal";
|
|
93
|
+
if (/^(open|osascript)\b/.test(cmd)) return "browser_control";
|
|
94
|
+
if (/^(rm|mkdir|touch|chmod|cp|mv)\b/.test(cmd)) return "file_mutation";
|
|
95
|
+
if (/^(pwd|ls|test)\b/.test(cmd) || /^(pwd|ls)\s*[;&|]/.test(cmd)) return "shell_inspect";
|
|
96
|
+
if (/[;&|]{1,2}/.test(cmd)) return "compound";
|
|
97
|
+
return "unknown";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function shellWords(command) {
|
|
101
|
+
const out = [];
|
|
102
|
+
const re = /"([^"\\]*(?:\\.[^"\\]*)*)"|'([^']*)'|(\S+)/g;
|
|
103
|
+
let match;
|
|
104
|
+
while ((match = re.exec(String(command || ""))) !== null) {
|
|
105
|
+
out.push(match[1] ?? match[2] ?? match[3] ?? "");
|
|
106
|
+
}
|
|
107
|
+
return out.filter(Boolean);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function unwrapShellCommand(words) {
|
|
111
|
+
if (words.length >= 3 && /^(bash|sh|zsh|fish)$/.test(words[0]) && words[1] === "-lc") {
|
|
112
|
+
return shellWords(words.slice(2).join(" "));
|
|
113
|
+
}
|
|
114
|
+
if (words.length >= 3 && /^(rtk|env|command|xcrun)$/.test(words[0])) {
|
|
115
|
+
return unwrapShellCommand(words.slice(1));
|
|
116
|
+
}
|
|
117
|
+
return words;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function sanitizeCommandSignature(command) {
|
|
121
|
+
const words = unwrapShellCommand(shellWords(command));
|
|
122
|
+
if (words.length === 0) return "unknown";
|
|
123
|
+
const executable = path.basename(words[0] || "unknown");
|
|
124
|
+
const subcommand = words.find((word, idx) => {
|
|
125
|
+
if (idx === 0) return false;
|
|
126
|
+
if (!word || word.startsWith("-")) return false;
|
|
127
|
+
if (/^[A-Z_][A-Z0-9_]*=/.test(word)) return false;
|
|
128
|
+
return true;
|
|
129
|
+
});
|
|
130
|
+
return subcommand ? `${executable} ${subcommand}` : executable;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getExecutableName(command) {
|
|
134
|
+
const words = unwrapShellCommand(shellWords(command));
|
|
135
|
+
if (words.length === 0) return "unknown";
|
|
136
|
+
return path.basename(words[0] || "unknown") || "unknown";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// Token totals helpers
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
function emptyTotals() {
|
|
144
|
+
return {
|
|
145
|
+
input_tokens: 0,
|
|
146
|
+
cached_input_tokens: 0,
|
|
147
|
+
cache_creation_input_tokens: 0,
|
|
148
|
+
output_tokens: 0,
|
|
149
|
+
reasoning_output_tokens: 0,
|
|
150
|
+
total_tokens: 0,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function addInto(target, source) {
|
|
155
|
+
target.input_tokens += source.input_tokens || 0;
|
|
156
|
+
target.cached_input_tokens += source.cached_input_tokens || 0;
|
|
157
|
+
target.cache_creation_input_tokens += source.cache_creation_input_tokens || 0;
|
|
158
|
+
target.output_tokens += source.output_tokens || 0;
|
|
159
|
+
target.reasoning_output_tokens += source.reasoning_output_tokens || 0;
|
|
160
|
+
target.total_tokens += source.total_tokens || 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function roundTotals(totals) {
|
|
164
|
+
return {
|
|
165
|
+
input_tokens: Math.round(totals?.input_tokens || 0),
|
|
166
|
+
cached_input_tokens: Math.round(totals?.cached_input_tokens || 0),
|
|
167
|
+
cache_creation_input_tokens: Math.round(totals?.cache_creation_input_tokens || 0),
|
|
168
|
+
output_tokens: Math.round(totals?.output_tokens || 0),
|
|
169
|
+
reasoning_output_tokens: Math.round(totals?.reasoning_output_tokens || 0),
|
|
170
|
+
total_tokens: Math.round(totals?.total_tokens || 0),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function buildExecStatsEntry() {
|
|
175
|
+
return {
|
|
176
|
+
calls: 0,
|
|
177
|
+
failures: 0,
|
|
178
|
+
duration_ms: 0,
|
|
179
|
+
max_duration_ms: 0,
|
|
180
|
+
output_chars: 0,
|
|
181
|
+
output_lines: 0,
|
|
182
|
+
totals: emptyTotals(),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
// Integer allocation
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
|
|
190
|
+
function allocateByLargestRemainder(total, weights, order) {
|
|
191
|
+
const out = {};
|
|
192
|
+
if (!Number.isFinite(total) || total <= 0) {
|
|
193
|
+
for (const key of order) out[key] = 0;
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let totalWeight = 0;
|
|
198
|
+
for (const key of order) {
|
|
199
|
+
const w = Number(weights[key] || 0);
|
|
200
|
+
if (Number.isFinite(w) && w > 0) totalWeight += w;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (totalWeight <= 0) {
|
|
204
|
+
for (const key of order) out[key] = 0;
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const exact = order.map((key) => (Number(weights[key] || 0) / totalWeight) * total);
|
|
209
|
+
const floored = exact.map((x) => Math.floor(x));
|
|
210
|
+
const remainder = total - floored.reduce((a, b) => a + b, 0);
|
|
211
|
+
const remainders = exact
|
|
212
|
+
.map((x, i) => ({ i, frac: x - Math.floor(x) }))
|
|
213
|
+
.sort((a, b) => b.frac - a.frac);
|
|
214
|
+
for (let k = 0; k < remainder; k++) floored[remainders[k % order.length].i] += 1;
|
|
215
|
+
|
|
216
|
+
for (let i = 0; i < order.length; i++) out[order[i]] = floored[i];
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
module.exports = {
|
|
221
|
+
categorizeTool,
|
|
222
|
+
inferExecCommandKind,
|
|
223
|
+
shellWords,
|
|
224
|
+
unwrapShellCommand,
|
|
225
|
+
sanitizeCommandSignature,
|
|
226
|
+
getExecutableName,
|
|
227
|
+
emptyTotals,
|
|
228
|
+
addInto,
|
|
229
|
+
roundTotals,
|
|
230
|
+
buildExecStatsEntry,
|
|
231
|
+
allocateByLargestRemainder,
|
|
232
|
+
};
|