@pikecode/api-key-manager 2.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 API Key Manager Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # API 密钥管理工具 (API Key Manager)
2
+
3
+ 一键管理和切换多个 API 提供商配置,支持 ANTHROPIC_API_KEY 和 ANTHROPIC_AUTH_TOKEN,让你轻松管理多个账号。
4
+
5
+ > 🤖 本项目完全由 AI 开发 - 使用 [Claude Code](https://claude.ai/code) 构建
6
+
7
+ ## 💡 这个工具是干什么的?
8
+
9
+ 如果你需要:
10
+
11
+ - ✅ 管理多个 API 密钥配置
12
+ - ✅ 支持 ANTHROPIC_API_KEY 和 ANTHROPIC_AUTH_TOKEN
13
+ - ✅ 快速切换不同的 API 提供商
14
+ - ✅ 配置第三方代理服务
15
+ - ✅ 快速保存和切换多套配置
16
+
17
+ 那这个工具就是为你准备的!只需要一条命令 `akm`,就能快速切换环境。
18
+
19
+ ## 🚀 快速开始
20
+
21
+ ### 第一步:安装工具
22
+
23
+ 打开终端(命令行),运行:
24
+
25
+ ```bash
26
+ npm install -g @pikecode/api-key-manager
27
+ ```
28
+
29
+ ### 第二步:添加你的第一个 API 配置
30
+
31
+ ```bash
32
+ akm add
33
+ ```
34
+
35
+ 根据提示输入:
36
+ 1. **名称**:给这个配置起个名字,比如 "官方账号"、"代理服务" 等
37
+ 2. **认证方式**:选择认证模式
38
+ - 🔑 通用API密钥模式 - 支持 ANTHROPIC_API_KEY 和 ANTHROPIC_AUTH_TOKEN
39
+ - 🔐 认证令牌模式 - 仅 ANTHROPIC_AUTH_TOKEN
40
+ - 🌐 OAuth令牌模式 - 用于官方 Claude Code
41
+ 3. **Token 类型**(仅通用API密钥模式):选择 ANTHROPIC_API_KEY 或 ANTHROPIC_AUTH_TOKEN
42
+ 4. **Token**:粘贴你的 API Token
43
+ 5. **基础 URL**(可选):如果使用第三方服务需要填写
44
+
45
+ ### 第三步:开始使用
46
+
47
+ ```bash
48
+ akm
49
+ ```
50
+
51
+ 用方向键选择要使用的 API 配置,按回车确认,工具会自动启动 Claude Code!
52
+
53
+ ## 📖 常用命令
54
+
55
+ ```bash
56
+ akm # 打开选择界面,切换 API 配置
57
+ akm add # 添加新的 API 配置
58
+ akm list # 查看所有已保存的配置
59
+ akm current # 查看当前正在使用的配置
60
+ akm edit # 修改某个配置
61
+ akm remove # 删除不需要的配置
62
+ ```
63
+
64
+ ## ✨ 主要功能
65
+
66
+ ### 🔄 智能环境切换
67
+ - 一键切换多个 Claude Code 供应商配置
68
+ - 支持官方和第三方 API 服务商
69
+ - 自动管理环境变量,无需手动配置
70
+
71
+ ### 🛡️ 冲突检测与处理
72
+ - **自动检测** Claude 设置文件中的环境变量冲突
73
+ - **智能备份** 在修改前自动创建配置备份
74
+ - **安全清理** 清空冲突变量,确保切换成功
75
+
76
+ ### 🚀 启动参数配置
77
+ - 支持 `--continue` 参数继续上次对话
78
+ - 支持 `--dangerously-skip-permissions` 沙盒模式
79
+ - 灵活选择启动参数组合
80
+
81
+ ### 📡 实时状态检测
82
+ - **流式检测** 供应商可用性实时检查,逐个显示结果
83
+ - **状态指示** 使用彩色图标直观显示供应商状态(🟢可用 / 🟡有限可用 / 🔴不可用)
84
+ - **延迟显示** 实时显示 API 响应延迟,帮助选择最快的服务
85
+ - **智能识别** 自动识别不同供应商的响应格式,兼容性强
86
+ - **异步更新** 状态检测不阻塞界面操作,边检测边选择
87
+
88
+ ### ⌨️ 优化的键盘操作
89
+ - **ESC 键** - 快速返回上一级菜单
90
+ - **Ctrl+C** - 随时强制退出程序
91
+ - **方向键** - 便捷的菜单导航
92
+ - **空格/A/I** - 多选操作(全选/反选)
93
+
94
+ ### 📊 使用统计
95
+ - 记录每个供应商的使用次数
96
+ - 显示最后使用时间
97
+ - 智能推荐常用供应商
98
+
99
+ ## 🎯 使用场景示例
100
+
101
+ ### 场景1:工作和个人账号切换
102
+
103
+ ```bash
104
+ # 上班时使用公司账号
105
+ akm 公司账号
106
+
107
+ # 下班后切换到个人账号
108
+ akm 个人账号
109
+ ```
110
+
111
+ ### 场景2:使用第三方服务
112
+
113
+ 有些第三方服务提供 Claude API 代理,你可以这样配置:
114
+
115
+ ```bash
116
+ akm add
117
+ # 输入名称: 第三方服务
118
+ # 选择认证方式: API 密钥模式
119
+ # 输入 API 密钥: 粘贴你的密钥
120
+ # 输入基础 URL: https://api.third-party.com
121
+ ```
122
+
123
+ ### 场景3:临时测试新服务
124
+
125
+ ```bash
126
+ # 添加测试配置
127
+ akm add
128
+
129
+ # 试用一下
130
+ akm 测试服务
131
+
132
+ # 不好用就删掉
133
+ akm remove 测试服务
134
+ ```
135
+
136
+ ## ⚙️ 认证方式说明
137
+
138
+ 添加供应商时,会让你选择认证方式,这里简单说明:
139
+
140
+ **1. OAuth 令牌模式** (最常用)
141
+ - 适用于官方 Claude Code
142
+ - Token 格式: `sk-ant-oat01-...`
143
+ - 只需要填 Token,不需要填 URL
144
+
145
+ **2. API 密钥模式**
146
+ - 适用于第三方服务商
147
+ - 需要填写 API 密钥和服务商提供的 URL
148
+
149
+ **3. 认证令牌模式**
150
+ - 适用于某些第三方服务商
151
+ - 需要填写认证令牌和服务商提供的 URL
152
+
153
+ > 💡 不确定选哪个?看你的 Token 是什么格式,或者咨询你的服务商。
154
+
155
+ ## ❓ 常见问题
156
+
157
+ **Q: 我的 Token 存在哪里,安全吗?**
158
+ A: 配置保存在你的电脑上 `~/.akm-config.json`,只有你能访问。Token 会被加密存储。
159
+
160
+ **Q: 什么是"环境变量冲突检测"?**
161
+ A: 如果你在 Claude Code 的设置文件(`.claude/settings.json` 或 `.claude/settings.local.json`)中配置了环境变量,这些变量可能会覆盖本工具设置的供应商配置。工具会自动检测这种冲突,并提示你备份和清理冲突的变量。
162
+
163
+ **Q: 备份文件保存在哪里?**
164
+ A: 备份文件保存在原设置文件的同目录下,命名格式为 `settings.backup-YYYYMMDD_HHmmss.json`,方便你随时恢复。
165
+
166
+ **Q: 状态检测是如何工作的?**
167
+ A: 工具会向每个供应商的 API 发送一个简单的测试请求,根据响应时间和结果显示状态。🟢表示可用,🟡表示有限可用(响应慢或部分功能受限),🔴表示不可用,⏳表示正在检测中。状态检测采用流式模式,逐个显示结果,不会阻塞界面操作。
168
+
169
+ **Q: 为什么某些供应商显示"暂不支持 OAuth 令牌检测"?**
170
+ A: OAuth 令牌模式的供应商(官方 Claude Code)暂不支持状态检测,因为 OAuth 认证流程比较复杂。工具只对使用 API 密钥或认证令牌的第三方供应商进行状态检测。
171
+
172
+ **Q: 如何重新来过?**
173
+ A: 删除配置文件 `~/.akm-config.json` 就会清空所有配置。
174
+
175
+ **Q: 可以导出配置到其他电脑吗?**
176
+ A: 可以!复制 `~/.akm-config.json` 文件到新电脑的相同位置即可。
177
+
178
+ **Q: 按 ESC 键没反应?**
179
+ A: 确保你的终端支持 ESC 键捕获。如果不行,使用 `Ctrl+C` 也可以退出程序。
180
+
181
+ **Q: 需要什么系统?**
182
+ A: Windows、macOS、Linux 都支持。需要安装 Node.js 14 或更高版本。
183
+
184
+ **Q: 启动参数有什么用?**
185
+ A: `--continue` 可以继续上次的对话,`--dangerously-skip-permissions` 适合在沙盒环境中使用(跳过权限检查)。你可以在启动时灵活选择需要的参数。
186
+
187
+ ## 📝 更新日志
188
+
189
+ ### v2.0.0 (最新)
190
+ - 🎉 **重大更新** - 项目重命名为 API Key Manager,全面升级
191
+ - 🔄 **命令变更** - `cc` 命令升级为 `akm`
192
+ - 🔑 **功能扩展** - API 密钥模式现支持 ANTHROPIC_API_KEY 和 ANTHROPIC_AUTH_TOKEN 两种 Token 类型选择
193
+ - 📋 **配置优化** - 配置文件从 `~/.cc-config.json` 升级为 `~/.akm-config.json`
194
+ - 🎯 **用户体验** - 优化交互设计,支持更灵活的 Token 类型选择
195
+
196
+ ### v1.0.13
197
+ - ✨ 实现供应商状态流式检测和实时更新
198
+ - 📡 将批量检测改为流式模式,逐个显示状态结果
199
+ - 🎨 优化状态图标显示,支持实时延迟展示
200
+ - 🚀 增强UI交互,支持动态更新选择界面
201
+ - 🔧 改进异步调度逻辑,提升响应速度
202
+
203
+ ### v1.0.12
204
+ - ✨ 新增供应商状态检测功能
205
+ - 📡 添加 ProviderStatusChecker 类用于检测供应商可用性
206
+ - 🎨 在供应商列表和选择界面显示实时状态信息
207
+ - 🟢 支持状态图标显示(可用/有限可用/不可用)和延迟时间展示
208
+ - 🚀 异步更新状态不阻塞界面操作
209
+
210
+ ### v1.0.9
211
+ - ✨ 新增 Claude 设置文件冲突检测功能
212
+ - 🛡️ 自动备份和清理冲突的环境变量
213
+ - ⌨️ 增强 ESC 键处理系统,支持各层级快速返回
214
+ - 🎨 改进输入提示行为和屏幕清空功能
215
+ - 🔧 重构 Windows 支持模块,提升跨平台兼容性
216
+
217
+ ### v1.0.8
218
+ - 🎯 优化用户界面和交互体验
219
+ - 📊 改进供应商使用统计功能
220
+
221
+ ## 🆘 遇到问题?
222
+
223
+ 1. 查看 [常见问题](https://github.com/pikecode/api-key-manager/issues)
224
+ 2. 提交 [新问题](https://github.com/pikecode/api-key-manager/issues/new)
225
+
226
+ ---
227
+
228
+ ## 🤖 关于本项目
229
+
230
+ 本项目完全由 AI 使用 [Claude Code](https://claude.ai/code) 开发完成。
231
+
232
+ **API Key Manager** - 让 API 密钥管理变得简单 🚀
package/bin/akm.js ADDED
@@ -0,0 +1,101 @@
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('添加新的API密钥配置')
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('删除API密钥配置')
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('列出所有API密钥配置')
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('编辑API密钥配置')
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();
package/bin/cc.js ADDED
@@ -0,0 +1,101 @@
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();
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@pikecode/api-key-manager",
3
+ "version": "2.0.0",
4
+ "description": "API密钥管理工具 - A powerful CLI tool for managing and switching multiple API provider configurations (supports ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, and more)",
5
+ "main": "src/index.js",
6
+ "bin": {
7
+ "akm": "bin/akm.js"
8
+ },
9
+ "files": [
10
+ "bin/",
11
+ "src/",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "scripts": {
16
+ "start": "node bin/akm.js",
17
+ "dev": "nodemon bin/akm.js",
18
+ "test": "jest",
19
+ "build": "echo 'No build needed'",
20
+ "prepare": "echo 'Preparing package'",
21
+ "prepublishOnly": "npm test",
22
+ "release": "npm version patch && npm publish --access public"
23
+ },
24
+ "keywords": [
25
+ "api",
26
+ "api-key",
27
+ "key-manager",
28
+ "authentication",
29
+ "anthropic",
30
+ "anthropic-api-key",
31
+ "anthropic-auth-token",
32
+ "environment",
33
+ "config",
34
+ "provider",
35
+ "switcher",
36
+ "cli",
37
+ "command-line",
38
+ "tool"
39
+ ],
40
+ "author": "Developer",
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/pikecode/api-key-manager.git"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/pikecode/api-key-manager/issues"
48
+ },
49
+ "homepage": "https://github.com/pikecode/api-key-manager#readme",
50
+ "dependencies": {
51
+ "@anthropic-ai/sdk": "^0.67.0",
52
+ "chalk": "^4.1.2",
53
+ "commander": "^9.4.1",
54
+ "cross-spawn": "^7.0.3",
55
+ "fs-extra": "^11.1.1",
56
+ "inquirer": "^8.2.6",
57
+ "supports-color": "^9.4.0"
58
+ },
59
+ "devDependencies": {
60
+ "jest": "^29.7.0",
61
+ "nodemon": "^3.0.1"
62
+ },
63
+ "engines": {
64
+ "node": ">=14.0.0"
65
+ },
66
+ "os": [
67
+ "win32",
68
+ "darwin",
69
+ "linux"
70
+ ]
71
+ }
@@ -0,0 +1,74 @@
1
+ class CommandRegistry {
2
+ constructor() {
3
+ this.commands = new Map();
4
+ this.lazyCommands = new Map();
5
+ }
6
+
7
+ // 注册懒加载命令
8
+ registerLazy(name, loader) {
9
+ this.lazyCommands.set(name, loader);
10
+ }
11
+
12
+ // 获取命令(懒加载)
13
+ async getCommand(name) {
14
+ if (this.commands.has(name)) {
15
+ return this.commands.get(name);
16
+ }
17
+
18
+ if (this.lazyCommands.has(name)) {
19
+ const loader = this.lazyCommands.get(name);
20
+ const command = await loader();
21
+ this.commands.set(name, command);
22
+ return command;
23
+ }
24
+
25
+ throw new Error(`命令 '${name}' 未注册`);
26
+ }
27
+
28
+ // 执行命令
29
+ async executeCommand(name, ...args) {
30
+ const command = await this.getCommand(name);
31
+ return await command(...args);
32
+ }
33
+
34
+ // 清理所有缓存的命令
35
+ clear() {
36
+ this.commands.clear();
37
+ }
38
+ }
39
+
40
+ // 单例实例
41
+ const registry = new CommandRegistry();
42
+
43
+ // 注册所有懒加载命令
44
+ registry.registerLazy('switch', async () => {
45
+ const { switchCommand } = require('./commands/switch');
46
+ return switchCommand;
47
+ });
48
+
49
+ registry.registerLazy('add', async () => {
50
+ const { addCommand } = require('./commands/add');
51
+ return addCommand;
52
+ });
53
+
54
+ registry.registerLazy('remove', async () => {
55
+ const { removeCommand } = require('./commands/remove');
56
+ return removeCommand;
57
+ });
58
+
59
+ registry.registerLazy('list', async () => {
60
+ const { listCommand } = require('./commands/list');
61
+ return listCommand;
62
+ });
63
+
64
+ registry.registerLazy('current', async () => {
65
+ const { currentCommand } = require('./commands/current');
66
+ return currentCommand;
67
+ });
68
+
69
+ registry.registerLazy('edit', async () => {
70
+ const { editCommand } = require('./commands/edit');
71
+ return editCommand;
72
+ });
73
+
74
+ module.exports = { CommandRegistry, registry };