| 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 { useAuth } from "../../context/Auth"; |
| 11 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 12 | import { AddModelForm } from "../../forms/AddModelForm"; |
| 13 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 14 | import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; |
| 15 | import { updateSelectedModelByRole } from "../../redux/thunks/updateSelectedModelByRole"; |
| 16 | import { |
| 17 | fontSize, |
| 18 | getMetaKeyLabel, |
| 19 | isMetaEquivalentKeyPressed, |
| 20 | } from "../../util"; |
| 21 | import { |
| 22 | Listbox, |
| 23 | ListboxButton, |
| 24 | ListboxOption, |
| 25 | ListboxOptions, |
| 26 | } from "../ui/Listbox"; |
| 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"> |
| 75 | <div className="flex items-center gap-2"> |
| 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 | |
| 102 | const isInEdit = useAppSelector((store) => store.session.isInEdit); |
| 103 | const config = useAppSelector((state) => state.config.config); |
| 104 | const isConfigLoading = useAppSelector((state) => state.config.loading); |
| 105 | const ideMessenger = useContext(IdeMessengerContext); |
| 106 | const buttonRef = useRef<HTMLButtonElement>(null); |
| 107 | const [options, setOptions] = useState<Option[]>([]); |
| 108 | const [sortedOptions, setSortedOptions] = useState<Option[]>([]); |
| 109 | const { selectedProfile } = useAuth(); |
| 110 | |
| 111 | let selectedModel = null; |
| 112 | let allModels = null; |
| 113 | if (isInEdit) { |
| 114 | allModels = config.modelsByRole.edit; |
| 115 | selectedModel = config.selectedModelByRole.edit; |
| 116 | } |
| 117 | if (!selectedModel) { |
| 118 | selectedModel = config.selectedModelByRole.chat; |
| 119 | } |
| 120 | if (!allModels || allModels.length === 0) { |
| 121 | allModels = config.modelsByRole.chat; |
| 122 | } |
| 123 | |
| 124 | // Sort so that options without an API key are at the end |
| 125 | useEffect(() => { |
| 126 | const alphaSort = options.sort((a, b) => a.title.localeCompare(b.title)); |
| 127 | const enabledOptions = alphaSort.filter((option) => option.apiKey !== ""); |
| 128 | const disabledOptions = alphaSort.filter((option) => option.apiKey === ""); |
| 129 | |
| 130 | const sorted = [...enabledOptions, ...disabledOptions]; |
| 131 | |
| 132 | setSortedOptions(sorted); |
| 133 | }, [options]); |
| 134 | |
| 135 | useEffect(() => { |
| 136 | setOptions( |
| 137 | allModels.map((model) => { |
| 138 | return { |
| 139 | value: model.title, |
| 140 | title: modelSelectTitle(model), |
| 141 | apiKey: model.apiKey, |
| 142 | sourceFile: model.sourceFile, |
| 143 | isAutoDetected: model.isFromAutoDetect, |
| 144 | }; |
| 145 | }), |
| 146 | ); |
| 147 | }, [allModels]); |
| 148 | |
| 149 | useEffect(() => { |
| 150 | const handleKeyDown = (event: KeyboardEvent) => { |
| 151 | if ( |
| 152 | event.key === "'" && |
| 153 | isMetaEquivalentKeyPressed(event as any) && |
| 154 | !event.shiftKey // To prevent collisions w/ assistant toggle logic |
| 155 | ) { |
| 156 | if (!selectedProfile) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | const direction = event.shiftKey ? -1 : 1; |
| 161 | const currentIndex = options.findIndex( |
| 162 | (option) => option.value === selectedModel?.title, |
| 163 | ); |
| 164 | let nextIndex = (currentIndex + 1 * direction) % options.length; |
| 165 | if (nextIndex < 0) nextIndex = options.length - 1; |
| 166 | const newModelTitle = options[nextIndex].value; |
| 167 | |
| 168 | void dispatch( |
| 169 | updateSelectedModelByRole({ |
| 170 | selectedProfile, |
| 171 | role: "chat", |
| 172 | modelTitle: newModelTitle, |
| 173 | }), |
| 174 | ); |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | window.addEventListener("keydown", handleKeyDown); |
| 179 | return () => { |
| 180 | window.removeEventListener("keydown", handleKeyDown); |
| 181 | }; |
| 182 | }, [options, selectedModel]); |
| 183 | |
| 184 | function onClickAddModel(e: MouseEvent) { |
| 185 | e.stopPropagation(); |
| 186 | e.preventDefault(); |
| 187 | |
| 188 | // Close the dropdown |
| 189 | if (buttonRef.current) { |
| 190 | buttonRef.current.click(); |
| 191 | } |
| 192 | dispatch(setShowDialog(true)); |
| 193 | dispatch( |
| 194 | setDialogMessage( |
| 195 | <AddModelForm |
| 196 | onDone={() => { |
| 197 | dispatch(setShowDialog(false)); |
| 198 | }} |
| 199 | />, |
| 200 | ), |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | const hasNoModels = allModels?.length === 0; |
| 205 | |
| 206 | return ( |
| 207 | <Listbox |
| 208 | onChange={async (val: string) => { |
| 209 | if (val === selectedModel?.title) return; |
| 210 | void dispatch( |
| 211 | updateSelectedModelByRole({ |
| 212 | selectedProfile, |
| 213 | role: isInEdit ? "edit" : "chat", |
| 214 | modelTitle: val, |
| 215 | }), |
| 216 | ); |
| 217 | }} |
| 218 | > |
| 219 | <div className="relative flex"> |
| 220 | <ListboxButton |
| 221 | data-testid="model-select-button" |
| 222 | ref={buttonRef} |
| 223 | className="text-description h-[18px] gap-1 border-none" |
| 224 | > |
| 225 | <span className="line-clamp-1 break-all hover:brightness-110"> |
| 226 | {modelSelectTitle(selectedModel) || "Select model"} |
| 227 | </span> |
| 228 | <ChevronDownIcon |
| 229 | className="hidden h-2 w-2 flex-shrink-0 hover:brightness-110 min-[200px]:flex" |
| 230 | aria-hidden="true" |
| 231 | /> |
| 232 | </ListboxButton> |
| 233 | <ListboxOptions className="min-w-[160px]"> |
| 234 | <div className="flex items-center justify-between gap-1 px-2 py-1"> |
| 235 | <span className="font-semibold">Models</span> |
| 236 | <Cog6ToothIcon |
| 237 | className="text-description h-3 w-3 cursor-pointer hover:brightness-125" |
| 238 | onClick={() => |
| 239 | ideMessenger.post("config/openProfile", { |
| 240 | profileId: undefined, |
| 241 | element: { sourceFile: selectedModel?.sourceFile }, |
| 242 | }) |
| 243 | } |
| 244 | /> |
| 245 | </div> |
| 246 | |
| 247 | <div className="no-scrollbar max-h-[300px] overflow-y-auto"> |
| 248 | {isConfigLoading ? ( |
| 249 | <div className="text-description flex items-center gap-2 px-2 pb-2 pt-1 text-xs"> |
| 250 | <ArrowPathIcon className="animate-spin-slow h-3 w-3" /> |
| 251 | <span>Loading config</span> |
| 252 | </div> |
| 253 | ) : hasNoModels ? ( |
| 254 | <div className="text-description-muted px-2 py-4 text-center text-sm"> |
| 255 | No models configured |
| 256 | </div> |
| 257 | ) : ( |
| 258 | sortedOptions.map((option, idx) => ( |
| 259 | <ModelOption |
| 260 | option={option} |
| 261 | idx={idx} |
| 262 | key={idx} |
| 263 | showMissingApiKeyMsg={option.apiKey === ""} |
| 264 | isSelected={option.value === selectedModel?.title} |
| 265 | /> |
| 266 | )) |
| 267 | )} |
| 268 | </div> |
| 269 | |
| 270 | {!isConfigLoading && selectedProfile?.profileType === "local" && ( |
| 271 | <ListboxOption |
| 272 | key={options.length} |
| 273 | onClick={onClickAddModel} |
| 274 | value={"addModel" as any} |
| 275 | className="border-border border-x-0 border-y border-solid" |
| 276 | > |
| 277 | <div |
| 278 | className="text-description flex items-center py-0.5 hover:text-inherit" |
| 279 | style={{ |
| 280 | fontSize: fontSize(-3), |
| 281 | }} |
| 282 | > |
| 283 | <PlusIcon className="mr-2 h-3 w-3" /> |
| 284 | Add Chat model |
| 285 | </div> |
| 286 | </ListboxOption> |
| 287 | )} |
| 288 | |
| 289 | {!isConfigLoading && ( |
| 290 | <div |
| 291 | className="text-description-muted px-2 py-1" |
| 292 | style={{ fontSize: fontSize(-3) }} |
| 293 | > |
| 294 | <code>{getMetaKeyLabel()}'</code> to toggle model |
| 295 | </div> |
| 296 | )} |
| 297 | </ListboxOptions> |
| 298 | </div> |
| 299 | </Listbox> |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | export default ModelSelect; |