| 1 | import { |
| 2 | ArrowPathIcon, |
| 3 | ChevronDownIcon, |
| 4 | Cog6ToothIcon, |
| 5 | CubeIcon, |
| 6 | PlusIcon, |
| 7 | } from "@heroicons/react/24/outline"; |
| 8 | import { useEffect, useRef, useState } from "react"; |
| 9 | import { useNavigate } from "react-router-dom"; |
| 10 | import { useAuth } from "../../context/Auth"; |
| 11 | import { AddModelForm } from "../../forms/AddModelForm"; |
| 12 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 13 | import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; |
| 14 | import { updateSelectedModelByRole } from "../../redux/thunks/updateSelectedModelByRole"; |
| 15 | import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util"; |
| 16 | import { CONFIG_ROUTES } from "../../util/navigation"; |
| 17 | import { |
| 18 | Button, |
| 19 | Listbox, |
| 20 | ListboxButton, |
| 21 | ListboxOption, |
| 22 | ListboxOptions, |
| 23 | useFontSize, |
| 24 | } from "../ui"; |
| 25 | import { Divider } from "../ui/Divider"; |
| 26 | |
| 27 | interface ModelOptionProps { |
| 28 | option: Option; |
| 29 | idx: number; |
| 30 | showMissingApiKeyMsg: boolean; |
| 31 | isSelected?: boolean; |
| 32 | } |
| 33 | |
| 34 | interface Option { |
| 35 | value: string; |
| 36 | title: string; |
| 37 | apiKey?: string; |
| 38 | sourceFile?: string; |
| 39 | isAutoDetected?: boolean; |
| 40 | } |
| 41 | |
| 42 | function modelSelectTitle(model: any): string { |
| 43 | if (model?.title) return model?.title; |
| 44 | if (model?.model !== undefined && model?.model.trim() !== "") { |
| 45 | if (model?.class_name) { |
| 46 | return `${model?.class_name} - ${model?.model}`; |
| 47 | } |
| 48 | return model?.model; |
| 49 | } |
| 50 | return model?.class_name; |
| 51 | } |
| 52 | |
| 53 | /**makeshift way to close the headlessui listbox due to absence of open state on the listbox */ |
| 54 | function closeDropDown(button: HTMLButtonElement | null) { |
| 55 | if (!button) return; |
| 56 | button.classList.add("hidden"); |
| 57 | setTimeout(() => { |
| 58 | button.classList.remove("hidden"); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | function ModelOption({ |
| 63 | option, |
| 64 | idx, |
| 65 | showMissingApiKeyMsg, |
| 66 | isSelected, |
| 67 | }: ModelOptionProps) { |
| 68 | const navigate = useNavigate(); |
| 69 | |
| 70 | function handleOptionClick(e: any) { |
| 71 | if (showMissingApiKeyMsg) { |
| 72 | e.preventDefault(); |
| 73 | e.stopPropagation(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function handleConfigureClick(e: React.MouseEvent) { |
| 78 | e.stopPropagation(); |
| 79 | navigate(CONFIG_ROUTES.MODELS); |
| 80 | } |
| 81 | |
| 82 | return ( |
| 83 | <ListboxOption |
| 84 | key={idx} |
| 85 | disabled={showMissingApiKeyMsg} |
| 86 | value={option.value} |
| 87 | onClick={handleOptionClick} |
| 88 | className={`group ${isSelected ? "bg-list-active text-list-active-foreground" : ""}`} |
| 89 | > |
| 90 | <div className="flex w-full items-center justify-between gap-5"> |
| 91 | <div className="flex items-center gap-2 py-0.5"> |
| 92 | <CubeIcon className="h-3 w-3 flex-shrink-0" /> |
| 93 | <span className="line-clamp-1"> |
| 94 | {option.title} |
| 95 | {option.isAutoDetected && ( |
| 96 | <span className="text-description-muted ml-1.5 text-[10px] italic"> |
| 97 | (autodetected) |
| 98 | </span> |
| 99 | )} |
| 100 | {showMissingApiKeyMsg && ( |
| 101 | <span className="ml-1.5 text-[10px] italic"> |
| 102 | (Missing API key) |
| 103 | </span> |
| 104 | )} |
| 105 | </span> |
| 106 | </div> |
| 107 | <Button |
| 108 | variant="ghost" |
| 109 | size="sm" |
| 110 | className="text-description-muted hover:enabled:text-foreground my-0 h-4 w-4 p-0 opacity-0 transition-opacity group-hover:opacity-100" |
| 111 | onClick={handleConfigureClick} |
| 112 | > |
| 113 | <Cog6ToothIcon className="h-3.5 w-3.5" /> |
| 114 | </Button> |
| 115 | </div> |
| 116 | </ListboxOption> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | function ModelSelect() { |
| 121 | const dispatch = useAppDispatch(); |
| 122 | const navigate = useNavigate(); |
| 123 | |
| 124 | const isInEdit = useAppSelector((store) => store.session.isInEdit); |
| 125 | const config = useAppSelector((state) => state.config.config); |
| 126 | const isConfigLoading = useAppSelector((state) => state.config.loading); |
| 127 | const buttonRef = useRef<HTMLButtonElement>(null); |
| 128 | const [options, setOptions] = useState<Option[]>([]); |
| 129 | const [sortedOptions, setSortedOptions] = useState<Option[]>([]); |
| 130 | const { selectedProfile } = useAuth(); |
| 131 | const tinyFont = useFontSize(-4); |
| 132 | |
| 133 | let selectedModel = null; |
| 134 | let allModels = null; |
| 135 | if (isInEdit) { |
| 136 | allModels = config.modelsByRole.edit; |
| 137 | selectedModel = config.selectedModelByRole.edit; |
| 138 | } |
| 139 | if (!selectedModel) { |
| 140 | selectedModel = config.selectedModelByRole.chat; |
| 141 | } |
| 142 | if (!allModels || allModels.length === 0) { |
| 143 | allModels = config.modelsByRole.chat; |
| 144 | } |
| 145 | |
| 146 | // Sort so that options without an API key are at the end |
| 147 | useEffect(() => { |
| 148 | const alphaSort = options.sort((a, b) => a.title.localeCompare(b.title)); |
| 149 | const enabledOptions = alphaSort.filter((option) => option.apiKey !== ""); |
| 150 | const disabledOptions = alphaSort.filter((option) => option.apiKey === ""); |
| 151 | |
| 152 | const sorted = [...enabledOptions, ...disabledOptions]; |
| 153 | |
| 154 | setSortedOptions(sorted); |
| 155 | }, [options]); |
| 156 | |
| 157 | useEffect(() => { |
| 158 | setOptions( |
| 159 | allModels.map((model) => { |
| 160 | return { |
| 161 | value: model.title, |
| 162 | title: modelSelectTitle(model), |
| 163 | apiKey: model.apiKey, |
| 164 | sourceFile: model.sourceFile, |
| 165 | isAutoDetected: model.isFromAutoDetect, |
| 166 | }; |
| 167 | }), |
| 168 | ); |
| 169 | }, [allModels]); |
| 170 | |
| 171 | useEffect(() => { |
| 172 | const handleKeyDown = (event: KeyboardEvent) => { |
| 173 | if ( |
| 174 | event.key === "'" && |
| 175 | isMetaEquivalentKeyPressed(event as any) && |
| 176 | !event.shiftKey // To prevent collisions w/ assistant toggle logic |
| 177 | ) { |
| 178 | if (!selectedProfile) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | const direction = event.shiftKey ? -1 : 1; |
| 183 | const currentIndex = options.findIndex( |
| 184 | (option) => option.value === selectedModel?.title, |
| 185 | ); |
| 186 | let nextIndex = (currentIndex + 1 * direction) % options.length; |
| 187 | if (nextIndex < 0) nextIndex = options.length - 1; |
| 188 | const newModelTitle = options[nextIndex].value; |
| 189 | |
| 190 | void dispatch( |
| 191 | updateSelectedModelByRole({ |
| 192 | selectedProfile, |
| 193 | role: "chat", |
| 194 | modelTitle: newModelTitle, |
| 195 | }), |
| 196 | ); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | window.addEventListener("keydown", handleKeyDown); |
| 201 | return () => { |
| 202 | window.removeEventListener("keydown", handleKeyDown); |
| 203 | }; |
| 204 | }, [options, selectedModel]); |
| 205 | |
| 206 | function onClickAddModel(e: MouseEvent) { |
| 207 | e.stopPropagation(); |
| 208 | e.preventDefault(); |
| 209 | |
| 210 | closeDropDown(buttonRef.current); |
| 211 | |
| 212 | dispatch(setShowDialog(true)); |
| 213 | dispatch( |
| 214 | setDialogMessage( |
| 215 | <AddModelForm |
| 216 | onDone={() => { |
| 217 | dispatch(setShowDialog(false)); |
| 218 | }} |
| 219 | />, |
| 220 | ), |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | function onClickConfigureModels(e: React.MouseEvent) { |
| 225 | e.stopPropagation(); |
| 226 | e.preventDefault(); |
| 227 | |
| 228 | closeDropDown(buttonRef.current); |
| 229 | |
| 230 | navigate(CONFIG_ROUTES.MODELS); |
| 231 | } |
| 232 | |
| 233 | const hasNoModels = allModels?.length === 0; |
| 234 | |
| 235 | return ( |
| 236 | <Listbox |
| 237 | onChange={async (val: string) => { |
| 238 | if (val === selectedModel?.title) return; |
| 239 | void dispatch( |
| 240 | updateSelectedModelByRole({ |
| 241 | selectedProfile, |
| 242 | role: isInEdit ? "edit" : "chat", |
| 243 | modelTitle: val, |
| 244 | }), |
| 245 | ); |
| 246 | }} |
| 247 | > |
| 248 | <div className="relative flex"> |
| 249 | <ListboxButton |
| 250 | data-testid="model-select-button" |
| 251 | ref={buttonRef} |
| 252 | className="text-description h-[18px] gap-1 border-none" |
| 253 | > |
| 254 | <span className="line-clamp-1 break-all hover:brightness-110"> |
| 255 | {modelSelectTitle(selectedModel) || "Select model"} |
| 256 | </span> |
| 257 | <ChevronDownIcon |
| 258 | className="hidden h-2 w-2 flex-shrink-0 hover:brightness-110 min-[200px]:flex" |
| 259 | aria-hidden="true" |
| 260 | /> |
| 261 | </ListboxButton> |
| 262 | <ListboxOptions className="min-w-[160px]"> |
| 263 | <div className="flex items-center justify-between px-1.5 py-1"> |
| 264 | <span className="text-description text-xs font-medium">Models</span> |
| 265 | <div className="flex items-center gap-0.5"> |
| 266 | <Button |
| 267 | onClick={(e) => { |
| 268 | e.stopPropagation(); |
| 269 | onClickConfigureModels(e); |
| 270 | }} |
| 271 | variant="ghost" |
| 272 | size="sm" |
| 273 | className="my-0 h-5 w-5 p-0" |
| 274 | > |
| 275 | <Cog6ToothIcon className="text-description h-3.5 w-3.5" /> |
| 276 | </Button> |
| 277 | </div> |
| 278 | </div> |
| 279 | |
| 280 | <div className="no-scrollbar max-h-[300px] overflow-y-auto"> |
| 281 | {isConfigLoading ? ( |
| 282 | <div className="text-description flex items-center gap-2 px-2 pb-2 pt-1 text-xs"> |
| 283 | <ArrowPathIcon className="animate-spin-slow h-3 w-3" /> |
| 284 | <span>Loading config</span> |
| 285 | </div> |
| 286 | ) : hasNoModels ? ( |
| 287 | <div className="text-description-muted px-2 py-4 text-center text-sm"> |
| 288 | No models configured |
| 289 | </div> |
| 290 | ) : ( |
| 291 | sortedOptions.map((option, idx) => ( |
| 292 | <ModelOption |
| 293 | option={option} |
| 294 | idx={idx} |
| 295 | key={idx} |
| 296 | showMissingApiKeyMsg={option.apiKey === ""} |
| 297 | isSelected={option.value === selectedModel?.title} |
| 298 | /> |
| 299 | )) |
| 300 | )} |
| 301 | </div> |
| 302 | |
| 303 | {!isConfigLoading && ( |
| 304 | <> |
| 305 | {selectedProfile?.profileType === "local" && ( |
| 306 | <> |
| 307 | <Divider className="!mb-0" /> |
| 308 | <ListboxOption |
| 309 | key={options.length} |
| 310 | onClick={onClickAddModel} |
| 311 | value={"addModel" as any} |
| 312 | fontSizeModifier={-2} |
| 313 | className="px-2 py-2" |
| 314 | > |
| 315 | <span className="text-description text-2xs flex flex-row items-center"> |
| 316 | <PlusIcon className="mr-1.5 h-3.5 w-3.5" /> |
| 317 | Add Chat model |
| 318 | </span> |
| 319 | </ListboxOption> |
| 320 | </> |
| 321 | )} |
| 322 | |
| 323 | <Divider className="!my-0" /> |
| 324 | <div className="text-description flex items-center justify-start p-2"> |
| 325 | <span className="block" style={{ fontSize: tinyFont }}> |
| 326 | <code>{getMetaKeyLabel()}'</code> to toggle model |
| 327 | </span> |
| 328 | </div> |
| 329 | </> |
| 330 | )} |
| 331 | </ListboxOptions> |
| 332 | </div> |
| 333 | </Listbox> |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | export default ModelSelect; |