@pikecode/api-key-manager 1.0.2 → 1.0.3
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
|
@@ -37,25 +37,43 @@ class ProviderStatusChecker {
|
|
|
37
37
|
return this._result('unknown', '认证模式不受支持', null);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
{
|
|
46
|
-
role: 'user',
|
|
47
|
-
content: this.testMessage
|
|
48
|
-
}
|
|
49
|
-
]
|
|
50
|
-
}, { timeout: this.timeout });
|
|
51
|
-
const latency = Number(process.hrtime.bigint() - start) / 1e6;
|
|
52
|
-
|
|
53
|
-
const text = this._extractText(response);
|
|
54
|
-
if (!text) {
|
|
55
|
-
return this._result('online', `可用 ${latency.toFixed(0)}ms (无文本响应)`, latency);
|
|
40
|
+
// 保存原始环境变量
|
|
41
|
+
const originalEnv = {};
|
|
42
|
+
if (provider.authMode === 'auth_token') {
|
|
43
|
+
originalEnv.ANTHROPIC_AUTH_TOKEN = process.env.ANTHROPIC_AUTH_TOKEN;
|
|
44
|
+
process.env.ANTHROPIC_AUTH_TOKEN = provider.authToken;
|
|
56
45
|
}
|
|
57
46
|
|
|
58
|
-
|
|
47
|
+
try {
|
|
48
|
+
const start = process.hrtime.bigint();
|
|
49
|
+
const response = await client.messages.create({
|
|
50
|
+
model,
|
|
51
|
+
max_tokens: this.maxTokens,
|
|
52
|
+
messages: [
|
|
53
|
+
{
|
|
54
|
+
role: 'user',
|
|
55
|
+
content: this.testMessage
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}, { timeout: this.timeout });
|
|
59
|
+
const latency = Number(process.hrtime.bigint() - start) / 1e6;
|
|
60
|
+
|
|
61
|
+
const text = this._extractText(response);
|
|
62
|
+
if (!text) {
|
|
63
|
+
return this._result('online', `可用 ${latency.toFixed(0)}ms (无文本响应)`, latency);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return this._result('online', `可用 ${latency.toFixed(0)}ms`, latency);
|
|
67
|
+
} finally {
|
|
68
|
+
// 恢复原始环境变量
|
|
69
|
+
if (provider.authMode === 'auth_token') {
|
|
70
|
+
if (originalEnv.ANTHROPIC_AUTH_TOKEN !== undefined) {
|
|
71
|
+
process.env.ANTHROPIC_AUTH_TOKEN = originalEnv.ANTHROPIC_AUTH_TOKEN;
|
|
72
|
+
} else {
|
|
73
|
+
delete process.env.ANTHROPIC_AUTH_TOKEN;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
59
77
|
} catch (error) {
|
|
60
78
|
return this._handleError(error);
|
|
61
79
|
}
|
|
@@ -102,13 +120,13 @@ class ProviderStatusChecker {
|
|
|
102
120
|
}
|
|
103
121
|
clientOptions.apiKey = provider.authToken;
|
|
104
122
|
} else if (provider.authMode === 'auth_token') {
|
|
105
|
-
// auth_token
|
|
123
|
+
// auth_token 模式:通过环境变量 ANTHROPIC_AUTH_TOKEN 传递
|
|
124
|
+
// SDK 会自动从环境变量中读取认证信息
|
|
125
|
+
// 注意:check() 方法已经设置了环境变量
|
|
106
126
|
if (provider.baseUrl) {
|
|
107
127
|
clientOptions.baseURL = provider.baseUrl;
|
|
108
128
|
}
|
|
109
|
-
//
|
|
110
|
-
// auth_token 格式通常以 'sk-ant-' 开头
|
|
111
|
-
clientOptions.apiKey = provider.authToken;
|
|
129
|
+
// 不设置 apiKey,让 SDK 从环境变量读取
|
|
112
130
|
} else {
|
|
113
131
|
return null;
|
|
114
132
|
}
|