@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.
- package/README.md +444 -151
- package/bin/akm.js +340 -11
- package/package.json +29 -12
- package/src/CommandRegistry.js +58 -3
- package/src/commands/BaseCommand.js +68 -1
- package/src/commands/add/prompts.js +280 -0
- package/src/commands/add/providerSaver.js +74 -0
- package/src/commands/add/summaryPrinter.js +29 -0
- package/src/commands/add.js +54 -439
- package/src/commands/backup.js +396 -0
- package/src/commands/batch.js +313 -0
- package/src/commands/benchmark.js +323 -0
- package/src/commands/claude-clean.js +253 -0
- package/src/commands/clone.js +265 -0
- package/src/commands/current.js +89 -41
- package/src/commands/edit.js +3 -207
- package/src/commands/health.js +264 -0
- package/src/commands/list.js +120 -35
- package/src/commands/mcp.js +467 -0
- package/src/commands/remove.js +89 -26
- package/src/commands/stats.js +281 -0
- package/src/commands/switch/claude-settings-compatibility.js +113 -0
- package/src/commands/switch/launch-args-helper.js +84 -0
- package/src/commands/switch/provider-choices-helper.js +72 -0
- package/src/commands/switch/provider-details-helper.js +46 -0
- package/src/commands/switch/provider-edit-questions-helper.js +124 -0
- package/src/commands/switch/provider-launcher.js +20 -0
- package/src/commands/switch/provider-manager.js +214 -0
- package/src/commands/switch/status-helper.js +124 -0
- package/src/commands/switch/ui-screen-renderer.js +77 -0
- package/src/commands/switch.js +471 -958
- package/src/commands/validate.js +310 -0
- package/src/config.js +541 -55
- package/src/constants/index.js +234 -0
- package/src/constants/ui.js +108 -0
- package/src/index.js +11 -4
- package/src/navigation/EscNavigationManager.js +5 -5
- package/src/utils/atomic-file.js +47 -0
- package/src/utils/claude-settings.js +4 -4
- package/src/utils/codex-files.js +418 -0
- package/src/utils/codex-launcher.js +104 -0
- package/src/utils/concurrency.js +30 -0
- package/src/utils/config-opener.js +22 -2
- package/src/utils/env-launcher.js +50 -44
- package/src/utils/env-utils.js +52 -0
- package/src/utils/error-handler.js +27 -28
- package/src/utils/health-checker.js +350 -0
- package/src/utils/import-validator.js +210 -0
- package/src/utils/inquirer-setup.js +11 -0
- package/src/utils/launch-args.js +155 -0
- package/src/utils/logger.js +0 -1
- package/src/utils/provider-status-checker.js +211 -64
- package/src/utils/secrets.js +22 -0
- package/src/utils/shell-snippets.js +32 -0
- package/src/utils/terminal-format.js +22 -4
- package/src/utils/ui-helper.js +96 -86
- package/src/utils/update-checker.js +41 -12
- package/src/utils/validator.js +115 -88
- package/bin/cc.js +0 -101
- package/src/utils/storage.js +0 -55
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
const Ajv = require('ajv');
|
|
2
|
+
const { validator } = require('./validator');
|
|
3
|
+
const { assertSafeImportLaunchArgs } = require('./launch-args');
|
|
4
|
+
const { containsUnsafeTerminalCharacters, escapeTerminalText } = require('./terminal-format');
|
|
5
|
+
|
|
6
|
+
const MAX_PROVIDERS = 200;
|
|
7
|
+
|
|
8
|
+
const nullableString = { type: ['string', 'null'], maxLength: 100 };
|
|
9
|
+
const launchArgsSchema = {
|
|
10
|
+
type: 'array',
|
|
11
|
+
maxItems: 16,
|
|
12
|
+
uniqueItems: true,
|
|
13
|
+
items: { type: 'string', maxLength: 200 }
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const providerSchema = {
|
|
17
|
+
type: 'object',
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
properties: {
|
|
20
|
+
name: { type: 'string', maxLength: 100 },
|
|
21
|
+
displayName: nullableString,
|
|
22
|
+
alias: nullableString,
|
|
23
|
+
ideName: { enum: ['claude', 'codex'] },
|
|
24
|
+
authMode: { enum: ['api_key', 'api_token', 'auth_token', 'openai_api_key', null] },
|
|
25
|
+
authToken: { type: ['string', 'null'], maxLength: 16384 },
|
|
26
|
+
baseUrl: { type: ['string', 'null'], maxLength: 2048 },
|
|
27
|
+
models: {
|
|
28
|
+
type: ['object', 'null'],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
properties: {
|
|
31
|
+
primary: nullableString,
|
|
32
|
+
smallFast: nullableString
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
launchArgs: launchArgsSchema,
|
|
36
|
+
lastUsedArgs: {
|
|
37
|
+
anyOf: [launchArgsSchema, { type: 'null' }]
|
|
38
|
+
},
|
|
39
|
+
current: { type: 'boolean' },
|
|
40
|
+
usageCount: { type: 'integer', minimum: 0 },
|
|
41
|
+
createdAt: nullableString,
|
|
42
|
+
lastUsed: nullableString,
|
|
43
|
+
importedAt: nullableString,
|
|
44
|
+
tokenExpiry: nullableString,
|
|
45
|
+
quota: {
|
|
46
|
+
type: ['object', 'null'],
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
properties: {
|
|
49
|
+
used: { type: 'number', minimum: 0 },
|
|
50
|
+
limit: { type: 'number', exclusiveMinimum: 0 }
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
stats: {
|
|
54
|
+
type: ['object', 'null'],
|
|
55
|
+
additionalProperties: false,
|
|
56
|
+
properties: {
|
|
57
|
+
totalSessions: { type: 'integer', minimum: 0 },
|
|
58
|
+
totalDurationMs: { type: 'number', minimum: 0 },
|
|
59
|
+
averageDurationMs: { type: 'number', minimum: 0 },
|
|
60
|
+
lastSessionDuration: { type: 'number', minimum: 0 },
|
|
61
|
+
firstUsed: nullableString
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const configSchema = {
|
|
68
|
+
type: 'object',
|
|
69
|
+
additionalProperties: false,
|
|
70
|
+
required: ['providers'],
|
|
71
|
+
properties: {
|
|
72
|
+
version: { type: 'string', maxLength: 20 },
|
|
73
|
+
currentProvider: { type: ['string', 'null'], minLength: 1, maxLength: 100 },
|
|
74
|
+
providers: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
maxProperties: MAX_PROVIDERS,
|
|
77
|
+
additionalProperties: providerSchema
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const importSchema = {
|
|
83
|
+
...configSchema,
|
|
84
|
+
properties: {
|
|
85
|
+
...configSchema.properties,
|
|
86
|
+
exportedAt: { type: 'string', maxLength: 100 },
|
|
87
|
+
secretsIncluded: { type: 'boolean' },
|
|
88
|
+
providers: {
|
|
89
|
+
...configSchema.properties.providers,
|
|
90
|
+
minProperties: 1
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const ajv = new Ajv({ allErrors: true, allowUnionTypes: true });
|
|
96
|
+
const validateImportSchema = ajv.compile(importSchema);
|
|
97
|
+
const validateConfigSchema = ajv.compile(configSchema);
|
|
98
|
+
|
|
99
|
+
function formatSchemaErrors(errors = []) {
|
|
100
|
+
return errors.map(error => `${error.instancePath || '/'} ${error.message}`).join(';');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 校验并标准化从文件读取的配置
|
|
105
|
+
* @param {unknown} data - JSON 配置内容
|
|
106
|
+
* @returns {object} 标准化后的导入数据
|
|
107
|
+
*/
|
|
108
|
+
function validateAndNormalizeData(data, source) {
|
|
109
|
+
const schemaValidator = source === 'import' ? validateImportSchema : validateConfigSchema;
|
|
110
|
+
if (!schemaValidator(data)) {
|
|
111
|
+
throw new Error(`配置 Schema 校验失败: ${formatSchemaErrors(schemaValidator.errors)}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const normalizedProviders = {};
|
|
115
|
+
for (const [name, provider] of Object.entries(data.providers)) {
|
|
116
|
+
const safeName = escapeTerminalText(name);
|
|
117
|
+
const nameError = validator.validateName(name);
|
|
118
|
+
if (nameError) {
|
|
119
|
+
throw new Error(`供应商名称 "${safeName}" 无效: ${nameError}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const ideName = provider.ideName || 'claude';
|
|
123
|
+
const displayNameError = validator.validateDisplayName(provider.displayName);
|
|
124
|
+
if (displayNameError) {
|
|
125
|
+
throw new Error(`供应商 "${safeName}" 的显示名称无效: ${displayNameError}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (provider.alias) {
|
|
129
|
+
const aliasError = validator.validateName(provider.alias);
|
|
130
|
+
if (aliasError) {
|
|
131
|
+
throw new Error(`供应商 "${safeName}" 的别名无效: ${aliasError}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const launchArgs = provider.launchArgs || [];
|
|
136
|
+
const lastUsedArgs = provider.lastUsedArgs || [];
|
|
137
|
+
|
|
138
|
+
// 本地配置、导入配置和交互式编辑必须共享同一套 URL 规则。
|
|
139
|
+
const baseUrlError = validator.validateUrl(provider.baseUrl, false);
|
|
140
|
+
if (baseUrlError) {
|
|
141
|
+
throw new Error(`供应商 "${safeName}" 的基础 URL 无效: ${baseUrlError}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (source === 'import') {
|
|
145
|
+
assertSafeImportLaunchArgs(ideName, launchArgs);
|
|
146
|
+
assertSafeImportLaunchArgs(ideName, lastUsedArgs);
|
|
147
|
+
if (typeof provider.authToken === 'string' && provider.authToken.includes('***')) {
|
|
148
|
+
throw new Error(`供应商 "${safeName}" 包含脱敏 Token,请重新设置密钥后再导入`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (ideName === 'claude' && provider.authMode === 'openai_api_key') {
|
|
153
|
+
throw new Error(`供应商 "${safeName}" 的认证模式与 IDE 类型不匹配`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (containsUnsafeTerminalCharacters(provider.authToken)) {
|
|
157
|
+
throw new Error(`供应商 "${safeName}" 的 Token 不能包含控制字符`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
normalizedProviders[name] = {
|
|
161
|
+
...provider,
|
|
162
|
+
name,
|
|
163
|
+
ideName,
|
|
164
|
+
authMode:
|
|
165
|
+
ideName === 'codex'
|
|
166
|
+
? null
|
|
167
|
+
: provider.authMode === 'api_token'
|
|
168
|
+
? 'auth_token'
|
|
169
|
+
: provider.authMode || 'api_key',
|
|
170
|
+
launchArgs,
|
|
171
|
+
lastUsedArgs: provider.lastUsedArgs === null ? null : lastUsedArgs
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (
|
|
176
|
+
data.currentProvider !== null &&
|
|
177
|
+
data.currentProvider !== undefined &&
|
|
178
|
+
!Object.prototype.hasOwnProperty.call(normalizedProviders, data.currentProvider)
|
|
179
|
+
) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
`currentProvider 指向不存在的供应商: ${escapeTerminalText(data.currentProvider)}`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const currentProvider = data.currentProvider || null;
|
|
186
|
+
for (const [name, provider] of Object.entries(normalizedProviders)) {
|
|
187
|
+
provider.current = name === currentProvider;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
...data,
|
|
192
|
+
providers: normalizedProviders
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function validateAndNormalizeImportData(data) {
|
|
197
|
+
return validateAndNormalizeData(data, 'import');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function validateAndNormalizeConfigData(data) {
|
|
201
|
+
return validateAndNormalizeData(data, 'local');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = {
|
|
205
|
+
MAX_PROVIDERS,
|
|
206
|
+
configSchema,
|
|
207
|
+
importSchema,
|
|
208
|
+
validateAndNormalizeConfigData,
|
|
209
|
+
validateAndNormalizeImportData
|
|
210
|
+
};
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inquirer Setup Utility
|
|
3
|
+
* 自定义 Inquirer.js 提示样式和本地化
|
|
4
|
+
* @module utils/inquirer-setup
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
const inquirer = require('inquirer');
|
|
2
8
|
const CheckboxPrompt = require('inquirer/lib/prompts/checkbox');
|
|
3
9
|
const chalk = require('chalk');
|
|
4
10
|
const figures = require('figures');
|
|
5
11
|
|
|
12
|
+
/**
|
|
13
|
+
* 本地化复选框提示类
|
|
14
|
+
* 扩展 Inquirer 的 CheckboxPrompt,添加中文提示和自定义样式
|
|
15
|
+
* @extends CheckboxPrompt
|
|
16
|
+
*/
|
|
6
17
|
class LocalizedCheckboxPrompt extends CheckboxPrompt {
|
|
7
18
|
render(error) {
|
|
8
19
|
let message = this.getQuestion();
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 启动参数定义模块
|
|
3
|
+
* 统一管理 Claude Code 和 Codex CLI 的启动参数
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const claudeLaunchArgs = [
|
|
7
|
+
{
|
|
8
|
+
name: '--continue',
|
|
9
|
+
label: '继续上次对话',
|
|
10
|
+
description: '恢复上次的对话记录',
|
|
11
|
+
checked: false
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: '--dangerously-skip-permissions',
|
|
15
|
+
label: '最高权限',
|
|
16
|
+
description: '仅在沙盒环境中使用',
|
|
17
|
+
checked: false
|
|
18
|
+
}
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const codexLaunchArgs = [
|
|
22
|
+
{
|
|
23
|
+
name: 'resume',
|
|
24
|
+
label: '继续上次对话',
|
|
25
|
+
description: '恢复之前的会话',
|
|
26
|
+
checked: false,
|
|
27
|
+
isSubcommand: true
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: '--full-auto',
|
|
31
|
+
label: '全自动模式',
|
|
32
|
+
description: '自动批准 + 工作区写入沙盒',
|
|
33
|
+
checked: false,
|
|
34
|
+
exclusive: ['--dangerously-bypass-approvals-and-sandbox']
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: '--dangerously-bypass-approvals-and-sandbox',
|
|
38
|
+
label: '跳过审批和沙盒',
|
|
39
|
+
description: '危险:跳过所有安全检查',
|
|
40
|
+
checked: false,
|
|
41
|
+
exclusive: ['--full-auto']
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: '--search',
|
|
45
|
+
label: '启用网页搜索',
|
|
46
|
+
description: '允许模型搜索网页',
|
|
47
|
+
checked: false
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
const dangerousImportArgs = new Set([
|
|
52
|
+
'--dangerously-skip-permissions',
|
|
53
|
+
'--dangerously-bypass-approvals-and-sandbox'
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
function getClaudeLaunchArgs() {
|
|
57
|
+
return claudeLaunchArgs.map(arg => ({ ...arg }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getCodexLaunchArgs() {
|
|
61
|
+
return codexLaunchArgs.map(arg => ({ ...arg }));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getLaunchArgs(ideName) {
|
|
65
|
+
return ideName === 'codex' ? getCodexLaunchArgs() : getClaudeLaunchArgs();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 校验启动参数是否属于当前支持列表
|
|
70
|
+
* @param {string} ideName - IDE 名称
|
|
71
|
+
* @param {string[]} selectedArgs - 待校验参数
|
|
72
|
+
* @returns {string|null} 校验错误,合法时返回 null
|
|
73
|
+
*/
|
|
74
|
+
function validateLaunchArgs(ideName, selectedArgs) {
|
|
75
|
+
if (!Array.isArray(selectedArgs)) {
|
|
76
|
+
return '启动参数必须是字符串数组';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const availableArgs = getLaunchArgs(ideName);
|
|
80
|
+
const allowedArgs = new Set(availableArgs.map(arg => arg.name));
|
|
81
|
+
const invalidArgs = selectedArgs.filter(arg => typeof arg !== 'string' || !allowedArgs.has(arg));
|
|
82
|
+
|
|
83
|
+
if (invalidArgs.length > 0) {
|
|
84
|
+
const printableArgs = invalidArgs.map(arg => {
|
|
85
|
+
const serialized = JSON.stringify(arg);
|
|
86
|
+
return serialized === undefined ? String(arg) : serialized;
|
|
87
|
+
});
|
|
88
|
+
return `不支持的启动参数: ${printableArgs.join(', ')}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return checkExclusiveArgs(selectedArgs, availableArgs);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 断言启动参数合法,供配置导入和子进程启动边界复用
|
|
96
|
+
* @param {string} ideName - IDE 名称
|
|
97
|
+
* @param {string[]} selectedArgs - 待校验参数
|
|
98
|
+
*/
|
|
99
|
+
function assertSupportedLaunchArgs(ideName, selectedArgs) {
|
|
100
|
+
const error = validateLaunchArgs(ideName, selectedArgs);
|
|
101
|
+
if (error) {
|
|
102
|
+
throw new Error(error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 外部配置不得静默启用跳过审批或沙盒的最高权限参数。
|
|
108
|
+
* @param {string} ideName - IDE 名称
|
|
109
|
+
* @param {string[]} selectedArgs - 导入配置中的启动参数
|
|
110
|
+
*/
|
|
111
|
+
function assertSafeImportLaunchArgs(ideName, selectedArgs) {
|
|
112
|
+
assertSupportedLaunchArgs(ideName, selectedArgs);
|
|
113
|
+
const dangerousArgs = selectedArgs.filter(arg => dangerousImportArgs.has(arg));
|
|
114
|
+
if (dangerousArgs.length > 0) {
|
|
115
|
+
throw new Error(`导入配置不能包含最高权限启动参数: ${dangerousArgs.join(', ')}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 检查互斥参数
|
|
121
|
+
* @param {string[]} selectedArgs - 选中的参数列表
|
|
122
|
+
* @param {Array} availableArgs - 可用参数定义
|
|
123
|
+
* @returns {string|null} 冲突错误信息或 null
|
|
124
|
+
*/
|
|
125
|
+
function checkExclusiveArgs(selectedArgs, availableArgs) {
|
|
126
|
+
if (!selectedArgs || selectedArgs.length < 2) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const argDef of availableArgs) {
|
|
131
|
+
if (!argDef.exclusive || !selectedArgs.includes(argDef.name)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (const exclusiveArg of argDef.exclusive) {
|
|
136
|
+
if (selectedArgs.includes(exclusiveArg)) {
|
|
137
|
+
const arg1 = availableArgs.find(a => a.name === argDef.name);
|
|
138
|
+
const arg2 = availableArgs.find(a => a.name === exclusiveArg);
|
|
139
|
+
return `"${arg1?.label || argDef.name}" 和 "${arg2?.label || exclusiveArg}" 不能同时选择`;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
module.exports = {
|
|
148
|
+
getClaudeLaunchArgs,
|
|
149
|
+
getCodexLaunchArgs,
|
|
150
|
+
getLaunchArgs,
|
|
151
|
+
checkExclusiveArgs,
|
|
152
|
+
validateLaunchArgs,
|
|
153
|
+
assertSupportedLaunchArgs,
|
|
154
|
+
assertSafeImportLaunchArgs
|
|
155
|
+
};
|
package/src/utils/logger.js
CHANGED