Elffuss BraidHub

kikocis/continue public

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