Elffuss BraidHub

kikocis/continue public

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