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