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