|
1 | 1 | /** |
2 | 2 | * 内置 API Key 服务 |
3 | 3 | * |
4 | | - * 内置模型的 API Key 已直接配置在 builtinModels.ts 中 |
5 | | - * 此服务仅作为兼容层,直接返回传入的 API Key |
| 4 | + * 从 Cloudflare Worker 代理服务获取真实的 API Key |
6 | 5 | */ |
7 | 6 |
|
| 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 | + */ |
8 | 29 | 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 | + } |
10 | 80 | } |
| 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