|
1 | | -import { useCallback, useState } from 'react'; |
| 1 | +import { useCallback, useState, useEffect } from 'react'; |
2 | 2 | import type { IProviderConfig } from '~/types/model'; |
| 3 | +import { updateProviderSettings } from '~/lib/stores/settings'; |
3 | 4 |
|
4 | 5 | export interface UseLocalProvidersReturn { |
5 | 6 | localProviders: IProviderConfig[]; |
6 | 7 | refreshLocalProviders: () => void; |
| 8 | + isChecking: boolean; |
7 | 9 | } |
8 | 10 |
|
9 | 11 | export function useLocalProviders(): UseLocalProvidersReturn { |
10 | 12 | const [localProviders, setLocalProviders] = useState<IProviderConfig[]>([]); |
| 13 | + const [isChecking, setIsChecking] = useState(false); |
11 | 14 |
|
12 | | - const refreshLocalProviders = useCallback(() => { |
13 | | - /* |
14 | | - * Refresh logic for local providers |
15 | | - * This would typically involve checking the status of Ollama and LMStudio |
16 | | - * For now, we'll just return an empty array |
17 | | - */ |
18 | | - setLocalProviders([]); |
19 | | - }, []); |
| 15 | + const checkLocalProvider = useCallback( |
| 16 | + async (name: string, baseUrl: string, icon: string): Promise<IProviderConfig | null> => { |
| 17 | + try { |
| 18 | + // Try to connect to the local provider |
| 19 | + const response = await fetch(`${baseUrl}/api/tags`, { |
| 20 | + method: 'GET', |
| 21 | + signal: AbortSignal.timeout(5000), // 5 second timeout |
| 22 | + }); |
| 23 | + |
| 24 | + if (response.ok) { |
| 25 | + return { |
| 26 | + name, |
| 27 | + icon, |
| 28 | + getApiKeyLink: `https://${name.toLowerCase()}.com/download`, |
| 29 | + labelForGetApiKey: `Download ${name}`, |
| 30 | + staticModels: [], |
| 31 | + settings: { |
| 32 | + enabled: true, |
| 33 | + baseUrl, |
| 34 | + }, |
| 35 | + }; |
| 36 | + } |
| 37 | + } catch { |
| 38 | + // Provider not available, return null |
| 39 | + } |
| 40 | + return null; |
| 41 | + }, |
| 42 | + [], |
| 43 | + ); |
| 44 | + |
| 45 | + const refreshLocalProviders = useCallback(async () => { |
| 46 | + setIsChecking(true); |
| 47 | + |
| 48 | + try { |
| 49 | + const providers: IProviderConfig[] = []; |
| 50 | + |
| 51 | + // Check Ollama |
| 52 | + |
| 53 | + const ollamaProvider = await checkLocalProvider( |
| 54 | + 'Ollama', |
| 55 | + 'http://localhost:11434', |
| 56 | + '/thirdparty/logos/ollama.svg', |
| 57 | + ); |
| 58 | + |
| 59 | + if (ollamaProvider) { |
| 60 | + providers.push(ollamaProvider); |
| 61 | + } |
| 62 | + |
| 63 | + // Check LMStudio |
| 64 | + |
| 65 | + const lmStudioProvider = await checkLocalProvider( |
| 66 | + 'LMStudio', |
| 67 | + 'http://localhost:1234', |
| 68 | + '/thirdparty/logos/lmstudio.svg', |
| 69 | + ); |
| 70 | + |
| 71 | + if (lmStudioProvider) { |
| 72 | + providers.push(lmStudioProvider); |
| 73 | + } |
| 74 | + |
| 75 | + setLocalProviders(providers); |
| 76 | + |
| 77 | + // Enable detected providers in settings |
| 78 | + providers.forEach((provider) => { |
| 79 | + updateProviderSettings(provider.name, { enabled: true, baseUrl: provider.settings.baseUrl }); |
| 80 | + }); |
| 81 | + |
| 82 | + // Disable providers that are no longer available |
| 83 | + const availableProviderNames = providers.map((p) => p.name); |
| 84 | + const allLocalProviderNames = ['Ollama', 'LMStudio']; |
| 85 | + |
| 86 | + allLocalProviderNames.forEach((providerName) => { |
| 87 | + if (!availableProviderNames.includes(providerName)) { |
| 88 | + updateProviderSettings(providerName, { enabled: false }); |
| 89 | + } |
| 90 | + }); |
| 91 | + } catch (error) { |
| 92 | + console.error('Error checking local providers:', error); |
| 93 | + setLocalProviders([]); |
| 94 | + |
| 95 | + // Disable all local providers if there's an error |
| 96 | + ['Ollama', 'LMStudio'].forEach((providerName) => { |
| 97 | + updateProviderSettings(providerName, { enabled: false }); |
| 98 | + }); |
| 99 | + } finally { |
| 100 | + setIsChecking(false); |
| 101 | + } |
| 102 | + }, [checkLocalProvider]); |
| 103 | + |
| 104 | + // Auto-refresh on mount |
| 105 | + useEffect(() => { |
| 106 | + refreshLocalProviders(); |
| 107 | + }, [refreshLocalProviders]); |
20 | 108 |
|
21 | 109 | return { |
22 | 110 | localProviders, |
23 | 111 | refreshLocalProviders, |
| 112 | + isChecking, |
24 | 113 | }; |
25 | 114 | } |
0 commit comments