|
| 1 | +import { LLMClasses, llmFromProviderAndOptions } from "./llms/index.js"; |
| 2 | + |
| 3 | +export interface FetchedModel { |
| 4 | + name: string; |
| 5 | + modelId?: string; |
| 6 | + description?: string; |
| 7 | + icon?: string; |
| 8 | + contextLength?: number; |
| 9 | + maxTokens?: number; |
| 10 | + supportsTools?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +const OLLAMA_EXCLUDED_CAPABILITIES = ["vision", "audio", "embedding"]; |
| 14 | + |
| 15 | +const OLLAMA_ICON_MAP: Record<string, string> = { |
| 16 | + llama: "meta.png", |
| 17 | + codellama: "meta.png", |
| 18 | + "phind-codellama": "meta.png", |
| 19 | + deepseek: "deepseek.png", |
| 20 | + deepcoder: "deepseek.png", |
| 21 | + deepscaler: "deepseek.png", |
| 22 | + mistral: "mistral.png", |
| 23 | + mixtral: "mistral.png", |
| 24 | + codestral: "mistral.png", |
| 25 | + devstral: "mistral.png", |
| 26 | + magistral: "mistral.png", |
| 27 | + mathstral: "mistral.png", |
| 28 | + ministral: "mistral.png", |
| 29 | + gemma: "gemini.png", |
| 30 | + codegemma: "gemini.png", |
| 31 | + "gemini-": "gemini.png", |
| 32 | + qwen: "qwen.png", |
| 33 | + codeqwen: "qwen.png", |
| 34 | + qwq: "qwen.png", |
| 35 | + command: "cohere.png", |
| 36 | + aya: "cohere.png", |
| 37 | + granite: "ibm.png", |
| 38 | + nemotron: "nvidia.png", |
| 39 | + kimi: "moonshot.png", |
| 40 | + glm: "zai.svg", |
| 41 | + codegeex: "zai.svg", |
| 42 | + wizardcoder: "wizardlm.png", |
| 43 | + wizardlm: "wizardlm.png", |
| 44 | + "wizard-": "wizardlm.png", |
| 45 | + olmo: "allenai.png", |
| 46 | + tulu: "allenai.png", |
| 47 | + firefunction: "fireworks.png", |
| 48 | + "gpt-oss": "openai.png", |
| 49 | +}; |
| 50 | + |
| 51 | +function getOllamaIcon(modelName: string): string { |
| 52 | + if (OLLAMA_ICON_MAP[modelName]) { |
| 53 | + return OLLAMA_ICON_MAP[modelName]; |
| 54 | + } |
| 55 | + let bestMatch = ""; |
| 56 | + for (const prefix of Object.keys(OLLAMA_ICON_MAP)) { |
| 57 | + if (modelName.startsWith(prefix) && prefix.length > bestMatch.length) { |
| 58 | + bestMatch = prefix; |
| 59 | + } |
| 60 | + } |
| 61 | + return bestMatch ? OLLAMA_ICON_MAP[bestMatch] : "ollama.png"; |
| 62 | +} |
| 63 | + |
| 64 | +async function fetchOllamaModels(): Promise<FetchedModel[]> { |
| 65 | + try { |
| 66 | + const response = await fetch("https://ollama.com/library"); |
| 67 | + if (!response.ok) { |
| 68 | + throw new Error(`Failed to fetch Ollama library: ${response.status}`); |
| 69 | + } |
| 70 | + |
| 71 | + const html = await response.text(); |
| 72 | + const models: FetchedModel[] = []; |
| 73 | + const items = html.split("x-test-model class="); |
| 74 | + const seen = new Set<string>(); |
| 75 | + |
| 76 | + for (let i = 1; i < items.length; i++) { |
| 77 | + const item = items[i]; |
| 78 | + const nameMatch = item.match(/href="\/library\/([^"]+)"/); |
| 79 | + if (!nameMatch) continue; |
| 80 | + const name = nameMatch[1]; |
| 81 | + if (seen.has(name)) continue; |
| 82 | + |
| 83 | + const capabilities: string[] = []; |
| 84 | + const capRegex = /x-test-capability[^>]*>([^<]+)</g; |
| 85 | + let capMatch; |
| 86 | + while ((capMatch = capRegex.exec(item)) !== null) { |
| 87 | + capabilities.push(capMatch[1].trim().toLowerCase()); |
| 88 | + } |
| 89 | + if ( |
| 90 | + capabilities.some((cap) => OLLAMA_EXCLUDED_CAPABILITIES.includes(cap)) |
| 91 | + ) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + const sizes: string[] = []; |
| 96 | + const sizeRegex = /x-test-size[^>]*>([^<]+)</g; |
| 97 | + let sizeMatch; |
| 98 | + while ((sizeMatch = sizeRegex.exec(item)) !== null) { |
| 99 | + sizes.push(sizeMatch[1].trim()); |
| 100 | + } |
| 101 | + |
| 102 | + const descMatch = item.match(/<p class="max-w-lg[^"]*">([^<]+)</); |
| 103 | + const sizeLabel = sizes.length > 0 ? ` (${sizes.join(", ")})` : ""; |
| 104 | + const description = descMatch |
| 105 | + ? descMatch[1].trim() |
| 106 | + : `Ollama model: ${name}${sizeLabel}`; |
| 107 | + |
| 108 | + seen.add(name); |
| 109 | + models.push({ |
| 110 | + name, |
| 111 | + description, |
| 112 | + icon: getOllamaIcon(name), |
| 113 | + supportsTools: capabilities.includes("tools"), |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return models; |
| 118 | + } catch (error) { |
| 119 | + console.error("Error fetching Ollama library models:", error); |
| 120 | + return []; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +async function fetchOpenRouterModels(): Promise<FetchedModel[]> { |
| 125 | + try { |
| 126 | + const response = await fetch("https://openrouter.ai/api/v1/models"); |
| 127 | + if (!response.ok) { |
| 128 | + throw new Error(`Failed to fetch OpenRouter models: ${response.status}`); |
| 129 | + } |
| 130 | + |
| 131 | + const data = await response.json(); |
| 132 | + if (!data.data || !Array.isArray(data.data)) { |
| 133 | + return []; |
| 134 | + } |
| 135 | + |
| 136 | + return data.data |
| 137 | + .filter((m: any) => m.id && m.name) |
| 138 | + .map((m: any) => ({ |
| 139 | + name: m.name, |
| 140 | + modelId: m.id, |
| 141 | + icon: "openrouter.png", |
| 142 | + contextLength: m.context_length, |
| 143 | + maxTokens: m.top_provider?.max_completion_tokens, |
| 144 | + supportsTools: (m.supported_parameters ?? []).includes("tools"), |
| 145 | + })); |
| 146 | + } catch (error) { |
| 147 | + console.error("Error fetching OpenRouter models:", error); |
| 148 | + return []; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +async function fetchAnthropicModels(apiKey?: string): Promise<FetchedModel[]> { |
| 153 | + const response = await fetch( |
| 154 | + "https://api.anthropic.com/v1/models?limit=100", |
| 155 | + { |
| 156 | + headers: { |
| 157 | + "x-api-key": apiKey ?? "", |
| 158 | + "anthropic-version": "2023-06-01", |
| 159 | + }, |
| 160 | + }, |
| 161 | + ); |
| 162 | + if (!response.ok) { |
| 163 | + throw new Error(`Failed to fetch Anthropic models: ${response.status}`); |
| 164 | + } |
| 165 | + const data = await response.json(); |
| 166 | + return (data.data ?? []).map((m: any) => ({ |
| 167 | + name: m.display_name ?? m.id, |
| 168 | + modelId: m.id, |
| 169 | + icon: "anthropic.png", |
| 170 | + contextLength: m.max_input_tokens, |
| 171 | + maxTokens: m.max_tokens, |
| 172 | + supportsTools: true, |
| 173 | + })); |
| 174 | +} |
| 175 | + |
| 176 | +async function fetchGeminiModels( |
| 177 | + apiKey?: string, |
| 178 | + apiBase?: string, |
| 179 | +): Promise<FetchedModel[]> { |
| 180 | + const base = apiBase || "https://generativelanguage.googleapis.com/v1beta/"; |
| 181 | + const url = new URL("models", base); |
| 182 | + url.searchParams.set("key", apiKey ?? ""); |
| 183 | + const response = await fetch(url); |
| 184 | + if (!response.ok) { |
| 185 | + throw new Error(`Failed to fetch Gemini models: ${response.status}`); |
| 186 | + } |
| 187 | + const data = await response.json(); |
| 188 | + return (data.models ?? []) |
| 189 | + .filter((m: any) => { |
| 190 | + const id: string = m.name?.replace("models/", "") ?? ""; |
| 191 | + const methods: string[] = m.supportedGenerationMethods ?? []; |
| 192 | + return ( |
| 193 | + !id.startsWith("gemini-2.0") && |
| 194 | + !id.startsWith("gemma-") && // Gemma models are supported through Ollama, not the Gemini API |
| 195 | + !id.startsWith("nano-banana") && |
| 196 | + !id.startsWith("lyria") && |
| 197 | + methods.includes("generateContent") && |
| 198 | + !methods.includes("embedContent") && |
| 199 | + !methods.includes("predict") && |
| 200 | + !methods.includes("predictLongRunning") && |
| 201 | + !methods.includes("bidiGenerateContent") && |
| 202 | + !id.includes("tts") && |
| 203 | + !id.includes("image") && |
| 204 | + !id.includes("robotics") && |
| 205 | + !id.includes("computer-use") |
| 206 | + ); |
| 207 | + }) |
| 208 | + .map((m: any) => ({ |
| 209 | + name: m.displayName ?? m.name?.replace("models/", ""), |
| 210 | + modelId: m.name?.replace("models/", ""), |
| 211 | + icon: "gemini.png", |
| 212 | + contextLength: m.inputTokenLimit, |
| 213 | + maxTokens: m.outputTokenLimit, |
| 214 | + supportsTools: true, |
| 215 | + })); |
| 216 | +} |
| 217 | + |
| 218 | +async function fetchProviderModelsViaListModels( |
| 219 | + provider: string, |
| 220 | + apiKey?: string, |
| 221 | + apiBase?: string, |
| 222 | +): Promise<FetchedModel[]> { |
| 223 | + try { |
| 224 | + const cls = LLMClasses.find((llm) => llm.providerName === provider); |
| 225 | + const defaultApiBase = cls?.defaultOptions?.apiBase; |
| 226 | + |
| 227 | + const llm = llmFromProviderAndOptions(provider, { |
| 228 | + apiKey, |
| 229 | + apiBase: apiBase || defaultApiBase, |
| 230 | + model: "", |
| 231 | + }); |
| 232 | + const modelIds = await llm.listModels(); |
| 233 | + return modelIds.map((id) => ({ name: id })); |
| 234 | + } catch (error: any) { |
| 235 | + throw new Error( |
| 236 | + `Failed to fetch models for ${provider}: ${error?.message ?? error}`, |
| 237 | + ); |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +export async function fetchModels( |
| 242 | + provider: string, |
| 243 | + apiKey?: string, |
| 244 | + apiBase?: string, |
| 245 | +): Promise<FetchedModel[]> { |
| 246 | + switch (provider) { |
| 247 | + case "ollama": |
| 248 | + return fetchOllamaModels(); |
| 249 | + case "openrouter": |
| 250 | + return fetchOpenRouterModels(); |
| 251 | + case "anthropic": |
| 252 | + return fetchAnthropicModels(apiKey); |
| 253 | + case "gemini": |
| 254 | + return fetchGeminiModels(apiKey, apiBase); |
| 255 | + default: |
| 256 | + return fetchProviderModelsViaListModels(provider, apiKey, apiBase); |
| 257 | + } |
| 258 | +} |
0 commit comments