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