Merge pull request #3147 from Dokploy/Add-search-functionality-to-AI-model-selection-dropdown

feat: enhance AI model selection with popover and search functionality
This commit is contained in:
Mauricio Siu
2025-11-30 23:47:20 -06:00
committed by GitHub
@@ -1,12 +1,19 @@
"use client"; "use client";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { PenBoxIcon, PlusIcon } from "lucide-react"; import { Check, ChevronDown, PenBoxIcon, PlusIcon } from "lucide-react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { toast } from "sonner"; import { toast } from "sonner";
import { z } from "zod"; import { z } from "zod";
import { AlertBlock } from "@/components/shared/alert-block"; import { AlertBlock } from "@/components/shared/alert-block";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import { import {
Dialog, Dialog,
DialogContent, DialogContent,
@@ -26,13 +33,12 @@ import {
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { import {
Select, Popover,
SelectContent, PopoverContent,
SelectItem, PopoverTrigger,
SelectTrigger, } from "@/components/ui/popover";
SelectValue,
} from "@/components/ui/select";
import { Switch } from "@/components/ui/switch"; import { Switch } from "@/components/ui/switch";
import { cn } from "@/lib/utils";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
const Schema = z.object({ const Schema = z.object({
@@ -53,6 +59,8 @@ export const HandleAi = ({ aiId }: Props) => {
const utils = api.useUtils(); const utils = api.useUtils();
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [modelPopoverOpen, setModelPopoverOpen] = useState(false);
const [modelSearch, setModelSearch] = useState("");
const { data, refetch } = api.ai.one.useQuery( const { data, refetch } = api.ai.one.useQuery(
{ {
aiId: aiId || "", aiId: aiId || "",
@@ -77,13 +85,17 @@ export const HandleAi = ({ aiId }: Props) => {
}); });
useEffect(() => { useEffect(() => {
form.reset({ if (data) {
name: data?.name ?? "", form.reset({
apiUrl: data?.apiUrl ?? "https://api.openai.com/v1", name: data?.name ?? "",
apiKey: data?.apiKey ?? "", apiUrl: data?.apiUrl ?? "https://api.openai.com/v1",
model: data?.model ?? "", apiKey: data?.apiKey ?? "",
isEnabled: data?.isEnabled ?? true, model: data?.model ?? "",
}); isEnabled: data?.isEnabled ?? true,
});
}
setModelSearch("");
setModelPopoverOpen(false);
}, [aiId, form, data]); }, [aiId, form, data]);
const apiUrl = form.watch("apiUrl"); const apiUrl = form.watch("apiUrl");
@@ -104,14 +116,6 @@ export const HandleAi = ({ aiId }: Props) => {
}, },
); );
useEffect(() => {
const apiUrl = form.watch("apiUrl");
const apiKey = form.watch("apiKey");
if (apiUrl && apiKey) {
form.setValue("model", "");
}
}, [form.watch("apiUrl"), form.watch("apiKey")]);
const onSubmit = async (data: Schema) => { const onSubmit = async (data: Schema) => {
try { try {
await mutateAsync({ await mutateAsync({
@@ -131,7 +135,16 @@ export const HandleAi = ({ aiId }: Props) => {
}; };
return ( return (
<Dialog open={open} onOpenChange={setOpen}> <Dialog
open={open}
onOpenChange={(isOpen) => {
setOpen(isOpen);
if (!isOpen) {
setModelSearch("");
setModelPopoverOpen(false);
}
}}
>
<DialogTrigger className="" asChild> <DialogTrigger className="" asChild>
{aiId ? ( {aiId ? (
<Button <Button
@@ -182,7 +195,17 @@ export const HandleAi = ({ aiId }: Props) => {
<FormItem> <FormItem>
<FormLabel>API URL</FormLabel> <FormLabel>API URL</FormLabel>
<FormControl> <FormControl>
<Input placeholder="https://api.openai.com/v1" {...field} /> <Input
placeholder="https://api.openai.com/v1"
{...field}
onChange={(e) => {
field.onChange(e);
// Reset model when user changes API URL
if (form.getValues("model")) {
form.setValue("model", "");
}
}}
/>
</FormControl> </FormControl>
<FormDescription> <FormDescription>
The base URL for your AI provider's API The base URL for your AI provider's API
@@ -205,6 +228,13 @@ export const HandleAi = ({ aiId }: Props) => {
placeholder="sk-..." placeholder="sk-..."
autoComplete="one-time-code" autoComplete="one-time-code"
{...field} {...field}
onChange={(e) => {
field.onChange(e);
// Reset model when user changes API Key
if (form.getValues("model")) {
form.setValue("model", "");
}
}}
/> />
</FormControl> </FormControl>
<FormDescription> <FormDescription>
@@ -232,30 +262,89 @@ export const HandleAi = ({ aiId }: Props) => {
<FormField <FormField
control={form.control} control={form.control}
name="model" name="model"
render={({ field }) => ( render={({ field }) => {
<FormItem> const selectedModel = models.find(
<FormLabel>Model</FormLabel> (m) => m.id === field.value,
<Select );
onValueChange={field.onChange} const filteredModels = models.filter((model) =>
value={field.value || ""} model.id.toLowerCase().includes(modelSearch.toLowerCase()),
> );
<FormControl>
<SelectTrigger> // Ensure selected model is always in the filtered list
<SelectValue placeholder="Select a model" /> const displayModels =
</SelectTrigger> field.value &&
</FormControl> !filteredModels.find((m) => m.id === field.value) &&
<SelectContent> selectedModel
{models.map((model) => ( ? [selectedModel, ...filteredModels]
<SelectItem key={model.id} value={model.id}> : filteredModels;
{model.id}
</SelectItem> return (
))} <FormItem>
</SelectContent> <FormLabel>Model</FormLabel>
</Select> <Popover
<FormDescription>Select an AI model to use</FormDescription> open={modelPopoverOpen}
<FormMessage /> onOpenChange={setModelPopoverOpen}
</FormItem> >
)} <PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
className={cn(
"w-full justify-between",
!field.value && "text-muted-foreground",
)}
>
{field.value
? (selectedModel?.id ?? field.value)
: "Select a model"}
<ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0" align="start">
<Command>
<CommandInput
placeholder="Search models..."
value={modelSearch}
onValueChange={setModelSearch}
/>
<CommandList>
<CommandEmpty>No models found.</CommandEmpty>
{displayModels.map((model) => {
const isSelected = field.value === model.id;
return (
<CommandItem
key={model.id}
value={model.id}
onSelect={() => {
field.onChange(model.id);
setModelPopoverOpen(false);
setModelSearch("");
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
isSelected
? "opacity-100"
: "opacity-0",
)}
/>
{model.id}
</CommandItem>
);
})}
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormDescription>
Select an AI model to use
</FormDescription>
<FormMessage />
</FormItem>
);
}}
/> />
)} )}