@pikecode/api-key-manager 1.3.91 → 2.0.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/README.md +79 -52
- package/bin/akm.js +47 -30
- package/package.json +13 -8
- package/src/CommandRegistry.js +2 -2
- package/src/commands/add/prompts.js +200 -171
- package/src/commands/add/providerSaver.js +6 -7
- package/src/commands/add.js +6 -2
- package/src/commands/backup.js +99 -45
- package/src/commands/current.js +41 -20
- package/src/commands/edit.js +3 -289
- package/src/commands/health.js +75 -31
- package/src/commands/list.js +39 -19
- package/src/commands/remove.js +2 -2
- package/src/commands/switch/provider-edit-questions-helper.js +29 -7
- package/src/commands/switch/provider-manager.js +39 -40
- package/src/commands/switch.js +9 -8
- package/src/config.js +68 -45
- package/src/index.js +1 -1
- package/src/utils/atomic-file.js +47 -0
- package/src/utils/claude-settings.js +4 -3
- package/src/utils/codex-files.js +227 -38
- package/src/utils/codex-launcher.js +16 -8
- package/src/utils/concurrency.js +30 -0
- package/src/utils/env-launcher.js +17 -1
- package/src/utils/import-validator.js +210 -0
- package/src/utils/launch-args.js +60 -1
- package/src/utils/provider-status-checker.js +49 -24
- package/src/utils/shell-snippets.js +32 -0
- package/src/utils/terminal-format.js +22 -4
- package/src/utils/ui-helper.js +0 -1
- package/src/utils/update-checker.js +3 -3
- package/src/utils/validator.js +81 -10
package/src/commands/backup.js
CHANGED
|
@@ -9,6 +9,14 @@ const path = require('path');
|
|
|
9
9
|
const chalk = require('chalk');
|
|
10
10
|
const { configManager } = require('../config');
|
|
11
11
|
const { Logger } = require('../utils/logger');
|
|
12
|
+
const {
|
|
13
|
+
validateAndNormalizeConfigData,
|
|
14
|
+
validateAndNormalizeImportData
|
|
15
|
+
} = require('../utils/import-validator');
|
|
16
|
+
const { writeJsonAtomic } = require('../utils/atomic-file');
|
|
17
|
+
|
|
18
|
+
const MAX_IMPORT_FILE_SIZE = 5 * 1024 * 1024;
|
|
19
|
+
let backupSequence = 0;
|
|
12
20
|
|
|
13
21
|
/**
|
|
14
22
|
* 备份管理器类
|
|
@@ -18,8 +26,8 @@ class BackupManager {
|
|
|
18
26
|
/**
|
|
19
27
|
* 创建备份管理器实例
|
|
20
28
|
*/
|
|
21
|
-
constructor() {
|
|
22
|
-
this.configManager =
|
|
29
|
+
constructor(manager = configManager) {
|
|
30
|
+
this.configManager = manager;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
/**
|
|
@@ -39,30 +47,52 @@ class BackupManager {
|
|
|
39
47
|
|
|
40
48
|
// 准备导出数据
|
|
41
49
|
const exportData = {
|
|
42
|
-
version: '
|
|
50
|
+
version: '2.0',
|
|
43
51
|
exportedAt: new Date().toISOString(),
|
|
52
|
+
secretsIncluded: false,
|
|
44
53
|
providers: config.providers,
|
|
45
54
|
currentProvider: config.currentProvider
|
|
46
55
|
};
|
|
47
56
|
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
// 默认不导出完整密钥;旧版 --mask 参数继续保持最高安全优先级。
|
|
58
|
+
const includeSecrets = options.includeSecrets === true && options.mask !== true;
|
|
59
|
+
exportData.secretsIncluded = includeSecrets;
|
|
60
|
+
if (!includeSecrets) {
|
|
61
|
+
exportData.providers = this.stripTokens(exportData.providers);
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
// 确定输出路径
|
|
54
65
|
const finalPath = outputPath || `akm-config-${this.getTimestamp()}.json`;
|
|
55
|
-
const absolutePath = path.isAbsolute(finalPath)
|
|
66
|
+
const absolutePath = path.isAbsolute(finalPath)
|
|
67
|
+
? finalPath
|
|
68
|
+
: path.join(process.cwd(), finalPath);
|
|
69
|
+
|
|
70
|
+
if (options.dryRun) {
|
|
71
|
+
console.log(
|
|
72
|
+
JSON.stringify(
|
|
73
|
+
{
|
|
74
|
+
operation: 'export',
|
|
75
|
+
outputPath: absolutePath,
|
|
76
|
+
providerCount: Object.keys(config.providers).length,
|
|
77
|
+
secretsIncluded: includeSecrets
|
|
78
|
+
},
|
|
79
|
+
null,
|
|
80
|
+
2
|
|
81
|
+
)
|
|
82
|
+
);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
56
85
|
|
|
57
86
|
// 写入文件
|
|
58
|
-
await
|
|
87
|
+
await writeJsonAtomic(absolutePath, exportData, { spaces: 2, mode: 0o600 });
|
|
59
88
|
|
|
60
89
|
Logger.success(`配置已导出到: ${absolutePath}`);
|
|
61
90
|
console.log(chalk.gray(` 供应商数量: ${Object.keys(config.providers).length}`));
|
|
62
|
-
if (
|
|
63
|
-
console.log(chalk.yellow(' 注意: Token
|
|
91
|
+
if (!includeSecrets) {
|
|
92
|
+
console.log(chalk.yellow(' 注意: Token 未导出,导入后需要重新设置'));
|
|
93
|
+
} else {
|
|
94
|
+
console.log(chalk.yellow(' 注意: 文件包含完整 Token,请妥善保管'));
|
|
64
95
|
}
|
|
65
|
-
|
|
66
96
|
} catch (error) {
|
|
67
97
|
Logger.error(`导出配置失败: ${error.message}`);
|
|
68
98
|
throw error;
|
|
@@ -81,23 +111,45 @@ class BackupManager {
|
|
|
81
111
|
return;
|
|
82
112
|
}
|
|
83
113
|
|
|
84
|
-
const absolutePath = path.isAbsolute(inputPath)
|
|
114
|
+
const absolutePath = path.isAbsolute(inputPath)
|
|
115
|
+
? inputPath
|
|
116
|
+
: path.join(process.cwd(), inputPath);
|
|
85
117
|
|
|
86
|
-
if (!await fs.pathExists(absolutePath)) {
|
|
118
|
+
if (!(await fs.pathExists(absolutePath))) {
|
|
87
119
|
Logger.error(`文件不存在: ${absolutePath}`);
|
|
88
120
|
return;
|
|
89
121
|
}
|
|
90
122
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (!importData.providers || typeof importData.providers !== 'object') {
|
|
95
|
-
Logger.error('无效的配置文件格式');
|
|
96
|
-
return;
|
|
123
|
+
const fileStat = await fs.stat(absolutePath);
|
|
124
|
+
if (fileStat.size > MAX_IMPORT_FILE_SIZE) {
|
|
125
|
+
throw new Error('导入配置文件不能超过 5 MB');
|
|
97
126
|
}
|
|
98
127
|
|
|
128
|
+
const importData = validateAndNormalizeImportData(await fs.readJson(absolutePath));
|
|
129
|
+
|
|
99
130
|
await this.configManager.ensureLoaded();
|
|
100
131
|
|
|
132
|
+
const conflicts = Object.keys(importData.providers).filter(name =>
|
|
133
|
+
Boolean(this.configManager.getProvider(name))
|
|
134
|
+
);
|
|
135
|
+
if (options.dryRun) {
|
|
136
|
+
console.log(
|
|
137
|
+
JSON.stringify(
|
|
138
|
+
{
|
|
139
|
+
operation: 'import',
|
|
140
|
+
inputPath: absolutePath,
|
|
141
|
+
providerCount: Object.keys(importData.providers).length,
|
|
142
|
+
conflicts,
|
|
143
|
+
overwrite: options.overwrite === true,
|
|
144
|
+
currentProvider: importData.currentProvider || null
|
|
145
|
+
},
|
|
146
|
+
null,
|
|
147
|
+
2
|
|
148
|
+
)
|
|
149
|
+
);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
101
153
|
// 导入前自动备份当前配置
|
|
102
154
|
await this.backup();
|
|
103
155
|
|
|
@@ -123,12 +175,20 @@ class BackupManager {
|
|
|
123
175
|
}
|
|
124
176
|
|
|
125
177
|
// 如果设置了 currentProvider 且该供应商存在
|
|
126
|
-
if (
|
|
178
|
+
if (
|
|
179
|
+
importData.currentProvider &&
|
|
180
|
+
this.configManager.config.providers[importData.currentProvider]
|
|
181
|
+
) {
|
|
127
182
|
if (!this.configManager.config.currentProvider || options.overwrite) {
|
|
128
183
|
this.configManager.config.currentProvider = importData.currentProvider;
|
|
129
184
|
}
|
|
130
185
|
}
|
|
131
186
|
|
|
187
|
+
const activeProvider = this.configManager.config.currentProvider;
|
|
188
|
+
for (const [name, provider] of Object.entries(this.configManager.config.providers)) {
|
|
189
|
+
provider.current = name === activeProvider;
|
|
190
|
+
}
|
|
191
|
+
|
|
132
192
|
await this.configManager.save();
|
|
133
193
|
|
|
134
194
|
Logger.success('配置导入完成');
|
|
@@ -137,7 +197,6 @@ class BackupManager {
|
|
|
137
197
|
if (skippedCount > 0) {
|
|
138
198
|
console.log(chalk.yellow(` 跳过: ${skippedCount} 个已存在的供应商`));
|
|
139
199
|
}
|
|
140
|
-
|
|
141
200
|
} catch (error) {
|
|
142
201
|
Logger.error(`导入配置失败: ${error.message}`);
|
|
143
202
|
throw error;
|
|
@@ -161,24 +220,27 @@ class BackupManager {
|
|
|
161
220
|
await fs.ensureDir(finalDir);
|
|
162
221
|
|
|
163
222
|
// 生成备份文件名
|
|
164
|
-
const backupFileName =
|
|
223
|
+
const backupFileName =
|
|
224
|
+
['akm-backup', this.getTimestamp(), process.pid, backupSequence++].join('-') + '.json';
|
|
165
225
|
const backupPath = path.join(finalDir, backupFileName);
|
|
166
226
|
|
|
167
227
|
// 读取原始配置文件
|
|
168
228
|
const configPath = this.configManager.configPath;
|
|
169
|
-
if (!await fs.pathExists(configPath)) {
|
|
229
|
+
if (!(await fs.pathExists(configPath))) {
|
|
170
230
|
Logger.warning('配置文件不存在,无需备份');
|
|
171
231
|
return;
|
|
172
232
|
}
|
|
173
233
|
|
|
174
234
|
// 复制配置文件
|
|
175
|
-
await fs.copy(configPath, backupPath);
|
|
235
|
+
await fs.copy(configPath, backupPath, { overwrite: false, errorOnExist: true });
|
|
236
|
+
if (process.platform !== 'win32') {
|
|
237
|
+
await fs.chmod(backupPath, 0o600);
|
|
238
|
+
}
|
|
176
239
|
|
|
177
240
|
Logger.success(`配置已备份到: ${backupPath}`);
|
|
178
241
|
|
|
179
242
|
// 清理旧备份(保留最近 10 个)
|
|
180
243
|
await this.cleanOldBackups(finalDir, 10);
|
|
181
|
-
|
|
182
244
|
} catch (error) {
|
|
183
245
|
Logger.error(`备份配置失败: ${error.message}`);
|
|
184
246
|
throw error;
|
|
@@ -195,7 +257,7 @@ class BackupManager {
|
|
|
195
257
|
const defaultBackupDir = path.join(homeDir, '.akm-backups');
|
|
196
258
|
const finalDir = backupDir || defaultBackupDir;
|
|
197
259
|
|
|
198
|
-
if (!await fs.pathExists(finalDir)) {
|
|
260
|
+
if (!(await fs.pathExists(finalDir))) {
|
|
199
261
|
Logger.warning('备份目录不存在');
|
|
200
262
|
return;
|
|
201
263
|
}
|
|
@@ -225,7 +287,6 @@ class BackupManager {
|
|
|
225
287
|
console.log(chalk.gray('═'.repeat(60)));
|
|
226
288
|
console.log(chalk.blue(`总计: ${backups.length} 个备份`));
|
|
227
289
|
console.log(chalk.gray(`备份目录: ${finalDir}`));
|
|
228
|
-
|
|
229
290
|
} catch (error) {
|
|
230
291
|
Logger.error(`列出备份失败: ${error.message}`);
|
|
231
292
|
throw error;
|
|
@@ -251,20 +312,18 @@ class BackupManager {
|
|
|
251
312
|
backupPath = path.join(defaultBackupDir, backupFile);
|
|
252
313
|
}
|
|
253
314
|
|
|
254
|
-
if (!await fs.pathExists(backupPath)) {
|
|
315
|
+
if (!(await fs.pathExists(backupPath))) {
|
|
255
316
|
Logger.error(`备份文件不存在: ${backupPath}`);
|
|
256
317
|
return;
|
|
257
318
|
}
|
|
258
319
|
|
|
259
|
-
|
|
260
|
-
await this.backup();
|
|
320
|
+
const restoredConfig = validateAndNormalizeConfigData(await fs.readJson(backupPath));
|
|
261
321
|
|
|
262
|
-
//
|
|
263
|
-
|
|
264
|
-
await
|
|
322
|
+
// 只有恢复文件校验通过后才备份并替换当前配置。
|
|
323
|
+
await this.backup();
|
|
324
|
+
await this.configManager.save(restoredConfig);
|
|
265
325
|
|
|
266
326
|
Logger.success(`配置已从备份恢复: ${backupPath}`);
|
|
267
|
-
|
|
268
327
|
} catch (error) {
|
|
269
328
|
Logger.error(`恢复备份失败: ${error.message}`);
|
|
270
329
|
throw error;
|
|
@@ -293,22 +352,17 @@ class BackupManager {
|
|
|
293
352
|
}
|
|
294
353
|
|
|
295
354
|
/**
|
|
296
|
-
* Token
|
|
355
|
+
* 从可分享配置中移除 Token
|
|
297
356
|
*/
|
|
298
|
-
|
|
299
|
-
const
|
|
357
|
+
stripTokens(providers) {
|
|
358
|
+
const withoutSecrets = {};
|
|
300
359
|
for (const [name, provider] of Object.entries(providers)) {
|
|
301
|
-
|
|
360
|
+
withoutSecrets[name] = {
|
|
302
361
|
...provider,
|
|
303
|
-
authToken:
|
|
362
|
+
authToken: null
|
|
304
363
|
};
|
|
305
364
|
}
|
|
306
|
-
return
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
maskToken(token) {
|
|
310
|
-
if (!token || token.length < 10) return '***';
|
|
311
|
-
return token.substring(0, 8) + '***' + token.substring(token.length - 4);
|
|
365
|
+
return withoutSecrets;
|
|
312
366
|
}
|
|
313
367
|
|
|
314
368
|
getTimestamp() {
|
package/src/commands/current.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
const chalk = require('chalk');
|
|
8
8
|
const { configManager } = require('../config');
|
|
9
9
|
const { Logger } = require('../utils/logger');
|
|
10
|
-
const {
|
|
10
|
+
const { formatShellAssignment } = require('../utils/shell-snippets');
|
|
11
11
|
const { AUTH_MODE_DISPLAY } = require('../constants');
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -18,20 +18,16 @@ class CurrentConfig {
|
|
|
18
18
|
/**
|
|
19
19
|
* 创建当前配置显示器实例
|
|
20
20
|
*/
|
|
21
|
-
constructor() {
|
|
22
|
-
this.configManager =
|
|
21
|
+
constructor(manager = configManager) {
|
|
22
|
+
this.configManager = manager;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* 显示当前供应商配置
|
|
27
|
-
* @param {Object} options - 显示选项
|
|
28
|
-
* @param {boolean} [options.showToken=true] - 是否显示完整 token
|
|
29
27
|
* @returns {Promise<void>}
|
|
30
28
|
*/
|
|
31
29
|
async show(options = {}) {
|
|
32
30
|
try {
|
|
33
|
-
// 默认显示完整 token,不再加密
|
|
34
|
-
const showToken = options.showToken !== false;
|
|
35
31
|
await this.configManager.ensureLoaded();
|
|
36
32
|
const currentProvider = this.configManager.getCurrentProvider();
|
|
37
33
|
|
|
@@ -41,6 +37,19 @@ class CurrentConfig {
|
|
|
41
37
|
return;
|
|
42
38
|
}
|
|
43
39
|
|
|
40
|
+
if (options.json) {
|
|
41
|
+
console.log(
|
|
42
|
+
JSON.stringify(
|
|
43
|
+
{ currentProvider: currentProvider.name, provider: currentProvider },
|
|
44
|
+
null,
|
|
45
|
+
2
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const shell = options.shell;
|
|
52
|
+
|
|
44
53
|
console.log(chalk.blue('\n📍 当前配置:'));
|
|
45
54
|
console.log(chalk.gray('═'.repeat(60)));
|
|
46
55
|
|
|
@@ -53,35 +62,44 @@ class CurrentConfig {
|
|
|
53
62
|
console.log(chalk.gray(`OPENAI_BASE_URL: ${currentProvider.baseUrl}`));
|
|
54
63
|
}
|
|
55
64
|
if (currentProvider.authToken) {
|
|
56
|
-
console.log(chalk.gray(`OPENAI_API_KEY: ${
|
|
65
|
+
console.log(chalk.gray(`OPENAI_API_KEY: ${currentProvider.authToken}`));
|
|
57
66
|
}
|
|
58
|
-
console.log(
|
|
67
|
+
console.log(
|
|
68
|
+
chalk.gray(`创建时间: ${new Date(currentProvider.createdAt).toLocaleString()}`)
|
|
69
|
+
);
|
|
59
70
|
console.log(chalk.gray(`最后使用: ${new Date(currentProvider.lastUsed).toLocaleString()}`));
|
|
60
71
|
console.log(chalk.gray('═'.repeat(60)));
|
|
61
72
|
|
|
62
73
|
console.log(chalk.blue('\n🔧 环境变量设置:'));
|
|
63
74
|
if (currentProvider.baseUrl) {
|
|
64
|
-
console.log(chalk.gray(
|
|
75
|
+
console.log(chalk.gray(formatShellAssignment('OPENAI_BASE_URL', currentProvider.baseUrl, shell)));
|
|
65
76
|
}
|
|
66
77
|
if (currentProvider.authToken) {
|
|
67
|
-
console.log(chalk.gray(
|
|
78
|
+
console.log(chalk.gray(formatShellAssignment('OPENAI_API_KEY', currentProvider.authToken, shell)));
|
|
68
79
|
}
|
|
69
80
|
console.log(chalk.gray('codex'));
|
|
70
81
|
return;
|
|
71
82
|
}
|
|
72
83
|
|
|
73
84
|
// 显示认证模式
|
|
74
|
-
console.log(
|
|
85
|
+
console.log(
|
|
86
|
+
chalk.gray(
|
|
87
|
+
`认证模式: ${AUTH_MODE_DISPLAY[currentProvider.authMode] || currentProvider.authMode}`
|
|
88
|
+
)
|
|
89
|
+
);
|
|
75
90
|
|
|
76
91
|
if (currentProvider.baseUrl) {
|
|
77
92
|
console.log(chalk.gray(`基础URL: ${currentProvider.baseUrl}`));
|
|
78
93
|
}
|
|
79
|
-
console.log(chalk.gray(`认证Token: ${
|
|
94
|
+
console.log(chalk.gray(`认证Token: ${currentProvider.authToken}`));
|
|
80
95
|
console.log(chalk.gray(`创建时间: ${new Date(currentProvider.createdAt).toLocaleString()}`));
|
|
81
96
|
console.log(chalk.gray(`最后使用: ${new Date(currentProvider.lastUsed).toLocaleString()}`));
|
|
82
97
|
|
|
83
98
|
// 显示模型配置
|
|
84
|
-
if (
|
|
99
|
+
if (
|
|
100
|
+
currentProvider.models &&
|
|
101
|
+
(currentProvider.models.primary || currentProvider.models.smallFast)
|
|
102
|
+
) {
|
|
85
103
|
console.log(chalk.gray(`主模型: ${currentProvider.models.primary || '未设置'}`));
|
|
86
104
|
console.log(chalk.gray(`快速模型: ${currentProvider.models.smallFast || '未设置'}`));
|
|
87
105
|
}
|
|
@@ -91,21 +109,24 @@ class CurrentConfig {
|
|
|
91
109
|
// 显示环境变量设置方式
|
|
92
110
|
console.log(chalk.blue('\n🔧 环境变量设置:'));
|
|
93
111
|
if (currentProvider.baseUrl) {
|
|
94
|
-
console.log(chalk.gray(
|
|
112
|
+
console.log(chalk.gray(formatShellAssignment('ANTHROPIC_BASE_URL', currentProvider.baseUrl, shell)));
|
|
95
113
|
}
|
|
96
114
|
if (currentProvider.authMode === 'auth_token') {
|
|
97
|
-
console.log(chalk.gray(
|
|
115
|
+
console.log(chalk.gray(formatShellAssignment('ANTHROPIC_AUTH_TOKEN', currentProvider.authToken, shell)));
|
|
98
116
|
} else {
|
|
99
|
-
console.log(chalk.gray(
|
|
117
|
+
console.log(chalk.gray(formatShellAssignment('ANTHROPIC_API_KEY', currentProvider.authToken, shell)));
|
|
100
118
|
}
|
|
101
119
|
if (currentProvider.models?.primary) {
|
|
102
|
-
console.log(chalk.gray(
|
|
120
|
+
console.log(chalk.gray(formatShellAssignment('ANTHROPIC_MODEL', currentProvider.models.primary, shell)));
|
|
103
121
|
}
|
|
104
122
|
if (currentProvider.models?.smallFast) {
|
|
105
|
-
console.log(
|
|
123
|
+
console.log(
|
|
124
|
+
chalk.gray(
|
|
125
|
+
formatShellAssignment('ANTHROPIC_SMALL_FAST_MODEL', currentProvider.models.smallFast, shell)
|
|
126
|
+
)
|
|
127
|
+
);
|
|
106
128
|
}
|
|
107
129
|
console.log(chalk.gray('claude'));
|
|
108
|
-
|
|
109
130
|
} catch (error) {
|
|
110
131
|
Logger.error(`获取当前配置失败: ${error.message}`);
|
|
111
132
|
throw error;
|