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,128 +1,128 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// Codex token_count events do not carry a model id. Every usage event is
|
|
4
|
-
// therefore attributed to the model named by the most recent turn_context,
|
|
5
|
-
// which Codex emits at the start of each turn and which tracks mid-session
|
|
6
|
-
// model switches exactly. That per-turn path is what actually runs today.
|
|
7
|
-
//
|
|
8
|
-
// The model/rerouted upgrade below is forward-looking: it promotes usage to
|
|
9
|
-
// the effective model when the app server reports a server-side reroute.
|
|
10
|
-
//
|
|
11
|
-
// Deliberately NOT used for attribution: event_msg/thread_settings_applied.
|
|
12
|
-
// It carries thread_settings.model, but it fires when the user picks a model
|
|
13
|
-
// in the UI - seconds to minutes before the switch takes effect - so usage
|
|
14
|
-
// still streaming from the in-flight turn belongs to the previous model.
|
|
15
|
-
// turn_context is the only signal that marks where a turn actually begins.
|
|
16
|
-
//
|
|
17
|
-
// session_meta is a defensive fallback, not an observed source. Codex has
|
|
18
|
-
// never written a model into it - 0 of 10310 session_meta rows across 5849
|
|
19
|
-
// local rollouts carry the field (codex-cli 0.151.0); the payload records
|
|
20
|
-
// model_provider ("openai"), which is provenance rather than a model. The
|
|
21
|
-
// fallback only fills selectedModel when nothing else has, so a future format
|
|
22
|
-
// that does carry one attributes instead of falling through to "unknown". It
|
|
23
|
-
// never overrides a turn_context model.
|
|
24
|
-
|
|
25
|
-
function cleanString(value) {
|
|
26
|
-
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function normalizeEventName(value) {
|
|
30
|
-
return cleanString(value)?.toLowerCase().replaceAll("_", "/") || "";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function eventEnvelope(obj) {
|
|
34
|
-
const candidates = [
|
|
35
|
-
{ name: obj?.method, data: obj?.params },
|
|
36
|
-
{ name: obj?.type, data: obj?.payload },
|
|
37
|
-
{ name: obj?.payload?.method, data: obj?.payload?.params },
|
|
38
|
-
{ name: obj?.payload?.type, data: obj?.payload },
|
|
39
|
-
{ name: obj?.payload?.msg?.method, data: obj?.payload?.msg?.params },
|
|
40
|
-
{ name: obj?.payload?.msg?.type, data: obj?.payload?.msg },
|
|
41
|
-
{ name: obj?.msg?.method, data: obj?.msg?.params },
|
|
42
|
-
{ name: obj?.msg?.type, data: obj?.msg },
|
|
43
|
-
];
|
|
44
|
-
return candidates.find(({ name }) => normalizeEventName(name) === "model/rerouted") || null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Forward-looking: no model/rerouted event has been observed in any real
|
|
48
|
-
// rollout (verified against 5832 local files, codex-cli 0.151.0), so the
|
|
49
|
-
// envelope shapes and the fromModel/toModel field names below are inferred
|
|
50
|
-
// from the app-server protocol rather than confirmed against logged data.
|
|
51
|
-
// Re-verify against a real sample before relying on rerouted attribution.
|
|
52
|
-
function extractModelReroute(obj) {
|
|
53
|
-
const envelope = eventEnvelope(obj);
|
|
54
|
-
if (!envelope) return null;
|
|
55
|
-
const data = envelope.data && typeof envelope.data === "object" ? envelope.data : {};
|
|
56
|
-
const fromModel = cleanString(data.fromModel ?? data.from_model);
|
|
57
|
-
const toModel = cleanString(data.toModel ?? data.to_model);
|
|
58
|
-
if (!toModel) return null;
|
|
59
|
-
return {
|
|
60
|
-
fromModel,
|
|
61
|
-
toModel,
|
|
62
|
-
reason: cleanString(data.reason),
|
|
63
|
-
threadId: cleanString(data.threadId ?? data.thread_id),
|
|
64
|
-
turnId: cleanString(data.turnId ?? data.turn_id),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function createCodexModelAttributionState(value = {}) {
|
|
69
|
-
const selectedModel = cleanString(value.selectedModel ?? value.selected_model ?? value.model);
|
|
70
|
-
const effectiveModel = cleanString(value.effectiveModel ?? value.effective_model) || selectedModel;
|
|
71
|
-
return {
|
|
72
|
-
selectedModel,
|
|
73
|
-
effectiveModel,
|
|
74
|
-
turnId: cleanString(value.turnId ?? value.turn_id),
|
|
75
|
-
rerouted: Boolean(value.rerouted),
|
|
76
|
-
rerouteReason: cleanString(value.rerouteReason ?? value.reroute_reason),
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function applyCodexModelEvent(state, obj) {
|
|
81
|
-
if (!state || typeof state !== "object") return null;
|
|
82
|
-
if (obj?.type === "turn_context" && obj.payload && typeof obj.payload === "object") {
|
|
83
|
-
const selectedModel = cleanString(obj.payload.model);
|
|
84
|
-
if (selectedModel) {
|
|
85
|
-
state.selectedModel = selectedModel;
|
|
86
|
-
}
|
|
87
|
-
state.effectiveModel = state.selectedModel || state.effectiveModel;
|
|
88
|
-
state.turnId = cleanString(obj.payload.turn_id ?? obj.payload.turnId) || state.turnId;
|
|
89
|
-
state.rerouted = false;
|
|
90
|
-
state.rerouteReason = null;
|
|
91
|
-
return { type: "turn_context", selectedModel };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Strictly weaker than turn_context, and never allowed to overwrite it: a
|
|
95
|
-
// forked rollout replays the parent's session_meta rows after its own.
|
|
96
|
-
if (obj?.type === "session_meta" && obj.payload && typeof obj.payload === "object") {
|
|
97
|
-
const metaModel = cleanString(obj.payload.model);
|
|
98
|
-
if (!metaModel || state.selectedModel) return null;
|
|
99
|
-
state.selectedModel = metaModel;
|
|
100
|
-
state.effectiveModel = state.effectiveModel || metaModel;
|
|
101
|
-
return { type: "session_meta", selectedModel: metaModel };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const reroute = extractModelReroute(obj);
|
|
105
|
-
if (!reroute) return null;
|
|
106
|
-
state.selectedModel = reroute.fromModel || state.selectedModel || state.effectiveModel;
|
|
107
|
-
state.effectiveModel = reroute.toModel;
|
|
108
|
-
state.turnId = reroute.turnId || state.turnId;
|
|
109
|
-
state.rerouted = true;
|
|
110
|
-
state.rerouteReason = reroute.reason;
|
|
111
|
-
return { type: "model_rerouted", ...reroute };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function currentCodexModel(state) {
|
|
115
|
-
return cleanString(state?.effectiveModel) || cleanString(state?.selectedModel);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function snapshotCodexModelAttributionState(state) {
|
|
119
|
-
return createCodexModelAttributionState(state);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
module.exports = {
|
|
123
|
-
applyCodexModelEvent,
|
|
124
|
-
createCodexModelAttributionState,
|
|
125
|
-
currentCodexModel,
|
|
126
|
-
extractModelReroute,
|
|
127
|
-
snapshotCodexModelAttributionState,
|
|
128
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Codex token_count events do not carry a model id. Every usage event is
|
|
4
|
+
// therefore attributed to the model named by the most recent turn_context,
|
|
5
|
+
// which Codex emits at the start of each turn and which tracks mid-session
|
|
6
|
+
// model switches exactly. That per-turn path is what actually runs today.
|
|
7
|
+
//
|
|
8
|
+
// The model/rerouted upgrade below is forward-looking: it promotes usage to
|
|
9
|
+
// the effective model when the app server reports a server-side reroute.
|
|
10
|
+
//
|
|
11
|
+
// Deliberately NOT used for attribution: event_msg/thread_settings_applied.
|
|
12
|
+
// It carries thread_settings.model, but it fires when the user picks a model
|
|
13
|
+
// in the UI - seconds to minutes before the switch takes effect - so usage
|
|
14
|
+
// still streaming from the in-flight turn belongs to the previous model.
|
|
15
|
+
// turn_context is the only signal that marks where a turn actually begins.
|
|
16
|
+
//
|
|
17
|
+
// session_meta is a defensive fallback, not an observed source. Codex has
|
|
18
|
+
// never written a model into it - 0 of 10310 session_meta rows across 5849
|
|
19
|
+
// local rollouts carry the field (codex-cli 0.151.0); the payload records
|
|
20
|
+
// model_provider ("openai"), which is provenance rather than a model. The
|
|
21
|
+
// fallback only fills selectedModel when nothing else has, so a future format
|
|
22
|
+
// that does carry one attributes instead of falling through to "unknown". It
|
|
23
|
+
// never overrides a turn_context model.
|
|
24
|
+
|
|
25
|
+
function cleanString(value) {
|
|
26
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function normalizeEventName(value) {
|
|
30
|
+
return cleanString(value)?.toLowerCase().replaceAll("_", "/") || "";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function eventEnvelope(obj) {
|
|
34
|
+
const candidates = [
|
|
35
|
+
{ name: obj?.method, data: obj?.params },
|
|
36
|
+
{ name: obj?.type, data: obj?.payload },
|
|
37
|
+
{ name: obj?.payload?.method, data: obj?.payload?.params },
|
|
38
|
+
{ name: obj?.payload?.type, data: obj?.payload },
|
|
39
|
+
{ name: obj?.payload?.msg?.method, data: obj?.payload?.msg?.params },
|
|
40
|
+
{ name: obj?.payload?.msg?.type, data: obj?.payload?.msg },
|
|
41
|
+
{ name: obj?.msg?.method, data: obj?.msg?.params },
|
|
42
|
+
{ name: obj?.msg?.type, data: obj?.msg },
|
|
43
|
+
];
|
|
44
|
+
return candidates.find(({ name }) => normalizeEventName(name) === "model/rerouted") || null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Forward-looking: no model/rerouted event has been observed in any real
|
|
48
|
+
// rollout (verified against 5832 local files, codex-cli 0.151.0), so the
|
|
49
|
+
// envelope shapes and the fromModel/toModel field names below are inferred
|
|
50
|
+
// from the app-server protocol rather than confirmed against logged data.
|
|
51
|
+
// Re-verify against a real sample before relying on rerouted attribution.
|
|
52
|
+
function extractModelReroute(obj) {
|
|
53
|
+
const envelope = eventEnvelope(obj);
|
|
54
|
+
if (!envelope) return null;
|
|
55
|
+
const data = envelope.data && typeof envelope.data === "object" ? envelope.data : {};
|
|
56
|
+
const fromModel = cleanString(data.fromModel ?? data.from_model);
|
|
57
|
+
const toModel = cleanString(data.toModel ?? data.to_model);
|
|
58
|
+
if (!toModel) return null;
|
|
59
|
+
return {
|
|
60
|
+
fromModel,
|
|
61
|
+
toModel,
|
|
62
|
+
reason: cleanString(data.reason),
|
|
63
|
+
threadId: cleanString(data.threadId ?? data.thread_id),
|
|
64
|
+
turnId: cleanString(data.turnId ?? data.turn_id),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function createCodexModelAttributionState(value = {}) {
|
|
69
|
+
const selectedModel = cleanString(value.selectedModel ?? value.selected_model ?? value.model);
|
|
70
|
+
const effectiveModel = cleanString(value.effectiveModel ?? value.effective_model) || selectedModel;
|
|
71
|
+
return {
|
|
72
|
+
selectedModel,
|
|
73
|
+
effectiveModel,
|
|
74
|
+
turnId: cleanString(value.turnId ?? value.turn_id),
|
|
75
|
+
rerouted: Boolean(value.rerouted),
|
|
76
|
+
rerouteReason: cleanString(value.rerouteReason ?? value.reroute_reason),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function applyCodexModelEvent(state, obj) {
|
|
81
|
+
if (!state || typeof state !== "object") return null;
|
|
82
|
+
if (obj?.type === "turn_context" && obj.payload && typeof obj.payload === "object") {
|
|
83
|
+
const selectedModel = cleanString(obj.payload.model);
|
|
84
|
+
if (selectedModel) {
|
|
85
|
+
state.selectedModel = selectedModel;
|
|
86
|
+
}
|
|
87
|
+
state.effectiveModel = state.selectedModel || state.effectiveModel;
|
|
88
|
+
state.turnId = cleanString(obj.payload.turn_id ?? obj.payload.turnId) || state.turnId;
|
|
89
|
+
state.rerouted = false;
|
|
90
|
+
state.rerouteReason = null;
|
|
91
|
+
return { type: "turn_context", selectedModel };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Strictly weaker than turn_context, and never allowed to overwrite it: a
|
|
95
|
+
// forked rollout replays the parent's session_meta rows after its own.
|
|
96
|
+
if (obj?.type === "session_meta" && obj.payload && typeof obj.payload === "object") {
|
|
97
|
+
const metaModel = cleanString(obj.payload.model);
|
|
98
|
+
if (!metaModel || state.selectedModel) return null;
|
|
99
|
+
state.selectedModel = metaModel;
|
|
100
|
+
state.effectiveModel = state.effectiveModel || metaModel;
|
|
101
|
+
return { type: "session_meta", selectedModel: metaModel };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const reroute = extractModelReroute(obj);
|
|
105
|
+
if (!reroute) return null;
|
|
106
|
+
state.selectedModel = reroute.fromModel || state.selectedModel || state.effectiveModel;
|
|
107
|
+
state.effectiveModel = reroute.toModel;
|
|
108
|
+
state.turnId = reroute.turnId || state.turnId;
|
|
109
|
+
state.rerouted = true;
|
|
110
|
+
state.rerouteReason = reroute.reason;
|
|
111
|
+
return { type: "model_rerouted", ...reroute };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function currentCodexModel(state) {
|
|
115
|
+
return cleanString(state?.effectiveModel) || cleanString(state?.selectedModel);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function snapshotCodexModelAttributionState(state) {
|
|
119
|
+
return createCodexModelAttributionState(state);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = {
|
|
123
|
+
applyCodexModelEvent,
|
|
124
|
+
createCodexModelAttributionState,
|
|
125
|
+
currentCodexModel,
|
|
126
|
+
extractModelReroute,
|
|
127
|
+
snapshotCodexModelAttributionState,
|
|
128
|
+
};
|