|
1 | 1 | import { GoogleGenAI, GenerateContentResponse } from "@google/genai"; |
2 | 2 | import { KeyManager } from '../../keyManager'; |
3 | 3 |
|
4 | | -/** |
5 | | - * 将请求URL转换为代理URL(如果有配置) |
6 | | - */ |
7 | | -function getProxiedUrl(urlString: string, apiEndpoint?: string): string { |
8 | | - if (!apiEndpoint?.trim()) return urlString; |
9 | | - |
10 | | - try { |
11 | | - const proxyUrl = new URL(apiEndpoint); |
12 | | - if (urlString.includes('generativelanguage.googleapis.com')) { |
13 | | - const originalUrl = new URL(urlString); |
14 | | - const finalProxyUrl = new URL(proxyUrl); |
15 | | - |
16 | | - const newPathname = (finalProxyUrl.pathname.replace(/\/$/, '') + originalUrl.pathname).replace(/\/\//g, '/'); |
17 | | - finalProxyUrl.pathname = newPathname; |
18 | | - finalProxyUrl.search = originalUrl.search; |
19 | | - |
20 | | - return finalProxyUrl.toString(); |
21 | | - } |
22 | | - } catch (e) { |
23 | | - console.error("提供的 API Base URL 无效:", apiEndpoint, e); |
24 | | - } |
25 | | - return urlString; |
26 | | -} |
27 | | - |
28 | 4 | /** |
29 | 5 | * 打印400错误的详细分析 |
30 | 6 | */ |
@@ -81,7 +57,10 @@ export async function executeWithKeyRotation<T>( |
81 | 57 | if (!key) continue; |
82 | 58 |
|
83 | 59 | try { |
84 | | - const ai = new GoogleGenAI({ apiKey: key }); |
| 60 | + const ai = new GoogleGenAI({ |
| 61 | + apiKey: key, |
| 62 | + httpOptions: trimmedApiEndpoint ? { baseUrl: trimmedApiEndpoint } : undefined |
| 63 | + }); |
85 | 64 | const result = await operation(ai); |
86 | 65 | keyManager.saveSuccessIndex(); |
87 | 66 | return result; |
@@ -118,14 +97,19 @@ export async function* executeStreamWithKeyRotation<T extends GenerateContentRes |
118 | 97 | return; |
119 | 98 | } |
120 | 99 |
|
| 100 | + const trimmedApiEndpoint = apiEndpoint?.trim(); |
| 101 | + |
121 | 102 | let lastError: unknown = null; |
122 | 103 | let success = false; |
123 | 104 | for (let i = 0; i < keyManager.getTotalKeys(); i++) { |
124 | 105 | const { key } = keyManager.getNextKey(); |
125 | 106 | if (!key) continue; |
126 | 107 |
|
127 | 108 | try { |
128 | | - const ai = new GoogleGenAI({ apiKey: key }); |
| 109 | + const ai = new GoogleGenAI({ |
| 110 | + apiKey: key, |
| 111 | + httpOptions: trimmedApiEndpoint ? { baseUrl: trimmedApiEndpoint } : undefined |
| 112 | + }); |
129 | 113 | const stream = await operation(ai); |
130 | 114 | keyManager.saveSuccessIndex(); |
131 | 115 | yield* stream; |
|
0 commit comments