Elffuss BraidHub

kikocis/continue public

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