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,163 +1,163 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const net = require("node:net");
|
|
4
|
-
|
|
5
|
-
const PROXY_MODES = new Set(["system", "manual", "off"]);
|
|
6
|
-
const PROXY_PROTOCOLS = new Set(["http", "https", "socks5"]);
|
|
7
|
-
|
|
8
|
-
function defaultProxyConfig() {
|
|
9
|
-
return {
|
|
10
|
-
mode: "system",
|
|
11
|
-
protocol: "http",
|
|
12
|
-
host: "",
|
|
13
|
-
port: 0,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function normalizeMode(value) {
|
|
18
|
-
if (typeof value !== "string") return null;
|
|
19
|
-
const mode = value.trim().toLowerCase();
|
|
20
|
-
return PROXY_MODES.has(mode) ? mode : null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function normalizeProtocol(value) {
|
|
24
|
-
if (typeof value !== "string") return null;
|
|
25
|
-
const protocol = value.trim().toLowerCase();
|
|
26
|
-
if (protocol === "socks") return "socks5";
|
|
27
|
-
return PROXY_PROTOCOLS.has(protocol) ? protocol : null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function normalizeHost(value) {
|
|
31
|
-
if (typeof value !== "string") return null;
|
|
32
|
-
const host = value.trim();
|
|
33
|
-
if (!host) return null;
|
|
34
|
-
if (/:\/\//.test(host) || host.includes("/")) return null;
|
|
35
|
-
// Reject credentials, query/hash fragments, Windows-style paths, and
|
|
36
|
-
// internal whitespace so a host cannot reshape the assembled proxy URL.
|
|
37
|
-
if (/[@?#\\]/.test(host) || /\s/.test(host)) return null;
|
|
38
|
-
return host;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function normalizePort(value) {
|
|
42
|
-
if (typeof value === "string") {
|
|
43
|
-
const trimmed = value.trim();
|
|
44
|
-
if (!trimmed || !/^\d+$/.test(trimmed)) return null;
|
|
45
|
-
value = Number(trimmed);
|
|
46
|
-
}
|
|
47
|
-
if (typeof value !== "number" || !Number.isInteger(value)) return null;
|
|
48
|
-
if (value < 1 || value > 65535) return null;
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function fallbackSystem(reason) {
|
|
53
|
-
return { ...defaultProxyConfig(), reason };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Normalize a persisted or inbound proxy object.
|
|
58
|
-
* Invalid values fall back to mode=system and never throw.
|
|
59
|
-
*/
|
|
60
|
-
function normalizeProxyConfig(raw) {
|
|
61
|
-
if (raw == null) return defaultProxyConfig();
|
|
62
|
-
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
63
|
-
return fallbackSystem("proxy config must be an object");
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const mode = raw.mode == null || raw.mode === ""
|
|
67
|
-
? "system"
|
|
68
|
-
: normalizeMode(raw.mode);
|
|
69
|
-
if (!mode) return fallbackSystem("invalid mode");
|
|
70
|
-
|
|
71
|
-
const protocol = normalizeProtocol(raw.protocol) || "http";
|
|
72
|
-
const host = typeof raw.host === "string" ? raw.host.trim() : "";
|
|
73
|
-
const port = normalizePort(raw.port) ?? 0;
|
|
74
|
-
|
|
75
|
-
if (mode !== "manual") {
|
|
76
|
-
return { mode, protocol, host, port };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const manualProtocol = normalizeProtocol(raw.protocol);
|
|
80
|
-
if (!manualProtocol) return fallbackSystem("invalid protocol");
|
|
81
|
-
const manualHost = normalizeHost(raw.host);
|
|
82
|
-
if (!manualHost) return fallbackSystem("invalid host");
|
|
83
|
-
const manualPort = normalizePort(raw.port);
|
|
84
|
-
if (manualPort == null) return fallbackSystem("invalid port");
|
|
85
|
-
return {
|
|
86
|
-
mode: "manual",
|
|
87
|
-
protocol: manualProtocol,
|
|
88
|
-
host: manualHost,
|
|
89
|
-
port: manualPort,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Strict payload check for POST /proxy-config.
|
|
95
|
-
* Rejects illegal values instead of coercing them.
|
|
96
|
-
*/
|
|
97
|
-
function parseProxyPayload(raw) {
|
|
98
|
-
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
99
|
-
return { ok: false, error: "proxy payload must be an object" };
|
|
100
|
-
}
|
|
101
|
-
const mode = normalizeMode(raw.mode);
|
|
102
|
-
if (!mode) {
|
|
103
|
-
return { ok: false, error: "mode must be system, manual, or off" };
|
|
104
|
-
}
|
|
105
|
-
if (mode !== "manual") {
|
|
106
|
-
return {
|
|
107
|
-
ok: true,
|
|
108
|
-
value: {
|
|
109
|
-
mode,
|
|
110
|
-
protocol: normalizeProtocol(raw.protocol) || "http",
|
|
111
|
-
host: typeof raw.host === "string" ? raw.host.trim() : "",
|
|
112
|
-
port: normalizePort(raw.port) ?? 0,
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
const protocol = normalizeProtocol(raw.protocol);
|
|
117
|
-
if (!protocol) {
|
|
118
|
-
return { ok: false, error: "protocol must be http, https, or socks5" };
|
|
119
|
-
}
|
|
120
|
-
const host = normalizeHost(raw.host);
|
|
121
|
-
if (!host) {
|
|
122
|
-
return { ok: false, error: "host must be a hostname or IP without a protocol prefix" };
|
|
123
|
-
}
|
|
124
|
-
const port = normalizePort(raw.port);
|
|
125
|
-
if (port == null) {
|
|
126
|
-
return { ok: false, error: "port must be an integer from 1 to 65535" };
|
|
127
|
-
}
|
|
128
|
-
return { ok: true, value: { mode, protocol, host, port } };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function bracketProxyHost(host) {
|
|
132
|
-
if (net.isIPv6(host)) return `[${host}]`;
|
|
133
|
-
if (host.startsWith("[") && host.endsWith("]")) {
|
|
134
|
-
const inner = host.slice(1, -1);
|
|
135
|
-
if (net.isIPv6(inner)) return `[${inner}]`;
|
|
136
|
-
}
|
|
137
|
-
// Bare or malformed hosts that merely contain a colon are not IPv6.
|
|
138
|
-
if (host.includes(":")) return null;
|
|
139
|
-
return host;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function buildProxyUrl(normalized) {
|
|
143
|
-
if (!normalized || normalized.mode !== "manual") return null;
|
|
144
|
-
const protocol = normalizeProtocol(normalized.protocol);
|
|
145
|
-
const host = normalizeHost(normalized.host);
|
|
146
|
-
const port = normalizePort(normalized.port);
|
|
147
|
-
if (!protocol || !host || port == null) return null;
|
|
148
|
-
const hostPart = bracketProxyHost(host);
|
|
149
|
-
if (!hostPart) return null;
|
|
150
|
-
return `${protocol}://${hostPart}:${port}`;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
module.exports = {
|
|
154
|
-
PROXY_MODES,
|
|
155
|
-
PROXY_PROTOCOLS,
|
|
156
|
-
defaultProxyConfig,
|
|
157
|
-
normalizeProxyConfig,
|
|
158
|
-
parseProxyPayload,
|
|
159
|
-
buildProxyUrl,
|
|
160
|
-
normalizeProtocol,
|
|
161
|
-
normalizeHost,
|
|
162
|
-
normalizePort,
|
|
163
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const net = require("node:net");
|
|
4
|
+
|
|
5
|
+
const PROXY_MODES = new Set(["system", "manual", "off"]);
|
|
6
|
+
const PROXY_PROTOCOLS = new Set(["http", "https", "socks5"]);
|
|
7
|
+
|
|
8
|
+
function defaultProxyConfig() {
|
|
9
|
+
return {
|
|
10
|
+
mode: "system",
|
|
11
|
+
protocol: "http",
|
|
12
|
+
host: "",
|
|
13
|
+
port: 0,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizeMode(value) {
|
|
18
|
+
if (typeof value !== "string") return null;
|
|
19
|
+
const mode = value.trim().toLowerCase();
|
|
20
|
+
return PROXY_MODES.has(mode) ? mode : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function normalizeProtocol(value) {
|
|
24
|
+
if (typeof value !== "string") return null;
|
|
25
|
+
const protocol = value.trim().toLowerCase();
|
|
26
|
+
if (protocol === "socks") return "socks5";
|
|
27
|
+
return PROXY_PROTOCOLS.has(protocol) ? protocol : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function normalizeHost(value) {
|
|
31
|
+
if (typeof value !== "string") return null;
|
|
32
|
+
const host = value.trim();
|
|
33
|
+
if (!host) return null;
|
|
34
|
+
if (/:\/\//.test(host) || host.includes("/")) return null;
|
|
35
|
+
// Reject credentials, query/hash fragments, Windows-style paths, and
|
|
36
|
+
// internal whitespace so a host cannot reshape the assembled proxy URL.
|
|
37
|
+
if (/[@?#\\]/.test(host) || /\s/.test(host)) return null;
|
|
38
|
+
return host;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function normalizePort(value) {
|
|
42
|
+
if (typeof value === "string") {
|
|
43
|
+
const trimmed = value.trim();
|
|
44
|
+
if (!trimmed || !/^\d+$/.test(trimmed)) return null;
|
|
45
|
+
value = Number(trimmed);
|
|
46
|
+
}
|
|
47
|
+
if (typeof value !== "number" || !Number.isInteger(value)) return null;
|
|
48
|
+
if (value < 1 || value > 65535) return null;
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function fallbackSystem(reason) {
|
|
53
|
+
return { ...defaultProxyConfig(), reason };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Normalize a persisted or inbound proxy object.
|
|
58
|
+
* Invalid values fall back to mode=system and never throw.
|
|
59
|
+
*/
|
|
60
|
+
function normalizeProxyConfig(raw) {
|
|
61
|
+
if (raw == null) return defaultProxyConfig();
|
|
62
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
63
|
+
return fallbackSystem("proxy config must be an object");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const mode = raw.mode == null || raw.mode === ""
|
|
67
|
+
? "system"
|
|
68
|
+
: normalizeMode(raw.mode);
|
|
69
|
+
if (!mode) return fallbackSystem("invalid mode");
|
|
70
|
+
|
|
71
|
+
const protocol = normalizeProtocol(raw.protocol) || "http";
|
|
72
|
+
const host = typeof raw.host === "string" ? raw.host.trim() : "";
|
|
73
|
+
const port = normalizePort(raw.port) ?? 0;
|
|
74
|
+
|
|
75
|
+
if (mode !== "manual") {
|
|
76
|
+
return { mode, protocol, host, port };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const manualProtocol = normalizeProtocol(raw.protocol);
|
|
80
|
+
if (!manualProtocol) return fallbackSystem("invalid protocol");
|
|
81
|
+
const manualHost = normalizeHost(raw.host);
|
|
82
|
+
if (!manualHost) return fallbackSystem("invalid host");
|
|
83
|
+
const manualPort = normalizePort(raw.port);
|
|
84
|
+
if (manualPort == null) return fallbackSystem("invalid port");
|
|
85
|
+
return {
|
|
86
|
+
mode: "manual",
|
|
87
|
+
protocol: manualProtocol,
|
|
88
|
+
host: manualHost,
|
|
89
|
+
port: manualPort,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Strict payload check for POST /proxy-config.
|
|
95
|
+
* Rejects illegal values instead of coercing them.
|
|
96
|
+
*/
|
|
97
|
+
function parseProxyPayload(raw) {
|
|
98
|
+
if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
99
|
+
return { ok: false, error: "proxy payload must be an object" };
|
|
100
|
+
}
|
|
101
|
+
const mode = normalizeMode(raw.mode);
|
|
102
|
+
if (!mode) {
|
|
103
|
+
return { ok: false, error: "mode must be system, manual, or off" };
|
|
104
|
+
}
|
|
105
|
+
if (mode !== "manual") {
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
value: {
|
|
109
|
+
mode,
|
|
110
|
+
protocol: normalizeProtocol(raw.protocol) || "http",
|
|
111
|
+
host: typeof raw.host === "string" ? raw.host.trim() : "",
|
|
112
|
+
port: normalizePort(raw.port) ?? 0,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const protocol = normalizeProtocol(raw.protocol);
|
|
117
|
+
if (!protocol) {
|
|
118
|
+
return { ok: false, error: "protocol must be http, https, or socks5" };
|
|
119
|
+
}
|
|
120
|
+
const host = normalizeHost(raw.host);
|
|
121
|
+
if (!host) {
|
|
122
|
+
return { ok: false, error: "host must be a hostname or IP without a protocol prefix" };
|
|
123
|
+
}
|
|
124
|
+
const port = normalizePort(raw.port);
|
|
125
|
+
if (port == null) {
|
|
126
|
+
return { ok: false, error: "port must be an integer from 1 to 65535" };
|
|
127
|
+
}
|
|
128
|
+
return { ok: true, value: { mode, protocol, host, port } };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function bracketProxyHost(host) {
|
|
132
|
+
if (net.isIPv6(host)) return `[${host}]`;
|
|
133
|
+
if (host.startsWith("[") && host.endsWith("]")) {
|
|
134
|
+
const inner = host.slice(1, -1);
|
|
135
|
+
if (net.isIPv6(inner)) return `[${inner}]`;
|
|
136
|
+
}
|
|
137
|
+
// Bare or malformed hosts that merely contain a colon are not IPv6.
|
|
138
|
+
if (host.includes(":")) return null;
|
|
139
|
+
return host;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function buildProxyUrl(normalized) {
|
|
143
|
+
if (!normalized || normalized.mode !== "manual") return null;
|
|
144
|
+
const protocol = normalizeProtocol(normalized.protocol);
|
|
145
|
+
const host = normalizeHost(normalized.host);
|
|
146
|
+
const port = normalizePort(normalized.port);
|
|
147
|
+
if (!protocol || !host || port == null) return null;
|
|
148
|
+
const hostPart = bracketProxyHost(host);
|
|
149
|
+
if (!hostPart) return null;
|
|
150
|
+
return `${protocol}://${hostPart}:${port}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = {
|
|
154
|
+
PROXY_MODES,
|
|
155
|
+
PROXY_PROTOCOLS,
|
|
156
|
+
defaultProxyConfig,
|
|
157
|
+
normalizeProxyConfig,
|
|
158
|
+
parseProxyPayload,
|
|
159
|
+
buildProxyUrl,
|
|
160
|
+
normalizeProtocol,
|
|
161
|
+
normalizeHost,
|
|
162
|
+
normalizePort,
|
|
163
|
+
};
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const crypto = require("node:crypto");
|
|
4
|
-
const fs = require("node:fs/promises");
|
|
5
|
-
const os = require("node:os");
|
|
6
|
-
const path = require("node:path");
|
|
7
|
-
|
|
8
|
-
const OPEN_API = "https://openapi.qoder.com.cn";
|
|
9
|
-
|
|
10
|
-
async function readQoderCnCliCredentials({ home = os.homedir(), env = process.env } = {}) {
|
|
11
|
-
const root = typeof env.QODERCN_CONFIG_DIR === "string" && env.QODERCN_CONFIG_DIR.trim()
|
|
12
|
-
? path.resolve(env.QODERCN_CONFIG_DIR.trim())
|
|
13
|
-
: path.join(home, ".qoder-cn");
|
|
14
|
-
try {
|
|
15
|
-
const [encrypted, machineId] = await Promise.all([
|
|
16
|
-
fs.readFile(path.join(root, ".auth", "user"), "utf8"),
|
|
17
|
-
fs.readFile(path.join(root, ".auth", "machine_id"), "utf8"),
|
|
18
|
-
]);
|
|
19
|
-
// Verified against the official CLI 1.1.68 WASM credential-storage codec.
|
|
20
|
-
const key = Buffer.from(machineId.trim().slice(0, 16), "utf8");
|
|
21
|
-
if (key.length !== 16) return null;
|
|
22
|
-
const decipher = crypto.createDecipheriv("aes-128-cbc", key, key);
|
|
23
|
-
const user = JSON.parse(Buffer.concat([
|
|
24
|
-
decipher.update(Buffer.from(encrypted.trim(), "base64")),
|
|
25
|
-
decipher.final(),
|
|
26
|
-
]).toString("utf8"));
|
|
27
|
-
const token = user.security_oauth_token || user.access_token;
|
|
28
|
-
if (typeof token !== "string" || !token.trim()) return null;
|
|
29
|
-
return { token, organizationId: user.organization_id || null };
|
|
30
|
-
} catch (_error) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function cnAuthError() {
|
|
36
|
-
const error = new Error("Qoder CN login expired. Sign in to Qoder CN CLI again.");
|
|
37
|
-
error.code = "AUTH_EXPIRED";
|
|
38
|
-
return error;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function cnRequest(url, options, fetchImpl, { tokenExchange = false } = {}) {
|
|
42
|
-
let response;
|
|
43
|
-
try {
|
|
44
|
-
response = await fetchImpl(url, {
|
|
45
|
-
...options,
|
|
46
|
-
redirect: "error",
|
|
47
|
-
signal: AbortSignal.timeout(15_000),
|
|
48
|
-
});
|
|
49
|
-
} catch (_error) {
|
|
50
|
-
throw new Error("Qoder CN usage request failed.");
|
|
51
|
-
}
|
|
52
|
-
if (response.status === 401 || response.status === 403 || (tokenExchange && response.status === 400)) throw cnAuthError();
|
|
53
|
-
if (!response.ok) throw new Error(`Qoder CN API returned HTTP ${response.status}.`);
|
|
54
|
-
try {
|
|
55
|
-
return await response.json();
|
|
56
|
-
} catch (_error) {
|
|
57
|
-
throw new Error("Qoder CN API returned invalid JSON.");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function normalizeQoderCnCliUsage(response, normalizeRpcUsage) {
|
|
62
|
-
const usage = response?.displayMode === "qoder" ? response.qoderUsage : response;
|
|
63
|
-
const rawQuota = usage?.userQuota ?? usage?.user_quota;
|
|
64
|
-
const addOnQuota = usage?.addOnQuota ?? usage?.add_on_quota;
|
|
65
|
-
if ((!rawQuota || typeof rawQuota !== "object") && !addOnQuota) {
|
|
66
|
-
throw new Error("Qoder CN usage response is missing the plan quota.");
|
|
67
|
-
}
|
|
68
|
-
const total = Number(rawQuota?.total);
|
|
69
|
-
const used = Number(rawQuota?.used);
|
|
70
|
-
const remaining = rawQuota?.remaining == null ? Math.max(0, total - used) : Number(rawQuota.remaining);
|
|
71
|
-
if (rawQuota && (rawQuota.total == null || rawQuota.used == null
|
|
72
|
-
|| ![total, used, remaining].every(Number.isFinite) || Math.min(total, used, remaining) < 0
|
|
73
|
-
|| (total === 0 && (used !== 0 || remaining !== 0)))) {
|
|
74
|
-
throw new Error("Qoder CN usage response has invalid quota values.");
|
|
75
|
-
}
|
|
76
|
-
// The OpenAPI returns fractions; the old desktop IPC already returns percents.
|
|
77
|
-
const result = normalizeRpcUsage({
|
|
78
|
-
userType: usage.userType ?? usage.user_type,
|
|
79
|
-
expiresAt: usage.expiresAt ?? usage.expires_at,
|
|
80
|
-
isQuotaExceeded: usage.isQuotaExceeded ?? usage.is_quota_exceeded,
|
|
81
|
-
userQuota: rawQuota ? { used, total, remaining, unit: rawQuota.unit } : null,
|
|
82
|
-
addOnQuota,
|
|
83
|
-
});
|
|
84
|
-
result.source = "cli-api";
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function fetchQoderCnCliLimits({
|
|
89
|
-
home = os.homedir(),
|
|
90
|
-
env = process.env,
|
|
91
|
-
fetchImpl = fetch,
|
|
92
|
-
normalizeRpcUsage,
|
|
93
|
-
} = {}) {
|
|
94
|
-
const pat = typeof env.QODERCN_PERSONAL_ACCESS_TOKEN === "string"
|
|
95
|
-
? env.QODERCN_PERSONAL_ACCESS_TOKEN.trim()
|
|
96
|
-
: "";
|
|
97
|
-
let credentials;
|
|
98
|
-
if (pat) {
|
|
99
|
-
const exchanged = await cnRequest(`${OPEN_API}/api/v1/jobToken/exchange`, {
|
|
100
|
-
method: "POST",
|
|
101
|
-
headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
102
|
-
body: JSON.stringify({ personal_token: pat }),
|
|
103
|
-
}, fetchImpl, { tokenExchange: true });
|
|
104
|
-
const token = exchanged?.token || exchanged?.device_token || exchanged?.access_token;
|
|
105
|
-
if (typeof token !== "string" || !token.trim()) throw cnAuthError();
|
|
106
|
-
credentials = { token };
|
|
107
|
-
} else {
|
|
108
|
-
credentials = await readQoderCnCliCredentials({ home, env });
|
|
109
|
-
}
|
|
110
|
-
if (!credentials) return null;
|
|
111
|
-
const endpoint = credentials.organizationId ? "/sash/api/v2/me/usage" : "/api/v2/quota/usage";
|
|
112
|
-
const response = await cnRequest(`${OPEN_API}${endpoint}`, {
|
|
113
|
-
method: "GET",
|
|
114
|
-
headers: { Accept: "application/json", Authorization: `Bearer ${credentials.token}` },
|
|
115
|
-
}, fetchImpl);
|
|
116
|
-
return normalizeQoderCnCliUsage(response, normalizeRpcUsage);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
module.exports = { readQoderCnCliCredentials, normalizeQoderCnCliUsage, fetchQoderCnCliLimits };
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs/promises");
|
|
5
|
+
const os = require("node:os");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
|
|
8
|
+
const OPEN_API = "https://openapi.qoder.com.cn";
|
|
9
|
+
|
|
10
|
+
async function readQoderCnCliCredentials({ home = os.homedir(), env = process.env } = {}) {
|
|
11
|
+
const root = typeof env.QODERCN_CONFIG_DIR === "string" && env.QODERCN_CONFIG_DIR.trim()
|
|
12
|
+
? path.resolve(env.QODERCN_CONFIG_DIR.trim())
|
|
13
|
+
: path.join(home, ".qoder-cn");
|
|
14
|
+
try {
|
|
15
|
+
const [encrypted, machineId] = await Promise.all([
|
|
16
|
+
fs.readFile(path.join(root, ".auth", "user"), "utf8"),
|
|
17
|
+
fs.readFile(path.join(root, ".auth", "machine_id"), "utf8"),
|
|
18
|
+
]);
|
|
19
|
+
// Verified against the official CLI 1.1.68 WASM credential-storage codec.
|
|
20
|
+
const key = Buffer.from(machineId.trim().slice(0, 16), "utf8");
|
|
21
|
+
if (key.length !== 16) return null;
|
|
22
|
+
const decipher = crypto.createDecipheriv("aes-128-cbc", key, key);
|
|
23
|
+
const user = JSON.parse(Buffer.concat([
|
|
24
|
+
decipher.update(Buffer.from(encrypted.trim(), "base64")),
|
|
25
|
+
decipher.final(),
|
|
26
|
+
]).toString("utf8"));
|
|
27
|
+
const token = user.security_oauth_token || user.access_token;
|
|
28
|
+
if (typeof token !== "string" || !token.trim()) return null;
|
|
29
|
+
return { token, organizationId: user.organization_id || null };
|
|
30
|
+
} catch (_error) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function cnAuthError() {
|
|
36
|
+
const error = new Error("Qoder CN login expired. Sign in to Qoder CN CLI again.");
|
|
37
|
+
error.code = "AUTH_EXPIRED";
|
|
38
|
+
return error;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function cnRequest(url, options, fetchImpl, { tokenExchange = false } = {}) {
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await fetchImpl(url, {
|
|
45
|
+
...options,
|
|
46
|
+
redirect: "error",
|
|
47
|
+
signal: AbortSignal.timeout(15_000),
|
|
48
|
+
});
|
|
49
|
+
} catch (_error) {
|
|
50
|
+
throw new Error("Qoder CN usage request failed.");
|
|
51
|
+
}
|
|
52
|
+
if (response.status === 401 || response.status === 403 || (tokenExchange && response.status === 400)) throw cnAuthError();
|
|
53
|
+
if (!response.ok) throw new Error(`Qoder CN API returned HTTP ${response.status}.`);
|
|
54
|
+
try {
|
|
55
|
+
return await response.json();
|
|
56
|
+
} catch (_error) {
|
|
57
|
+
throw new Error("Qoder CN API returned invalid JSON.");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function normalizeQoderCnCliUsage(response, normalizeRpcUsage) {
|
|
62
|
+
const usage = response?.displayMode === "qoder" ? response.qoderUsage : response;
|
|
63
|
+
const rawQuota = usage?.userQuota ?? usage?.user_quota;
|
|
64
|
+
const addOnQuota = usage?.addOnQuota ?? usage?.add_on_quota;
|
|
65
|
+
if ((!rawQuota || typeof rawQuota !== "object") && !addOnQuota) {
|
|
66
|
+
throw new Error("Qoder CN usage response is missing the plan quota.");
|
|
67
|
+
}
|
|
68
|
+
const total = Number(rawQuota?.total);
|
|
69
|
+
const used = Number(rawQuota?.used);
|
|
70
|
+
const remaining = rawQuota?.remaining == null ? Math.max(0, total - used) : Number(rawQuota.remaining);
|
|
71
|
+
if (rawQuota && (rawQuota.total == null || rawQuota.used == null
|
|
72
|
+
|| ![total, used, remaining].every(Number.isFinite) || Math.min(total, used, remaining) < 0
|
|
73
|
+
|| (total === 0 && (used !== 0 || remaining !== 0)))) {
|
|
74
|
+
throw new Error("Qoder CN usage response has invalid quota values.");
|
|
75
|
+
}
|
|
76
|
+
// The OpenAPI returns fractions; the old desktop IPC already returns percents.
|
|
77
|
+
const result = normalizeRpcUsage({
|
|
78
|
+
userType: usage.userType ?? usage.user_type,
|
|
79
|
+
expiresAt: usage.expiresAt ?? usage.expires_at,
|
|
80
|
+
isQuotaExceeded: usage.isQuotaExceeded ?? usage.is_quota_exceeded,
|
|
81
|
+
userQuota: rawQuota ? { used, total, remaining, unit: rawQuota.unit } : null,
|
|
82
|
+
addOnQuota,
|
|
83
|
+
});
|
|
84
|
+
result.source = "cli-api";
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function fetchQoderCnCliLimits({
|
|
89
|
+
home = os.homedir(),
|
|
90
|
+
env = process.env,
|
|
91
|
+
fetchImpl = fetch,
|
|
92
|
+
normalizeRpcUsage,
|
|
93
|
+
} = {}) {
|
|
94
|
+
const pat = typeof env.QODERCN_PERSONAL_ACCESS_TOKEN === "string"
|
|
95
|
+
? env.QODERCN_PERSONAL_ACCESS_TOKEN.trim()
|
|
96
|
+
: "";
|
|
97
|
+
let credentials;
|
|
98
|
+
if (pat) {
|
|
99
|
+
const exchanged = await cnRequest(`${OPEN_API}/api/v1/jobToken/exchange`, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
102
|
+
body: JSON.stringify({ personal_token: pat }),
|
|
103
|
+
}, fetchImpl, { tokenExchange: true });
|
|
104
|
+
const token = exchanged?.token || exchanged?.device_token || exchanged?.access_token;
|
|
105
|
+
if (typeof token !== "string" || !token.trim()) throw cnAuthError();
|
|
106
|
+
credentials = { token };
|
|
107
|
+
} else {
|
|
108
|
+
credentials = await readQoderCnCliCredentials({ home, env });
|
|
109
|
+
}
|
|
110
|
+
if (!credentials) return null;
|
|
111
|
+
const endpoint = credentials.organizationId ? "/sash/api/v2/me/usage" : "/api/v2/quota/usage";
|
|
112
|
+
const response = await cnRequest(`${OPEN_API}${endpoint}`, {
|
|
113
|
+
method: "GET",
|
|
114
|
+
headers: { Accept: "application/json", Authorization: `Bearer ${credentials.token}` },
|
|
115
|
+
}, fetchImpl);
|
|
116
|
+
return normalizeQoderCnCliUsage(response, normalizeRpcUsage);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = { readQoderCnCliCredentials, normalizeQoderCnCliUsage, fetchQoderCnCliLimits };
|