()
| 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 | } |
no test coverage detected