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
|
@@ -1,237 +1,237 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { canonicalUsage } = require("./codex-token-usage");
|
|
4
|
-
|
|
5
|
-
const SUM_FIELDS = [
|
|
6
|
-
"estimated_output_tokens", "estimated_duration_ms", "estimated_request_count",
|
|
7
|
-
"first_response_total_ms", "first_response_sample_count",
|
|
8
|
-
];
|
|
9
|
-
const MIN_OUTPUT = 200;
|
|
10
|
-
const MIN_DURATION_MS = 1000;
|
|
11
|
-
const MAX_DURATION_MS = 60 * 60 * 1000;
|
|
12
|
-
const SAME_FLUSH_MS = 100;
|
|
13
|
-
|
|
14
|
-
function nonNegative(value) {
|
|
15
|
-
const number = Number(value);
|
|
16
|
-
return Number.isFinite(number) && number >= 0 ? number : 0;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function normalizePerformance(value) {
|
|
20
|
-
const out = Object.fromEntries(SUM_FIELDS.map((field) => [field, nonNegative(value?.[field])]));
|
|
21
|
-
out.estimated_tokens_per_second = out.estimated_request_count > 0 && out.estimated_output_tokens > 0 && out.estimated_duration_ms > 0
|
|
22
|
-
? out.estimated_output_tokens / (out.estimated_duration_ms / 1000) : null;
|
|
23
|
-
out.first_response_ms = out.first_response_sample_count > 0
|
|
24
|
-
? out.first_response_total_ms / out.first_response_sample_count : null;
|
|
25
|
-
return out;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function mergePerformance(...values) {
|
|
29
|
-
const out = normalizePerformance();
|
|
30
|
-
for (const value of values) {
|
|
31
|
-
for (const field of SUM_FIELDS) out[field] += nonNegative(value?.[field]);
|
|
32
|
-
}
|
|
33
|
-
return normalizePerformance(out);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function timestampMs(value) {
|
|
37
|
-
if (typeof value !== "string") return null;
|
|
38
|
-
const number = Date.parse(value);
|
|
39
|
-
return Number.isFinite(number) ? number : null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function accumulator() {
|
|
43
|
-
let total = normalizePerformance();
|
|
44
|
-
const byModel = new Map();
|
|
45
|
-
function add(model, delta) {
|
|
46
|
-
total = mergePerformance(total, delta);
|
|
47
|
-
if (model) byModel.set(model, mergePerformance(byModel.get(model), delta));
|
|
48
|
-
}
|
|
49
|
-
function request(model, outputTokens, start, end) {
|
|
50
|
-
const duration = start !== null && end !== null ? end - start : 0;
|
|
51
|
-
record(model, outputTokens, duration);
|
|
52
|
-
}
|
|
53
|
-
function requestDuration(model, outputTokens, durationMs) {
|
|
54
|
-
record(model, outputTokens, durationMs);
|
|
55
|
-
}
|
|
56
|
-
function record(model, outputTokens, duration) {
|
|
57
|
-
if (outputTokens < MIN_OUTPUT || duration < MIN_DURATION_MS || duration > MAX_DURATION_MS) return;
|
|
58
|
-
add(model, { estimated_output_tokens: outputTokens, estimated_duration_ms: duration, estimated_request_count: 1 });
|
|
59
|
-
}
|
|
60
|
-
return { add, request, requestDuration, finish: () => ({ performance: total, byModel }) };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function createClaudePerformanceCollector() {
|
|
64
|
-
const nodes = new Map();
|
|
65
|
-
const messages = new Map();
|
|
66
|
-
const totals = accumulator();
|
|
67
|
-
|
|
68
|
-
function consume(obj, key, countedUsage, model) {
|
|
69
|
-
const timestamp = timestampMs(obj?.timestamp || obj?.message?.timestamp);
|
|
70
|
-
if (typeof obj?.uuid === "string" && !nodes.has(obj.uuid)) {
|
|
71
|
-
nodes.set(obj.uuid, {
|
|
72
|
-
parent: obj.parentUuid, timestamp, type: obj.type,
|
|
73
|
-
invalid: Boolean(obj.isApiErrorMessage || obj.isMeta),
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
if (obj?.type !== "assistant" || !key) return;
|
|
77
|
-
let timing = messages.get(key);
|
|
78
|
-
if (!timing) {
|
|
79
|
-
// Tokens come from scanClaudeSession's canonical, once-counted delta.
|
|
80
|
-
// Later content blocks extend only the timing, never the numerator.
|
|
81
|
-
timing = {
|
|
82
|
-
parent: obj.parentUuid, startsAtFirstBlock: obj.apiBlockIndex == null || obj.apiBlockIndex === 0,
|
|
83
|
-
end: null, complete: false, invalid: false, model,
|
|
84
|
-
output: nonNegative(countedUsage?.output_tokens) + nonNegative(countedUsage?.reasoning_output_tokens),
|
|
85
|
-
};
|
|
86
|
-
messages.set(key, timing);
|
|
87
|
-
}
|
|
88
|
-
if (timestamp !== null) timing.end = timing.end === null ? timestamp : Math.max(timing.end, timestamp);
|
|
89
|
-
timing.complete ||= ["end_turn", "tool_use", "max_tokens", "stop_sequence"].includes(obj.message?.stop_reason);
|
|
90
|
-
timing.invalid ||= Boolean(obj.isApiErrorMessage || obj.message?.error);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function finish() {
|
|
94
|
-
for (const timing of messages.values()) {
|
|
95
|
-
if (!timing.complete || timing.invalid || !timing.startsAtFirstBlock) continue;
|
|
96
|
-
let parent = timing.parent;
|
|
97
|
-
for (let hop = 0; hop < 32 && parent; hop += 1) {
|
|
98
|
-
const node = nodes.get(parent);
|
|
99
|
-
if (!node) break;
|
|
100
|
-
if (node.type === "attachment") { parent = node.parent; continue; }
|
|
101
|
-
// A user row includes tool results. Their completion timestamp keeps
|
|
102
|
-
// time spent executing the tool outside the next inference window.
|
|
103
|
-
if (node.type === "user" && !node.invalid) totals.request(timing.model, timing.output, node.timestamp, timing.end);
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return totals.finish();
|
|
108
|
-
}
|
|
109
|
-
return { consume, finish };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function createCodexPerformanceCollector() {
|
|
113
|
-
const totals = accumulator();
|
|
114
|
-
let boundary = null;
|
|
115
|
-
let lastTokenCount = null;
|
|
116
|
-
let start = null;
|
|
117
|
-
let lastOutput = null;
|
|
118
|
-
let toolAfterOutput = null;
|
|
119
|
-
let usageRecord = null;
|
|
120
|
-
let invalid = false;
|
|
121
|
-
let turnKey = null;
|
|
122
|
-
let turnModels = new Set();
|
|
123
|
-
const seenFirstResponses = new Set();
|
|
124
|
-
|
|
125
|
-
function reset() {
|
|
126
|
-
start = null;
|
|
127
|
-
lastOutput = null;
|
|
128
|
-
toolAfterOutput = null;
|
|
129
|
-
usageRecord = null;
|
|
130
|
-
invalid = false;
|
|
131
|
-
}
|
|
132
|
-
function noteBoundary(ms) {
|
|
133
|
-
if (ms !== null) boundary = boundary === null ? ms : Math.max(boundary, ms);
|
|
134
|
-
}
|
|
135
|
-
function consume(obj, model) {
|
|
136
|
-
const ms = timestampMs(obj?.timestamp);
|
|
137
|
-
const payload = obj?.payload || {};
|
|
138
|
-
if (obj?.type === "compacted") {
|
|
139
|
-
reset();
|
|
140
|
-
noteBoundary(ms);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (obj?.type === "event_msg" && ["task_started", "turn_aborted"].includes(payload.type)) {
|
|
144
|
-
reset();
|
|
145
|
-
noteBoundary(ms);
|
|
146
|
-
turnModels = new Set();
|
|
147
|
-
turnKey = payload.turn_id || null;
|
|
148
|
-
if (payload.type === "turn_aborted") invalid = true;
|
|
149
|
-
}
|
|
150
|
-
if (obj?.type === "turn_context") {
|
|
151
|
-
const nextKey = payload.turn_id || null;
|
|
152
|
-
if (turnKey && nextKey && turnKey !== nextKey) { reset(); turnModels = new Set(); }
|
|
153
|
-
turnKey = nextKey || turnKey;
|
|
154
|
-
noteBoundary(ms);
|
|
155
|
-
}
|
|
156
|
-
const responseType = String(payload.type || "");
|
|
157
|
-
const isModelOutput = obj?.type === "response_item" && (responseType === "reasoning"
|
|
158
|
-
|| responseType === "agent_message" || responseType.endsWith("_call")
|
|
159
|
-
|| (responseType === "message" && payload.role === "assistant"));
|
|
160
|
-
if (model && (obj?.type === "turn_context" || isModelOutput
|
|
161
|
-
|| payload.type === "model_rerouted")) turnModels.add(model);
|
|
162
|
-
if (obj?.type === "event_msg" && ["error", "api_error"].includes(payload.type)) invalid = true;
|
|
163
|
-
if (obj?.type === "event_msg" && payload.type === "task_complete") {
|
|
164
|
-
const raw = payload.time_to_first_token_ms;
|
|
165
|
-
const key = payload.turn_id || turnKey || obj.timestamp;
|
|
166
|
-
if (!invalid && typeof raw === "number" && Number.isFinite(raw) && raw >= 0 && !seenFirstResponses.has(key)) {
|
|
167
|
-
seenFirstResponses.add(key);
|
|
168
|
-
totals.add(turnModels.size === 1 ? [...turnModels][0] : null, { first_response_total_ms: raw, first_response_sample_count: 1 });
|
|
169
|
-
}
|
|
170
|
-
reset();
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (obj?.type === "token_usage_record") {
|
|
174
|
-
usageRecord = { end: ms, usage: canonicalUsage(payload.usage) };
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
if (obj?.type !== "response_item") return;
|
|
178
|
-
const type = String(payload.type || "");
|
|
179
|
-
if (type.endsWith("_output")) {
|
|
180
|
-
noteBoundary(ms);
|
|
181
|
-
if (lastOutput !== null) toolAfterOutput = ms;
|
|
182
|
-
} else if ((type === "message" && payload.role !== "assistant") || type === "user_message") {
|
|
183
|
-
noteBoundary(ms);
|
|
184
|
-
} else if (type === "reasoning" || type === "agent_message" || type.endsWith("_call") || (type === "message" && payload.role === "assistant")) {
|
|
185
|
-
// Old rollouts flush an output item just after its token_count.
|
|
186
|
-
if (ms === null || (start === null && lastTokenCount !== null && ms >= lastTokenCount && ms - lastTokenCount <= SAME_FLUSH_MS)) return;
|
|
187
|
-
if (start === null) start = boundary;
|
|
188
|
-
lastOutput = ms;
|
|
189
|
-
toolAfterOutput = null;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
function consumeUsage({ timestamp, delta, rawUsage, model }) {
|
|
193
|
-
// A repeated cumulative snapshot can be written while the next response
|
|
194
|
-
// is still generating. It is not a completion boundary for that response.
|
|
195
|
-
if (!delta || delta.total_tokens <= 0) return;
|
|
196
|
-
const ms = timestampMs(timestamp);
|
|
197
|
-
const raw = canonicalUsage(rawUsage);
|
|
198
|
-
const matches = raw && usageRecord?.usage && ["input_tokens", "cached_input_tokens", "output_tokens"]
|
|
199
|
-
.every((field) => raw[field] === usageRecord.usage[field]);
|
|
200
|
-
const waitedForTools = toolAfterOutput !== null && ms !== null && ms >= toolAfterOutput;
|
|
201
|
-
// Once a tool result follows the final output marker, the token_count's
|
|
202
|
-
// timestamp contains execution time. The output marker is the available
|
|
203
|
-
// response boundary even if the count was flushed much later.
|
|
204
|
-
const end = matches ? usageRecord.end : waitedForTools ? lastOutput : ms;
|
|
205
|
-
// Missing output markers cannot distinguish response timing from a
|
|
206
|
-
// snapshot or inherited context, so even a positive token delta is skipped.
|
|
207
|
-
if (!invalid && lastOutput !== null) totals.request(model, nonNegative(delta.output_tokens), start, end);
|
|
208
|
-
noteBoundary(ms);
|
|
209
|
-
lastTokenCount = ms;
|
|
210
|
-
reset();
|
|
211
|
-
}
|
|
212
|
-
return { consume, consumeUsage, finish: totals.finish };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// ZCode's CLI DB stores per-request wall-clock counters directly in its
|
|
216
|
-
// model_usage table (duration_ms, time_to_first_token_ms) instead of an event
|
|
217
|
-
// stream, so this collector is a thin adapter: each completed request becomes
|
|
218
|
-
// one duration-bounded sample, never a token total (those come from the
|
|
219
|
-
// shared usage reader so the browser cannot drift from the usage card).
|
|
220
|
-
function createZcodePerformanceCollector() {
|
|
221
|
-
const totals = accumulator();
|
|
222
|
-
function consumeUsage({ model, outputTokens, durationMs, timeToFirstTokenMs } = {}) {
|
|
223
|
-
totals.requestDuration(model, nonNegative(outputTokens), nonNegative(durationMs));
|
|
224
|
-
if (typeof timeToFirstTokenMs === "number" && Number.isFinite(timeToFirstTokenMs) && timeToFirstTokenMs >= 0) {
|
|
225
|
-
totals.add(null, { first_response_total_ms: timeToFirstTokenMs, first_response_sample_count: 1 });
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return { consumeUsage, finish: totals.finish };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
module.exports = {
|
|
232
|
-
normalizePerformance,
|
|
233
|
-
mergePerformance,
|
|
234
|
-
createClaudePerformanceCollector,
|
|
235
|
-
createCodexPerformanceCollector,
|
|
236
|
-
createZcodePerformanceCollector,
|
|
237
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { canonicalUsage } = require("./codex-token-usage");
|
|
4
|
+
|
|
5
|
+
const SUM_FIELDS = [
|
|
6
|
+
"estimated_output_tokens", "estimated_duration_ms", "estimated_request_count",
|
|
7
|
+
"first_response_total_ms", "first_response_sample_count",
|
|
8
|
+
];
|
|
9
|
+
const MIN_OUTPUT = 200;
|
|
10
|
+
const MIN_DURATION_MS = 1000;
|
|
11
|
+
const MAX_DURATION_MS = 60 * 60 * 1000;
|
|
12
|
+
const SAME_FLUSH_MS = 100;
|
|
13
|
+
|
|
14
|
+
function nonNegative(value) {
|
|
15
|
+
const number = Number(value);
|
|
16
|
+
return Number.isFinite(number) && number >= 0 ? number : 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizePerformance(value) {
|
|
20
|
+
const out = Object.fromEntries(SUM_FIELDS.map((field) => [field, nonNegative(value?.[field])]));
|
|
21
|
+
out.estimated_tokens_per_second = out.estimated_request_count > 0 && out.estimated_output_tokens > 0 && out.estimated_duration_ms > 0
|
|
22
|
+
? out.estimated_output_tokens / (out.estimated_duration_ms / 1000) : null;
|
|
23
|
+
out.first_response_ms = out.first_response_sample_count > 0
|
|
24
|
+
? out.first_response_total_ms / out.first_response_sample_count : null;
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function mergePerformance(...values) {
|
|
29
|
+
const out = normalizePerformance();
|
|
30
|
+
for (const value of values) {
|
|
31
|
+
for (const field of SUM_FIELDS) out[field] += nonNegative(value?.[field]);
|
|
32
|
+
}
|
|
33
|
+
return normalizePerformance(out);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function timestampMs(value) {
|
|
37
|
+
if (typeof value !== "string") return null;
|
|
38
|
+
const number = Date.parse(value);
|
|
39
|
+
return Number.isFinite(number) ? number : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function accumulator() {
|
|
43
|
+
let total = normalizePerformance();
|
|
44
|
+
const byModel = new Map();
|
|
45
|
+
function add(model, delta) {
|
|
46
|
+
total = mergePerformance(total, delta);
|
|
47
|
+
if (model) byModel.set(model, mergePerformance(byModel.get(model), delta));
|
|
48
|
+
}
|
|
49
|
+
function request(model, outputTokens, start, end) {
|
|
50
|
+
const duration = start !== null && end !== null ? end - start : 0;
|
|
51
|
+
record(model, outputTokens, duration);
|
|
52
|
+
}
|
|
53
|
+
function requestDuration(model, outputTokens, durationMs) {
|
|
54
|
+
record(model, outputTokens, durationMs);
|
|
55
|
+
}
|
|
56
|
+
function record(model, outputTokens, duration) {
|
|
57
|
+
if (outputTokens < MIN_OUTPUT || duration < MIN_DURATION_MS || duration > MAX_DURATION_MS) return;
|
|
58
|
+
add(model, { estimated_output_tokens: outputTokens, estimated_duration_ms: duration, estimated_request_count: 1 });
|
|
59
|
+
}
|
|
60
|
+
return { add, request, requestDuration, finish: () => ({ performance: total, byModel }) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function createClaudePerformanceCollector() {
|
|
64
|
+
const nodes = new Map();
|
|
65
|
+
const messages = new Map();
|
|
66
|
+
const totals = accumulator();
|
|
67
|
+
|
|
68
|
+
function consume(obj, key, countedUsage, model) {
|
|
69
|
+
const timestamp = timestampMs(obj?.timestamp || obj?.message?.timestamp);
|
|
70
|
+
if (typeof obj?.uuid === "string" && !nodes.has(obj.uuid)) {
|
|
71
|
+
nodes.set(obj.uuid, {
|
|
72
|
+
parent: obj.parentUuid, timestamp, type: obj.type,
|
|
73
|
+
invalid: Boolean(obj.isApiErrorMessage || obj.isMeta),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (obj?.type !== "assistant" || !key) return;
|
|
77
|
+
let timing = messages.get(key);
|
|
78
|
+
if (!timing) {
|
|
79
|
+
// Tokens come from scanClaudeSession's canonical, once-counted delta.
|
|
80
|
+
// Later content blocks extend only the timing, never the numerator.
|
|
81
|
+
timing = {
|
|
82
|
+
parent: obj.parentUuid, startsAtFirstBlock: obj.apiBlockIndex == null || obj.apiBlockIndex === 0,
|
|
83
|
+
end: null, complete: false, invalid: false, model,
|
|
84
|
+
output: nonNegative(countedUsage?.output_tokens) + nonNegative(countedUsage?.reasoning_output_tokens),
|
|
85
|
+
};
|
|
86
|
+
messages.set(key, timing);
|
|
87
|
+
}
|
|
88
|
+
if (timestamp !== null) timing.end = timing.end === null ? timestamp : Math.max(timing.end, timestamp);
|
|
89
|
+
timing.complete ||= ["end_turn", "tool_use", "max_tokens", "stop_sequence"].includes(obj.message?.stop_reason);
|
|
90
|
+
timing.invalid ||= Boolean(obj.isApiErrorMessage || obj.message?.error);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function finish() {
|
|
94
|
+
for (const timing of messages.values()) {
|
|
95
|
+
if (!timing.complete || timing.invalid || !timing.startsAtFirstBlock) continue;
|
|
96
|
+
let parent = timing.parent;
|
|
97
|
+
for (let hop = 0; hop < 32 && parent; hop += 1) {
|
|
98
|
+
const node = nodes.get(parent);
|
|
99
|
+
if (!node) break;
|
|
100
|
+
if (node.type === "attachment") { parent = node.parent; continue; }
|
|
101
|
+
// A user row includes tool results. Their completion timestamp keeps
|
|
102
|
+
// time spent executing the tool outside the next inference window.
|
|
103
|
+
if (node.type === "user" && !node.invalid) totals.request(timing.model, timing.output, node.timestamp, timing.end);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return totals.finish();
|
|
108
|
+
}
|
|
109
|
+
return { consume, finish };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function createCodexPerformanceCollector() {
|
|
113
|
+
const totals = accumulator();
|
|
114
|
+
let boundary = null;
|
|
115
|
+
let lastTokenCount = null;
|
|
116
|
+
let start = null;
|
|
117
|
+
let lastOutput = null;
|
|
118
|
+
let toolAfterOutput = null;
|
|
119
|
+
let usageRecord = null;
|
|
120
|
+
let invalid = false;
|
|
121
|
+
let turnKey = null;
|
|
122
|
+
let turnModels = new Set();
|
|
123
|
+
const seenFirstResponses = new Set();
|
|
124
|
+
|
|
125
|
+
function reset() {
|
|
126
|
+
start = null;
|
|
127
|
+
lastOutput = null;
|
|
128
|
+
toolAfterOutput = null;
|
|
129
|
+
usageRecord = null;
|
|
130
|
+
invalid = false;
|
|
131
|
+
}
|
|
132
|
+
function noteBoundary(ms) {
|
|
133
|
+
if (ms !== null) boundary = boundary === null ? ms : Math.max(boundary, ms);
|
|
134
|
+
}
|
|
135
|
+
function consume(obj, model) {
|
|
136
|
+
const ms = timestampMs(obj?.timestamp);
|
|
137
|
+
const payload = obj?.payload || {};
|
|
138
|
+
if (obj?.type === "compacted") {
|
|
139
|
+
reset();
|
|
140
|
+
noteBoundary(ms);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (obj?.type === "event_msg" && ["task_started", "turn_aborted"].includes(payload.type)) {
|
|
144
|
+
reset();
|
|
145
|
+
noteBoundary(ms);
|
|
146
|
+
turnModels = new Set();
|
|
147
|
+
turnKey = payload.turn_id || null;
|
|
148
|
+
if (payload.type === "turn_aborted") invalid = true;
|
|
149
|
+
}
|
|
150
|
+
if (obj?.type === "turn_context") {
|
|
151
|
+
const nextKey = payload.turn_id || null;
|
|
152
|
+
if (turnKey && nextKey && turnKey !== nextKey) { reset(); turnModels = new Set(); }
|
|
153
|
+
turnKey = nextKey || turnKey;
|
|
154
|
+
noteBoundary(ms);
|
|
155
|
+
}
|
|
156
|
+
const responseType = String(payload.type || "");
|
|
157
|
+
const isModelOutput = obj?.type === "response_item" && (responseType === "reasoning"
|
|
158
|
+
|| responseType === "agent_message" || responseType.endsWith("_call")
|
|
159
|
+
|| (responseType === "message" && payload.role === "assistant"));
|
|
160
|
+
if (model && (obj?.type === "turn_context" || isModelOutput
|
|
161
|
+
|| payload.type === "model_rerouted")) turnModels.add(model);
|
|
162
|
+
if (obj?.type === "event_msg" && ["error", "api_error"].includes(payload.type)) invalid = true;
|
|
163
|
+
if (obj?.type === "event_msg" && payload.type === "task_complete") {
|
|
164
|
+
const raw = payload.time_to_first_token_ms;
|
|
165
|
+
const key = payload.turn_id || turnKey || obj.timestamp;
|
|
166
|
+
if (!invalid && typeof raw === "number" && Number.isFinite(raw) && raw >= 0 && !seenFirstResponses.has(key)) {
|
|
167
|
+
seenFirstResponses.add(key);
|
|
168
|
+
totals.add(turnModels.size === 1 ? [...turnModels][0] : null, { first_response_total_ms: raw, first_response_sample_count: 1 });
|
|
169
|
+
}
|
|
170
|
+
reset();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (obj?.type === "token_usage_record") {
|
|
174
|
+
usageRecord = { end: ms, usage: canonicalUsage(payload.usage) };
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (obj?.type !== "response_item") return;
|
|
178
|
+
const type = String(payload.type || "");
|
|
179
|
+
if (type.endsWith("_output")) {
|
|
180
|
+
noteBoundary(ms);
|
|
181
|
+
if (lastOutput !== null) toolAfterOutput = ms;
|
|
182
|
+
} else if ((type === "message" && payload.role !== "assistant") || type === "user_message") {
|
|
183
|
+
noteBoundary(ms);
|
|
184
|
+
} else if (type === "reasoning" || type === "agent_message" || type.endsWith("_call") || (type === "message" && payload.role === "assistant")) {
|
|
185
|
+
// Old rollouts flush an output item just after its token_count.
|
|
186
|
+
if (ms === null || (start === null && lastTokenCount !== null && ms >= lastTokenCount && ms - lastTokenCount <= SAME_FLUSH_MS)) return;
|
|
187
|
+
if (start === null) start = boundary;
|
|
188
|
+
lastOutput = ms;
|
|
189
|
+
toolAfterOutput = null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function consumeUsage({ timestamp, delta, rawUsage, model }) {
|
|
193
|
+
// A repeated cumulative snapshot can be written while the next response
|
|
194
|
+
// is still generating. It is not a completion boundary for that response.
|
|
195
|
+
if (!delta || delta.total_tokens <= 0) return;
|
|
196
|
+
const ms = timestampMs(timestamp);
|
|
197
|
+
const raw = canonicalUsage(rawUsage);
|
|
198
|
+
const matches = raw && usageRecord?.usage && ["input_tokens", "cached_input_tokens", "output_tokens"]
|
|
199
|
+
.every((field) => raw[field] === usageRecord.usage[field]);
|
|
200
|
+
const waitedForTools = toolAfterOutput !== null && ms !== null && ms >= toolAfterOutput;
|
|
201
|
+
// Once a tool result follows the final output marker, the token_count's
|
|
202
|
+
// timestamp contains execution time. The output marker is the available
|
|
203
|
+
// response boundary even if the count was flushed much later.
|
|
204
|
+
const end = matches ? usageRecord.end : waitedForTools ? lastOutput : ms;
|
|
205
|
+
// Missing output markers cannot distinguish response timing from a
|
|
206
|
+
// snapshot or inherited context, so even a positive token delta is skipped.
|
|
207
|
+
if (!invalid && lastOutput !== null) totals.request(model, nonNegative(delta.output_tokens), start, end);
|
|
208
|
+
noteBoundary(ms);
|
|
209
|
+
lastTokenCount = ms;
|
|
210
|
+
reset();
|
|
211
|
+
}
|
|
212
|
+
return { consume, consumeUsage, finish: totals.finish };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ZCode's CLI DB stores per-request wall-clock counters directly in its
|
|
216
|
+
// model_usage table (duration_ms, time_to_first_token_ms) instead of an event
|
|
217
|
+
// stream, so this collector is a thin adapter: each completed request becomes
|
|
218
|
+
// one duration-bounded sample, never a token total (those come from the
|
|
219
|
+
// shared usage reader so the browser cannot drift from the usage card).
|
|
220
|
+
function createZcodePerformanceCollector() {
|
|
221
|
+
const totals = accumulator();
|
|
222
|
+
function consumeUsage({ model, outputTokens, durationMs, timeToFirstTokenMs } = {}) {
|
|
223
|
+
totals.requestDuration(model, nonNegative(outputTokens), nonNegative(durationMs));
|
|
224
|
+
if (typeof timeToFirstTokenMs === "number" && Number.isFinite(timeToFirstTokenMs) && timeToFirstTokenMs >= 0) {
|
|
225
|
+
totals.add(null, { first_response_total_ms: timeToFirstTokenMs, first_response_sample_count: 1 });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return { consumeUsage, finish: totals.finish };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = {
|
|
232
|
+
normalizePerformance,
|
|
233
|
+
mergePerformance,
|
|
234
|
+
createClaudePerformanceCollector,
|
|
235
|
+
createCodexPerformanceCollector,
|
|
236
|
+
createZcodePerformanceCollector,
|
|
237
|
+
};
|