-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathapi.ts
More file actions
125 lines (105 loc) · 4.12 KB
/
api.ts
File metadata and controls
125 lines (105 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { GoogleGenAI } from "@google/genai";
import OpenAI from "openai";
import { ApiProvider, CustomModel, AIClient } from './types';
// --- Configuration & Types ---
type AIProviderConfig = {
provider?: ApiProvider;
apiKey?: string;
baseUrl?: string;
};
// --- Provider Detection ---
export const isGoogleProvider = (ai: AIClient | unknown): ai is GoogleGenAIClient => {
return typeof ai === 'object' && ai !== null && 'models' in ai &&
typeof (ai as GoogleGenAIClient).models?.generateContent === 'function';
};
// --- Custom Fetch (per-instance, not global) ---
/**
* Creates a custom fetch function scoped to a specific base URL.
* Only handles API version prefix deduplication as a safety net.
* URL routing is handled by SDK-level options (httpOptions.baseUrl / baseURL).
*/
const createCustomFetch = (baseUrl: string | null): typeof globalThis.fetch => {
if (!baseUrl) return window.fetch.bind(window);
const nativeFetch = window.fetch.bind(window);
const cleanBaseUrl = baseUrl.replace(/\/+$/, '');
return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
let urlString: string;
if (typeof input === 'string') {
urlString = input;
} else if (input instanceof URL) {
urlString = input.toString();
} else {
urlString = input.url;
}
// Safety: deduplicate API version prefix (e.g., /v1beta/v1beta → /v1beta)
// Some Google SDK versions may double-append version prefixes when httpOptions.baseUrl includes one
if (cleanBaseUrl) {
try {
const baseHost = new URL(cleanBaseUrl).host;
if (baseHost && urlString.includes(baseHost)) {
const url = new URL(urlString);
const basePath = new URL(cleanBaseUrl).pathname.replace(/\/$/, '');
const versionPrefix = basePath.match(/\/v\d+(beta|alpha)?$/)?.[0];
if (versionPrefix && url.pathname.includes(versionPrefix + versionPrefix)) {
url.pathname = url.pathname.replace(versionPrefix + versionPrefix, versionPrefix);
return nativeFetch(url.toString(), init);
}
}
} catch { /* ignore URL parse errors */ }
}
return nativeFetch(input, init);
};
};
// --- Helper Functions ---
export const findCustomModel = (modelName: string, customModels?: CustomModel[]): CustomModel | undefined => {
return customModels?.find(m => m.name === modelName);
};
/**
* Detect API provider from model name prefix.
* Fallback when customModelConfig is unavailable.
*/
export const getAIProvider = (model: string): ApiProvider => {
const openaiPrefixes = [
'gpt-', 'o1-', 'o3-', 'o4-',
'deepseek-', 'claude-',
'grok-', 'mistral-', 'mixtral-',
'qwen-', 'yi-', 'glm-',
];
if (openaiPrefixes.some(p => model.startsWith(p))) return 'openai';
if (model === 'custom') return 'openai';
return 'google';
};
// --- API Client Factory ---
export const getAI = (config?: AIProviderConfig): AIClient => {
const provider = config?.provider || 'google';
const apiKey = config?.apiKey || import.meta.env?.VITE_API_KEY;
const baseUrl = config?.baseUrl || null;
const customFetch = createCustomFetch(baseUrl);
// Handle OpenAI-compatible providers
if (provider === 'openai') {
const options: ConstructorParameters<typeof OpenAI>[0] = {
apiKey: apiKey,
dangerouslyAllowBrowser: true,
fetch: customFetch,
baseURL: baseUrl || 'https://api.openai.com/v1',
};
return new OpenAI(options) as unknown as OpenAIClient;
}
// Handle Google — use httpOptions.baseUrl for custom endpoint support
else {
const options: ConstructorParameters<typeof GoogleGenAI>[0] = {
apiKey: apiKey,
httpOptions: { fetch: customFetch },
};
// Strip trailing API version prefix (e.g. /v1beta) since the SDK adds it automatically
if (baseUrl) {
let cleanUrl = baseUrl.replace(/\/+$/, '');
const versionMatch = cleanUrl.match(/\/(v\d+(?:alpha|beta)?)$/);
if (versionMatch) {
cleanUrl = cleanUrl.slice(0, -versionMatch[0].length);
}
options.httpOptions!.baseUrl = cleanUrl;
}
return new GoogleGenAI(options) as unknown as GoogleGenAIClient;
}
};