| 1 | import { |
| 2 | CheckIcon, |
| 3 | ChevronUpDownIcon, |
| 4 | Cog6ToothIcon, |
| 5 | CubeIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { ModelDescription } from "core"; |
| 8 | import { LLMConfigurationStatuses } from "core/llm/constants"; |
| 9 | import { MouseEvent, useContext, useState } from "react"; |
| 10 | import { defaultBorderRadius } from "../../components"; |
| 11 | import { ToolTip } from "../../components/gui/Tooltip"; |
| 12 | import InfoHover from "../../components/InfoHover"; |
| 13 | import { |
| 14 | Listbox, |
| 15 | ListboxButton, |
| 16 | ListboxOption, |
| 17 | ListboxOptions, |
| 18 | Transition, |
| 19 | } from "../../components/ui"; |
| 20 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 21 | import { fontSize } from "../../util"; |
| 22 | |
| 23 | interface ModelRoleSelectorProps { |
| 24 | models: ModelDescription[]; |
| 25 | selectedModel: ModelDescription | null; |
| 26 | onSelect: (model: ModelDescription | null) => void; |
| 27 | displayName: string; |
| 28 | description: string; |
| 29 | setupURL: string; |
| 30 | } |
| 31 | |
| 32 | const ModelRoleSelector = ({ |
| 33 | models, |
| 34 | selectedModel, |
| 35 | onSelect, |
| 36 | displayName, |
| 37 | description, |
| 38 | setupURL, |
| 39 | }: ModelRoleSelectorProps) => { |
| 40 | const ideMessenger = useContext(IdeMessengerContext); |
| 41 | const [hoveredIdx, setHoveredIdx] = useState<number | null>(null); |
| 42 | |
| 43 | const noConfiguredModels = models.every( |
| 44 | (model) => model.configurationStatus !== LLMConfigurationStatuses.VALID, |
| 45 | ); |
| 46 | |
| 47 | function handleSelect(title: string | null) { |
| 48 | onSelect(models.find((m) => m.title === title) ?? null); |
| 49 | } |
| 50 | |
| 51 | function onClickGear(e: MouseEvent<SVGSVGElement>, model: ModelDescription) { |
| 52 | e.stopPropagation(); |
| 53 | e.preventDefault(); |
| 54 | ideMessenger.post("config/openProfile", { |
| 55 | profileId: undefined, |
| 56 | element: model, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | function handleOptionClick( |
| 61 | showMissingApiKeyMsg: boolean, |
| 62 | e: MouseEvent<HTMLLIElement>, |
| 63 | ) { |
| 64 | if (showMissingApiKeyMsg) { |
| 65 | e.preventDefault(); |
| 66 | e.stopPropagation(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return ( |
| 71 | <> |
| 72 | <div className="mt-2 flex flex-row items-center gap-1 sm:mt-0"> |
| 73 | <span style={{ fontSize: fontSize(-3) }}>{displayName}</span> |
| 74 | <InfoHover size="3" id={displayName} msg={description} /> |
| 75 | <ToolTip id={`${displayName}-description`} place={"bottom"}> |
| 76 | {description} |
| 77 | </ToolTip> |
| 78 | </div> |
| 79 | |
| 80 | <Listbox value={selectedModel?.title ?? null} onChange={handleSelect}> |
| 81 | <div className="relative"> |
| 82 | {models.length === 0 ? ( |
| 83 | <ListboxButton |
| 84 | onClick={() => ideMessenger.post("openUrl", setupURL)} |
| 85 | className="bg-vsc-editor-background hover:bg-list-active hover:text-list-active-foreground text-description w-full justify-between px-2 py-1 underline hover:underline" |
| 86 | > |
| 87 | <span className="line-clamp-1" style={{ fontSize: fontSize(-3) }}> |
| 88 | Setup {displayName} model |
| 89 | </span> |
| 90 | </ListboxButton> |
| 91 | ) : ( |
| 92 | <> |
| 93 | <ListboxButton |
| 94 | disabled={models.length === 0} |
| 95 | className="bg-vsc-editor-background hover:bg-list-active hover:text-list-active-foreground w-full justify-between px-2 py-1" |
| 96 | > |
| 97 | {models.length === 0 || noConfiguredModels ? ( |
| 98 | <span className="text-lightgray line-clamp-1 italic"> |
| 99 | {`No ${models.length === 0 ? "" : "valid "}${displayName} models${ |
| 100 | ["Chat", "Apply", "Edit"].includes(displayName) |
| 101 | ? ". Using Chat model" |
| 102 | : "" |
| 103 | }`} |
| 104 | </span> |
| 105 | ) : ( |
| 106 | <span className="line-clamp-1"> |
| 107 | {selectedModel?.title ?? `Select ${displayName} model`} |
| 108 | </span> |
| 109 | )} |
| 110 | |
| 111 | {models.length > 0 && ( |
| 112 | <div className="pointer-events-none flex items-center"> |
| 113 | <ChevronUpDownIcon className="h-3 w-3" aria-hidden="true" /> |
| 114 | </div> |
| 115 | )} |
| 116 | </ListboxButton> |
| 117 | <Transition> |
| 118 | <ListboxOptions |
| 119 | style={{ borderRadius: defaultBorderRadius }} |
| 120 | className="min-w-40" |
| 121 | > |
| 122 | {[...models] |
| 123 | .sort((a, b) => a.title.localeCompare(b.title)) |
| 124 | .map((option, idx) => { |
| 125 | const isConfigInvalid = |
| 126 | option.configurationStatus !== |
| 127 | LLMConfigurationStatuses.VALID; |
| 128 | let invalidMessage = "(Invalid config)"; |
| 129 | if ( |
| 130 | option.configurationStatus === |
| 131 | LLMConfigurationStatuses.MISSING_ENV_SECRET |
| 132 | ) { |
| 133 | invalidMessage = "(Missing env secret)"; |
| 134 | } |
| 135 | if ( |
| 136 | option.configurationStatus === |
| 137 | LLMConfigurationStatuses.MISSING_API_KEY |
| 138 | ) { |
| 139 | invalidMessage = "(Missing API Key)"; |
| 140 | } |
| 141 | |
| 142 | return ( |
| 143 | <ListboxOption |
| 144 | key={idx} |
| 145 | value={option.title} |
| 146 | disabled={isConfigInvalid} |
| 147 | onMouseEnter={() => setHoveredIdx(idx)} |
| 148 | onMouseLeave={() => setHoveredIdx(null)} |
| 149 | onClick={(e: any) => |
| 150 | handleOptionClick(isConfigInvalid, e) |
| 151 | } |
| 152 | className="" |
| 153 | > |
| 154 | <div className="flex flex-col gap-0.5"> |
| 155 | <div className="flex flex-1 flex-row items-center justify-between gap-2"> |
| 156 | <div className="flex flex-1 flex-row items-center gap-2"> |
| 157 | <CubeIcon className="h-3 w-3 flex-shrink-0" /> |
| 158 | <span |
| 159 | className="line-clamp-1 flex-1" |
| 160 | style={{ fontSize: fontSize(-3) }} |
| 161 | > |
| 162 | {option.title} |
| 163 | {isConfigInvalid && ( |
| 164 | <span className="ml-2 text-[10px] italic"> |
| 165 | {invalidMessage} |
| 166 | </span> |
| 167 | )} |
| 168 | </span> |
| 169 | </div> |
| 170 | |
| 171 | <div className="flex flex-shrink-0 flex-row items-center gap-1"> |
| 172 | {hoveredIdx === idx && ( |
| 173 | <Cog6ToothIcon |
| 174 | className="h-3 w-3 flex-shrink-0" |
| 175 | onClick={(e: MouseEvent<SVGSVGElement>) => |
| 176 | onClickGear(e, option) |
| 177 | } |
| 178 | /> |
| 179 | )} |
| 180 | {option.title === selectedModel?.title && ( |
| 181 | <CheckIcon className="h-3 w-3 flex-shrink-0" /> |
| 182 | )} |
| 183 | </div> |
| 184 | </div> |
| 185 | </div> |
| 186 | </ListboxOption> |
| 187 | ); |
| 188 | })} |
| 189 | </ListboxOptions> |
| 190 | </Transition> |
| 191 | </> |
| 192 | )} |
| 193 | </div> |
| 194 | </Listbox> |
| 195 | </> |
| 196 | ); |
| 197 | }; |
| 198 | |
| 199 | export default ModelRoleSelector; |