@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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikecode/api-key-manager",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A CLI tool for managing and switching multiple API provider configurations",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -37,25 +37,43 @@ class ProviderStatusChecker {
37
37
  return this._result('unknown', '认证模式不受支持', null);
38
38
  }
39
39
 
40
- const start = process.hrtime.bigint();
41
- const response = await client.messages.create({
42
- model,
43
- max_tokens: this.maxTokens,
44
- messages: [
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
- return this._result('online', `可用 ${latency.toFixed(0)}ms`, latency);
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 模式:如果有 baseUrl 说明是第三方服务,否则是官方 API
123
+ // auth_token 模式:通过环境变量 ANTHROPIC_AUTH_TOKEN 传递
124
+ // SDK 会自动从环境变量中读取认证信息
125
+ // 注意:check() 方法已经设置了环境变量
106
126
  if (provider.baseUrl) {
107
127
  clientOptions.baseURL = provider.baseUrl;
108
128
  }
109
- // Anthropic SDK 需要通过 apiKey 参数传递 auth token
110
- // auth_token 格式通常以 'sk-ant-' 开头
111
- clientOptions.apiKey = provider.authToken;
129
+ // 不设置 apiKey,让 SDK 从环境变量读取
112
130
  } else {
113
131
  return null;
114
132
  }