Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 9.1 KiBRawPermalink
1import {
2 AtSymbolIcon,
3 LightBulbIcon as LightBulbIconOutline,
4 PhotoIcon,
5} from "@heroicons/react/24/outline";
6import { LightBulbIcon as LightBulbIconSolid } from "@heroicons/react/24/solid";
7import { InputModifiers } from "core";
8import {
9 modelSupportsImages,
10 modelSupportsReasoning,
11} from "core/llm/autodetect";
12import { memo, useContext, useRef } from "react";
13import { IdeMessengerContext } from "../../context/IdeMessenger";
14import { useAppDispatch, useAppSelector } from "../../redux/hooks";
15import { selectUseActiveFile } from "../../redux/selectors";
16import { selectSelectedChatModel } from "../../redux/slices/configSlice";
17import { setHasReasoningEnabled } from "../../redux/slices/sessionSlice";
18import { exitEdit } from "../../redux/thunks/edit";
19import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util";
20import { ToolTip } from "../gui/Tooltip";
21import ModelSelect from "../modelSelection/ModelSelect";
22import { ModeSelect } from "../ModeSelect";
23import { Button } from "../ui";
24import { useFontSize } from "../ui/font";
25import ContextStatus from "./ContextStatus";
26import HoverItem from "./InputToolbar/HoverItem";
27
28export interface ToolbarOptions {
29 hideUseCodebase?: boolean;
30 hideImageUpload?: boolean;
31 hideAddContext?: boolean;
32 enterText?: string;
33 hideSelectModel?: boolean;
34}
35
36interface InputToolbarProps {
37 onEnter?: (modifiers: InputModifiers) => void;
38 onAddContextItem?: () => void;
39 onClick?: () => void;
40 onImageFileSelected?: (file: File) => void;
41 hidden?: boolean;
42 activeKey: string | null;
43 toolbarOptions?: ToolbarOptions;
44 disabled?: boolean;
45 isMainInput?: boolean;
46}
47
48function InputToolbar(props: InputToolbarProps) {
49 const dispatch = useAppDispatch();
50 const ideMessenger = useContext(IdeMessengerContext);
51 const fileInputRef = useRef<HTMLInputElement | null>(null);
52 const defaultModel = useAppSelector(selectSelectedChatModel);
53 const useActiveFile = useAppSelector(selectUseActiveFile);
54 const isInEdit = useAppSelector((store) => store.session.isInEdit);
55 const codeToEdit = useAppSelector((store) => store.editModeState.codeToEdit);
56 const hasReasoningEnabled = useAppSelector(
57 (store) => store.session.hasReasoningEnabled,
58 );
59
60 const isEnterDisabled =
61 props.disabled || (isInEdit && codeToEdit.length === 0);
62
63 const supportsImages =
64 defaultModel &&
65 modelSupportsImages(
66 defaultModel.provider,
67 defaultModel.model,
68 defaultModel.title,
69 defaultModel.capabilities,
70 );
71
72 const supportsReasoning = modelSupportsReasoning(defaultModel);
73
74 const smallFont = useFontSize(-2);
75 const tinyFont = useFontSize(-3);
76
77 return (
78 <>
79 <div
80 onClick={props.onClick}
81 className={`find-widget-skip bg-vsc-input-background flex select-none flex-row items-center justify-between gap-1 pt-1 ${props.hidden ? "pointer-events-none h-0 cursor-default opacity-0" : "pointer-events-auto mt-2 cursor-text opacity-100"}`}
82 style={{
83 fontSize: smallFont,
84 }}
85 >
86 <div className="xs:gap-1.5 flex flex-row items-center gap-1">
87 {!isInEdit && (
88 <ToolTip place="top" content="Select Mode">
89 <HoverItem className="!p-0">
90 <ModeSelect />
91 </HoverItem>
92 </ToolTip>
93 )}
94 <ToolTip place="top" content="Select Model">
95 <HoverItem className="!p-0">
96 <ModelSelect />
97 </HoverItem>
98 </ToolTip>
99 <div className="xs:flex text-description -mb-1 hidden items-center transition-colors duration-200">
100 {props.toolbarOptions?.hideImageUpload ||
101 (supportsImages && (
102 <>
103 <input
104 type="file"
105 ref={fileInputRef}
106 style={{ display: "none" }}
107 accept=".jpg,.jpeg,.png,.gif,.svg,.webp"
108 onChange={(e) => {
109 const files = e.target?.files ?? [];
110 for (const file of files) {
111 props.onImageFileSelected?.(file);
112 }
113 if (fileInputRef.current) {
114 fileInputRef.current.value = "";
115 }
116 }}
117 />
118
119 <ToolTip place="top" content="Attach Image">
120 <HoverItem className="">
121 <PhotoIcon
122 className="h-3 w-3 hover:brightness-125"
123 onClick={(e) => {
124 fileInputRef.current?.click();
125 }}
126 />
127 </HoverItem>
128 </ToolTip>
129 </>
130 ))}
131 {props.toolbarOptions?.hideAddContext || (
132 <ToolTip place="top" content="Attach Context">
133 <HoverItem onClick={props.onAddContextItem}>
134 <AtSymbolIcon className="h-3 w-3 hover:brightness-125" />
135 </HoverItem>
136 </ToolTip>
137 )}
138 {supportsReasoning && (
139 <HoverItem
140 onClick={() =>
141 dispatch(setHasReasoningEnabled(!hasReasoningEnabled))
142 }
143 >
144 <ToolTip
145 place="top"
146 content={
147 hasReasoningEnabled
148 ? "Disable model reasoning"
149 : "Enable model reasoning"
150 }
151 >
152 {hasReasoningEnabled ? (
153 <LightBulbIconSolid className="h-3 w-3 brightness-200 hover:brightness-150" />
154 ) : (
155 <LightBulbIconOutline className="h-3 w-3 hover:brightness-150" />
156 )}
157 </ToolTip>
158 </HoverItem>
159 )}
160 </div>
161 </div>
162
163 <div
164 className="text-description flex items-center gap-2 whitespace-nowrap"
165 style={{
166 fontSize: tinyFont,
167 }}
168 >
169 {!isInEdit && <ContextStatus />}
170 {!props.toolbarOptions?.hideUseCodebase && !isInEdit && (
171 <div className="hidden transition-colors duration-200 hover:underline md:flex">
172 <HoverItem
173 className={
174 props.activeKey === "Meta" ||
175 props.activeKey === "Control" ||
176 props.activeKey === "Alt"
177 ? "underline"
178 : ""
179 }
180 onClick={(e) =>
181 props.onEnter?.({
182 useCodebase: false,
183 noContext: !useActiveFile,
184 })
185 }
186 >
187 <ToolTip
188 place="top-end"
189 content={`${
190 useActiveFile
191 ? "Send Without Active File"
192 : "Send With Active File"
193 } (${getMetaKeyLabel()}⏎)`}
194 >
195 <span>
196 {getMetaKeyLabel()}⏎{" "}
197 {useActiveFile ? "No active file" : "Active file"}
198 </span>
199 </ToolTip>
200 </HoverItem>
201 </div>
202 )}
203 {isInEdit && (
204 <HoverItem
205 className="hidden hover:underline sm:flex"
206 onClick={async () => {
207 void dispatch(exitEdit({}));
208 ideMessenger.post("focusEditor", undefined);
209 }}
210 >
211 <span>
212 <i>Esc</i> to exit Edit
213 </span>
214 </HoverItem>
215 )}
216 <ToolTip place="top" content="Send (⏎)">
217 <Button
218 variant={props.isMainInput ? "primary" : "secondary"}
219 size="sm"
220 data-testid="submit-input-button"
221 onClick={async (e) => {
222 if (props.onEnter) {
223 props.onEnter({
224 useCodebase: false,
225 noContext: useActiveFile
226 ? !(isMetaEquivalentKeyPressed(e as any) || e.altKey)
227 : isMetaEquivalentKeyPressed(e as any) || e.altKey,
228 });
229 }
230 }}
231 disabled={isEnterDisabled}
232 >
233 <span className="hidden md:inline">
234 ⏎ {props.toolbarOptions?.enterText ?? "Enter"}
235 </span>
236 <span className="md:hidden">⏎</span>
237 </Button>
238 </ToolTip>
239 </div>
240 </div>
241 </>
242 );
243}
244
245function shallowToolbarOptionsEqual(a?: ToolbarOptions, b?: ToolbarOptions) {
246 if (a === b) return true;
247 if (!a || !b) return false;
248 return (
249 a.hideAddContext === b.hideAddContext &&
250 a.hideImageUpload === b.hideImageUpload &&
251 a.hideUseCodebase === b.hideUseCodebase &&
252 a.hideSelectModel === b.hideSelectModel &&
253 a.enterText === b.enterText
254 );
255}
256
257export default memo(
258 InputToolbar,
259 (prev, next) =>
260 prev.hidden === next.hidden &&
261 prev.disabled === next.disabled &&
262 prev.isMainInput === next.isMainInput &&
263 prev.activeKey === next.activeKey &&
264 shallowToolbarOptionsEqual(prev.toolbarOptions, next.toolbarOptions),
265);