diff --git a/frontend/src/components/model/ModelSelectDialog.tsx b/frontend/src/components/model/ModelSelectDialog.tsx index 28f70f38..2f88d6ff 100644 --- a/frontend/src/components/model/ModelSelectDialog.tsx +++ b/frontend/src/components/model/ModelSelectDialog.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useMemo } from "react"; +import { useState, useEffect, useMemo, useCallback, memo } from "react"; import { Dialog, DialogContent, @@ -19,7 +19,7 @@ import { useSettings } from "@/hooks/useSettings"; import { useOpenCodeClient } from "@/hooks/useOpenCode"; import { useParams } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; -import type { Model, ProviderSource } from "@/api/providers"; +import type { Model, ProviderWithModels } from "@/api/providers"; interface ModelSelectDialogProps { open: boolean; @@ -28,6 +28,296 @@ interface ModelSelectDialogProps { currentSessionModel?: string | null; } +interface FlatModel { + model: Model; + provider: ProviderWithModels; + modelKey: string; +} + +interface SearchInputProps { + onSearch: (query: string) => void; + initialValue?: string; +} + +function SearchInput({ onSearch, initialValue = "" }: SearchInputProps) { + const [value, setValue] = useState(initialValue); + + useEffect(() => { + const timer = setTimeout(() => onSearch(value), 150); + return () => clearTimeout(timer); + }, [value, onSearch]); + + useEffect(() => { + setValue(initialValue); + }, [initialValue]); + + return ( +
+
+ + setValue(e.target.value)} + className="pl-10 text-sm" + /> +
+
+ ); +} + +interface ModelCardProps { + model: Model; + provider: ProviderWithModels; + modelKey: string; + isSelected: boolean; + onSelect: (providerId: string, modelId: string) => void; +} + +const ModelCard = memo(function ModelCard({ + model, + provider, + isSelected, + onSelect +}: ModelCardProps) { + const capabilities = useMemo(() => { + const caps = []; + if (model.reasoning) caps.push("Reasoning"); + if (model.tool_call) caps.push("Tools"); + if (model.attachment) caps.push("Files"); + return caps; + }, [model.reasoning, model.tool_call, model.attachment]); + + const statusBadge = useMemo(() => { + if (model.experimental) return Experimental; + if (model.status === "alpha") return Alpha; + if (model.status === "beta") return Beta; + return null; + }, [model.experimental, model.status]); + + const sourceBadge = useMemo(() => { + switch (provider.source) { + case "configured": + return Custom; + case "local": + return Local; + default: + return null; + } + }, [provider.source]); + + return ( +
onSelect(provider.id, model.id)} + > +
+
+
+

+ {formatModelName(model)} +

+ {sourceBadge} +
+

+ {formatProviderName(provider)} +

+
+ {isSelected && ( + + )} +
+ +
+ {model.id} +
+ + {capabilities.length > 0 && ( +
+ {capabilities.slice(0, 2).map((cap) => ( + + {cap} + + ))} + {capabilities.length > 2 && ( + + +{capabilities.length - 2} + + )} +
+ )} + + {statusBadge &&
{statusBadge}
} + +
+ {model.limit?.context && ( +
+ Context: + + {model.limit.context >= 1000000 + ? `${(model.limit.context / 1000000).toFixed(1)}M` + : model.limit.context.toLocaleString() + } tokens + +
+ )} + {model.cost && ( +
+ Cost: + ${model.cost.input.toFixed(4)}/1K +
+ )} +
+
+ ); +}); + +interface ModelGridProps { + models: FlatModel[]; + currentModel: string; + onSelect: (providerId: string, modelId: string) => void; + loading: boolean; +} + +const ModelGrid = memo(function ModelGrid({ + models, + currentModel, + onSelect, + loading +}: ModelGridProps) { + if (loading) { + return ( +
+ +
+ ); + } + + if (models.length === 0) { + return ( +
+ No models found +
+ ); + } + + return ( +
+ {models.map(({ model, provider, modelKey }) => ( + + ))} +
+ ); +}); + +interface ProviderSidebarProps { + groupedProviders: { + configured: ProviderWithModels[]; + local: ProviderWithModels[]; + builtin: ProviderWithModels[]; + }; + selectedProvider: string; + onSelect: (providerId: string) => void; +} + +const ProviderSidebar = memo(function ProviderSidebar({ + groupedProviders, + selectedProvider, + onSelect, +}: ProviderSidebarProps) { + return ( +
+
+ + + {groupedProviders.configured.length > 0 && ( +
+

+ + Custom Providers +

+
+ {groupedProviders.configured.map((provider) => ( + + ))} +
+
+ )} + + {groupedProviders.local.length > 0 && ( +
+

+ + Local Providers +

+
+ {groupedProviders.local.map((provider) => ( + + ))} +
+
+ )} + + {groupedProviders.builtin.length > 0 && ( +
+

+ + Built-in Providers +

+
+ {groupedProviders.builtin.map((provider) => ( + + ))} +
+
+ )} +
+
+ ); +}); + export function ModelSelectDialog({ open, onOpenChange, @@ -46,6 +336,8 @@ export function ModelSelectDialog({ queryKey: ["providers-with-models"], queryFn: () => getProvidersWithModels(), enabled: open, + staleTime: 5 * 60 * 1000, + gcTime: 10 * 60 * 1000, }); useEffect(() => { @@ -55,21 +347,30 @@ export function ModelSelectDialog({ } }, [currentModel, providers]); - const filteredProviders = providers.filter((provider) => { - const matchesSearch = - provider.name.toLowerCase().includes(searchQuery.toLowerCase()) || - provider.models.some( - (model) => - model.name.toLowerCase().includes(searchQuery.toLowerCase()) || - model.id.toLowerCase().includes(searchQuery.toLowerCase()), + const flatModels = useMemo((): FlatModel[] => { + return providers.flatMap((provider) => + provider.models.map((model) => ({ + model, + provider, + modelKey: `${provider.id}/${model.id}`, + })) + ); + }, [providers]); + + const filteredModels = useMemo(() => { + const search = searchQuery.toLowerCase(); + return flatModels.filter((item) => { + if (selectedProvider && item.provider.id !== selectedProvider) { + return false; + } + if (!search) return true; + return ( + item.model.name.toLowerCase().includes(search) || + item.model.id.toLowerCase().includes(search) || + item.provider.name.toLowerCase().includes(search) ); - - const matchesSelectedProvider = !selectedProvider || provider.id === selectedProvider; - - return matchesSearch && matchesSelectedProvider; - }); - - const selectedProviderData = providers.find(p => p.id === selectedProvider); + }); + }, [flatModels, selectedProvider, searchQuery]); const groupedProviders = useMemo(() => { const configured = providers.filter(p => p.source === "configured"); @@ -78,29 +379,24 @@ export function ModelSelectDialog({ return { configured, local, builtin }; }, [providers]); - const getSourceBadge = (source: ProviderSource) => { - switch (source) { - case "configured": - return Custom; - case "local": - return Local; - case "builtin": - return null; - } - }; + const selectedProviderData = useMemo( + () => providers.find(p => p.id === selectedProvider), + [providers, selectedProvider] + ); - const handleProviderSelect = (providerId: string) => { + const handleProviderSelect = useCallback((providerId: string) => { setSelectedProvider(providerId); setSearchQuery(""); - }; + }, []); - const handleModelSelect = async (providerId: string, modelId: string) => { + const handleSearch = useCallback((query: string) => { + setSearchQuery(query); + }, []); + + const handleModelSelect = useCallback(async (providerId: string, modelId: string) => { const newModel = `${providerId}/${modelId}`; - - // Update settings for future sessions updateSettings({ defaultModel: newModel }); - // If we're in a session, try to update the current session's model if (sessionID && client) { try { await client.sendCommand(sessionID, { @@ -109,31 +405,14 @@ export function ModelSelectDialog({ model: newModel, }); } catch { - // Ignore errors when updating session model + // Ignore errors } } onOpenChange(false); - }; + }, [sessionID, client, updateSettings, onOpenChange]); - - - const getStatusBadge = (model: Model) => { - if (model.experimental) - return Experimental; - if (model.status === "alpha") - return Alpha; - if (model.status === "beta") return Beta; - return null; - }; - - const getModelCapabilities = (model: Model) => { - const capabilities = []; - if (model.reasoning) capabilities.push("Reasoning"); - if (model.tool_call) capabilities.push("Tools"); - if (model.attachment) capabilities.push("Files"); - return capabilities; - }; + const searchResetKey = selectedProvider; return ( @@ -145,93 +424,14 @@ export function ModelSelectDialog({
- {/* Sidebar - Provider List (Desktop) */} -
-
- + - {groupedProviders.configured.length > 0 && ( -
-

- - Custom Providers -

-
- {groupedProviders.configured.map((provider) => ( - - ))} -
-
- )} - - {groupedProviders.local.length > 0 && ( -
-

- - Local Providers -

-
- {groupedProviders.local.map((provider) => ( - - ))} -
-
- )} - - {groupedProviders.builtin.length > 0 && ( -
-

- - Built-in Providers -

-
- {groupedProviders.builtin.map((provider) => ( - - ))} -
-
- )} -
-
- - {/* Main Content */} -
- {/* Mobile Provider Dropdown */} -
+
+
- {/* Search Bar */} -
-
- - setSearchQuery(e.target.value)} - className="pl-10 text-sm" - /> -
-
+ - {/* Models Grid */}
- {loading ? ( -
- -
- ) : filteredProviders.length === 0 ? ( -
- No models found -
- ) : ( -
- {filteredProviders.flatMap((provider) => - provider.models.map((model) => { - const modelKey = `${provider.id}/${model.id}`; - const isSelected = currentModel === modelKey; - const capabilities = getModelCapabilities(model); - - return ( -
handleModelSelect(provider.id, model.id)} - > -
-
-
-

- {formatModelName(model)} -

- {getSourceBadge(provider.source)} -
-

- {formatProviderName(provider)} -

-
- {isSelected && ( - - )} -
- -
- {model.id} -
- - {/* Capabilities */} - {capabilities.length > 0 && ( -
- {capabilities.slice(0, 2).map((cap) => ( - - {cap} - - ))} - {capabilities.length > 2 && ( - - +{capabilities.length - 2} - - )} -
- )} - - {/* Status Badge */} -
- {getStatusBadge(model)} -
- - {/* Specs */} -
- {model.limit?.context && ( -
- Context: - - {model.limit.context >= 1000000 - ? `${(model.limit.context / 1000000).toFixed(1)}M` - : model.limit.context.toLocaleString() - } tokens - -
- )} - {model.cost && ( -
- Cost: - - ${model.cost.input.toFixed(4)}/1K - -
- )} -
-
- ); - }) - )} -
- )} +
- {/* Footer */} {currentModel && (

@@ -411,4 +507,3 @@ export function ModelSelectDialog({

); } -