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