This commit is contained in:
从何开始123
2026-01-12 18:03:31 +08:00
parent bd297716b0
commit 25dffcc02e
10 changed files with 290 additions and 27 deletions

View File

@@ -1,6 +1,8 @@
import { GoogleGenAI } from "@google/genai";
import OpenAI from "openai";
import { ApiProvider, CustomModel } from './types';
import { logger } from './services/logger';
type AIProviderConfig = {
provider?: ApiProvider;
@@ -46,7 +48,8 @@ if (typeof window !== 'undefined' && originalFetch) {
if (urlString.includes('/custom-api') && currentCustomApiUrl) {
const headers = new Headers(init?.headers);
headers.set('X-Target-URL', currentCustomApiUrl);
console.log('[Fetch] Adding X-Target-URL header:', currentCustomApiUrl);
logger.debug('API', 'Using Custom Proxy', { target: currentCustomApiUrl, path: urlString });
return originalFetch(input, {
...init,
@@ -59,6 +62,7 @@ if (typeof window !== 'undefined' && originalFetch) {
try {
window.fetch = proxyFetch;
logger.info('System', 'Fetch proxy interceptor installed');
} catch (e) {
try {
Object.defineProperty(window, 'fetch', {
@@ -69,6 +73,7 @@ if (typeof window !== 'undefined' && originalFetch) {
});
} catch (e2) {
console.error('[API] Failed to intercept fetch:', e2);
logger.error('System', 'Failed to intercept fetch', e2);
}
}
}
@@ -90,10 +95,6 @@ export const getAI = (config?: AIProviderConfig) => {
currentCustomApiUrl = config.baseUrl;
// Use proxy path
options.baseURL = `${window.location.origin}/custom-api`;
console.log('[API] Using custom API proxy:', {
proxyPath: options.baseURL,
targetUrl: currentCustomApiUrl,
});
} else {
// In production, use the URL directly
options.baseURL = config.baseUrl;
@@ -110,12 +111,12 @@ export const getAI = (config?: AIProviderConfig) => {
}
}
console.log('[API] OpenAI client config:', {
logger.info('API', 'Initializing OpenAI Client', {
provider,
baseURL: options.baseURL,
hasApiKey: !!options.apiKey,
customTarget: currentCustomApiUrl,
baseURL: options.baseURL,
isCustom: provider === 'custom'
});
return new OpenAI(options);
} else {
const options: any = {
@@ -126,6 +127,7 @@ export const getAI = (config?: AIProviderConfig) => {
options.baseUrl = config.baseUrl;
}
logger.info('API', 'Initializing Google GenAI Client');
return new GoogleGenAI(options);
}
};
@@ -150,4 +152,4 @@ export const getAIProvider = (model: string): ApiProvider => {
return 'custom';
}
return 'google';
};
};