@pikecode/api-key-manager 2.0.0 → 2.0.2

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 +558 -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 +212 -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 +136 -89
  59. package/bin/cc.js +0 -101
  60. package/src/utils/storage.js +0 -55
@@ -1,17 +1,67 @@
1
+ const { getClaudeLaunchArgs } = require('./launch-args');
2
+ const { containsUnsafeTerminalCharacters } = require('./terminal-format');
3
+
4
+ function isLoopbackHostname(hostname) {
5
+ const normalizedHostname = hostname.toLowerCase();
6
+ return (
7
+ normalizedHostname === 'localhost' ||
8
+ normalizedHostname.endsWith('.localhost') ||
9
+ normalizedHostname === '::1' ||
10
+ normalizedHostname === '[::1]' ||
11
+ /^127(?:\.\d{1,3}){3}$/.test(normalizedHostname)
12
+ );
13
+ }
14
+
1
15
  const validator = {
2
16
  validateName(name) {
3
17
  if (!name || typeof name !== 'string') {
4
18
  return '供应商名称不能为空';
5
19
  }
6
-
20
+
7
21
  if (name.trim().length === 0) {
8
22
  return '供应商名称不能为空或只包含空格';
9
23
  }
10
-
24
+
25
+ if (containsUnsafeTerminalCharacters(name)) {
26
+ return '供应商名称不能包含控制字符';
27
+ }
28
+
29
+ // 禁止使用保留名称 (Windows)
30
+ const reserved = [
31
+ '__PROTO__',
32
+ 'PROTOTYPE',
33
+ 'CONSTRUCTOR',
34
+ 'CON',
35
+ 'PRN',
36
+ 'AUX',
37
+ 'NUL',
38
+ 'COM1',
39
+ 'COM2',
40
+ 'COM3',
41
+ 'COM4',
42
+ 'COM5',
43
+ 'COM6',
44
+ 'COM7',
45
+ 'COM8',
46
+ 'COM9',
47
+ 'LPT1',
48
+ 'LPT2',
49
+ 'LPT3',
50
+ 'LPT4',
51
+ 'LPT5',
52
+ 'LPT6',
53
+ 'LPT7',
54
+ 'LPT8',
55
+ 'LPT9'
56
+ ];
57
+ if (reserved.includes(name.toUpperCase())) {
58
+ return '供应商名称不能使用系统保留名称';
59
+ }
60
+
11
61
  if (name.length > 100) {
12
62
  return '供应商名称不能超过100个字符';
13
63
  }
14
-
64
+
15
65
  return null;
16
66
  },
17
67
 
@@ -20,45 +70,108 @@ const validator = {
20
70
  if (displayName === null || displayName === undefined || displayName === '') {
21
71
  return null;
22
72
  }
23
-
73
+
24
74
  if (typeof displayName !== 'string') {
25
75
  return '显示名称必须是字符串';
26
76
  }
27
-
77
+
78
+ if (containsUnsafeTerminalCharacters(displayName)) {
79
+ return '显示名称不能包含控制字符';
80
+ }
81
+
28
82
  if (displayName.length > 100) {
29
83
  return '显示名称不能超过100个字符';
30
84
  }
31
-
85
+
32
86
  return null;
33
87
  },
34
88
 
35
- validateUrl(url, required = true) {
36
- if (!url || typeof url !== 'string') {
89
+ validateUrl(url, required = true, options = {}) {
90
+ if (url === null || url === undefined || url === '') {
37
91
  return required ? 'URL不能为空' : null;
38
92
  }
39
-
93
+
94
+ if (typeof url !== 'string') {
95
+ return 'URL必须是字符串';
96
+ }
97
+
98
+ let parsedUrl;
40
99
  try {
41
- new URL(url);
42
- } catch (error) {
100
+ parsedUrl = new URL(url);
101
+ } catch {
43
102
  return '请输入有效的URL';
44
103
  }
45
-
46
- if (!url.startsWith('http://') && !url.startsWith('https://')) {
104
+
105
+ if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
47
106
  return 'URL必须以http://或https://开头';
48
107
  }
49
-
108
+
109
+ if (/[\x00-\x1F"\\]/.test(url)) {
110
+ return 'URL包含不安全字符';
111
+ }
112
+
113
+ if (
114
+ parsedUrl.protocol === 'http:' &&
115
+ !isLoopbackHostname(parsedUrl.hostname) &&
116
+ options.allowInsecureHttp === false
117
+ ) {
118
+ return '远程 API 地址必须使用 HTTPS;HTTP 仅允许 localhost 或回环地址';
119
+ }
120
+
50
121
  return null;
51
122
  },
52
123
 
124
+ isInsecureRemoteHttp(url) {
125
+ if (typeof url !== 'string') {
126
+ return false;
127
+ }
128
+
129
+ try {
130
+ const parsedUrl = new URL(url);
131
+ return parsedUrl.protocol === 'http:' && !isLoopbackHostname(parsedUrl.hostname);
132
+ } catch {
133
+ return false;
134
+ }
135
+ },
136
+
53
137
  validateToken(token) {
54
138
  if (!token || typeof token !== 'string') {
55
139
  return 'Token不能为空';
56
140
  }
57
-
58
- if (token.length < 10) {
59
- return 'Token长度不能少于10个字符';
141
+
142
+ if (token.trim().length === 0) {
143
+ return 'Token不能只包含空格';
144
+ }
145
+
146
+ if (containsUnsafeTerminalCharacters(token)) {
147
+ return 'Token不能包含控制字符';
148
+ }
149
+
150
+ // 检测常见的占位符文本
151
+ const placeholders = [
152
+ 'your-key-here',
153
+ 'your-token',
154
+ 'your_key',
155
+ 'your_token',
156
+ 'example',
157
+ 'test-key',
158
+ 'demo',
159
+ 'placeholder',
160
+ 'replace-me',
161
+ 'insert-key',
162
+ 'api-key-here',
163
+ 'token-here',
164
+ 'xxx',
165
+ 'yyy',
166
+ 'zzz',
167
+ 'abc123',
168
+ '123456'
169
+ ];
170
+ const lowerToken = token.toLowerCase();
171
+ if (placeholders.some(p => lowerToken.includes(p))) {
172
+ return 'Token 似乎是占位符,请输入真实的 API Token';
60
173
  }
61
-
174
+
62
175
  return null;
63
176
  },
64
177
 
@@ -67,91 +180,25 @@ const validator = {
67
180
  if (!model) {
68
181
  return null;
69
182
  }
70
-
183
+
71
184
  if (typeof model !== 'string') {
72
185
  return '模型名称必须是字符串';
73
186
  }
74
-
187
+
75
188
  if (model.trim().length === 0) {
76
189
  return '模型名称不能为空字符串';
77
190
  }
78
-
191
+
79
192
  if (model.length > 100) {
80
193
  return '模型名称不能超过100个字符';
81
194
  }
82
-
83
- return null;
84
- },
85
195
 
86
- validateLaunchArgs(args) {
87
- if (!Array.isArray(args)) {
88
- return '启动参数必须是数组';
89
- }
90
-
91
- const validArgs = [
92
- '--dangerously-skip-permissions',
93
- '--no-confirm',
94
- '--allow-all',
95
- '--auto-approve',
96
- '--yes',
97
- '--force'
98
- ];
99
-
100
- for (const arg of args) {
101
- if (!validArgs.includes(arg)) {
102
- return `无效的启动参数: ${arg}`;
103
- }
104
- }
105
-
106
196
  return null;
107
197
  },
108
198
 
109
199
  getAvailableLaunchArgs() {
110
- return [
111
- {
112
- name: '--continue',
113
- label: '继续上次对话',
114
- description: '恢复上次的对话记录',
115
- checked: false
116
- },
117
- {
118
- name: '--dangerously-skip-permissions',
119
- label: '最高权限',
120
- description: '仅限沙盒环境使用',
121
- checked: false
122
- },
123
- {
124
- name: '--no-confirm',
125
- label: '直接执行操作',
126
- description: '跳过确认提示',
127
- checked: false
128
- },
129
- {
130
- name: '--allow-all',
131
- label: '允许全部操作',
132
- description: '移除安全限制',
133
- checked: false
134
- },
135
- {
136
- name: '--auto-approve',
137
- label: '自动批准请求',
138
- description: '无需人工同意',
139
- checked: false
140
- },
141
- {
142
- name: '--yes',
143
- label: '默认回答 yes',
144
- description: '自动同意所有询问',
145
- checked: false
146
- },
147
- {
148
- name: '--force',
149
- label: '强制执行',
150
- description: '忽略可能的警告',
151
- checked: false
152
- }
153
- ];
200
+ return getClaudeLaunchArgs();
154
201
  }
155
202
  };
156
203
 
157
- module.exports = { validator };
204
+ module.exports = { validator };
package/bin/cc.js DELETED
@@ -1,101 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const { program } = require('commander');
4
- const chalk = require('chalk');
5
- const { main } = require('../src/index');
6
- const { registry } = require('../src/CommandRegistry');
7
- const pkg = require('../package.json');
8
- const { checkForUpdates } = require('../src/utils/update-checker');
9
-
10
- // Set up CLI
11
- program
12
- .name('akm')
13
- .description('API密钥管理工具 - Manage and switch multiple API provider configurations')
14
- .version(pkg.version, '-v, -V, --version', '显示版本号');
15
-
16
- // Check for updates before any command runs
17
- program.hook('preAction', async () => {
18
- await checkForUpdates({ packageName: pkg.name, currentVersion: pkg.version });
19
- });
20
-
21
- // Default command - show provider selection
22
- program
23
- .argument('[provider]', '直接切换到指定供应商')
24
- .action(async (provider) => {
25
- try {
26
- await main(provider);
27
- } catch (error) {
28
- console.error(chalk.red('❌ 执行失败:'), error.message);
29
- process.exit(1);
30
- }
31
- });
32
-
33
- // Add command
34
- program
35
- .command('add')
36
- .description('添加新供应商配置')
37
- .action(async () => {
38
- try {
39
- await registry.executeCommand('add');
40
- } catch (error) {
41
- console.error(chalk.red('❌ 添加失败:'), error.message);
42
- process.exit(1);
43
- }
44
- });
45
-
46
- // Remove command
47
- program
48
- .command('remove')
49
- .argument('[provider]', '要删除的供应商名称')
50
- .description('删除供应商配置')
51
- .action(async (provider) => {
52
- try {
53
- await registry.executeCommand('remove', provider);
54
- } catch (error) {
55
- console.error(chalk.red('❌ 删除失败:'), error.message);
56
- process.exit(1);
57
- }
58
- });
59
-
60
- // List command
61
- program
62
- .command('list')
63
- .description('列出所有供应商')
64
- .action(async () => {
65
- try {
66
- await registry.executeCommand('list');
67
- } catch (error) {
68
- console.error(chalk.red('❌ 列表失败:'), error.message);
69
- process.exit(1);
70
- }
71
- });
72
-
73
- // Current command
74
- program
75
- .command('current')
76
- .description('显示当前配置')
77
- .action(async () => {
78
- try {
79
- await registry.executeCommand('current');
80
- } catch (error) {
81
- console.error(chalk.red('❌ 获取当前配置失败:'), error.message);
82
- process.exit(1);
83
- }
84
- });
85
-
86
- // Edit command
87
- program
88
- .command('edit')
89
- .argument('[provider]', '要编辑的供应商名称')
90
- .description('编辑供应商配置')
91
- .action(async (provider) => {
92
- try {
93
- await registry.executeCommand('edit', provider);
94
- } catch (error) {
95
- console.error(chalk.red('❌ 编辑失败:'), error.message);
96
- process.exit(1);
97
- }
98
- });
99
-
100
- // Parse arguments
101
- program.parse();
@@ -1,55 +0,0 @@
1
- const fs = require('fs-extra');
2
- const path = require('path');
3
- const os = require('os');
4
-
5
- class Storage {
6
- static async ensureConfigDir() {
7
- const configDir = path.join(os.homedir(), '.akm-config');
8
- await fs.ensureDir(configDir);
9
- return configDir;
10
- }
11
-
12
- static async readConfig() {
13
- const configPath = path.join(os.homedir(), '.akm-config.json');
14
- try {
15
- if (await fs.pathExists(configPath)) {
16
- return await fs.readJSON(configPath);
17
- }
18
- return null;
19
- } catch (error) {
20
- throw new Error(`读取配置文件失败: ${error.message}`);
21
- }
22
- }
23
-
24
- static async writeConfig(config) {
25
- const configPath = path.join(os.homedir(), '.akm-config.json');
26
- try {
27
- await fs.writeJSON(configPath, config, { spaces: 2 });
28
- return true;
29
- } catch (error) {
30
- throw new Error(`写入配置文件失败: ${error.message}`);
31
- }
32
- }
33
-
34
- static async backupConfig() {
35
- const configPath = path.join(os.homedir(), '.akm-config.json');
36
- const backupPath = path.join(os.homedir(), '.akm-config.backup.json');
37
-
38
- if (await fs.pathExists(configPath)) {
39
- await fs.copy(configPath, backupPath);
40
- return backupPath;
41
- }
42
- return null;
43
- }
44
-
45
- static async restoreConfig(backupPath) {
46
- const configPath = path.join(os.homedir(), '.akm-config.json');
47
- if (await fs.pathExists(backupPath)) {
48
- await fs.copy(backupPath, configPath);
49
- return true;
50
- }
51
- return false;
52
- }
53
- }
54
-
55
- module.exports = { Storage };