"use client"; import { memo, useState } from "react"; import { IconCheck, IconChevronDown } from "@tabler/icons-react"; import { cn } from "@/lib/utils"; import { Button } from "@kandev/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@kandev/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover"; import { ScrollArea } from "@kandev/ui/scroll-area"; import { Separator } from "@kandev/ui/separator"; export type ModelSelectorOption = { id: string; name: string; description?: string; usageMultiplier?: string; }; export type DynamicConfigOption = { type: string; id: string; name: string; currentValue: string; category?: string; options?: { value: string; name: string }[]; }; export type SelectConfigOption = DynamicConfigOption & { options: { value: string; name: string }[]; }; const MODEL_CONFIG_CATEGORY = "model"; const MODE_CONFIG_CATEGORY = "mode"; export function isModelConfigOption(option: Pick): boolean { return option.id === MODEL_CONFIG_CATEGORY || option.category === MODEL_CONFIG_CATEGORY; } export function isModeConfigOption(option: Pick): boolean { return option.id === MODE_CONFIG_CATEGORY || option.category === MODE_CONFIG_CATEGORY; } export function usableConfigOptions( options: DynamicConfigOption[] | undefined, ): SelectConfigOption[] { return (options ?? []).filter( (option): option is SelectConfigOption => option.type === "select" && !isModeConfigOption(option) && Array.isArray(option.options) && option.options.length > 0, ); } export function configOptionToModelOptions( option: SelectConfigOption | undefined, ): ModelSelectorOption[] { if (!option) return []; return option.options.map((item) => ({ id: item.value, name: item.name, description: item.value !== item.name ? item.value : undefined, })); } function currentOptionName(option: DynamicConfigOption): string { return ( option.options?.find((item) => item.value === option.currentValue)?.name ?? option.currentValue ); } export function displayModelName( modelOptions: ModelSelectorOption[], currentModel: string, ): string { return modelOptions.find((m) => m.id === currentModel)?.name ?? currentModel; } export function triggerLabel( modelOptions: ModelSelectorOption[], currentModel: string, configOptions: DynamicConfigOption[], ): string { const modelConfig = configOptions.find(isModelConfigOption); const modelValue = modelConfig ? currentOptionName(modelConfig) : displayModelName(modelOptions, currentModel); const extras = configOptions .filter((option) => !isModelConfigOption(option)) .map(currentOptionName) .filter(Boolean); return [modelValue, ...extras].join(" / "); } export function resolveTriggerLabel( modelOptions: ModelSelectorOption[], currentModel: string | null, modelConfig: DynamicConfigOption | undefined, configOptions: DynamicConfigOption[], ): string { const modelValue = currentModel || modelConfig?.currentValue; if (!modelValue) return ""; return triggerLabel(modelOptions, modelValue, configOptions); } function ModelRow({ model, selected, onSelect, }: { model: ModelSelectorOption; selected: boolean; onSelect: (value: string) => void; }) { return ( onSelect(model.id)} className="relative pr-7" >
{model.name}
{model.description && (
{model.description}
)}
{model.usageMultiplier && ( {model.usageMultiplier} )}
); } function ConfigOptionSection({ option, onChange, }: { option: SelectConfigOption; onChange?: (configId: string, value: string) => void; }) { return (
{option.name}
{option.options.map((item) => ( ))}
); } export type ModelConfigSelectorProps = { modelOptions: ModelSelectorOption[]; currentModel: string | null; configOptions?: DynamicConfigOption[]; onModelChange: (modelId: string) => void; onConfigChange?: (configId: string, value: string) => void; disabled?: boolean; placeholder?: string; ariaLabel?: string; variant?: "compact" | "field"; popoverSide?: "top" | "bottom"; triggerClassName?: string; }; export const ModelConfigSelector = memo(function ModelConfigSelector({ modelOptions, currentModel, configOptions = [], onModelChange, onConfigChange, disabled, placeholder = "Select model...", ariaLabel = "Model settings", variant = "field", popoverSide = "bottom", triggerClassName: customTriggerClassName, }: ModelConfigSelectorProps) { const [open, setOpen] = useState(false); const selectConfigOptions = usableConfigOptions(configOptions); const modelConfig = selectConfigOptions.find(isModelConfigOption); const extraConfigOptions = selectConfigOptions.filter((option) => !isModelConfigOption(option)); const currentModelValue = modelConfig?.currentValue || currentModel || ""; const label = resolveTriggerLabel(modelOptions, currentModel, modelConfig, configOptions); const hasExtraConfigOptions = extraConfigOptions.length > 0; const onModelSelect = (value: string) => { if (!value) return; onModelChange(value); if (!hasExtraConfigOptions) { setOpen(false); } }; const baseTriggerClassName = variant === "compact" ? "h-7 max-w-[min(18rem,70vw)] cursor-pointer gap-1 px-2 text-xs hover:bg-muted/40" : "w-full justify-between font-normal cursor-pointer"; return ( No models found. {modelOptions.map((model) => ( ))} {extraConfigOptions.length > 0 && ( <>
{extraConfigOptions.map((option) => ( ))}
)}
); });