Elffuss BraidHub

kikocis/continue public

0999e1dfd2e1 · 6.7 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 showMissingApiKeyMsg =
123 option.configurationStatus ===
124 LLMConfigurationStatuses.MISSING_API_KEY;
125
126 return (
127 <ListboxOption
128 key={idx}
129 value={option.title}
130 disabled={showMissingApiKeyMsg}
131 onMouseEnter={() => setHoveredIdx(idx)}
132 onMouseLeave={() => setHoveredIdx(null)}
133 onClick={(e: any) =>
134 handleOptionClick(showMissingApiKeyMsg, e)
135 }
136 className=""
137 >
138 <div className="flex flex-col gap-0.5">
139 <div className="flex flex-1 flex-row items-center justify-between gap-2">
140 <div className="flex flex-1 flex-row items-center gap-2">
141 <CubeIcon className="h-3 w-3 flex-shrink-0" />
142 <span
143 className="line-clamp-1 flex-1"
144 style={{ fontSize: fontSize(-3) }}
145 >
146 {option.title}
147 {showMissingApiKeyMsg && (
148 <span className="ml-2 text-[10px] italic">
149 (Missing API key)
150 </span>
151 )}
152 </span>
153 </div>
154
155 <div className="flex flex-shrink-0 flex-row items-center gap-1">
156 {hoveredIdx === idx && (
157 <Cog6ToothIcon
158 className="h-3 w-3 flex-shrink-0"
159 onClick={onClickGear}
160 />
161 )}
162 {option.title === selectedModel?.title && (
163 <CheckIcon className="h-3 w-3 flex-shrink-0" />
164 )}
165 </div>
166 </div>
167 </div>
168 </ListboxOption>
169 );
170 })}
171 </ListboxOptions>
172 </Transition>
173 </>
174 )}
175 </div>
176 </Listbox>
177 </>
178 );
179};
180
181export default ModelRoleSelector;