@pikecode/api-key-manager 2.0.0 → 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.
Files changed (60) hide show
  1. package/README.md +444 -151
  2. package/bin/akm.js +340 -11
  3. package/package.json +29 -12
  4. package/src/CommandRegistry.js +58 -3
  5. package/src/commands/BaseCommand.js +68 -1
  6. package/src/commands/add/prompts.js +280 -0
  7. package/src/commands/add/providerSaver.js +74 -0
  8. package/src/commands/add/summaryPrinter.js +29 -0
  9. package/src/commands/add.js +54 -439
  10. package/src/commands/backup.js +396 -0
  11. package/src/commands/batch.js +313 -0
  12. package/src/commands/benchmark.js +323 -0
  13. package/src/commands/claude-clean.js +253 -0
  14. package/src/commands/clone.js +265 -0
  15. package/src/commands/current.js +89 -41
  16. package/src/commands/edit.js +3 -207
  17. package/src/commands/health.js +264 -0
  18. package/src/commands/list.js +120 -35
  19. package/src/commands/mcp.js +467 -0
  20. package/src/commands/remove.js +89 -26
  21. package/src/commands/stats.js +281 -0
  22. package/src/commands/switch/claude-settings-compatibility.js +113 -0
  23. package/src/commands/switch/launch-args-helper.js +84 -0
  24. package/src/commands/switch/provider-choices-helper.js +72 -0
  25. package/src/commands/switch/provider-details-helper.js +46 -0
  26. package/src/commands/switch/provider-edit-questions-helper.js +124 -0
  27. package/src/commands/switch/provider-launcher.js +20 -0
  28. package/src/commands/switch/provider-manager.js +214 -0
  29. package/src/commands/switch/status-helper.js +124 -0
  30. package/src/commands/switch/ui-screen-renderer.js +77 -0
  31. package/src/commands/switch.js +471 -958
  32. package/src/commands/validate.js +310 -0
  33. package/src/config.js +541 -55
  34. package/src/constants/index.js +234 -0
  35. package/src/constants/ui.js +108 -0
  36. package/src/index.js +11 -4
  37. package/src/navigation/EscNavigationManager.js +5 -5
  38. package/src/utils/atomic-file.js +47 -0
  39. package/src/utils/claude-settings.js +4 -4
  40. package/src/utils/codex-files.js +418 -0
  41. package/src/utils/codex-launcher.js +104 -0
  42. package/src/utils/concurrency.js +30 -0
  43. package/src/utils/config-opener.js +22 -2
  44. package/src/utils/env-launcher.js +50 -44
  45. package/src/utils/env-utils.js +52 -0
  46. package/src/utils/error-handler.js +27 -28
  47. package/src/utils/health-checker.js +350 -0
  48. package/src/utils/import-validator.js +210 -0
  49. package/src/utils/inquirer-setup.js +11 -0
  50. package/src/utils/launch-args.js +155 -0
  51. package/src/utils/logger.js +0 -1
  52. package/src/utils/provider-status-checker.js +211 -64
  53. package/src/utils/secrets.js +22 -0
  54. package/src/utils/shell-snippets.js +32 -0
  55. package/src/utils/terminal-format.js +22 -4
  56. package/src/utils/ui-helper.js +96 -86
  57. package/src/utils/update-checker.js +41 -12
  58. package/src/utils/validator.js +115 -88
  59. package/bin/cc.js +0 -101
  60. package/src/utils/storage.js +0 -55
@@ -0,0 +1,280 @@
1
+ /**
2
+ * Helper functions for ProviderAdder prompts.
3
+ * Each function returns a promise that resolves to the collected answers.
4
+ * The implementation mirrors the original large prompt array but is split
5
+ * into focused pieces to keep each function under 50 lines.
6
+ */
7
+
8
+ const { validator } = require('../../utils/validator');
9
+ const { UIHelper } = require('../../utils/ui-helper');
10
+ const { UI_MESSAGES } = require('../../constants/ui');
11
+ const { Logger } = require('../../utils/logger');
12
+ const { registry } = require('../../CommandRegistry');
13
+ const { getCodexLaunchArgs } = require('../../utils/launch-args');
14
+
15
+ /**
16
+ * Prompt the initial provider information (name, IDE, auth mode, URLs, tokens, etc.).
17
+ * @param {BaseCommand} adder - the ProviderAdder instance (used for ESC handling)
18
+ * @returns {Promise<Object>} answers object
19
+ */
20
+ async function promptProviderInfo(adder) {
21
+ return await adder.promptWithESC(
22
+ [
23
+ {
24
+ type: 'list',
25
+ name: 'ideName',
26
+ message: UI_MESSAGES.SELECT_IDE,
27
+ choices: [
28
+ { name: UI_MESSAGES.IDE_CLAUDE_CODE, value: 'claude' },
29
+ { name: UI_MESSAGES.IDE_CODEX, value: 'codex' }
30
+ ],
31
+ default: adder.presetIdeName || 'claude',
32
+ when: () => !adder.presetIdeName
33
+ },
34
+ {
35
+ type: 'input',
36
+ name: 'name',
37
+ message: UI_MESSAGES.INPUT_PROVIDER_NAME,
38
+ validate: input => {
39
+ const err = validator.validateName(input);
40
+ return err || true;
41
+ }
42
+ },
43
+ {
44
+ type: 'list',
45
+ name: 'authMode',
46
+ message: UI_MESSAGES.SELECT_AUTH_MODE,
47
+ choices: [
48
+ { name: UI_MESSAGES.AUTH_MODE_API_KEY, value: 'api_key' },
49
+ { name: UI_MESSAGES.AUTH_MODE_AUTH_TOKEN, value: 'auth_token' }
50
+ ],
51
+ default: 'api_key',
52
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
53
+ },
54
+ {
55
+ type: 'input',
56
+ name: 'baseUrl',
57
+ message: UI_MESSAGES.INPUT_BASE_URL,
58
+ validate: input => {
59
+ if (!input) return 'API 基础URL不能为空';
60
+ const err = validator.validateUrl(input);
61
+ return err || true;
62
+ },
63
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
64
+ },
65
+ {
66
+ type: 'input',
67
+ name: 'authToken',
68
+ message: answers => {
69
+ const envVar =
70
+ answers.authMode === 'auth_token' ? 'ANTHROPIC_AUTH_TOKEN' : 'ANTHROPIC_API_KEY';
71
+ return `${UI_MESSAGES.INPUT_TOKEN} (${envVar}):`;
72
+ },
73
+ validate: input => {
74
+ const err = validator.validateToken(input);
75
+ return err || true;
76
+ },
77
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
78
+ },
79
+ // Codex‑specific prompts (manual entry)
80
+ {
81
+ type: 'input',
82
+ name: 'baseUrl',
83
+ message: UI_MESSAGES.INPUT_OPENAI_BASE_URL,
84
+ default: '',
85
+ validate: input => {
86
+ if (!input) return true;
87
+ const err = validator.validateUrl(input);
88
+ return err || true;
89
+ },
90
+ when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
91
+ },
92
+ {
93
+ type: 'input',
94
+ name: 'authToken',
95
+ message: UI_MESSAGES.INPUT_OPENAI_API_KEY,
96
+ validate: input => {
97
+ if (!input) return 'API Key 不能为空';
98
+ const err = validator.validateToken(input);
99
+ return err || true;
100
+ },
101
+ when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
102
+ },
103
+ {
104
+ type: 'confirm',
105
+ name: 'setAsDefault',
106
+ message: UI_MESSAGES.SET_AS_DEFAULT,
107
+ default: true
108
+ },
109
+ {
110
+ type: 'confirm',
111
+ name: 'configureLaunchArgs',
112
+ message: UI_MESSAGES.CONFIGURE_LAUNCH_ARGS,
113
+ default: false,
114
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
115
+ },
116
+ {
117
+ type: 'confirm',
118
+ name: 'configureCodexLaunchArgs',
119
+ message: UI_MESSAGES.CONFIGURE_CODEX_LAUNCH_ARGS,
120
+ default: false,
121
+ when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
122
+ },
123
+ {
124
+ type: 'confirm',
125
+ name: 'configureModels',
126
+ message: UI_MESSAGES.CONFIGURE_MODELS,
127
+ default: false,
128
+ when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
129
+ }
130
+ ],
131
+ UI_MESSAGES.ESC_CANCEL_ADD,
132
+ async () => {
133
+ Logger.info(UI_MESSAGES.ADD_PROVIDER_CANCELLED);
134
+ if (adder.returnToParent) {
135
+ return;
136
+ }
137
+ await registry.executeCommand('switch');
138
+ }
139
+ );
140
+ }
141
+
142
+ /**
143
+ * Prompt for launch arguments (non‑Codex).
144
+ */
145
+ async function promptLaunchArgs(adder) {
146
+ console.log(UIHelper.createTitle(UI_MESSAGES.CONFIG_LAUNCH_ARGS_TITLE, UIHelper.icons.settings));
147
+ console.log();
148
+ console.log(UIHelper.createTooltip(UI_MESSAGES.CONFIG_LAUNCH_ARGS_TOOLTIP));
149
+ console.log();
150
+ console.log(UIHelper.createStepIndicator(2, 2, UI_MESSAGES.ADD_PROVIDER_STEP_2_LAUNCH_ARGS));
151
+ console.log(
152
+ UIHelper.createHintLine([
153
+ [UI_MESSAGES.HINT_SPACE, UI_MESSAGES.HINT_SPACE_DESC],
154
+ [UI_MESSAGES.HINT_A, UI_MESSAGES.HINT_A_DESC],
155
+ [UI_MESSAGES.HINT_I, UI_MESSAGES.HINT_I_DESC],
156
+ [UI_MESSAGES.HINT_ENTER, UI_MESSAGES.HINT_ENTER_CONFIRM],
157
+ [UI_MESSAGES.HINT_ESC, UI_MESSAGES.HINT_ESC_SKIP]
158
+ ])
159
+ );
160
+ console.log();
161
+
162
+ const result = await adder.promptWithESCAndDefault(
163
+ [
164
+ {
165
+ type: 'checkbox',
166
+ name: 'launchArgs',
167
+ message: UI_MESSAGES.SELECT_LAUNCH_ARGS,
168
+ choices: validator.getAvailableLaunchArgs().map(arg => ({
169
+ name: `${arg.name} - ${arg.description}`,
170
+ value: arg.name,
171
+ checked: false
172
+ }))
173
+ }
174
+ ],
175
+ UI_MESSAGES.ESC_SKIP_CONFIG,
176
+ () => {
177
+ Logger.info(UI_MESSAGES.CONFIG_LAUNCH_ARGS_SKIP);
178
+ },
179
+ { launchArgs: [] }
180
+ );
181
+
182
+ return result.launchArgs;
183
+ }
184
+
185
+ /**
186
+ * Prompt for model configuration (non‑Codex).
187
+ */
188
+ async function promptModelConfig(adder) {
189
+ console.log(UIHelper.createTitle(UI_MESSAGES.CONFIG_MODELS_TITLE, UIHelper.icons.settings));
190
+ console.log();
191
+ console.log(UIHelper.createTooltip(UI_MESSAGES.CONFIG_MODELS_TOOLTIP));
192
+ console.log();
193
+ console.log(UIHelper.createStepIndicator(2, 2, UI_MESSAGES.ADD_PROVIDER_STEP_2_MODELS));
194
+ console.log(
195
+ UIHelper.createHintLine([
196
+ [UI_MESSAGES.HINT_ENTER, UI_MESSAGES.HINT_ENTER_DESC],
197
+ [UI_MESSAGES.HINT_ESC, UI_MESSAGES.HINT_ESC_SKIP]
198
+ ])
199
+ );
200
+ console.log();
201
+
202
+ const responses = await adder.promptWithESCAndDefault(
203
+ [
204
+ {
205
+ type: 'input',
206
+ name: 'primaryModel',
207
+ message: UI_MESSAGES.INPUT_PRIMARY_MODEL,
208
+ default: '',
209
+ validate: input => validator.validateModel(input) || true
210
+ },
211
+ {
212
+ type: 'input',
213
+ name: 'smallFastModel',
214
+ message: UI_MESSAGES.INPUT_SMALL_FAST_MODEL,
215
+ default: '',
216
+ validate: input => validator.validateModel(input) || true
217
+ }
218
+ ],
219
+ UI_MESSAGES.ESC_SKIP_CONFIG,
220
+ () => {
221
+ Logger.info(UI_MESSAGES.CONFIG_MODELS_SKIP);
222
+ },
223
+ { primaryModel: null, smallFastModel: null }
224
+ );
225
+
226
+ return { primaryModel: responses.primaryModel, smallFastModel: responses.smallFastModel };
227
+ }
228
+
229
+ /**
230
+ * Prompt for Codex‑specific launch arguments.
231
+ */
232
+ async function promptCodexLaunchArgs(adder) {
233
+ // Reuse the same UI as the generic launch args but fetch Codex args.
234
+ console.log(
235
+ UIHelper.createTitle(UI_MESSAGES.CONFIG_CODEX_LAUNCH_ARGS_TITLE, UIHelper.icons.settings)
236
+ );
237
+ console.log();
238
+ console.log(UIHelper.createTooltip(UI_MESSAGES.CONFIG_CODEX_LAUNCH_ARGS_TOOLTIP));
239
+ console.log();
240
+ console.log(UIHelper.createStepIndicator(2, 2, UI_MESSAGES.ADD_PROVIDER_STEP_2_LAUNCH_ARGS));
241
+ console.log(
242
+ UIHelper.createHintLine([
243
+ [UI_MESSAGES.HINT_SPACE, UI_MESSAGES.HINT_SPACE_DESC],
244
+ [UI_MESSAGES.HINT_A, UI_MESSAGES.HINT_A_DESC],
245
+ [UI_MESSAGES.HINT_I, UI_MESSAGES.HINT_I_DESC],
246
+ [UI_MESSAGES.HINT_ENTER, UI_MESSAGES.HINT_ENTER_CONFIRM],
247
+ [UI_MESSAGES.HINT_ESC, UI_MESSAGES.HINT_ESC_SKIP]
248
+ ])
249
+ );
250
+ console.log();
251
+
252
+ const result = await adder.promptWithESCAndDefault(
253
+ [
254
+ {
255
+ type: 'checkbox',
256
+ name: 'launchArgs',
257
+ message: UI_MESSAGES.SELECT_LAUNCH_ARGS,
258
+ choices: getCodexLaunchArgs().map(arg => ({
259
+ name: `${arg.name} - ${arg.description}`,
260
+ value: arg.name,
261
+ checked: false
262
+ }))
263
+ }
264
+ ],
265
+ UI_MESSAGES.ESC_SKIP_CONFIG,
266
+ () => {
267
+ Logger.info(UI_MESSAGES.CONFIG_CODEX_LAUNCH_ARGS_SKIP);
268
+ },
269
+ { launchArgs: [] }
270
+ );
271
+
272
+ return result.launchArgs;
273
+ }
274
+
275
+ module.exports = {
276
+ promptProviderInfo,
277
+ promptLaunchArgs,
278
+ promptModelConfig,
279
+ promptCodexLaunchArgs
280
+ };
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Helper for saving a new provider and printing a summary.
3
+ * This mirrors the original `saveProvider` implementation but is split
4
+ * into a dedicated module to keep each file under 50 lines.
5
+ */
6
+
7
+ const { Logger } = require('../../utils/logger');
8
+ const { promptCodexLaunchArgs, promptLaunchArgs, promptModelConfig } = require('./prompts');
9
+ const { printProviderSummary } = require('./summaryPrinter');
10
+ const { registry } = require('../../CommandRegistry');
11
+
12
+ /**
13
+ * Save the provider configuration.
14
+ * @param {BaseCommand} adder - ProviderAdder instance (provides configManager, prompts, etc.)
15
+ * @param {Object} answers - Collected answers from the prompts.
16
+ */
17
+ async function saveProvider(adder, answers) {
18
+ try {
19
+ await adder.configManager.load();
20
+
21
+ // 如果已经存在同名供应商,询问是否覆盖
22
+ if (adder.configManager.getProvider(answers.name)) {
23
+ const shouldOverwrite = await adder.confirmOverwrite(answers.name);
24
+ if (!shouldOverwrite) {
25
+ Logger.warning('操作已取消');
26
+ return;
27
+ }
28
+ }
29
+
30
+ // 处理启动参数
31
+ let launchArgs = [];
32
+ if (answers.ideName === 'codex') {
33
+ launchArgs = answers.configureCodexLaunchArgs ? await promptCodexLaunchArgs(adder) : [];
34
+ } else {
35
+ launchArgs = answers.configureLaunchArgs ? await promptLaunchArgs(adder) : [];
36
+ }
37
+
38
+ // 处理模型配置(仅非 Codex)
39
+ let modelConfig = { primaryModel: null, smallFastModel: null };
40
+ if (answers.configureModels) {
41
+ modelConfig = await promptModelConfig(adder);
42
+ }
43
+
44
+ // 保存到配置管理器(保持不可变写法在 configManager 中实现)
45
+ await adder.configManager.addProvider(answers.name, {
46
+ displayName: answers.displayName || answers.name,
47
+ ideName: answers.ideName || 'claude',
48
+ baseUrl: answers.baseUrl,
49
+ authToken: answers.authToken,
50
+ authMode: answers.authMode,
51
+ codexFiles: answers.codexFiles || null,
52
+ launchArgs,
53
+ primaryModel: modelConfig.primaryModel,
54
+ smallFastModel: modelConfig.smallFastModel,
55
+ setAsDefault: answers.setAsDefault
56
+ });
57
+
58
+ // 打印摘要并返回主界面
59
+ await printProviderSummary(adder, answers, launchArgs, modelConfig);
60
+ await adder.pauseBeforeReturn();
61
+
62
+ if (adder.returnToParent) {
63
+ return;
64
+ }
65
+
66
+ return await registry.executeCommand('switch');
67
+ } catch (error) {
68
+ if (adder.isEscCancelled(error)) return;
69
+ Logger.error(`添加供应商失败: ${error.message}`);
70
+ throw error;
71
+ }
72
+ }
73
+
74
+ module.exports = { saveProvider };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Helper for printing provider summary after creation.
3
+ * Mirrors the original `printProviderSummary` logic but isolated.
4
+ */
5
+ const chalk = require('chalk');
6
+ const { Logger } = require('../../utils/logger');
7
+ const { UIHelper } = require('../../utils/ui-helper');
8
+ const { UI_MESSAGES } = require('../../constants/ui');
9
+
10
+ async function printProviderSummary(adder, answers, launchArgs, modelConfig) {
11
+ console.log(UIHelper.createTitle('供应商已创建', UIHelper.icons.success));
12
+ console.log();
13
+ console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_NAME}: ${answers.name}`));
14
+ console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_IDE}: ${answers.ideName || 'claude'}`));
15
+ if (answers.baseUrl) console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_BASE_URL}: ${answers.baseUrl}`));
16
+ if (answers.authToken) console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_TOKEN}: ${answers.authToken}`));
17
+ if (launchArgs && launchArgs.length) {
18
+ console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_LAUNCH_ARGS}:`));
19
+ launchArgs.forEach(arg => console.log(chalk.gray(` - ${arg}`)));
20
+ }
21
+ if (modelConfig && (modelConfig.primaryModel || modelConfig.smallFastModel)) {
22
+ console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_MODEL_CONFIG}:`));
23
+ if (modelConfig.primaryModel) console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_PRIMARY_MODEL}: ${modelConfig.primaryModel}`));
24
+ if (modelConfig.smallFastModel) console.log(chalk.gray(` ${UI_MESSAGES.PROVIDER_SMALL_FAST_MODEL}: ${modelConfig.smallFastModel}`));
25
+ }
26
+ Logger.success(UI_MESSAGES.ADD_PROVIDER_SUCCESS);
27
+ }
28
+
29
+ module.exports = { printProviderSummary };