Elffuss BraidHub

kikocis/continue public

972ea8cfaef0 · 5.8 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}
30
31const ModelRoleSelector = ({
32 models,
33 selectedModel,
34 onSelect,
35 displayName,
36 description,
37}: ModelRoleSelectorProps) => {
38 const ideMessenger = useContext(IdeMessengerContext);
39 const [hoveredIdx, setHoveredIdx] = useState<number | null>(null);
40
41 const noConfiguredModels = models.every(
42 (model) => model.configurationStatus !== LLMConfigurationStatuses.VALID,
43 );
44
45 function handleSelect(title: string | null) {
46 onSelect(models.find((m) => m.title === title) ?? null);
47 }
48
49 function onClickGear(e: MouseEvent<SVGSVGElement>) {
50 e.stopPropagation();
51 e.preventDefault();
52 ideMessenger.post("config/openProfile", { profileId: undefined });
53 }
54
55 function handleOptionClick(
56 showMissingApiKeyMsg: boolean,
57 e: MouseEvent<HTMLLIElement>,
58 ) {
59 if (showMissingApiKeyMsg) {
60 e.preventDefault();
61 e.stopPropagation();
62 }
63 }
64
65 return (
66 <>
67 <div className="mt-2 flex flex-row items-center gap-1 sm:mt-0">
68 <span
69 style={{
70 fontSize: fontSize(-3),
71 }}
72 >
73 {displayName}
74 </span>
75 <InfoHover size="3" id={displayName} msg={description} />
76 <ToolTip id={`${displayName}-description`} place={"bottom"}>
77 {description}
78 </ToolTip>
79 </div>
80 <Listbox value={selectedModel?.title ?? null} onChange={handleSelect}>
81 <div className="relative">
82 <ListboxButton
83 disabled={models.length === 0}
84 className={`bg-vsc-editor-background hover:bg-list-active hover:text-list-active-foreground w-full justify-between`}
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 className="line-clamp-1">
96 {selectedModel?.title ?? `Select ${displayName} model`}
97 </span>
98 )}
99 {models.length ? (
100 <div className="pointer-events-none flex items-center">
101 <ChevronUpDownIcon className="h-3 w-3" aria-hidden="true" />
102 </div>
103 ) : null}
104 </ListboxButton>
105
106 <Transition>
107 <ListboxOptions
108 style={{ borderRadius: defaultBorderRadius }}
109 className="min-w-40"
110 >
111 {[...models]
112 .sort((a, b) => a.title.localeCompare(b.title))
113 .map((option, idx) => {
114 const showMissingApiKeyMsg =
115 option.configurationStatus ===
116 LLMConfigurationStatuses.MISSING_API_KEY;
117
118 return (
119 <ListboxOption
120 key={idx}
121 value={option.title}
122 disabled={showMissingApiKeyMsg}
123 onMouseEnter={() => setHoveredIdx(idx)}
124 onMouseLeave={() => setHoveredIdx(null)}
125 onClick={(e: any) =>
126 handleOptionClick(showMissingApiKeyMsg, e)
127 }
128 className={""}
129 >
130 <div className="flex flex-col gap-0.5">
131 <div className="flex flex-1 flex-row items-center justify-between gap-2">
132 <div className="flex flex-1 flex-row items-center gap-2">
133 <CubeIcon className="h-3 w-3 flex-shrink-0" />
134 <span
135 className="line-clamp-1 flex-1"
136 style={{ fontSize: fontSize(-3) }}
137 >
138 {option.title}
139 {showMissingApiKeyMsg && (
140 <span className="ml-2 text-[10px] italic">
141 (Missing API key)
142 </span>
143 )}
144 </span>
145 </div>
146 <div className="flex flex-shrink-0 flex-row items-center gap-1">
147 {hoveredIdx === idx && (
148 <Cog6ToothIcon
149 className="h-3 w-3 flex-shrink-0"
150 onClick={onClickGear}
151 />
152 )}
153 {option.title === selectedModel?.title && (
154 <CheckIcon className="h-3 w-3 flex-shrink-0" />
155 )}
156 </div>
157 </div>
158 </div>
159 </ListboxOption>
160 );
161 })}
162 </ListboxOptions>
163 </Transition>
164 </div>
165 </Listbox>
166 </>
167 );
168};
169
170export default ModelRoleSelector;