Skip to content

Commit 176a4ad

Browse files
author
echoVic
committed
feat(内置模型): 更新智谱 API 代理服务地址并实现密钥获取逻辑
将智谱模型的代理服务地址更新为官方地址,并实现从 Cloudflare Worker 获取真实 API Key 的逻辑 添加缓存机制以提高性能并减少网络请求
1 parent f055e1e commit 176a4ad

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

src/config/builtinModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const BUILTIN_MODELS: BuiltinModelDefinition[] = [
2626
{
2727
name: '✨ GLM-4.7 (内置免费)',
2828
provider: 'openai-compatible',
29-
baseUrl: 'https://blade-api-proxy.137844255.workers.dev/v1',
29+
baseUrl: 'https://open.bigmodel.cn/api/coding/paas/v4',
3030
model: 'glm-4.7',
3131
apiKey: BUILTIN_API_KEY,
3232
description: '智谱 GLM-4.7 Thinking - 由 Blade 提供免费额度',

src/services/BuiltinKeyService.ts

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,96 @@
11
/**
22
* 内置 API Key 服务
33
*
4-
* 内置模型的 API Key 已直接配置在 builtinModels.ts 中
5-
* 此服务仅作为兼容层,直接返回传入的 API Key
4+
* 从 Cloudflare Worker 代理服务获取真实的 API Key
65
*/
76

7+
import { createLogger, LogCategory } from '../logging/Logger.js';
8+
import { proxyFetch } from '../utils/proxyFetch.js';
9+
10+
const logger = createLogger(LogCategory.SERVICE);
11+
12+
const PROXY_URL = 'https://blade-api-proxy.137844255.workers.dev/v1/get-zhipu-key';
13+
const BUILTIN_TOKEN = 'blade-free-tier';
14+
15+
interface ZhipuKeyResponse {
16+
apiKey: string;
17+
baseUrl: string;
18+
provider: string;
19+
model: string;
20+
message?: string;
21+
}
22+
23+
let cachedApiKey: string | null = null;
24+
let cachedBaseUrl: string | null = null;
25+
26+
/**
27+
* 从代理服务获取真实的智谱 API Key
28+
*/
829
export async function resolveBuiltinApiKey(apiKey: string): Promise<string> {
9-
return apiKey;
30+
// 如果不是内置 token,直接返回
31+
if (apiKey !== BUILTIN_TOKEN) {
32+
return apiKey;
33+
}
34+
35+
// 使用缓存
36+
if (cachedApiKey) {
37+
logger.debug('使用缓存的内置 API Key');
38+
return cachedApiKey;
39+
}
40+
41+
try {
42+
logger.info('🔑 正在从代理服务获取内置 API Key...');
43+
44+
const response = await proxyFetch(PROXY_URL, {
45+
method: 'GET',
46+
headers: {
47+
'Authorization': `Bearer ${BUILTIN_TOKEN}`,
48+
'Content-Type': 'application/json',
49+
},
50+
timeout: 10000,
51+
});
52+
53+
if (!response.ok) {
54+
const errorText = await response.text();
55+
throw new Error(`获取 API Key 失败: ${response.status} - ${errorText}`);
56+
}
57+
58+
const data = await response.json() as ZhipuKeyResponse;
59+
60+
if (!data.apiKey) {
61+
throw new Error('代理服务返回的数据中没有 apiKey');
62+
}
63+
64+
cachedApiKey = data.apiKey;
65+
cachedBaseUrl = data.baseUrl;
66+
67+
logger.info('✅ 成功获取内置 API Key');
68+
if (data.message) {
69+
logger.debug(`提示: ${data.message}`);
70+
}
71+
72+
return cachedApiKey;
73+
} catch (error) {
74+
logger.error('❌ 获取内置 API Key 失败:', error);
75+
throw new Error(
76+
`无法获取内置模型的 API Key: ${error instanceof Error ? error.message : '未知错误'}\n` +
77+
'请检查网络连接或使用自己的 API Key (/config)'
78+
);
79+
}
1080
}
81+
82+
/**
83+
* 获取缓存的 baseUrl(用于更新模型配置)
84+
*/
85+
export function getCachedBaseUrl(): string | null {
86+
return cachedBaseUrl;
87+
}
88+
89+
/**
90+
* 清除缓存(用于测试或重新获取)
91+
*/
92+
export function clearBuiltinKeyCache(): void {
93+
cachedApiKey = null;
94+
cachedBaseUrl = null;
95+
}
96+

0 commit comments

Comments
 (0)