mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Improve performance of useProviderModels hook
This commit is contained in:
2 files changed
+19
-14
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { ReactNode, useState, useEffect } from 'react';
|
||||
import { AISettingsContext, type AISettings, type AIConnection, type AISettingsContextType } from '@/contexts/AISettingsContext';
|
||||
|
||||
@@ -13,6 +14,8 @@ const DEFAULT_SETTINGS: AISettings = {
|
||||
const STORAGE_KEY = 'ai-settings';
|
||||
|
||||
export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [settings, setSettings] = useState<AISettings>(() => {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
@@ -48,6 +51,7 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
[name]: connection,
|
||||
},
|
||||
}));
|
||||
queryClient.invalidateQueries({ queryKey: ['provider-models'] });
|
||||
};
|
||||
|
||||
const removeProvider = (name: string) => {
|
||||
@@ -58,6 +62,7 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
providers: rest,
|
||||
};
|
||||
});
|
||||
queryClient.invalidateQueries({ queryKey: ['provider-models'] });
|
||||
};
|
||||
|
||||
const updateProvider = (name: string, connection: Partial<AIConnection>) => {
|
||||
@@ -71,6 +76,7 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
|
||||
},
|
||||
},
|
||||
}));
|
||||
queryClient.invalidateQueries({ queryKey: ['provider-models'] });
|
||||
};
|
||||
|
||||
const addRecentlyUsedModel = (modelId: string) => {
|
||||
|
||||
@@ -38,13 +38,12 @@ export function useProviderModels(): ModelFetchResult {
|
||||
isLoading,
|
||||
refetch,
|
||||
} = useQuery({
|
||||
queryKey: ['provider-models', settings.providers],
|
||||
queryKey: ['provider-models'],
|
||||
queryFn: async () => {
|
||||
const allModels: ProviderModel[] = [];
|
||||
const errors: string[] = [];
|
||||
|
||||
// Fetch models from each configured provider
|
||||
for (const [providerKey, connection] of Object.entries(settings.providers)) {
|
||||
// Fetch models from each configured provider in parallel
|
||||
const providerPromises = Object.entries(settings.providers).map(async ([providerKey, connection]) => {
|
||||
try {
|
||||
const openai = createAIClient(connection, user);
|
||||
|
||||
@@ -84,13 +83,14 @@ export function useProviderModels(): ModelFetchResult {
|
||||
return providerModel;
|
||||
});
|
||||
|
||||
allModels.push(...providerModels);
|
||||
return providerModels;
|
||||
} catch (fetchError) {
|
||||
if (fetchError instanceof Error && fetchError.name === 'AbortError') {
|
||||
errors.push(`${providerKey}: Request timeout`);
|
||||
} else {
|
||||
throw fetchError;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
} catch (providerError) {
|
||||
console.warn(`Failed to fetch models from ${providerKey}:`, providerError);
|
||||
@@ -99,8 +99,15 @@ export function useProviderModels(): ModelFetchResult {
|
||||
providerError instanceof Error ? providerError.message : 'Unknown error'
|
||||
}`
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Wait for all provider requests to complete
|
||||
const providerResults = await Promise.all(providerPromises);
|
||||
|
||||
// Flatten all models into a single array
|
||||
const allModels = providerResults.flat();
|
||||
|
||||
// Set error state if there were any errors
|
||||
if (errors.length > 0) {
|
||||
@@ -109,14 +116,6 @@ export function useProviderModels(): ModelFetchResult {
|
||||
setError(null);
|
||||
}
|
||||
|
||||
// Sort models by provider, then by name
|
||||
allModels.sort((a, b) => {
|
||||
if (a.provider !== b.provider) {
|
||||
return a.provider.localeCompare(b.provider);
|
||||
}
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
|
||||
return allModels;
|
||||
},
|
||||
enabled: Object.keys(settings.providers).length > 0,
|
||||
|
||||
Reference in new issue
Block a user