"use client"; import { useEffect, useMemo, useState } from "react"; import { Check, ChevronsUpDown, Loader2, Plus, RefreshCw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Label } from "@/components/ui/label"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { useLLMProviders } from "@/hooks/use-llm-providers"; import { useLLMProviderModels } from "@/hooks/use-llm-provider-models"; import { useI18n } from "@/i18n/i18n-context"; import { cn } from "@/lib/utils"; interface LLMModelSelectorProps { providerId: string; model: string; onProviderChange: (providerId: string, defaultModel?: string) => void; onModelChange: (model: string) => void; /** @deprecated Free-text typing is now part of the unified combobox in * every surface; this flag is ignored. Kept for callsite compatibility. */ allowFreeText?: boolean; /** Auto-select the first provider on mount when no providerId is set. */ autoSelectFirst?: boolean; /** Provider label override (defaults to "Provider"). */ providerLabel?: string; /** Model label override (defaults to "Model"). */ modelLabel?: string; } /** * Reusable Provider + Model selector. The provider stays a plain Select * (the list is small and authoritative), but the model widget is a * typeable combobox: it filters known model ids as the user types and * always offers a `使用 ""` row so callers can submit a model * the upstream `/models` endpoint did not return — useful when a * provider exposes a partial list, or when the user wants to test a * preview / private model id. */ export function LLMModelSelector({ providerId, model, onProviderChange, onModelChange, autoSelectFirst = false, providerLabel = "Provider", modelLabel = "Model", }: LLMModelSelectorProps) { const { t } = useI18n(); const { data: providers = [], isLoading: providersLoading } = useLLMProviders(); const { data: modelsFromApi, isFetching: modelsFetching, refetch: refetchModels, } = useLLMProviderModels(providerId || undefined, { enabled: Boolean(providerId), }); // Auto-select the first provider on first paint when nothing is chosen. useEffect(() => { if (!autoSelectFirst) return; if (providerId) return; if (providers.length === 0) return; const first = providers[0]; onProviderChange(first.id, first.defaultModel ?? ""); // Provider list is stable per session; we deliberately depend on // `providerId` to trigger only when it transitions to empty. // eslint-disable-next-line react-hooks/exhaustive-deps }, [autoSelectFirst, providerId, providers]); const modelOptions = useMemo(() => { const ids = new Set(); if (model) ids.add(model); for (const m of modelsFromApi ?? []) ids.add(m.id); const current = providers.find((p) => p.id === providerId); if (current?.defaultModel) ids.add(current.defaultModel); return Array.from(ids); }, [model, modelsFromApi, providers, providerId]); return (
); } interface LLMModelComboboxProps { value: string; options: string[]; onChange: (model: string) => void; disabled?: boolean; fetching?: boolean; } /** * Typeable model picker with custom-value escape hatch. Built on * Popover + Command (cmdk) so the input filters the known list as the * user types; if no exact match exists, an "use " row appears at * the bottom of the list, letting the user commit a model id the * upstream `/models` endpoint never returned. */ function LLMModelCombobox({ value, options, onChange, disabled, fetching, }: LLMModelComboboxProps) { const { t } = useI18n(); const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); // We do our own filtering so we can show a "use custom" row even when // the typed value doesn't match any existing option. const trimmed = query.trim(); const filtered = useMemo(() => { if (!trimmed) return options; const lower = trimmed.toLowerCase(); return options.filter((opt) => opt.toLowerCase().includes(lower)); }, [options, trimmed]); const hasExactMatch = options.some( (opt) => opt.toLowerCase() === trimmed.toLowerCase(), ); const showCustom = trimmed.length > 0 && !hasExactMatch; const commit = (next: string) => { onChange(next); setQuery(""); setOpen(false); }; return ( // `modal` is load-bearing: this selector is rendered inside the chat config // Sheet, and a Radix Dialog's scroll lock cancels wheel events on portalled // content it doesn't own — without it the model list refuses to scroll. // See components/ui/__tests__/popover-in-dialog.test.ts. {filtered.length === 0 && !showCustom ? ( {fetching ? t("common.modelSelector.fetchingModels") : options.length === 0 ? t("common.modelSelector.emptyModels") : t("common.modelSelector.noMatch")} ) : null} {filtered.length > 0 && ( {filtered.map((id) => ( commit(id)}> {id} ))} )} {showCustom && ( commit(trimmed)} > {t("common.modelSelector.useTyped", { value: trimmed })} )} ); }