Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 5.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}
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].sort((a,b)=>a.title.localeCompare(b.title)).map((option, idx) => {
112 const showMissingApiKeyMsg =
113 option.configurationStatus ===
114 LLMConfigurationStatuses.MISSING_API_KEY;
115
116 return (
117 <ListboxOption
118 key={idx}
119 value={option.title}
120 disabled={showMissingApiKeyMsg}
121 onMouseEnter={() => setHoveredIdx(idx)}
122 onMouseLeave={() => setHoveredIdx(null)}
123 onClick={(e: any) =>
124 handleOptionClick(showMissingApiKeyMsg, e)
125 }
126 className={""}
127 >
128 <div className="flex flex-col gap-0.5">
129 <div className="flex flex-1 flex-row items-center justify-between gap-2">
130 <div className="flex flex-1 flex-row items-center gap-2">
131 <CubeIcon className="h-3 w-3 flex-shrink-0" />
132 <span
133 className="line-clamp-1 flex-1"
134 style={{ fontSize: fontSize(-3) }}
135 >
136 {option.title}
137 {showMissingApiKeyMsg && (
138 <span className="ml-2 text-[10px] italic">
139 (Missing API key)
140 </span>
141 )}
142 </span>
143 </div>
144 <div className="flex flex-shrink-0 flex-row items-center gap-1">
145 {hoveredIdx === idx && (
146 <Cog6ToothIcon
147 className="h-3 w-3 flex-shrink-0"
148 onClick={onClickGear}
149 />
150 )}
151 {option.title === selectedModel?.title && (
152 <CheckIcon className="h-3 w-3 flex-shrink-0" />
153 )}
154 </div>
155 </div>
156 </div>
157 </ListboxOption>
158 );
159 })}
160 </ListboxOptions>
161 </Transition>
162 </div>
163 </Listbox>
164 </>
165 );
166};
167
168export default ModelRoleSelector;