@pikecode/api-key-manager 1.0.0 → 1.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.
package/package.json
CHANGED
package/src/commands/add.js
CHANGED
|
@@ -219,8 +219,22 @@ class ProviderAdder extends BaseCommand {
|
|
|
219
219
|
{
|
|
220
220
|
type: 'input',
|
|
221
221
|
name: 'baseUrl',
|
|
222
|
-
message:
|
|
223
|
-
|
|
222
|
+
message: (answers) => {
|
|
223
|
+
// 根据认证模式显示不同的提示
|
|
224
|
+
if (answers.authMode === 'auth_token') {
|
|
225
|
+
return '请输入API基础URL (如使用官方API可留空):';
|
|
226
|
+
}
|
|
227
|
+
return '请输入API基础URL:';
|
|
228
|
+
},
|
|
229
|
+
validate: (input, answers) => {
|
|
230
|
+
// auth_token 模式允许空值(使用官方 API)
|
|
231
|
+
if (input === '' && answers.authMode === 'auth_token') {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
// 其他模式需要有效的 URL
|
|
235
|
+
if (!input && answers.authMode === 'api_key') {
|
|
236
|
+
return 'API基础URL不能为空';
|
|
237
|
+
}
|
|
224
238
|
const error = validator.validateUrl(input);
|
|
225
239
|
if (error) return error;
|
|
226
240
|
return true;
|
|
@@ -17,7 +17,12 @@ class ProviderStatusChecker {
|
|
|
17
17
|
return this._result('unknown', '暂不支持 OAuth 令牌检测', null);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// auth_token 和 api_key 模式在官方 API 中不需要 baseUrl
|
|
21
|
+
// 仅当 authMode 为 auth_token 且设置了 baseUrl 时,才表示使用第三方服务
|
|
22
|
+
if (provider.authMode === 'auth_token' && !provider.baseUrl) {
|
|
23
|
+
// 对于官方 Anthropic API 的 auth_token 模式,不需要 baseUrl
|
|
24
|
+
// 直接使用官方 API
|
|
25
|
+
} else if (!provider.baseUrl && provider.authMode !== 'auth_token') {
|
|
21
26
|
return this._result('unknown', '未配置基础地址', null);
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -88,12 +93,22 @@ class ProviderStatusChecker {
|
|
|
88
93
|
}
|
|
89
94
|
|
|
90
95
|
_createClient(provider) {
|
|
91
|
-
const clientOptions = {
|
|
96
|
+
const clientOptions = {};
|
|
92
97
|
|
|
93
98
|
if (provider.authMode === 'api_key') {
|
|
99
|
+
// api_key 模式:使用 ANTHROPIC_API_KEY
|
|
100
|
+
if (provider.baseUrl) {
|
|
101
|
+
clientOptions.baseURL = provider.baseUrl;
|
|
102
|
+
}
|
|
94
103
|
clientOptions.apiKey = provider.authToken;
|
|
95
104
|
} else if (provider.authMode === 'auth_token') {
|
|
96
|
-
|
|
105
|
+
// auth_token 模式:如果有 baseUrl 说明是第三方服务,否则是官方 API
|
|
106
|
+
if (provider.baseUrl) {
|
|
107
|
+
clientOptions.baseURL = provider.baseUrl;
|
|
108
|
+
}
|
|
109
|
+
// Anthropic SDK 需要通过 apiKey 参数传递 auth token
|
|
110
|
+
// auth_token 格式通常以 'sk-ant-' 开头
|
|
111
|
+
clientOptions.apiKey = provider.authToken;
|
|
97
112
|
} else {
|
|
98
113
|
return null;
|
|
99
114
|
}
|
|
@@ -192,6 +207,14 @@ class ProviderStatusChecker {
|
|
|
192
207
|
if (error.status === 404) {
|
|
193
208
|
return this._result('offline', '接口不存在 (404)', null);
|
|
194
209
|
}
|
|
210
|
+
if (error.status === 400) {
|
|
211
|
+
// 400 错误可能是因为认证方式不对
|
|
212
|
+
const message = error.message || '';
|
|
213
|
+
if (message.includes('auth') || message.includes('authentication')) {
|
|
214
|
+
return this._result('offline', `认证配置错误 (${error.status})`, null);
|
|
215
|
+
}
|
|
216
|
+
return this._result('offline', `请求参数错误 (${error.status})`, null);
|
|
217
|
+
}
|
|
195
218
|
return this._result('offline', `请求失败 (${error.status})`, null);
|
|
196
219
|
}
|
|
197
220
|
|