@pikecode/api-key-manager 2.0.2 → 2.0.4
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/package.json +1 -1
- package/src/commands/add/prompts.js +20 -2
- package/src/commands/add/providerSaver.js +23 -0
- package/src/commands/add.js +2 -1
- package/src/commands/remove.js +7 -5
- package/src/commands/switch/provider-choices-helper.js +6 -1
- package/src/commands/switch.js +8 -4
- package/src/config.js +4 -3
- package/src/constants/ui.js +7 -0
- package/src/utils/codex-files.js +38 -0
- package/src/utils/codex-launcher.js +32 -11
- package/src/utils/env-launcher.js +6 -7
- package/src/utils/import-validator.js +2 -2
- package/src/utils/provider-status-checker.js +2 -1
package/package.json
CHANGED
|
@@ -77,6 +77,17 @@ async function promptProviderInfo(adder) {
|
|
|
77
77
|
when: answers => (answers.ideName || adder.presetIdeName) !== 'codex'
|
|
78
78
|
},
|
|
79
79
|
// Codex‑specific prompts (manual entry)
|
|
80
|
+
{
|
|
81
|
+
type: 'list',
|
|
82
|
+
name: 'authMode',
|
|
83
|
+
message: UI_MESSAGES.SELECT_CODEX_AUTH_MODE,
|
|
84
|
+
choices: [
|
|
85
|
+
{ name: UI_MESSAGES.AUTH_MODE_CODEX_API_KEY, value: 'api_key' },
|
|
86
|
+
{ name: UI_MESSAGES.AUTH_MODE_CODEX_CHATGPT_LOGIN, value: 'chatgpt_login' }
|
|
87
|
+
],
|
|
88
|
+
default: 'api_key',
|
|
89
|
+
when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
|
|
90
|
+
},
|
|
80
91
|
{
|
|
81
92
|
type: 'input',
|
|
82
93
|
name: 'baseUrl',
|
|
@@ -87,7 +98,7 @@ async function promptProviderInfo(adder) {
|
|
|
87
98
|
const err = validator.validateUrl(input);
|
|
88
99
|
return err || true;
|
|
89
100
|
},
|
|
90
|
-
when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
|
|
101
|
+
when: answers => (answers.ideName || adder.presetIdeName) === 'codex' && answers.authMode !== 'chatgpt_login'
|
|
91
102
|
},
|
|
92
103
|
{
|
|
93
104
|
type: 'input',
|
|
@@ -98,7 +109,14 @@ async function promptProviderInfo(adder) {
|
|
|
98
109
|
const err = validator.validateToken(input);
|
|
99
110
|
return err || true;
|
|
100
111
|
},
|
|
101
|
-
when: answers => (answers.ideName || adder.presetIdeName) === 'codex'
|
|
112
|
+
when: answers => (answers.ideName || adder.presetIdeName) === 'codex' && answers.authMode !== 'chatgpt_login'
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: 'confirm',
|
|
116
|
+
name: 'confirmCodexLogin',
|
|
117
|
+
message: 'Codex 将使用官方 OpenAI 网页登录方式,启动时需要在浏览器中登录。确认继续?',
|
|
118
|
+
default: true,
|
|
119
|
+
when: answers => (answers.ideName || adder.presetIdeName) === 'codex' && answers.authMode === 'chatgpt_login'
|
|
102
120
|
},
|
|
103
121
|
{
|
|
104
122
|
type: 'confirm',
|
|
@@ -55,6 +55,29 @@ async function saveProvider(adder, answers) {
|
|
|
55
55
|
setAsDefault: answers.setAsDefault
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
+
// 如果是首个 Codex 配置,自动创建官方登录配置
|
|
59
|
+
if (answers.ideName === 'codex') {
|
|
60
|
+
const existingCodexProviders = Object.values(adder.configManager.getAllProviders())
|
|
61
|
+
.filter(p => p.ideName === 'codex');
|
|
62
|
+
|
|
63
|
+
// 只有当刚才添加的是第一个 Codex 配置,且没有官方配置时才创建
|
|
64
|
+
if (existingCodexProviders.length === 1 && !adder.configManager.getProvider('openai-official')) {
|
|
65
|
+
await adder.configManager.addProvider('openai-official', {
|
|
66
|
+
displayName: 'OpenAI Official',
|
|
67
|
+
ideName: 'codex',
|
|
68
|
+
authMode: 'chatgpt_login',
|
|
69
|
+
baseUrl: null,
|
|
70
|
+
authToken: null,
|
|
71
|
+
codexFiles: null,
|
|
72
|
+
launchArgs: [],
|
|
73
|
+
primaryModel: null,
|
|
74
|
+
smallFastModel: null,
|
|
75
|
+
setAsDefault: false
|
|
76
|
+
});
|
|
77
|
+
Logger.info('✨ 自动创建官方登录配置: openai-official');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
58
81
|
// 打印摘要并返回主界面
|
|
59
82
|
await printProviderSummary(adder, answers, launchArgs, modelConfig);
|
|
60
83
|
await adder.pauseBeforeReturn();
|
package/src/commands/add.js
CHANGED
package/src/commands/remove.js
CHANGED
|
@@ -78,7 +78,7 @@ class ProviderRemover extends BaseCommand {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
async interactiveRemove() {
|
|
81
|
+
async interactiveRemove(options = {}) {
|
|
82
82
|
await this.configManager.ensureLoaded();
|
|
83
83
|
const providers = this.configManager.listProviders();
|
|
84
84
|
|
|
@@ -98,12 +98,14 @@ class ProviderRemover extends BaseCommand {
|
|
|
98
98
|
checked: false
|
|
99
99
|
}));
|
|
100
100
|
|
|
101
|
-
// 设置 ESC 键监听
|
|
102
101
|
const escListener = this.createESCListener(async () => {
|
|
103
102
|
Logger.info('取消删除供应商');
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
if (typeof options.onBack === 'function') {
|
|
104
|
+
await options.onBack();
|
|
105
|
+
} else {
|
|
106
|
+
const { registry } = require('../CommandRegistry');
|
|
107
|
+
await registry.executeCommand('switch');
|
|
108
|
+
}
|
|
107
109
|
}, '取消删除');
|
|
108
110
|
|
|
109
111
|
try {
|
|
@@ -49,8 +49,13 @@ class ProviderChoicesHelper {
|
|
|
49
49
|
: chalk.magenta('[Claude]');
|
|
50
50
|
const lastUsedLabel = isLastUsed ? UIHelper.colors.muted(' --- 上次使用') : '';
|
|
51
51
|
|
|
52
|
+
// 官方登录特殊标记
|
|
53
|
+
const authModeLabel = provider.authMode === 'chatgpt_login'
|
|
54
|
+
? chalk.green(' 🌐 官方登录')
|
|
55
|
+
: '';
|
|
56
|
+
|
|
52
57
|
return {
|
|
53
|
-
name: `${icon} ${ideTag} ${UIHelper.formatProvider(provider)}${lastUsedLabel} ${statusLabel}`,
|
|
58
|
+
name: `${icon} ${ideTag} ${UIHelper.formatProvider(provider)}${authModeLabel}${lastUsedLabel} ${statusLabel}`,
|
|
54
59
|
value: provider.name,
|
|
55
60
|
short: provider.name
|
|
56
61
|
};
|
package/src/commands/switch.js
CHANGED
|
@@ -214,7 +214,7 @@ class EnvSwitcher extends BaseCommand {
|
|
|
214
214
|
}
|
|
215
215
|
|
|
216
216
|
// 确定使用的启动参数
|
|
217
|
-
let selectedArgs;
|
|
217
|
+
let selectedArgs = [];
|
|
218
218
|
if (options.noArgs) {
|
|
219
219
|
selectedArgs = [];
|
|
220
220
|
Logger.info('使用空参数启动');
|
|
@@ -538,7 +538,7 @@ class EnvSwitcher extends BaseCommand {
|
|
|
538
538
|
return await this.showQuickSettings();
|
|
539
539
|
}
|
|
540
540
|
|
|
541
|
-
return await this.
|
|
541
|
+
return await this.showLaunchArgsSelection(result.provider);
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
async showStatistics() {
|
|
@@ -556,12 +556,16 @@ class EnvSwitcher extends BaseCommand {
|
|
|
556
556
|
console.log(UIHelper.createTitle('使用统计', UIHelper.icons.info));
|
|
557
557
|
console.log();
|
|
558
558
|
|
|
559
|
+
const earliestCreated = providers.length > 0
|
|
560
|
+
? providers.reduce((min, p) => (p.createdAt && (!min.createdAt || p.createdAt < min.createdAt)) ? p : min, providers[0])
|
|
561
|
+
: null;
|
|
562
|
+
|
|
559
563
|
const stats = [
|
|
560
564
|
['总供应商数', totalProviders],
|
|
561
565
|
['当前供应商', currentProvider ? currentProvider.displayName : '无'],
|
|
562
566
|
['总使用次数', totalUsage],
|
|
563
567
|
['最常用供应商', mostUsed ? mostUsed.displayName : '无'],
|
|
564
|
-
['
|
|
568
|
+
['最早创建时间', earliestCreated?.createdAt ? UIHelper.formatTime(earliestCreated.createdAt) : '无']
|
|
565
569
|
];
|
|
566
570
|
|
|
567
571
|
console.log(UIHelper.createTable(['项目', '数据'], stats));
|
|
@@ -923,7 +927,7 @@ class EnvSwitcher extends BaseCommand {
|
|
|
923
927
|
const { ProviderRemover } = require('./remove');
|
|
924
928
|
const remover = new ProviderRemover();
|
|
925
929
|
try {
|
|
926
|
-
await remover.
|
|
930
|
+
await remover.interactiveRemove({ onBack: null });
|
|
927
931
|
} finally {
|
|
928
932
|
remover.destroy();
|
|
929
933
|
}
|
package/src/config.js
CHANGED
|
@@ -58,7 +58,7 @@ class ConfigManager {
|
|
|
58
58
|
return null;
|
|
59
59
|
}
|
|
60
60
|
if (typeof value !== 'string') {
|
|
61
|
-
return
|
|
61
|
+
return null;
|
|
62
62
|
}
|
|
63
63
|
const trimmed = value.trim();
|
|
64
64
|
return trimmed.length === 0 ? null : trimmed;
|
|
@@ -383,8 +383,9 @@ class ConfigManager {
|
|
|
383
383
|
smallFast: smallFastModel
|
|
384
384
|
};
|
|
385
385
|
} else {
|
|
386
|
-
// Codex
|
|
387
|
-
|
|
386
|
+
// Codex 支持 authMode(api_key 或 chatgpt_login)
|
|
387
|
+
const authMode = providerConfig.authMode || existing?.authMode || null;
|
|
388
|
+
newProvider.authMode = authMode;
|
|
388
389
|
newProvider.models = null;
|
|
389
390
|
}
|
|
390
391
|
|
package/src/constants/ui.js
CHANGED
|
@@ -89,6 +89,13 @@ const UI_MESSAGES = {
|
|
|
89
89
|
// 认证模式
|
|
90
90
|
AUTH_MODE_API_KEY: '🔑 ANTHROPIC_API_KEY - 大多数第三方代理使用',
|
|
91
91
|
AUTH_MODE_AUTH_TOKEN: '🔐 ANTHROPIC_AUTH_TOKEN - 部分服务商使用',
|
|
92
|
+
AUTH_MODE_CODEX_API_KEY: '🔑 API Key - 使用 OpenAI API Key',
|
|
93
|
+
AUTH_MODE_CODEX_CHATGPT_LOGIN: '🌐 官方网页登录 - 使用 OpenAI 账号登录',
|
|
94
|
+
|
|
95
|
+
// Codex 特定提示
|
|
96
|
+
SELECT_CODEX_AUTH_MODE: '选择 Codex 认证方式:',
|
|
97
|
+
CODEX_CHATGPT_LOGIN_INFO: '使用官方 OpenAI 网页登录,Codex 启动时会打开浏览器',
|
|
98
|
+
CODEX_API_KEY_INFO: '使用 API Key 方式,支持官方或第三方代理',
|
|
92
99
|
|
|
93
100
|
// 操作结果
|
|
94
101
|
OPERATION_CANCELLED: '操作已取消',
|
package/src/utils/codex-files.js
CHANGED
|
@@ -404,11 +404,49 @@ async function applyCodexConfig(config, options = {}) {
|
|
|
404
404
|
return { codexHome };
|
|
405
405
|
}
|
|
406
406
|
|
|
407
|
+
/**
|
|
408
|
+
* 清理 AKM 的 Codex 配置,让 Codex 使用官方网页登录
|
|
409
|
+
* 删除 auth.json(这样 Codex 会提示登录)
|
|
410
|
+
* 清理 config.toml 中的 [model_providers.akm] 配置
|
|
411
|
+
* @param {object} options - 选项
|
|
412
|
+
* @returns {Promise<{codexHome: string}>}
|
|
413
|
+
*/
|
|
414
|
+
async function clearCodexAkmConfig(options = {}) {
|
|
415
|
+
try {
|
|
416
|
+
const codexHome = await ensureCodexHome(options.codexHome);
|
|
417
|
+
const { configTomlPath, authJsonPath } = buildCodexPaths(codexHome);
|
|
418
|
+
|
|
419
|
+
// 备份当前配置
|
|
420
|
+
await backupCodexFiles(codexHome);
|
|
421
|
+
|
|
422
|
+
// 如果 auth.json 存在就删除它(chatgpt_login 模式不需要)
|
|
423
|
+
if (await fs.pathExists(authJsonPath)) {
|
|
424
|
+
await fs.remove(authJsonPath);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 清理 config.toml 中的 AKM 配置
|
|
428
|
+
if (await fs.pathExists(configTomlPath)) {
|
|
429
|
+
const existingToml = await fs.readFile(configTomlPath, 'utf8');
|
|
430
|
+
const cleanedToml = removeAkmModelProvider(removeTopLevelApiBaseUrl(existingToml));
|
|
431
|
+
|
|
432
|
+
if (cleanedToml !== existingToml) {
|
|
433
|
+
await writeFileAtomic(configTomlPath, cleanedToml, { encoding: 'utf8', mode: 0o600 });
|
|
434
|
+
await setSecurePermissions(configTomlPath);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return { codexHome };
|
|
439
|
+
} catch (error) {
|
|
440
|
+
throw new Error(`清理 Codex AKM 配置失败: ${error.message}`, { cause: error });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
407
444
|
module.exports = {
|
|
408
445
|
resolveCodexHome,
|
|
409
446
|
buildCodexPaths,
|
|
410
447
|
readCodexFiles,
|
|
411
448
|
applyCodexConfig,
|
|
449
|
+
clearCodexAkmConfig,
|
|
412
450
|
backupCodexFiles,
|
|
413
451
|
removeTopLevelApiBaseUrl,
|
|
414
452
|
removeAkmModelProvider,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const spawn = require('cross-spawn');
|
|
2
2
|
const { sanitizeEnvValue, clearTerminal } = require('./env-utils');
|
|
3
|
-
const { applyCodexConfig } = require('./codex-files');
|
|
3
|
+
const { applyCodexConfig, clearCodexAkmConfig } = require('./codex-files');
|
|
4
4
|
const { assertSupportedLaunchArgs } = require('./launch-args');
|
|
5
5
|
const { validator } = require('./validator');
|
|
6
6
|
|
|
@@ -17,7 +17,15 @@ function buildCodexEnvVariables(config) {
|
|
|
17
17
|
delete env.OPENAI_MODEL;
|
|
18
18
|
|
|
19
19
|
try {
|
|
20
|
-
//
|
|
20
|
+
// 确定认证模式,默认为 api_key(向后兼容)
|
|
21
|
+
const authMode = config.authMode || 'api_key';
|
|
22
|
+
|
|
23
|
+
// chatgpt_login 模式不设置环境变量,使用 Codex 官方登录
|
|
24
|
+
if (authMode === 'chatgpt_login') {
|
|
25
|
+
return env;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// api_key 模式使用 OpenAI 环境变量
|
|
21
29
|
if (config.authToken) {
|
|
22
30
|
env.OPENAI_API_KEY = sanitizeEnvValue(config.authToken);
|
|
23
31
|
}
|
|
@@ -52,7 +60,11 @@ async function executeCodexWithEnv(config, launchArgs = []) {
|
|
|
52
60
|
throw new Error('无效的 Codex 供应商配置');
|
|
53
61
|
}
|
|
54
62
|
|
|
55
|
-
|
|
63
|
+
// 确定认证模式,默认为 api_key(向后兼容)
|
|
64
|
+
const authMode = config.authMode || 'api_key';
|
|
65
|
+
|
|
66
|
+
// api_key 模式需要 authToken,chatgpt_login 模式不需要
|
|
67
|
+
if (authMode === 'api_key' && !config.authToken) {
|
|
56
68
|
throw new Error(`供应商 '${config.name}' 未配置 API Key,请使用 'akm edit ${config.name}' 添加`);
|
|
57
69
|
}
|
|
58
70
|
|
|
@@ -61,10 +73,15 @@ async function executeCodexWithEnv(config, launchArgs = []) {
|
|
|
61
73
|
// 在写入 Codex 配置文件前完成全部输入校验。
|
|
62
74
|
const env = buildCodexEnvVariables(config);
|
|
63
75
|
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
// 根据认证模式处理配置文件
|
|
77
|
+
if (authMode === 'api_key') {
|
|
78
|
+
// 写入 ~/.codex/config.toml 和 ~/.codex/auth.json
|
|
79
|
+
// 确保 Codex CLI 使用 API Key 认证方式
|
|
80
|
+
await applyCodexConfig(config);
|
|
81
|
+
} else if (authMode === 'chatgpt_login') {
|
|
82
|
+
// chatgpt_login 模式:清理 AKM 的配置,让 Codex 使用官方登录
|
|
83
|
+
await clearCodexAkmConfig();
|
|
84
|
+
}
|
|
68
85
|
|
|
69
86
|
// 处理参数:子命令放前面,选项放后面
|
|
70
87
|
const rawArgs = Array.isArray(launchArgs) ? [...launchArgs] : [];
|
|
@@ -74,7 +91,11 @@ async function executeCodexWithEnv(config, launchArgs = []) {
|
|
|
74
91
|
|
|
75
92
|
clearTerminal();
|
|
76
93
|
|
|
77
|
-
|
|
94
|
+
if (authMode === 'chatgpt_login') {
|
|
95
|
+
console.log('\n启动 Codex CLI (官方网页登录)...\n');
|
|
96
|
+
} else {
|
|
97
|
+
console.log('\n启动 Codex CLI...\n');
|
|
98
|
+
}
|
|
78
99
|
|
|
79
100
|
return new Promise((resolve, reject) => {
|
|
80
101
|
const child = spawn('codex', args, {
|
|
@@ -83,11 +104,11 @@ async function executeCodexWithEnv(config, launchArgs = []) {
|
|
|
83
104
|
shell: false
|
|
84
105
|
});
|
|
85
106
|
|
|
86
|
-
child.on('close', (code) => {
|
|
87
|
-
if (code === 0) {
|
|
107
|
+
child.on('close', (code, signal) => {
|
|
108
|
+
if (code === 0 || code === 130 || signal === 'SIGINT') {
|
|
88
109
|
resolve();
|
|
89
110
|
} else {
|
|
90
|
-
reject(new Error(`Codex CLI
|
|
111
|
+
reject(new Error(`Codex CLI 异常退出,退出代码: ${code}\n提示: 请检查 API 配置是否正确`));
|
|
91
112
|
}
|
|
92
113
|
});
|
|
93
114
|
|
|
@@ -27,11 +27,10 @@ function buildEnvVariables(config) {
|
|
|
27
27
|
}
|
|
28
28
|
env.ANTHROPIC_AUTH_TOKEN = sanitizeEnvValue(config.authToken);
|
|
29
29
|
} else {
|
|
30
|
-
// api_key
|
|
31
|
-
if (
|
|
32
|
-
|
|
30
|
+
// api_key 模式(默认):baseUrl 为空时使用官方 Anthropic API
|
|
31
|
+
if (config.baseUrl) {
|
|
32
|
+
env.ANTHROPIC_BASE_URL = sanitizeEnvValue(config.baseUrl);
|
|
33
33
|
}
|
|
34
|
-
env.ANTHROPIC_BASE_URL = sanitizeEnvValue(config.baseUrl);
|
|
35
34
|
env.ANTHROPIC_API_KEY = sanitizeEnvValue(config.authToken);
|
|
36
35
|
}
|
|
37
36
|
|
|
@@ -65,11 +64,11 @@ async function executeWithEnv(config, launchArgs = []) {
|
|
|
65
64
|
shell: false
|
|
66
65
|
});
|
|
67
66
|
|
|
68
|
-
child.on('close', (code) => {
|
|
69
|
-
if (code === 0) {
|
|
67
|
+
child.on('close', (code, signal) => {
|
|
68
|
+
if (code === 0 || code === 130 || signal === 'SIGINT') {
|
|
70
69
|
resolve();
|
|
71
70
|
} else {
|
|
72
|
-
reject(new Error(`Claude Code
|
|
71
|
+
reject(new Error(`Claude Code 异常退出,退出代码: ${code}\n提示: 请检查 API 配置是否正确`));
|
|
73
72
|
}
|
|
74
73
|
});
|
|
75
74
|
|
|
@@ -21,7 +21,7 @@ const providerSchema = {
|
|
|
21
21
|
displayName: nullableString,
|
|
22
22
|
alias: nullableString,
|
|
23
23
|
ideName: { enum: ['claude', 'codex'] },
|
|
24
|
-
authMode: { enum: ['api_key', 'api_token', 'auth_token', 'openai_api_key', null] },
|
|
24
|
+
authMode: { enum: ['api_key', 'api_token', 'auth_token', 'openai_api_key', 'chatgpt_login', null] },
|
|
25
25
|
authToken: { type: ['string', 'null'], maxLength: 16384 },
|
|
26
26
|
baseUrl: { type: ['string', 'null'], maxLength: 2048 },
|
|
27
27
|
models: {
|
|
@@ -165,7 +165,7 @@ function validateAndNormalizeData(data, source) {
|
|
|
165
165
|
ideName,
|
|
166
166
|
authMode:
|
|
167
167
|
ideName === 'codex'
|
|
168
|
-
? null
|
|
168
|
+
? provider.authMode || null
|
|
169
169
|
: provider.authMode === 'api_token'
|
|
170
170
|
? 'auth_token'
|
|
171
171
|
: provider.authMode || 'api_key',
|
|
@@ -313,7 +313,8 @@ class ProviderStatusChecker {
|
|
|
313
313
|
}
|
|
314
314
|
|
|
315
315
|
const baseUrl = provider.baseUrl || 'https://api.openai.com/v1';
|
|
316
|
-
const
|
|
316
|
+
const base = baseUrl.replace(/\/$/, '');
|
|
317
|
+
const modelsUrl = base.endsWith('/models') ? base : `${base}/models`;
|
|
317
318
|
|
|
318
319
|
try {
|
|
319
320
|
const controller = new AbortController();
|