新增自定义模型
This commit is contained in:
@@ -1,18 +1,5 @@
|
||||
/**
|
||||
* Network Interceptor
|
||||
*
|
||||
* Intercepts global fetch calls to redirect Gemini API requests
|
||||
* from the default endpoint to a user-defined custom base URL.
|
||||
*
|
||||
* Uses Object.defineProperty to bypass "getter-only" restrictions on window.fetch
|
||||
* in certain sandboxed or strict environments.
|
||||
*/
|
||||
|
||||
const originalFetch = window.fetch;
|
||||
|
||||
/**
|
||||
* Robustly applies a function to the window.fetch property.
|
||||
*/
|
||||
const applyFetch = (fn: typeof window.fetch) => {
|
||||
try {
|
||||
Object.defineProperty(window, 'fetch', {
|
||||
@@ -22,7 +9,6 @@ const applyFetch = (fn: typeof window.fetch) => {
|
||||
enumerable: true
|
||||
});
|
||||
} catch (e) {
|
||||
// Fallback for environments where defineProperty on window might fail
|
||||
try {
|
||||
(window as any).fetch = fn;
|
||||
} catch (err) {
|
||||
@@ -33,15 +19,12 @@ const applyFetch = (fn: typeof window.fetch) => {
|
||||
|
||||
export const setInterceptorUrl = (baseUrl: string | null) => {
|
||||
if (!baseUrl) {
|
||||
// Restore original fetch when disabled
|
||||
applyFetch(originalFetch);
|
||||
return;
|
||||
}
|
||||
|
||||
// Normalize the base URL
|
||||
let normalizedBase = baseUrl.trim();
|
||||
try {
|
||||
// Basic validation
|
||||
new URL(normalizedBase);
|
||||
} catch (e) {
|
||||
console.warn("[Prisma] Invalid Base URL provided:", normalizedBase);
|
||||
@@ -65,28 +48,22 @@ export const setInterceptorUrl = (baseUrl: string | null) => {
|
||||
|
||||
const defaultHost = 'generativelanguage.googleapis.com';
|
||||
|
||||
// Check if the request is targeting the Google Gemini API
|
||||
if (urlString.includes(defaultHost)) {
|
||||
try {
|
||||
const url = new URL(urlString);
|
||||
const proxy = new URL(normalizedBase);
|
||||
|
||||
// Replace protocol and host
|
||||
url.protocol = proxy.protocol;
|
||||
url.host = proxy.host;
|
||||
|
||||
// Prepend proxy path if it exists (e.g., /v1/proxy)
|
||||
if (proxy.pathname !== '/') {
|
||||
const cleanPath = proxy.pathname.endsWith('/') ? proxy.pathname.slice(0, -1) : proxy.pathname;
|
||||
// Ensure we don't double up slashes
|
||||
url.pathname = cleanPath + url.pathname;
|
||||
}
|
||||
|
||||
const newUrl = url.toString();
|
||||
|
||||
// Handle the different types of fetch inputs
|
||||
if (input instanceof Request) {
|
||||
// Re-create the request with the new URL and original properties
|
||||
const requestData: RequestInit = {
|
||||
method: input.method,
|
||||
headers: input.headers,
|
||||
@@ -99,9 +76,7 @@ export const setInterceptorUrl = (baseUrl: string | null) => {
|
||||
integrity: input.integrity,
|
||||
};
|
||||
|
||||
// Merge with init if provided
|
||||
const mergedInit = { ...requestData, ...init };
|
||||
|
||||
return originalFetch(new URL(newUrl), mergedInit);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user