aihezu 2.6.2 → 2.6.3
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/bin/aihezu.js +1 -0
- package/commands/usage.js +86 -9
- package/package.json +1 -1
package/bin/aihezu.js
CHANGED
|
@@ -36,6 +36,7 @@ function showHelp() {
|
|
|
36
36
|
console.log(' aihezu usage cc 只显示 Claude Code 用量');
|
|
37
37
|
console.log(' aihezu usage codex 只显示 Codex 用量');
|
|
38
38
|
console.log(' aihezu usage gemini 只显示 Google Gemini 用量');
|
|
39
|
+
console.log(' aihezu usage --key sk-xxx 使用指定 API Key 查询');
|
|
39
40
|
console.log('\n服务:');
|
|
40
41
|
console.log(' claude Claude Code');
|
|
41
42
|
console.log(' codex Codex');
|
package/commands/usage.js
CHANGED
|
@@ -17,6 +17,69 @@ function maskToken(token) {
|
|
|
17
17
|
return '***' + token.slice(-8);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function parseKeyArg(args) {
|
|
21
|
+
let overrideKey = null;
|
|
22
|
+
const remaining = [];
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
25
|
+
const arg = args[i];
|
|
26
|
+
|
|
27
|
+
if (arg === '--key' || arg === '-k') {
|
|
28
|
+
const next = args[i + 1];
|
|
29
|
+
if (!next || next.startsWith('-')) {
|
|
30
|
+
console.error('[错误] --key 需要提供 API Key');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
overrideKey = next;
|
|
34
|
+
i += 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (arg.startsWith('--key=')) {
|
|
39
|
+
const value = arg.slice('--key='.length);
|
|
40
|
+
if (!value) {
|
|
41
|
+
console.error('[错误] --key 需要提供 API Key');
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
overrideKey = value;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (arg.startsWith('-k=')) {
|
|
49
|
+
const value = arg.slice('-k='.length);
|
|
50
|
+
if (!value) {
|
|
51
|
+
console.error('[错误] -k 需要提供 API Key');
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
overrideKey = value;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
remaining.push(arg);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { overrideKey, remaining };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function uniqueConfigsByOrigin(configs) {
|
|
65
|
+
const result = [];
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
|
|
68
|
+
for (const config of configs) {
|
|
69
|
+
let originKey = config.baseUrl || '';
|
|
70
|
+
try {
|
|
71
|
+
originKey = new URL(config.baseUrl).origin;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// fallback to baseUrl string
|
|
74
|
+
}
|
|
75
|
+
if (seen.has(originKey)) continue;
|
|
76
|
+
seen.add(originKey);
|
|
77
|
+
result.push(config);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
|
|
20
83
|
// 读取 Claude 配置
|
|
21
84
|
function readClaudeConfig() {
|
|
22
85
|
const configs = [];
|
|
@@ -454,12 +517,13 @@ function displayUsageStats(stats, origin, source) {
|
|
|
454
517
|
}
|
|
455
518
|
|
|
456
519
|
function showUsageHelp() {
|
|
457
|
-
console.log('用法: aihezu usage [service] [--json]');
|
|
520
|
+
console.log('用法: aihezu usage [service] [--json] [--key <apiKey>]');
|
|
458
521
|
console.log('');
|
|
459
522
|
console.log('说明:');
|
|
460
523
|
console.log(' - 自动检测 Claude Code、Codex、Google Gemini 的配置');
|
|
461
524
|
console.log(' - 从环境变量和配置文件读取配置');
|
|
462
525
|
console.log(' - 如果两者都存在且不同,将分别查询两个账号的用量');
|
|
526
|
+
console.log(' - 使用 --key 可查询指定 API Key 的用量');
|
|
463
527
|
console.log('');
|
|
464
528
|
console.log('可选服务参数:');
|
|
465
529
|
console.log(' cc, claude 只显示 Claude Code 用量');
|
|
@@ -467,6 +531,10 @@ function showUsageHelp() {
|
|
|
467
531
|
console.log(' gemini 只显示 Google Gemini 用量');
|
|
468
532
|
console.log(' (不指定) 显示所有已配置服务的用量');
|
|
469
533
|
console.log('');
|
|
534
|
+
console.log('可选参数:');
|
|
535
|
+
console.log(' --key, -k <apiKey> 使用指定 API Key 查询用量');
|
|
536
|
+
console.log(' --json JSON 格式输出');
|
|
537
|
+
console.log('');
|
|
470
538
|
console.log('示例:');
|
|
471
539
|
console.log(' npx aihezu usage # 显示所有服务');
|
|
472
540
|
console.log(' npx aihezu usage cc # 只显示 Claude Code');
|
|
@@ -474,6 +542,8 @@ function showUsageHelp() {
|
|
|
474
542
|
console.log(' npx aihezu usage gemini # 只显示 Gemini');
|
|
475
543
|
console.log(' npx aihezu usage --json # JSON 格式输出所有服务');
|
|
476
544
|
console.log(' npx aihezu usage cc --json # JSON 格式输出 Claude Code');
|
|
545
|
+
console.log(' npx aihezu usage --key sk-xxx # 使用指定 Key 查询');
|
|
546
|
+
console.log(' npx aihezu usage cc --key sk-xxx');
|
|
477
547
|
}
|
|
478
548
|
|
|
479
549
|
async function usageCommand(args = []) {
|
|
@@ -483,10 +553,11 @@ async function usageCommand(args = []) {
|
|
|
483
553
|
return;
|
|
484
554
|
}
|
|
485
555
|
|
|
486
|
-
const
|
|
556
|
+
const { overrideKey, remaining } = parseKeyArg(args);
|
|
557
|
+
const outputJson = remaining.includes('--json');
|
|
487
558
|
|
|
488
559
|
// 提取服务过滤参数(第一个非选项参数)
|
|
489
|
-
const serviceFilter =
|
|
560
|
+
const serviceFilter = remaining.find(arg => !arg.startsWith('-'));
|
|
490
561
|
|
|
491
562
|
// 服务别名映射
|
|
492
563
|
const serviceAliases = {
|
|
@@ -538,24 +609,30 @@ async function usageCommand(args = []) {
|
|
|
538
609
|
|
|
539
610
|
allResults[service.name] = [];
|
|
540
611
|
|
|
541
|
-
|
|
542
|
-
|
|
612
|
+
const configsToUse = overrideKey
|
|
613
|
+
? uniqueConfigsByOrigin(service.configs)
|
|
614
|
+
: service.configs;
|
|
615
|
+
|
|
616
|
+
for (const config of configsToUse) {
|
|
617
|
+
const effectiveKey = overrideKey || config.authToken;
|
|
618
|
+
const sourceLabel = overrideKey ? `命令行 Key (${config.source})` : config.source;
|
|
619
|
+
console.log(`[查询中] ${sourceLabel} - Token: ${maskToken(effectiveKey)}`);
|
|
543
620
|
|
|
544
|
-
const result = await queryUsage(config.baseUrl,
|
|
621
|
+
const result = await queryUsage(config.baseUrl, effectiveKey);
|
|
545
622
|
|
|
546
623
|
if (result.error) {
|
|
547
624
|
console.log(`[错误] ${result.error}`);
|
|
548
625
|
console.log('');
|
|
549
626
|
allResults[service.name].push({
|
|
550
|
-
source:
|
|
627
|
+
source: sourceLabel,
|
|
551
628
|
error: result.error
|
|
552
629
|
});
|
|
553
630
|
} else {
|
|
554
631
|
if (!outputJson) {
|
|
555
|
-
displayUsageStats(result.stats, result.origin,
|
|
632
|
+
displayUsageStats(result.stats, result.origin, sourceLabel);
|
|
556
633
|
}
|
|
557
634
|
allResults[service.name].push({
|
|
558
|
-
source:
|
|
635
|
+
source: sourceLabel,
|
|
559
636
|
origin: result.origin,
|
|
560
637
|
stats: result.stats
|
|
561
638
|
});
|