@spacex110/core 0.1.13 → 0.1.14

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": "@spacex110/core",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Framework-agnostic AI Provider Selector core utilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/api.ts CHANGED
@@ -58,7 +58,7 @@ export async function testConnection(options: TestConnectionOptions): Promise<Te
58
58
  };
59
59
  }
60
60
 
61
- const headers = strategy.buildHeaders(apiKey);
61
+ const headers = strategy.buildHeaders(apiKey, actualBaseUrl);
62
62
  // 使用聊天接口进行测试,发送最简单的请求
63
63
  const testPayload = strategy.buildChatPayload(targetModel, [{ role: 'user', content: 'Hi' }]);
64
64
  const endpoint = strategy.getChatEndpoint(actualBaseUrl, apiKey, targetModel);
@@ -124,7 +124,7 @@ export async function fetchModels(options: FetchModelsOptions): Promise<Model[]>
124
124
  const strategy = getStrategy(provider.apiFormat);
125
125
  if (strategy.getModelsEndpoint) {
126
126
  const endpoint = strategy.getModelsEndpoint(baseUrl || provider.baseUrl, apiKey || '');
127
- const headers = strategy.buildHeaders(apiKey || '');
127
+ const headers = strategy.buildHeaders(apiKey || '', baseUrl || provider.baseUrl);
128
128
 
129
129
  const response = await fetch(endpoint, {
130
130
  method: 'GET',
package/src/strategies.ts CHANGED
@@ -12,7 +12,7 @@ export interface ProviderStrategy {
12
12
  /** 从一个 SSE data 块中提取增量文本(流式)。返回空字符串表示无内容。 */
13
13
  parseStreamChunk?: (data: any) => string;
14
14
  // Headers
15
- buildHeaders: (apiKey: string) => Record<string, string>;
15
+ buildHeaders: (apiKey: string, baseUrl?: string) => Record<string, string>;
16
16
  }
17
17
 
18
18
  /**
@@ -115,13 +115,16 @@ const openaiStrategy: ProviderStrategy = {
115
115
  format: 'openai',
116
116
  getModelsEndpoint: (baseUrl) => `${baseUrl}/models`,
117
117
  getChatEndpoint: (baseUrl) => `${baseUrl}/chat/completions`,
118
- buildHeaders: (apiKey) => {
118
+ buildHeaders: (apiKey, baseUrl) => {
119
119
  const headers: Record<string, string> = {
120
120
  'Content-Type': 'application/json',
121
121
  'Authorization': `Bearer ${apiKey}`,
122
122
  };
123
- // OpenRouter compatibility & Best Practices
124
- if (typeof window !== 'undefined' && window.location) {
123
+ // 仅对 OpenRouter HTTP-Referer / X-Title(OpenRouter Best Practices 推荐);
124
+ // 其他 OpenAI 兼容厂商(如 DeepSeek/月之暗面/MiniMax/通义/智谱等)的 CORS 预检响应
125
+ // 通常不包含这两个头,加了会导致浏览器拒绝实际请求(net::ERR_FAILED)。
126
+ const isOpenRouter = !!baseUrl && /openrouter\.ai/i.test(baseUrl);
127
+ if (isOpenRouter && typeof window !== 'undefined' && window.location) {
125
128
  headers['HTTP-Referer'] = window.location.origin;
126
129
  // document.title 可能含中文等非 ASCII 字符,HTTP header 只允许 ISO-8859-1
127
130
  const safeTitle = document.title ? document.title.replace(/[^\x20-\x7E]/g, '').slice(0, 80) : 'AI Selector';
@@ -309,7 +312,7 @@ export async function sendDirectChat(options: DirectChatOptions): Promise<Direct
309
312
  const strategy = getStrategy(apiFormat);
310
313
 
311
314
  const endpoint = strategy.getChatEndpoint(baseUrl, apiKey, model);
312
- const headers = strategy.buildHeaders(apiKey);
315
+ const headers = strategy.buildHeaders(apiKey, baseUrl);
313
316
  const payload = strategy.buildChatPayload(model, messages, maxTokens);
314
317
 
315
318
  const startTime = performance.now();
@@ -373,7 +376,7 @@ export async function sendDirectChatStream(options: DirectChatStreamOptions): Pr
373
376
  endpoint = `${baseUrl}/models/${model}:streamGenerateContent?alt=sse&key=${encodeURIComponent(apiKey)}`;
374
377
  }
375
378
 
376
- const headers = strategy.buildHeaders(apiKey);
379
+ const headers = strategy.buildHeaders(apiKey, baseUrl);
377
380
  const payload = strategy.buildChatPayload(model, messages, maxTokens);
378
381
  (payload as any).stream = true; // gemini 通过端点参数控制流式,stream 字段会被忽略,无副作用
379
382